Files
hartman-server/workflow/.old/apps/r/ScriptTemplates/JoinInteractExps.R
2024-07-29 18:12:57 -04:00

95 lines
4.2 KiB
R

#JoinCSVfilesREMc.R
#The input files should be entered in order from the greatest number of rows(Orfs) to least.
Args <- commandArgs(TRUE)
Wstudy= getwd()
dir.create("../JoinFiles") #(paste0(Wstudy,"/JoinFiles"))
outDir <- "../JoinFiles" #paste0(Wstudy,"/JoinFiles")
print(outDir)
#ArgsJoin= 'asdf'
#ArgsJoin[1]= "../JoinFiles"
#ArgsJoin[2]= "../Exp1/ZScores/ZScores_Interaction.csv" #paste0(Wstudy,"/Exp1/ZScores/ZScores_Interaction.csv")
#ArgsJoin[3]= "../Exp2/ZScores/ZScores_Interaction.csv" #paste0(Wstudy,"/Exp2/ZScores/ZScores_Interaction.csv")
#outDir <- ArgsJoin[1] #Output Directory
print(length(Args)) #display the number of arguments on terminal
#open required library for the join function (libraries must already be install on R)
library(plyr)
library(dplyr)
library(sos)
#read in the files for your experiment and
#join the two files at a time as a function of how many Args, list the larger file first ? in this example X2 has the larger number of genes.
#if X1 has a larger number of genes, switch the order of X1 and X2
if(length(Args)==2) {
X1 <- read.csv(file= Args[1],stringsAsFactors = FALSE)
X2 <- read.csv(file= Args[2],stringsAsFactors = FALSE)
X <- join(X1,X2,by="OrfRep")
OBH=X[,order(colnames(X))] #OrderByHeader
headSel= select(OBH, contains('OrfRep'), matches('Gene'), contains('Z_lm_K'), contains('Z_Shift_K'),contains('Z_lm_L'), contains('Z_Shift_L'))
headSel= select(headSel, -'Gene.1') #remove 'Gene.1 column
headSel2 = select(OBH, contains('OrfRep'), matches('Gene')) #Frame for interleaving Z_lm with Shift colums
headSel2 = select(headSel2, -'Gene.1') #remove 'Gene.1 column #Frame for interleaving Z_lm with Shift colums
}else if(length(Args)==3){
X1 <- read.csv(file= Args[1],stringsAsFactors = FALSE) #exp1File,stringsAsFactors = FALSE)
X2 <- read.csv(file= Args[2],stringsAsFactors = FALSE) #exp2File,stringsAsFactors = FALSE)
X3 <- read.csv(file= Args[3],stringsAsFactors = FALSE) #exp3File,stringsAsFactors = FALSE)
X <- join(X1,X2,by="OrfRep")
X <- join(X,X3,by="OrfRep")
OBH=X[,order(colnames(X))] #OrderByHeader
headSel= select(OBH, contains('OrfRep'), matches('Gene'), contains('Z_lm_K'), contains('Z_Shift_K'),contains('Z_lm_L'), contains('Z_Shift_L'))
headSel= select(headSel, -'Gene.1',-'Gene.2')
headSel2 = select(OBH, contains('OrfRep'), matches('Gene'))
headSel2 = select(headSel2, -'Gene.1',-'Gene.2')
}else if(length(Args==4)){
X1 <- read.csv(file= Args[1],stringsAsFactors = FALSE) #exp1File,stringsAsFactors = FALSE)
X2 <- read.csv(file= Args[2],stringsAsFactors = FALSE) #exp2File,stringsAsFactors = FALSE)
X3 <- read.csv(file= Args[3],stringsAsFactors = FALSE) #exp3File,stringsAsFactors = FALSE)
X4 <- read.csv(file= Args[4],stringsAsFactors = FALSE) #exp4File,stringsAsFactors = FALSE)
X <- join(X1,X2,by="OrfRep")
X <- join(X,X3,by="OrfRep")
X <- join(X,X4,by="OrfRep")
OBH=X[,order(colnames(X))] #OrderByHeader
headSel= select(OBH, contains('OrfRep'), matches('Gene'), contains('Z_lm_K'), contains('Z_Shift_K'),contains('Z_lm_L'), contains('Z_Shift_L'))
headSel= select(headSel, -'Gene.1',-'Gene.2',-'Gene.3')
headSel2 = select(OBH, contains('OrfRep'), matches('Gene'))
headSel2 = select(headSel2, -'Gene.1',-'Gene.2',-'Gene.3')
}
#headSel$contains('Z_Shift') %>% replace_na(0.001)
headers<-colnames(headSel)
i=0
for(i in 1:length(headers)){
if(grepl("Shift",headers[i])) {
headSel[headers[i]][is.na(headSel[headers[i]])] = 0.001
}
if(grepl("Z_lm_",headers[i])) {
headSel[headers[i]][is.na(headSel[headers[i]])] = 0.0001
}
}
REMcRdy= select(headSel, contains('OrfRep'), matches('Gene'), contains('Z_lm_'))
shiftOnly= select(headSel, contains('OrfRep'), matches('Gene'), contains('Z_Shift'))
combI= headSel2 #Starting Template orf, Genename columns
headersRemc<-colnames(REMcRdy)
#Reoder columns to produce an interleaved set of Z_lm and Shift data for all the cpps.
for(i in 3:length(headersRemc)){
combI=cbind.data.frame(combI, shiftOnly[i])
combI=cbind.data.frame(combI, REMcRdy[i])
}
write.csv(combI,file = file.path(outDir,"CombinedKLzscores.csv"))
write.csv(REMcRdy,file = file.path(outDir,"REMcRdy_lm_only.csv"))
write.csv(shiftOnly,file = file.path(outDir,"Shift_only.csv"))