comparison RnBeadsGalaxy.R @ 37:450c8ff1d11a draft

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