comparison r-gpca.R @ 0:ed24bacaa4d0 draft

planemo upload for repository https://github.com/blankenberglab/galaxy-tools-blankenberg/tree/master/tools/r-gpca commit ec2f585063522efa001e4d2a639e92a67758ad8c-dirty
author blankenberglab
date Mon, 19 Nov 2018 10:15:19 -0500
parents
children d93629e79f5e
comparison
equal deleted inserted replaced
-1:000000000000 0:ed24bacaa4d0
1 #!/usr/bin/env Rscript
2 library(optparse)
3 library(gPCA)
4
5
6 # parse options
7 option_list = list(
8 make_option(
9 c("--version"),
10 action = "store_true",
11 default = FALSE,
12 help = "Print version and exit"
13 ),
14 make_option(
15 c("-i", "--batch"),
16 action = "store",
17 default = NA,
18 type = 'character',
19 help = "Input batch"
20 ),
21 make_option(
22 c("-d", "--data"),
23 action = "store",
24 default = NA,
25 type = 'character',
26 help = "Input data"
27 ),
28 make_option(
29 c("-n", "--nperm"),
30 action = "store",
31 default = 1000,
32 type = 'numeric',
33 help = "Number of permutations to preform"
34 ),
35 make_option(
36 c("-c", "--center"),
37 action = "store_true",
38 default = FALSE,
39 help = "Is the input batch and data centered [default %default]"
40 ),
41 make_option(
42 c("-y", "--scaleY"),
43 action = "store_true",
44 default = FALSE,
45 help = "Scale to Y axis"
46 ),
47 make_option(
48 c("-f", "--filt"),
49 action = "store",
50 default = NULL,
51 type = 'numeric',
52 help = "Retain features"
53 ),
54 make_option(
55 c( "--npcs"),
56 action = "store",
57 default = 3,
58 type = 'numeric',
59 help = "Number of principal components to plot"
60 ),
61 make_option(
62 c("-p", "--path"),
63 action = "store",
64 default = '$html_outfile.extra_files_path',
65 type = 'character',
66 help = "File path"
67 ),
68 make_option(
69 c("-s", "--seed"),
70 action = "store",
71 default = NULL,
72 type = 'numeric',
73 help = "Set a seed number"
74 ),
75 make_option(
76 c("-x", "--numbers_outfile"),
77 action = "store",
78 default = NA,
79 type = 'character',
80 help = "Numbers output"
81 ),
82 make_option(
83 c("-o", "--html_outfile"),
84 action = "store",
85 default = NA,
86 type = 'character',
87 help = "Output",
88 )
89 )
90
91 opt <-parse_args(OptionParser(option_list = option_list))
92
93 if (opt$version){
94 # print version and exit
95 cat(paste("gPCA version", toString(packageVersion("gPCA"))), "\n")
96 quit()
97 }
98
99 # Check parameter values
100
101 if ( ! file.exists(opt$batch)){
102 stop((paste('File for batch', opt$batch, 'does not exist')))
103 }
104 if ( ! file.exists(opt$data)){
105 stop((paste('File for data', opt$data, 'does not exist')))
106 }
107
108 batch<-as.numeric(factor(as.matrix(read.delim(opt$batch, header=TRUE, row.names=1, sep="\t"))))
109
110 data<-t(as.matrix(read.delim(opt$data, row.names=1, header=TRUE, sep="\t")))
111
112 out<-gPCA.batchdetect(x=data, batch=batch, center=opt$center, scaleY=opt$scaleY, nperm=opt$nperm, filt=opt$filt, seed=opt$seed)
113
114 out$varPCg1<-((out$varPCg1-out$varPCu1)/out$varPCg1)*100
115
116 dir.create(opt$path);
117
118 write.table(data.frame(out$delta, out$p.val, out$varPCg1),file=opt$numbers_outfile, sep="\t", row.name=FALSE, quote=FALSE)
119
120 # General Distribution
121 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
122 png(paste(opt$path,'/gDist.png', sep=""), width=1020, height=800, units='px')
123 gDist(out)
124 invisible(dev.off())
125
126
127 # Guided/Unguided 1v2
128 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
129 png(paste(opt$path,'/guided_1v2.png', sep=""), width=1020, height=800, units='px')
130 PCplot(out,ug="unguided",type="1v2")
131 invisible(dev.off())
132
133 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
134 png(paste(opt$path,'/unguided_1v2.png', sep=""), width=1020, height=800, units='px')
135 PCplot(out,ug="unguided",type="1v2")
136 invisible(dev.off())
137
138
139 # Guided/Unguided comp,3
140 par(mai=c(0.65,0.65,0.1,0.1),cex=0.8)
141 png(paste(opt$path,'/guided_npcs.png', sep=""), width=1020, height=800, units='px')
142 PCplot(out,ug="guided",type="comp",npcs=opt$npcs)
143 invisible(dev.off())
144
145 par(mai=c(0.65,0.65,0.1,0.1),cex=0.8)
146 png(paste(opt$path,'/unguided_npcs.png', sep=""), width=1020, height=800, units='px')
147 PCplot(out,ug="unguided",type="comp",npcs=opt$npcs)
148 invisible(dev.off())
149
150
151 # Guided/Unguided CumlativeVarPlot
152 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
153 png(paste(opt$path,'/guided_var.png', sep=""), width=1020, height=800, units='px')
154 CumulativeVarPlot(out,ug="guided",col="blue")
155 invisible(dev.off())
156
157 par(mai=c(0.8,0.8,0.1,0.1),cex=0.8)
158 png(paste(opt$path,'/unguided_var.png', sep=""), width=1020, height=800, units='px')
159 CumulativeVarPlot(out,ug="unguided",col="blue")
160 invisible(dev.off())
161
162
163 write(paste('<html>
164 <table id="r-gpca-wrap" align="center" border="1">
165 <tr>
166 <th>Delta</th><th>P-value</th><th>varPCg1</th>
167 </tr>
168 <tr>
169 <td id=delta>',out$delta,'</td><td id=p.val>',out$p.val,'</td><td id=varPCg1>',out$varPCg1,'</td>
170 </tr>
171 </table>
172
173 <center><img src="gDist.png"/></center><br>
174 <center><title>Guided 1v2</title><br><img src="guided_1v2.png"/></center><br>
175 <center><title>Unguided 1v2</title><br><img src="unguided_1v2.png"/></center><br>
176
177 <center><title>Guided Compare to ',opt$npcs,'</title><br><img src="guided_npcs.png"/></center><br>
178 <center><title>Unguided Compare to ',opt$npcs,'</title><br><img src="unguided_npcs.png"/></center><br>
179
180 <center><title>Guided Cumulative Variance</title><br><img src="guided_var.png"/></center><br>
181 <center><title>Unguided Cumulative Variance</title><br><img src="unguided_var.png"/></center><br>
182
183 </html>'
184 ),file = opt$html_outfile)
185
186 sessionInfo()