Files
hartman-server/workflow/templates/qhtcp/REMc/AddShiftVals2.R

88 lines
3.2 KiB
R

#AddShiftVals2Dev.R
#for use as a command line script (Rscript)
#need two starting files
#first file is the finalTable.csv created by clustering
#second file needs to contain the list of ORFs and Genes and the shift data for L and K for each gene
#third need to give the name of what the output should be called
#May want to reorder columns in excel before making heatmaps - otherwise all the shift data will be plotted next to each other.
version
timestamp()
library(plyr)
library(dplyr)
library(sos)
Args = commandArgs(TRUE)
#GTF/REMcOutput/20_0310_2KR_vs_WT_EG_Above2SD_NoDAMPs_REMcReady.csv-finalTable.csv ../JoinFiles/Shift_only.csv REMcHeatmaps/REMcWithShift.csv
#Args= "GTF/REMcOutput/TESTjwr4exp_REMcReady.csv-finalTable.csv" #20_0310_2KR_vs_WT_EG_Above2SD_NoDAMPs_REMcReady.csv-finalTable.csv"
#Args[2]= "../JoinFiles/Shift_only.csv"
#Args[3]= "REMcHeatmaps/REMcWithShift.csv"
finaltable = "REMcRdy_lm_only.csv-finalTable.csv" #Args[1]
ShiftFile = "Shift_only.csv" #Args[2]
output = "REMcHeatmaps/REMcWithShift.csv" #Args[3]
#read in the REMc finalTable data
X = data.frame(read.csv(file=finaltable,header=TRUE,stringsAsFactors = FALSE))
#read in the shift data From ../JoinInteractions
Y = data.frame(read.csv(file=ShiftFile,header=TRUE,stringsAsFactors = FALSE))
Labels <- read.delim("../Code/StudyInfo.csv",skip=0,as.is=T,row.names=1,strip.white=TRUE)
#determine the number of cols - needed to create the correct number of new cols
Xcolnum <- length(X[1,])
ADDnum <- Xcolnum + length(Y[1,]) - 2
#create new columns filled with NAs to be filled with data
Xtemp= X
Xtemp[,(Xcolnum+1):ADDnum] <- NA
#match the orf names in each row to a orf name in the shift data file and then add the shift data to the finalTable file
shiftTbl <-as.data.frame(matrix(nrow=1,ncol=length(Y)-2)) #the df shiftTbl must be initialized before for loop
for(i in 1:length(X[,1])){
Shiftrownum = match(X[i,2],Y[,1])
shiftTbl[i,]= Y[Shiftrownum,3:length(Y[1,])]
Xtemp[i,(Xcolnum+1):ADDnum] <- Y[Shiftrownum,3:length(Y[1,])]
}
headerX= colnames(Xtemp)
headerY= colnames(Y)
shfHdr= headerY[3:length(headerY)]
combTbl<- X[,1:3]
lmTbl= select(Xtemp, contains('Z_lm')) #X[,(4:Xcolnum-2)]
shiftTbl<- select(Xtemp, contains('V'))
clustTbl<- select(Xtemp, contains('cluster.'))
#give the new column names the same names as in the shift file
Xcols = colnames(X)
Ycols = colnames(Y)[3:length(Y[1,])]
newCols = c(Xcols[1:Xcolnum],Ycols)
#+++++++++++++++++++++REORDER COLUMNS FOR GENERATING HEATMAPS+++++++++++++++++++++++
combI= combTbl #Starting Template orf, Genename columns
headersRemc<-newCols #colnames(X)
newHeaders= newCols[1:3]
lmHdr= colnames(lmTbl) #newCols[4:(length(Xcols)-2)]
clstHdr= colnames(clustTbl) #select(newCols, contains('cluster.')) #newCols[3+length(lmHdr):2]
intLvHdr= vector()
#Reorder columns to produce an interleaved set of Z_lm and Shift data for all the cpps.
for(i in 1:(length(shiftTbl[1,]))){
combI=cbind.data.frame(combI, shiftTbl[i])
combI=cbind.data.frame(combI, lmTbl[i])
intLvHdrx= c(shfHdr[i],lmHdr[i] )
intLvHdr= c(intLvHdr,intLvHdrx)
}
combIHdr= c(colnames(combTbl),intLvHdr,clstHdr)
combI=cbind.data.frame(combI, clustTbl)
colnames(combI)= combIHdr
write.csv(combI,file=output, row.names=FALSE)
timestamp()