Mercurial > repos > moheydarian > ggplot_scatterplot
comparison ggplotpointscript.R @ 14:b342326db6dd draft
Uploaded
author | moheydarian |
---|---|
date | Wed, 11 Jan 2017 10:37:50 -0500 |
parents | 37e17dfc9add |
children |
comparison
equal
deleted
inserted
replaced
13:08ae6379dfbb | 14:b342326db6dd |
---|---|
1 #TO ACCOMPANY ggplot_point.xml version 0.1.2 | |
2 # Setup R error handling to go to stderr | |
3 options(show.error.messages=F, error=function(){cat(geterrmessage(),file=stderr());q("no",1,F)}) | |
4 | |
5 # We need to not crash galaxy with an UTF8 error on German LC settings. | |
6 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | |
7 | |
8 | |
9 # Import library | |
10 library("getopt") | |
11 library("ggplot2") | |
12 options(stringAsfactors = FALSE, useFancyQuotes = FALSE) | |
13 # Take in trailing command line arguments | |
14 args <- commandArgs(trailingOnly = TRUE) | |
15 | |
16 | |
17 # get options, using the spec as defined by the enclosed list. | |
18 # we read the options from the default: commandArgs(TRUE). | |
19 option_specification = matrix(c( | |
20 'input', 'i', 2, 'character', | |
21 'title', 't',2, 'character', | |
22 'size', 's', 2, 'double', | |
23 'xlab', 'x', 2, 'character', | |
24 'ylab', 'y', 2, 'character', | |
25 'xplot', 'z', 2, 'integer', | |
26 'yplot', 'j', 2, 'integer', | |
27 'xaxismin', 'e', 2, 'integer', | |
28 'xaxismax', 'f', 2, 'integer', | |
29 'yaxismin', 'g', 2, 'integer', | |
30 'yaxismax', 'h', 2, 'integer', | |
31 'alpha', 'a', 2, 'double', | |
32 'points', 'p', 2, 'character', | |
33 'theme', 'l', 2, 'character', | |
34 'scaling', 'b', 2, 'character', | |
35 'transform', 'w', 2, 'character', | |
36 'dim', 'k', 2, 'character', | |
37 'woutputdim', 'c', 2, 'integer', | |
38 'houtputdim', 'd', 2, 'integer', | |
39 'factor', 'n', 2, 'character', | |
40 'factorcol', 'm', 2, 'integer', | |
41 'colors', 'q', 2, 'character', | |
42 'colororder', 'r', 2, 'integer', | |
43 'pointcolor', 'u', 2, 'character', | |
44 'axistitlecust', 'v', 2, 'character', | |
45 'axistitlecolor', '1', 2, 'character', | |
46 'axistitlesize', '2', 2, 'integer', | |
47 'axistitleface', '3', 2, 'character', | |
48 'axistextcust', '4', 2, 'character', | |
49 'axistextcolor', '5', 2, 'character', | |
50 'axistextsize', '6', 2, 'integer', | |
51 'axistextface', '7', 2, 'character', | |
52 'plottitlecust', '8', 2, 'character', | |
53 'plottitlecolor', '9', 2, 'character', | |
54 'plottitlesize', '10', 2, 'integer', | |
55 'plottitleface', '11', 2, 'character', | |
56 'gridlinecust', '12', 2, 'character', | |
57 'output', 'o', 2, 'character' | |
58 ), byrow=TRUE, ncol=4); | |
59 | |
60 # Parse options | |
61 options = getopt(option_specification); | |
62 | |
63 | |
64 | |
65 # Print options to see what is going on | |
66 cat("\n input: ",options$input) | |
67 cat("\n title: ",options$title) | |
68 cat("\n xlab: ",options$xlab) | |
69 cat("\n ylab: ",options$ylab) | |
70 cat("\n points: ",options$points) | |
71 cat("\n theme: ",options$theme) | |
72 cat("\n scaling: ",options$scaling) | |
73 cat("\n transform: ",options$transform) | |
74 | |
75 | |
76 | |
77 #Choose between automatically scaled x and y axis or user defined | |
78 if(options$scaling == "Automatic"){ | |
79 gg_scalex = NULL | |
80 gg_scaley = NULL | |
81 } else { | |
82 gg_scalex = xlim(options$xaxismin,options$xaxismax) | |
83 gg_scaley = ylim(options$yaxismin,options$yaxismax) | |
84 cat("\n xaxismin: ",options$xaxismin) | |
85 cat("\n xaxismax: ",options$xaxismax) | |
86 cat("\n yaxismin: ",options$yaxismin) | |
87 cat("\n yaxismax: ",options$yaxismax) | |
88 } | |
89 | |
90 # Choose theme for plot | |
91 if(options$theme == "bw"){ | |
92 gg_theme = theme_bw() | |
93 } else { | |
94 gg_theme = NULL | |
95 } | |
96 | |
97 #Choose dimensions of output pdf | |
98 if(options$dim == "Default"){ | |
99 gg_width = 7 | |
100 gg_height = 7 | |
101 } else { | |
102 gg_width = options$woutputdim | |
103 gg_height = options$houtputdim | |
104 } | |
105 | |
106 input <-read.table(options$input, check.names=TRUE, header=TRUE, row.names=NULL) | |
107 | |
108 #renaming columns so ggplot can use them | |
109 names(input)[options$xplot] <- "xcol" | |
110 names(input)[options$yplot] <- "ycol" | |
111 | |
112 #choosing whether to plot data as multiple groups on one plot(factoring) OR multiple groups on different plots | |
113 if(options$factor == "Multiple"){ | |
114 gg_facet = facet_wrap( ~ factor) | |
115 gg_factor = NULL | |
116 color_scale = NULL | |
117 | |
118 if(options$points == "Default"){ | |
119 gg_point = geom_point(size=1, alpha=1, gg_factor) | |
120 } else { | |
121 gg_point = geom_point(size=options$size, alpha=options$alpha, colour=options$pointcolor) | |
122 cat("\n size: ",options$size) | |
123 cat("\n alpha: ",options$alpha) | |
124 cat("\n pointcolor: ",options$pointcolor) | |
125 } | |
126 | |
127 names(input)[options$factorcol] <- "factor" | |
128 | |
129 cat("\n factor: ",options$factor) | |
130 cat("\n factorcol: ",options$factorcol) | |
131 } else if(options$factor == "Single"){ | |
132 gg_facet = NULL | |
133 gg_factor = aes(colour=factor(factor)) | |
134 | |
135 if(options$points == "Default"){ | |
136 gg_point = geom_point(size=1, alpha=1, gg_factor) | |
137 } else { | |
138 gg_point = geom_point(size=options$size, alpha=options$alpha, gg_factor) | |
139 cat("\n size: ",options$size) | |
140 cat("\n alpha: ",options$alpha) | |
141 cat("\n pointcolor: ",options$pointcolor) | |
142 } | |
143 | |
144 if(options$colors == "Default"){ | |
145 color_scale = scale_colour_hue(direction=options$colororder) | |
146 } else { | |
147 color_scale = scale_color_brewer(palette=options$colors, direction=options$colororder) | |
148 } | |
149 | |
150 names(input)[options$factorcol] <- "factor" | |
151 | |
152 cat("\n factor: ",options$factor) | |
153 cat("\n factorcol: ",options$factorcol) | |
154 cat("\n color_scale: ",options$colors) | |
155 cat("\n color_order: ",options$colororder) | |
156 } else{ | |
157 gg_facet = NULL | |
158 gg_factor = NULL | |
159 color_scale = NULL | |
160 | |
161 if(options$points == "Default"){ | |
162 gg_point = geom_point(size=1, alpha=1, gg_factor) | |
163 } else { | |
164 gg_point = geom_point(size=options$size, alpha=options$alpha, colour=options$pointcolor) | |
165 cat("\n size: ",options$size) | |
166 cat("\n alpha: ",options$alpha) | |
167 cat("\n pointcolor: ",options$pointcolor) | |
168 } | |
169 } | |
170 | |
171 | |
172 #Selecting whether or not to log transform data | |
173 if(options$transform == "log2"){ | |
174 input[, options$xplot] <- log2(input[options$xplot]) | |
175 input[, options$yplot] <- log2(input[options$yplot]) | |
176 }else if(options$transform == "log2plus1"){ | |
177 input[, options$xplot] <- log2(input[options$xplot]+1) | |
178 input[, options$yplot] <- log2(input[options$yplot]+1) | |
179 }else if(options$transform == "log10"){ | |
180 input[, options$xplot] <- log10(input[options$xplot]) | |
181 input[, options$yplot] <- log10(input[options$yplot]) | |
182 }else if(options$transform == "log10plus1"){ | |
183 input[, options$xplot] <- log10(input[options$xplot]+1) | |
184 input[, options$yplot] <- log10(input[options$yplot]+1) | |
185 }else{ | |
186 } | |
187 | |
188 | |
189 #axis label custization | |
190 if(options$axistitlecust == "Default"){ | |
191 gg_axistitle = theme(axis.title = element_text(color = NULL, size = NULL, face = NULL)) | |
192 } else { | |
193 gg_axistitle = theme(axis.title = element_text(color = options$axistitlecolor, size = options$axistitlesize, face = options$axistitleface)) | |
194 } | |
195 | |
196 | |
197 #axis text(tick) custization | |
198 if(options$axistextcust == "Default"){ | |
199 gg_axistext = theme(axis.text = element_text(color = NULL, size = NULL, face = NULL)) | |
200 } else { | |
201 gg_axistext = theme(axis.text = element_text(color = options$axistextcolor, size = options$axistextsize, face = options$axistextface)) | |
202 } | |
203 | |
204 | |
205 #plot title custimization | |
206 if(options$plottitlecust == "Default"){ | |
207 gg_plottitle = theme(plot.title = element_text(color = NULL, size = NULL, face = NULL)) | |
208 } else { | |
209 gg_plottitle = theme(plot.title = element_text(color = options$plottitlecolor, size = options$plottitlesize, face = options$plottitleface)) | |
210 } | |
211 | |
212 #grid line customization | |
213 if(options$gridlinecust == "Default"){ | |
214 gg_gridline = NULL | |
215 } else if(options$gridlinecust == "hidemajor"){ | |
216 gg_gridline = theme(panel.grid.major = element_blank()) | |
217 } else if(options$gridlinecust == "hideminor"){ | |
218 gg_gridline = theme(panel.grid.minor = element_blank()) | |
219 } else if(options$gridlinecust == "hideboth"){ | |
220 gg_gridline = theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) | |
221 } else { | |
222 } | |
223 | |
224 | |
225 #this is the actual ggplot command to make the final plot(s) | |
226 ggplot(input, aes(xcol,ycol))+gg_point+gg_facet+ | |
227 gg_theme+gg_scalex+gg_scaley+color_scale+ggtitle(options$title)+xlab(options$xlab)+ylab(options$ylab)+ | |
228 gg_axistitle+gg_axistext+gg_plottitle+gg_gridline | |
229 | |
230 ggsave(file=options$output, width=gg_width, height=gg_height) |