aboutsummaryrefslogtreecommitdiff
path: root/scripts/ctl_analysis.R
blob: dd47759dfffe2a0850c0489fc4fdfd6b2ca760e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
library(ctl)
library(stringi);
library(rjson)

options(stringsAsFactors = FALSE);

# The genotypes.csv file containing the genotype matrix is stored individuals (rows) x genetic marker (columns):

args = commandArgs(trailingOnly=TRUE)

imgDir = Sys.getenv("GENERATED_IMAGE_DIR")

if (length(args)==0) {
  stop("Argument for the data file", call.=FALSE)
} else {
  # default output file
  json_file_path = args[1]
}

json_file_path
# add validation for the files
input <- fromJSON(file = json_file_path)

cat("The input data is \n")



genoData <- input$genoData
phenoData <- input$phenoData

# formData <- input$form
# create the matixes

# genotypes Matrix of genotypes. (individuals x markers)
# phenotypes Matrix of phenotypes. (individuals x phenotypes)

geno_matrix = t(matrix(unlist(genoData$genotypes),
	nrow=length(genoData$markernames), ncol=length(genoData$individuals),
	dimnames=list(genoData$markernames, genoData$individuals), byrow=TRUE))


pheno_matrix = t(matrix(as.numeric(unlist(phenoData$traits)), nrow=length(phenoData$trait_db_list), ncol=length(
    phenoData$individuals), dimnames= list(phenoData$trait_db_list, phenoData$individuals), byrow=TRUE))

# # Use a data frame to store the objects


ctls <- CTLscan(geno_matrix,pheno_matrix,nperm=input$nperm,strategy=input$strategy,parametric=TRUE,nthreads=3,verbose=TRUE)



# # same function used in a different script:refactor
genImageRandStr <- function(prefix){

	randStr <- paste(prefix,stri_rand_strings(1, 9, pattern = "[A-Za-z0-9]"),sep="_")

	return(paste(randStr,".png",sep=""))
}


genRandomFileName <- function(prefix,file_ext=".png"){

	randStr = paste(prefix,stri_rand_strings(1, 9, pattern = "[A-Za-z0-9]"),sep="_")

	return(paste(randStr,file_ext,sep=""))
}


# #output matrix significant CTL interactions with 4 columns: trait, marker, trait, lod
ctl_significant <- CTLsignificant(ctls,significance = 0.05)
 
colnames(ctl_significant) = c("trait","marker","trait_2","LOD","dcor")


# # Create the lineplot


imageLoc = file.path(input$imgDir,genRandomFileName("CTLline"))

png(imageLoc,width=1000,height=600,type='cairo-png')

ctl.lineplot(ctls,significance = 0.05, gap = 50, 
col = "orange", bg.col = "lightgray", cex = 1, verbose = FALSE)

dev.off()


n = 2
ctl_plots = c()

for (trait in phenoData$trait_db_list)
{
	image_loc = file.path(input$imgDir,genRandomFileName(paste("CTL",n,sep="")))
	png(image_loc,width=1000, height=600, type='cairo-png')
  plot.CTLobject(ctls,n-1,significance= 0.05, main=paste("Phenotype",trait,sep=""))

  ctl_plots = append(ctl_plots,image_loc)

  dev.off()
  n = n + 1

}
# rename coz of duplicate key names 


# refactor this

network_file_name = paste("ctlnet",stri_rand_strings(1, 9, pattern = "[A-Za-z0-9]"),sep="_")
netfile =  file.path(input$imgDir,paste(network_file_name,".sif",sep=""))

nodefile = file.path(input$imgDir,paste(network_file_name,".nodes",sep=""))

# temp fix override ctlnetwork function to target gn2 use case

# xtodo refactor function

CTLnetworkGn<- function(CTLobject, mapinfo, significance = 0.05, LODdrop = 2, what = c("names","ids"), short = FALSE, add.qtls = FALSE,verbose = TRUE){
  if(missing(CTLobject) || is.null(CTLobject)) stop("argument 'CTLobject' is missing, with no default")
  if("CTLscan" %in% class(CTLobject)) CTLobject = list(CTLobject)
  if(length(what) > 1) what = what[1]

  results <- NULL
  significant <- CTLsignificant(CTLobject, significance, what = "ids")
  if(!is.null(significant)){
    all_m <- NULL; all_p <- NULL;


    cat("",file=netfile); cat("",file=nodefile);
    if(verbose) cat("NETWORK.SIF\n")
    edges <- NULL
    for(x in 1:nrow(significant)){
      data    <- as.numeric(significant[x,])
      CTLscan <- CTLobject[[data[1]]]
      markern <- rownames(CTLscan$dcor)
      traitsn <- colnames(CTLscan$dcor)
      name    <- ctl.name(CTLscan)
      if(what=="ids"){
        tid     <- which(traitsn %in% ctl.name(CTLobject[[data[1]]]))
        name    <- paste("P",tid,sep="")
        markern <- paste("M",1:nrow(CTLobject[[data[1]]]$dcor), sep="")
        traitsn <- paste("P", 1:ncol(CTLobject[[data[1]]]$dcor), sep="")
      }
      if(add.qtls){ # Add QTL to the output SIF
        bfc    <- length(CTLscan$qtl)
        above  <- which(CTLscan$qtl > -log10(significance))
        qtlnms <- names(above); qtlmid <- 1
        for(m in above){
          cat(name,"\tQTL\t",markern[m],"\tQTL\t",CTLscan$qtl[m],"\n",sep="",file=netfile,append=TRUE)
          all_m  <- CTLnetwork.addmarker(all_m, mapinfo, markern[data[2]], qtlnms[qtlmid])
          qtlmid <- qtlmid+1
        }
      }
      lod <- CTLscan$ctl[data[2],data[3]]
      qlod1    <- CTLscan$qtl[data[2]]
      qlod2    <- qlod1
      edgetype <- NA
      if(length(CTLobject) >= data[3]){  # Edge type based on QTL LOD scores
        qlod2 <- CTLobject[[data[3]]]$qtl[data[2]]
        if((qlod1-qlod2) > LODdrop){
          edgetype <- 1
        }else if((qlod1-qlod2) < -LODdrop){
          edgetype <- -1
        }else{ edgetype <- 0; }
      } else { 
        cat("Warning: Phenotype", data[3], "from", data[1], "no CTL/QTL information\n")
        qlod2 <- NA; 
      }
      #Store the results
      results <- rbind(results, c(data[1], data[2], data[3], lod, edgetype, qlod1, qlod2))

      if(nodefile == "" && !verbose){ }else{
        if(short){
          edge <- paste(name,traitsn[data[3]])
          edgeI <- paste(traitsn[data[3]],name)
          if(!edge %in% edges && !edgeI %in% edges){
            cat(name, "\t", markern[data[2]],"\t", traitsn[data[3]],"\n",file=netfile, append=TRUE,sep="")
            edges <- c(edges,edge)
          }
        }else{
          cat(name, "\t", "CTL_", data[1],"_",data[3], "\t", markern[data[2]],file=netfile, append=TRUE,sep="")
          cat("\tCTL\t", lod, "\n", file=netfile, append=TRUE,sep="")
          cat(markern[data[2]], "\t", "CTL_", data[1],"_",data[3], "\t",file=netfile, append=TRUE,sep="")
          cat(traitsn[data[3]],"\tCTL\t", lod, "\n", file=netfile,append=TRUE,sep="")
        }
      }
      all_m <- CTLnetwork.addmarker(all_m, mapinfo, markern[data[2]], rownames(CTLscan$dcor)[data[2]])
      all_p <- unique(c(all_p, name, traitsn[data[3]]))
    }
    colnames(results) <- c("TRAIT1","MARKER","TRAIT2","LOD_C","CAUSAL","LOD_T1","LOD_T2")
    if(verbose) cat("NODE.DESCRIPTION\n")
    if(nodefile == "" && !verbose){ }else{
      for(m in all_m){ cat(m,"\n",    sep="", file=nodefile, append=TRUE); }
      for(p in all_p){ cat(p,"\tPHENOTYPE\n", sep="", file=nodefile, append=TRUE); }
    }
  }
  if(!is.null(results)){
    class(results) <- c(class(results),"CTLnetwork")
  }
  invisible(results)
}

CTLnetwork.addmarker <- function(markers, mapinfo, name, realname){
  if(!missing(mapinfo)){
    id      <- which(rownames(mapinfo) %in% realname)
    fname   <- paste(name,"\tMARKER\t",mapinfo[id,1],"\t",mapinfo[id,2],sep="")
    markers <- unique(c(markers, fname))
  }
  return(markers)
}


# generating network


ctl_network = CTLnetworkGn(ctls, significance = 0.05, LODdrop = 2,short = FALSE, add.qtls = FALSE, verbose = TRUE)



json_data <- list(significance_table = ctl_significant,image_loc = imageLoc,ctl_plots=ctl_plots)


json_data <- toJSON(json_data)

write(json_data,file= json_file_path)