Mercurial > repos > mir-bioinf > heatmap_colormanipulation
comparison heatmap_colormanipulation/heatmap_extra_v2beta_VERSION.R @ 0:3797463c65f8 draft
Initial upload
author | mir-bioinf |
---|---|
date | Mon, 20 Apr 2015 15:26:53 -0400 |
parents | |
children | aa592c2c26cb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3797463c65f8 |
---|---|
1 sink(file="/tmp/none") | |
2 sink("/dev/null") | |
3 options(warn=-1) | |
4 options(echo=F) | |
5 | |
6 args <- commandArgs(trailingOnly = T) | |
7 #title <- args[17] | |
8 Rowcorr <- args[2] | |
9 Rowlink <- args[3] | |
10 Colcorr <- args[4] | |
11 Collink <- args[5] | |
12 #Xlab <- args[18] | |
13 #Ylab <- args[19] | |
14 inputfile <- args[1] | |
15 Var_cols <- args[6] | |
16 Scale_var <- args[7] | |
17 Remove_na <- args[8] | |
18 header_yes <- args[9] | |
19 rowhead_yes <- args[10] | |
20 color_grad <- args[11] | |
21 color_min <- args[12] | |
22 #max_val_mincol <- args[13] | |
23 color_max <- args[13] | |
24 #min_val_maxcol <- args[15] | |
25 out_file <- args[14] | |
26 #logfile <- args[15] | |
27 | |
28 ##Now for title, xlabel, and ylabel (spaces are hard to deal with here): | |
29 #title <- args[17] | |
30 #Xlab <- args[18] | |
31 #Ylab <- args[19] | |
32 | |
33 stoptime = 0 | |
34 argIndex = 16 | |
35 everything = args[argIndex] | |
36 | |
37 debugcounter = 0 | |
38 | |
39 suppressMessages(library(gplots)) | |
40 Rinfo = sessionInfo() | |
41 Rinfo_pkg = sessionInfo(package="gplots") | |
42 gplots_info = Rinfo_pkg$otherPkgs | |
43 #sink(logfile) | |
44 Rinfo | |
45 gplots_info | |
46 sink(file="/tmp/none") | |
47 sink("/dev/null") | |
48 | |
49 #cat(paste("arg value is ",args[argIndex],".\n"),file=logfile,append="TRUE") | |
50 | |
51 #while (stoptime < 1){ | |
52 while ((stoptime < 1)&&(debugcounter<50)){ | |
53 argIndex=argIndex+1 | |
54 # cat(paste("in while loop now, arg index is ",argIndex,".\n"),file=logfile,append="TRUE") | |
55 # cat(paste("arg value is ",args[argIndex],".\n"),file=logfile,append="TRUE") | |
56 everything = paste(everything,args[argIndex]) | |
57 if (args[argIndex]=="ZZZZ_END") { | |
58 stoptime = 1 | |
59 } | |
60 debugcounter=debugcounter+1 | |
61 } | |
62 | |
63 argIndex=argIndex+1 | |
64 #cat(paste("Out of while loop. arg index value is now ",argIndex,".\n"),file=logfile,append="TRUE") | |
65 | |
66 splitThese = strsplit(everything,"[@]") | |
67 title = splitThese[[1]][1] | |
68 Xlab = splitThese[[1]][2] | |
69 Ylab = splitThese[[1]][3] | |
70 | |
71 ##Now grab the rest of the arguments passed in: | |
72 colorManip = args[argIndex] | |
73 argIndex = argIndex+1 | |
74 | |
75 #cat(paste("Color manip value is ",colorManip,".\n"),file=logfile,append="TRUE") | |
76 | |
77 if ((colorManip == "InnerClip") || (colorManip == "OuterClip")) { | |
78 LowClipVal = as.numeric(args[argIndex]) | |
79 HighClipVal = as.numeric(args[argIndex+1]) | |
80 # cat(paste("Two vals to clip: ",LowClipVal,HighClipVal,".\n"),file=logfile,append="TRUE") | |
81 | |
82 } else { | |
83 ClipVal = as.numeric(args[argIndex]) | |
84 # cat(paste("One val to clip: ",ClipVal,".\n"),file=logfile,append="TRUE") | |
85 } | |
86 | |
87 if (header_yes == "yes") { | |
88 inp = read.table(inputfile,stringsAsFactors=F, header=T, sep="\t") | |
89 } else { | |
90 inp = read.table(inputfile,stringsAsFactors=F, sep="\t") | |
91 } | |
92 | |
93 | |
94 these_cols = read.csv(text=Var_cols,header=F) | |
95 | |
96 if (ncol(these_cols)<2) { | |
97 x = data.frame(cbind(inp[, c(as.matrix(these_cols))],inp[, c(as.matrix(these_cols))])) | |
98 currentColNames=colnames(x) | |
99 labColVar = c(currentColNames[1],"") | |
100 } else { | |
101 x = inp[, c(as.matrix(these_cols))] | |
102 labColVar = colnames(x) | |
103 } | |
104 | |
105 | |
106 genemat = do.call(cbind,x) | |
107 x = apply(genemat,2,as.numeric) | |
108 | |
109 scale_value = Scale_var | |
110 na_rm_value = FALSE | |
111 | |
112 if (Remove_na == "yes") { | |
113 na_rm_value = TRUE | |
114 } | |
115 | |
116 | |
117 if (rowhead_yes == "yes") { | |
118 rownames(x)=inp[[1]] | |
119 } | |
120 | |
121 pdf(out_file) | |
122 | |
123 | |
124 if ((Rowcorr=="none") && (Colcorr!="none")) { | |
125 dendro_val = "column" | |
126 } else if ((Rowcorr!="none") && (Colcorr=="none")) { | |
127 dendro_val = "row" | |
128 } | |
129 | |
130 if ((Rowcorr=="none") && (Colcorr=="none")) { | |
131 dendro_val = "none" | |
132 } | |
133 | |
134 if ((Rowcorr!="none") && (Colcorr!="none")) { | |
135 dendro_val = "both" | |
136 } | |
137 | |
138 if (Rowcorr == "none") { | |
139 Rowv_val = FALSE | |
140 } else { | |
141 Rcor = cor(t(x),method=Rowcorr) | |
142 R_clust = hclust(as.dist(1-Rcor),method=Rowlink) | |
143 R_dendro = as.dendrogram(R_clust) | |
144 Rowv_val = R_dendro | |
145 } | |
146 | |
147 ##Column clustering (if any) set up: | |
148 if (Colcorr == "none") { | |
149 Colv_val = FALSE | |
150 } else { | |
151 Ccor = cor(x,method=Colcorr) | |
152 C_clust = hclust(as.dist(1-Ccor),method=Collink) | |
153 C_dendro = as.dendrogram(C_clust) | |
154 Colv_val = C_dendro | |
155 } | |
156 | |
157 par(cex.main=0.8) ##font size for title | |
158 ##Estimate good guesses for font sizes of rows and columns: | |
159 font_r1 = 0.2 + 1/log10(nrow(x)) ##default done in heatmap, based on number of rows | |
160 font_size_r = min(0.8,font_r1) | |
161 | |
162 font_c1 = 0.2 + 1/log10(ncol(x)) ##default done in heatmap, based on number of columns | |
163 font_size_c = min(0.8,font_c1) | |
164 | |
165 #min_value = min(x) ##x should be the original data matrix | |
166 #max_value = max(x) | |
167 | |
168 if (colorManip == "InnerClip") { | |
169 min_value = min(x) | |
170 max_value = max(x) | |
171 max_val_mincol = LowClipVal | |
172 min_val_maxcol = HighClipVal | |
173 | |
174 } else if (colorManip == "OuterClip") { | |
175 min_value = LowClipVal | |
176 max_value = HighClipVal | |
177 ##How do we set the other values if 0 isn't included in the range? Probably want central color to be center value: | |
178 if ((min_value<=0)&&(max_value>=0)) { | |
179 max_val_mincol = 0 ##will be reset later to account for slight tolerance (so black is included) | |
180 min_val_maxcol = 0 | |
181 } else { | |
182 ##0 is not in range, center around halfway point | |
183 max_val_mincol = (min_value+max_value)/2 + 0.00005 | |
184 min_val_maxcol = (min_value+max_value)/2 - 0.00005 | |
185 } | |
186 } else if (colorManip == "ClipMax") { | |
187 min_value = min(x) | |
188 max_value = ClipVal | |
189 if ((min_value<=0)&&(max_value>=0)) { | |
190 max_val_mincol = 0 ##will be reset later to account for slight tolerance (so black is included) | |
191 min_val_maxcol = 0 | |
192 } else { | |
193 ##0 is not in range, center around halfway point | |
194 max_val_mincol = (min_value+max_value)/2 + 0.00005 | |
195 min_val_maxcol = (min_value+max_value)/2 - 0.00005 | |
196 } | |
197 } else { | |
198 min_value = ClipVal | |
199 max_value = max(x) | |
200 if ((min_value<=0)&&(max_value>=0)) { | |
201 max_val_mincol = 0 ##will be reset later to account for slight tolerance (so black is included) | |
202 min_val_maxcol = 0 | |
203 } else { | |
204 ##0 is not in range, center around halfway point | |
205 max_val_mincol = (min_value+max_value)/2 + 0.00005 | |
206 min_val_maxcol = (min_value+max_value)/2 - 0.00005 | |
207 } | |
208 } | |
209 | |
210 | |
211 ##is 0 included in the data range? if so we want it centered | |
212 if ((min_value <= 0) && (max_value >=0)) { | |
213 sym_breaks_value = "TRUE" | |
214 sym_key_value = "TRUE" | |
215 } else { | |
216 sym_breaks_value = "FALSE" | |
217 sym_key_value = "FALSE" | |
218 } | |
219 | |
220 if (color_grad == "double") { | |
221 my_palette = colorRampPalette(c(color_min,"black",color_max))(n=299) | |
222 } else { | |
223 my_palette = colorRampPalette(c(color_min,color_max))(n=299) | |
224 } | |
225 | |
226 ##Need some tolerance otherwise black won't be included for 0 | |
227 if ((max_val_mincol==0) && (min_val_maxcol==0)) { | |
228 max_val_mincol = -0.00005 | |
229 min_val_maxcol = 0.00005 | |
230 } | |
231 | |
232 #cat(paste("max_val_mincol value is ",max_val_mincol,".\n"),file=logfile,append="TRUE") | |
233 #cat(paste("min_val_maxcol value is ",min_val_maxcol,".\n"),file=logfile,append="TRUE") | |
234 | |
235 colors = c(seq(min_value,max_val_mincol,length=100),seq(max_val_mincol,min_val_maxcol,length=100),seq(min_val_maxcol,max_value,length=100)) | |
236 | |
237 ##Call heatmap.2. cexCol value is constant to account for long sample names | |
238 heatmap.2(x, margins=c(9,10), main=title, xlab=Xlab, ylab=Ylab, cexCol=font_size_c, cexRow=font_size_r, scale=scale_value, symbreaks=sym_breaks_value, symm=F, symkey=sym_key_value, na.rm=na_rm_value, trace="none", col=my_palette, breaks=colors, dendrogram=dendro_val, Rowv=Rowv_val, Colv=Colv_val, labCol=labColVar) | |
239 | |
240 | |
241 ## Close the PDF file | |
242 devname = dev.off() | |
243 | |
244 |