Merge branch 'master' of https://github.com/solgenomics/sgn
[sgn.git] / cgi-bin / phenome / correlation.r
blob561b91e07978dcae4a6f4846f6b76133ab0775b7
1 #SNOPSIS
3 #commands for running correlation analyis,
4 #and generating heatmap and the correlation
5 #coefficients and their p-values
8 #AUTHOR
9 # Isaak Y Tecle (iyt2@cornell.edu)
12 options(echo = FALSE)
14 library(gplots)
15 library(RColorBrewer)
16 library(ltm)
18 allargs<-commandArgs()
20 phenodata<-grep("phenodata",
21 allargs,
22 ignore.case=TRUE,
23 perl=TRUE,
24 value=TRUE
27 cortable<-grep("corre_table",
28 allargs,
29 ignore.case=TRUE,
30 perl=TRUE,
31 value=TRUE
34 print(c("correlation table file:", cortable))
36 heatmap<-grep("heatmap",
37 allargs,
38 ignore.case=TRUE,
39 perl=TRUE,
40 value=TRUE
43 print(c("heatmap file:", heatmap))
45 #reading phenotype data into an R object
46 phenodata<-read.csv(phenodata,
47 header=TRUE,
48 dec=".",
49 sep=",",
50 na.strings=c("NA", "-")
53 phenodata$ID=NULL
55 #running Pearson correlation analysis
56 coefpvalues<-rcor.test(phenodata,
57 method="pearson",
58 use="pairwise"
61 coefficients<-coefpvalues$cor.mat
62 allcordata<-coefpvalues$cor.mat
63 allcordata[lower.tri(allcordata)]<-coefpvalues$p.values[, 3]
64 diag(allcordata)<-1.00
67 pvalues<-as.matrix(allcordata)
70 pvalues<-round(pvalues,
71 digits=2
75 #rounding correlation coeficients into 2 decimal places
76 coefficients<-round(coefficients,
77 digits=2
80 allcordata<-round(allcordata,
81 digits=2
84 #remove rows and columns that are all "NA"
85 if ( apply(coefficients,
87 function(x)any(is.na(x))
90 apply(coefficients,
92 function(x)any(is.na(x))
97 coefficients<-coefficients[-which(apply(coefficients,
99 function(x)all(is.na(x)))
101 -which(apply(coefficients,
103 function(x)all(is.na(x)))
108 print("correlation coeffients:")
109 print(coefficients)
111 pvalues[upper.tri(pvalues)]<-NA
112 coefficients[upper.tri(coefficients)]<-NA
114 png(file=heatmap,
115 height=600,
116 width=600,
117 bg="transparent"
121 heatmap.2(coefficients,
122 Rowv=FALSE,
123 Colv=FALSE,
124 dendrogram="none",
125 na.rm=TRUE,
126 col = rev(colorRampPalette(brewer.pal(10,"Spectral"))(128)),
127 trace="none",
128 ColSideColors,
129 RowSideColors,
130 keysize = 1,
131 density.info="none",
132 lmat=rbind( c(0,3), c(2,1), c(0,4)),
133 lhei=c(0.25, 4, 0.75),
134 lwid=c(0.25, 4),
135 cexRow = 1.25,
136 cexCol = 1.25,
137 margins = c(10, 6)
140 dev.off()
142 write.table(coefficients,
143 file=cortable,
144 col.names=TRUE,
145 row.names=TRUE,
146 quote=FALSE,
147 dec="."
150 q(save = "no", runLast = FALSE)