aboutsummaryrefslogtreecommitdiff
path: root/scripts/ctl_analysis.R
blob: e7348760b5251936ddbe8e195691503a2f95daf9 (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
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=""))
}



# #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,genImageRandStr("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,genImageRandStr(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 

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)