Merge branch 'master' into topic/upload_fieldbook_images
[sgn.git] / R / genetic_correlation.r
blob96a4d997a17d48ee8ee81d4ed15e85e65f79255c
1 #SNOPSIS
3 #runs genetic correlation analyis.
4 #correlation coeffiecients are stored in tabular and json formats
6 #AUTHOR
7 # Isaak Y Tecle (iyt2@cornell.edu)
10 options(echo = FALSE)
12 library(gplots)
13 library(ltm)
14 library(plyr)
15 library(rjson)
18 allargs<-commandArgs()
20 geneticDataFile <- grep("combined_gebvs",
21 allargs,
22 ignore.case=TRUE,
23 perl=TRUE,
24 value=TRUE
27 correTableFile <- grep("genetic_corre_table",
28 allargs,
29 ignore.case=TRUE,
30 perl=TRUE,
31 value=TRUE
34 correJsonFile <- grep("genetic_corre_json",
35 allargs,
36 ignore.case=TRUE,
37 perl=TRUE,
38 value=TRUE
41 geneticData <- read.table(geneticDataFile,
42 header = TRUE,
43 row.names = 1,
44 sep = "\t",
45 na.strings = c("NA"),
46 dec = "."
49 coefpvalues <- rcor.test(geneticData,
50 method="pearson",
51 use="pairwise"
55 coefficients <- coefpvalues$cor.mat
56 allcordata <- coefpvalues$cor.mat
58 allcordata[lower.tri(allcordata)] <- coefpvalues$p.values[, 3]
59 diag(allcordata) <- 1.00
61 pvalues <- as.matrix(allcordata)
62 pvalues <- round(pvalues,
63 digits=2
66 coefficients <- round(coefficients,
67 digits=3
70 allcordata <- round(allcordata,
71 digits=3
74 #remove rows and columns that are all "NA"
75 if ( apply(coefficients,
77 function(x)any(is.na(x))
80 apply(coefficients,
82 function(x)any(is.na(x))
87 coefficients<-coefficients[-which(apply(coefficients,
89 function(x)all(is.na(x)))
91 -which(apply(coefficients,
93 function(x)all(is.na(x)))
99 pvalues[upper.tri(pvalues)] <- NA
100 coefficients[upper.tri(coefficients)] <- NA
102 coefficients2json <- function(mat){
103 mat <- as.list(as.data.frame(t(mat)))
104 names(mat) <- NULL
105 toJSON(mat)
108 traits <- colnames(coefficients)
110 correlationList <- list(
111 "traits"=toJSON(traits),
112 "coefficients"=coefficients2json(coefficients)
115 correlationJson <- paste("{",paste("\"", names(correlationList), "\":", correlationList, collapse=","), "}")
117 write.table(coefficients,
118 file=correTableFile,
119 col.names=TRUE,
120 row.names=TRUE,
121 quote=FALSE,
122 dec="."
125 write.table(correlationJson,
126 file=correJsonFile,
127 col.names=FALSE,
128 row.names=FALSE,
131 q(save = "no", runLast = FALSE)