comparison RnBeadsGalaxy.R @ 13:d6cdc73fd57e draft

Uploaded
author pavlo-lutsik
date Sun, 21 Apr 2013 18:04:11 -0400
parents
children
comparison
equal deleted inserted replaced
12:73e7d00a9a19 13:d6cdc73fd57e
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 sample.sheet<-rnb.read.annotation(opts[["pheno"]])
139 if(length(files) < nrow(sample.sheet))
140 stop("Not all bed files are present")
141
142 cn<-colnames(sample.sheet)
143 sample.sheet<-cbind(sample.sheet, gsub("\\.dat", ".bed", files))
144 colnames(sample.sheet)<-c(cn, "BED_files")
145 data.source[["sample.sheet"]]<-sample.sheet
146 }
147
148 if("logging" %in% names(opts)){ # TODO create a cleaner way of checking whether the full options set was supplied
149
150 dump<-sapply(names(opt.class), function(on){
151 getoptname<-gsub("-", "\\.","-",on)
152 if(getoptname %in% names(opts)){
153 if(opt.class[on]=="logical"){
154 ov<-TRUE
155 }else if(opt.class %in% c("character","character.vector")){
156 ov<-opts[[getoptname]]
157 ov<-gsub("\"", "", ov)
158 if(opt.class=="character.vector"){
159 ov<-as.character(strsplit(ov,","))
160 }
161
162 }else if(opt.class %in% c("integer","numeric","integer.vector","numeric.vector")){
163 ov<-opts[[getoptname]]
164 ov<-gsub("\"", "", ov)
165 if(opt.class %in% c("integer.vector","numeric.vector")){
166 ov<-as.character(strsplit(ov,","))
167 }
168 }
169 eval(parse(text=sprintf("rnb.options(%s=ov)",on)))
170 }
171 })
172
173 logical.opts<-names(opt.class[opt.class=="logical"])
174 logical.opts.false<-logical.opts[!logical.opts %in% gsub("-",".",names(opts))]
175
176
177 dump<-sapply(logical.opts.false, function(on){
178 eval(parse(text=sprintf("rnb.options(%s=FALSE)",on)))
179 })
180 }
181
182 print(rnb.options())
183
184 #report.out.dir<-sprintf("%s_rnbReport", tempdir())
185 report.out.dir<-opts[["report-dir"]]
186 #print("Starting RnBeads with the following inputs:")
187 #print(data.source)
188 #print(report.out.dir)
189 #print(data.type)
190 rnb.run.analysis(data.source=data.source, dir.report=report.out.dir, data.type=data.type)
191
192
193 #sink(file=NULL)
194 #flush(msg.file)
195 #close(msg.file)