Mercurial > repos > pavlo-lutsik > rnbeads
comparison RnBeadsGalaxy.R @ 43:39b78c48d064 draft
Uploaded
author | pavlo-lutsik |
---|---|
date | Fri, 05 Jul 2013 17:39:25 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
42:16f6ef2b3251 | 43:39b78c48d064 |
---|---|
1 #msg.file<-file("RnBeads.messages.out", open="w") | |
2 #sink(file=msg.file) | |
3 | |
4 | |
5 ## add the RnBeads dependencies if we are on a cloud share-instance | |
6 if("Rsitelibrary" %in% list.files("/mnt")){ | |
7 | |
8 .libPaths("/mnt/galaxy/Rsitelibrary") | |
9 | |
10 | |
11 } | |
12 | |
13 if(!'wordcloud' %in% rownames(installed.packages())){ | |
14 install.packages('wordcloud',repos='http://cran.us.r-project.org') | |
15 } | |
16 | |
17 suppressWarnings(suppressPackageStartupMessages(library(RnBeads))) | |
18 suppressWarnings(suppressPackageStartupMessages(library(getopt))) | |
19 | |
20 #all.opts<-names(rnb.options()) | |
21 opt.class<-RnBeads:::OPTION.TYPES[-28] | |
22 all.opts<-names(opt.class) | |
23 #all.opts<-paste("--", all.opts, sep="") | |
24 #all.opts<-gsub("\\.([a-z])", "\\U\\1", all.opts, perl=TRUE) | |
25 all.opts<-gsub("\\.","-", all.opts) | |
26 #opt.class<-sapply(rnb.options(), class) | |
27 | |
28 rnb.opt.spec<-data.frame( | |
29 Long=all.opts, | |
30 Short=as.character(1:length(all.opts)), | |
31 Mask=c(1,2)[as.integer((opt.class=="logical"))+1], | |
32 Type=opt.class) | |
33 | |
34 ### automated xml file preparation | |
35 xml.strings<-apply(rnb.opt.spec,1, function(row){ | |
36 | |
37 opt.lab<-gsub("-", ".", row[1]) | |
38 opt.def.val<-rnb.getOption(opt.lab) | |
39 opt.name<-gsub("-([0-9a-z])", "\\U\\1", row[1], perl=TRUE) | |
40 tf.opt<-"\t\t\t<option value=\"True\">True</option>\n\t\t\t<option value=\"False\">False</option>" | |
41 opt.lab<-paste(opt.lab, gsub("\\."," ", row[4]), sep=", ") | |
42 if(row[4]=="logical"){ | |
43 opt.type<-'select' | |
44 if(!is.null(opt.def.val) && opt.def.val) | |
45 opt.def.val<-"1" else | |
46 opt.def.val<-"0" | |
47 string<-sprintf("\t\t<param name=\"%s\" type=\"%s\" label=\"%s\" value=\"%s\">\n%s\n\t\t</param>\n", opt.name, opt.type, opt.lab, opt.def.val, tf.opt) | |
48 }else{ | |
49 opt.type<-'text' | |
50 if(!is.null(opt.def.val) && opt.def.val!="") | |
51 opt.def.val<-paste(opt.def.val, collapse=",") else | |
52 opt.def.val<-"" | |
53 string<-sprintf("\t\t<param name=\"%s\" type=\"%s\" label=\"%s\" value=\"%s\"/>\n", opt.name, opt.type, opt.lab, opt.def.val) | |
54 } | |
55 string | |
56 }) | |
57 | |
58 cat(xml.strings, sep="", file="automated.settings.xml.txt") | |
59 | |
60 opt.def.strings<-apply(rnb.opt.spec,1, function(row){ | |
61 | |
62 opt.name<-gsub("-([0-9a-z])", "\\U\\1", row[1], perl=TRUE) | |
63 opt.long<-row[1] | |
64 opt.short<-row[2] | |
65 | |
66 if(row[4]=="logical"){ | |
67 def.string<-sprintf("#if str( $options.%s ) == \"True\"\n\t--%s\n#end if\n", opt.name, opt.long) | |
68 }else{ | |
69 def.string<-sprintf("#if str( $options.%s ) != \"\"\n\t--%s=\"$options.%s\" \n#end if\n", opt.name, opt.long, opt.name) | |
70 } | |
71 def.string | |
72 | |
73 }) | |
74 cat(opt.def.strings, sep="", file="automated.option.assignments.txt") | |
75 | |
76 | |
77 rnb.opt.spec$Type<-gsub("\\.vector", "", rnb.opt.spec$Type) | |
78 rnb.opt.spec$Type<-gsub("numeric", "double", rnb.opt.spec$Type) | |
79 rnb.opt.spec<-rbind(data.frame( | |
80 Long=c("data-type", "pheno", "idat-dir","idat-files","bed-files", "gs-report", "geo-series", "betas", "pvals","output-file", "report-dir"), | |
81 Short=c("d","s","a","i","f","g","e","b","p","r","o"), | |
82 Mask=c(1,2,2,2,2,2,2,2,2,1,1), | |
83 Type=c("character","character","character","character","character","character","character","character","character","character", "character")), | |
84 rnb.opt.spec) | |
85 | |
86 opts<-getopt(as.matrix(rnb.opt.spec)) | |
87 #opts<-getopt(as.matrix(rnb.opt.spec), opt=list("--data-type=idats","--report-dir=dir", "--idats=file1\tfile2")) | |
88 print(opts) | |
89 | |
90 if(opts[["data-type"]]=="idatDir"){ | |
91 | |
92 data.source<-list() | |
93 data.type<-"idat.dir" | |
94 data.source[["idat.dir"]]<-opts[["idat-dir"]] | |
95 data.source[["sample.sheet"]]<-opts[["pheno"]] | |
96 | |
97 }else if(opts[["data-type"]]=="idatFiles"){ | |
98 | |
99 data.type<-"idat.dir" | |
100 file.string<-gsub(" ","", opts[["idat-files"]]) | |
101 files<-strsplit(file.string, ",")[[1]] | |
102 files<-files[files!=""] | |
103 bed.dir<-sprintf("%s_beds",opts[["report-dir"]]) | |
104 dir.create(bed.dir) | |
105 file.copy(files, bed.dir) | |
106 for(dat.file in list.files(bed.dir, full.names = TRUE)){ | |
107 file.rename(dat.file, gsub("\\.dat", ".bed", dat.file)) | |
108 } | |
109 data.source<-list() | |
110 data.source[["bed.dir"]]<-bed.dir | |
111 data.source[["sample.sheet"]]<-opts[["pheno"]] | |
112 | |
113 | |
114 }else if(opts[["data-type"]]=="GS.report"){ | |
115 | |
116 data.type<-"GS.report" | |
117 data.source<-opts[["gs-report"]] | |
118 | |
119 }else if(opts[["data-type"]]=="GEO"){ | |
120 | |
121 data.type<-"GEO" | |
122 data.source<-opts[["geo-series"]] | |
123 | |
124 }else if(opts[["data-type"]]=="data.files"){ | |
125 | |
126 data.type<-"GEO" | |
127 data.source<-opts[["geo-series"]] | |
128 | |
129 }else if(opts[["data-type"]]=="data.files"){ | |
130 | |
131 data.type<-"data.files" | |
132 data.source<-c(opts[["pheno"]], opts[["betas"]]) | |
133 if(!is.null(opts[["pvals"]])) | |
134 data.source<-c(data.source, opts[["pvals"]]) | |
135 | |
136 }else if(opts[["data-type"]]=="bed.dir"){ | |
137 | |
138 data.type<-"bed.dir" | |
139 file.string<-gsub(" ","", opts[["bed-files"]]) | |
140 files<-strsplit(file.string, ",")[[1]] | |
141 files<-files[files!=""] | |
142 bed.dir<-sprintf("%s_beds",opts[["report-dir"]]) | |
143 dir.create(bed.dir) | |
144 file.copy(files, bed.dir) | |
145 for(dat.file in list.files(bed.dir, full.names = TRUE)){ | |
146 file.rename(dat.file, gsub("\\.dat", ".bed", dat.file)) | |
147 } | |
148 data.source<-list() | |
149 data.source[["bed.dir"]]<-bed.dir | |
150 | |
151 logger.start(fname="NA") | |
152 sample.sheet<-rnb.read.annotation(opts[["pheno"]]) | |
153 logger.close() | |
154 if(length(files) < nrow(sample.sheet)) | |
155 stop("Not all bed files are present") | |
156 | |
157 cn<-colnames(sample.sheet) | |
158 dat.files<-sapply(strsplit(files, "\\/"), function(el) el[length(el)]) | |
159 sample.sheet<-cbind(sample.sheet, gsub("\\.dat", ".bed", dat.files)) | |
160 colnames(sample.sheet)<-c(cn, "BED_files") | |
161 data.source[["sample.sheet"]]<-sample.sheet | |
162 } | |
163 | |
164 if("logging" %in% names(opts)){ # TODO create a cleaner way of checking whether the full options set was supplied | |
165 | |
166 dump<-sapply(names(opt.class), function(on){ | |
167 getoptname<-gsub("-", "\\.","-",on) | |
168 if(getoptname %in% names(opts)){ | |
169 if(opt.class[on]=="logical"){ | |
170 ov<-TRUE | |
171 }else if(opt.class %in% c("character","character.vector")){ | |
172 ov<-opts[[getoptname]] | |
173 ov<-gsub("\"", "", ov) | |
174 if(opt.class=="character.vector"){ | |
175 ov<-as.character(strsplit(ov,",")) | |
176 } | |
177 | |
178 }else if(opt.class %in% c("integer","numeric","integer.vector","numeric.vector")){ | |
179 ov<-opts[[getoptname]] | |
180 ov<-gsub("\"", "", ov) | |
181 if(opt.class %in% c("integer.vector","numeric.vector")){ | |
182 ov<-as.character(strsplit(ov,",")) | |
183 } | |
184 } | |
185 eval(parse(text=sprintf("rnb.options(%s=ov)",on))) | |
186 } | |
187 }) | |
188 | |
189 logical.opts<-names(opt.class[opt.class=="logical"]) | |
190 logical.opts.false<-logical.opts[!logical.opts %in% gsub("-",".",names(opts))] | |
191 | |
192 | |
193 dump<-sapply(logical.opts.false, function(on){ | |
194 eval(parse(text=sprintf("rnb.options(%s=FALSE)",on))) | |
195 }) | |
196 } | |
197 | |
198 print(rnb.options()) | |
199 | |
200 #report.out.dir<-sprintf("%s_rnbReport", tempdir()) | |
201 report.out.dir<-opts[["report-dir"]] | |
202 print("Starting RnBeads with the following inputs:") | |
203 print(data.source) | |
204 print(report.out.dir) | |
205 print(data.type) | |
206 rnb.run.analysis(data.source=data.source, dir.report=report.out.dir, data.type=data.type) | |
207 | |
208 | |
209 #sink(file=NULL) | |
210 #flush(msg.file) | |
211 #close(msg.file) |