|
| 1 | +library(data.table) |
| 2 | + |
| 3 | +#arg1 file containing TF affinities computed on the randomised data |
| 4 | +#arg2 file containing TF affinities computed on the actual data |
| 5 | +#arg3 output file |
| 6 | +#arg4 cut off used |
| 7 | +args = commandArgs(trailingOnly=TRUE) |
| 8 | +if (length(args)!=4){ |
| 9 | + print("Usage: <Affinities computed on random set> <Affinities computed on the actual data> <Output file> <Cut off (The quantile used is 1-value)>") |
| 10 | + q(save="no") |
| 11 | +} |
| 12 | + |
| 13 | +#Function defintions |
| 14 | +getLength<-function(x){as.numeric(x[3])-as.numeric(x[2])} |
| 15 | +normalise<-function(x,y){x/y} |
| 16 | +determineQuantile<-function(x,y){quantile(x,c(y))} |
| 17 | + |
| 18 | +#Setting file names |
| 19 | +randomRegionsFile=args[1] |
| 20 | +originalRegionsFile=args[2] |
| 21 | + |
| 22 | +##Computing quantiles |
| 23 | +#Loading affinity data based on random regions |
| 24 | +randomData<-fread(randomRegionsFile,stringsAsFactors = F) |
| 25 | +sLength<-strsplit(randomData$V1,"[[:punct:][:punct:]]") |
| 26 | + |
| 27 | +#Computing length of oc region |
| 28 | +lengthV<-rep(0,length(sLength)) |
| 29 | +for (i in c(1:length(sLength))){ |
| 30 | + lengthV[i]<-getLength(sLength[[i]]) |
| 31 | +} |
| 32 | + |
| 33 | +#Normalise affinities |
| 34 | +randomDataNormalised<-randomData[,-1] |
| 35 | +randomDataNormalised<-apply(randomData[,-1],2,normalise,lengthV) |
| 36 | + |
| 37 | +#Compute quantiles |
| 38 | +quantiles<-apply(randomDataNormalised,2,determineQuantile,1-as.numeric(args[4])) |
| 39 | + |
| 40 | +#Remove objects that are not required anymore |
| 41 | +rm(randomData,randomDataNormalised,sLength) |
| 42 | + |
| 43 | +#Load affinity data computed on the actual data |
| 44 | +originalData<-fread(originalRegionsFile,stringsAsFactors = F) |
| 45 | +sLength<-strsplit(originalData$V1,"[[:punct:][:punct:]]") |
| 46 | +lengthV<-rep(0,length(sLength)) |
| 47 | +for (i in c(1:length(sLength))){ |
| 48 | + lengthV[i]<-getLength(sLength[[i]]) |
| 49 | +} |
| 50 | + |
| 51 | +#Normalise the data |
| 52 | +originalDataNormalised<-originalData[,-1] |
| 53 | +originalDataNormalised<-apply(originalData[,-1],2,normalise,lengthV) |
| 54 | + |
| 55 | +#Filter the data |
| 56 | +reprocessedData<-originalData |
| 57 | +for (i in c(2:dim(originalData)[2])){ |
| 58 | + reprocessedData[which(originalDataNormalised[,i-1]<quantiles[i-1]),i]<-0.0 |
| 59 | +} |
| 60 | +colnames(reprocessedData)[1]<-"\t" |
| 61 | +write.table(reprocessedData,args[3],quote=F,sep="\t",col.names=TRUE,row.names=FALSE) |
0 commit comments