view RnBeadsGalaxy.R @ 11:ce411254e4a9 draft

Uploaded
author pavlo-lutsik
date Sat, 20 Apr 2013 07:14:22 -0400
parents
children
line wrap: on
line source

msg.file<-file("RnBeads.messages.out")
sink(file=open(msg.file, "w"))

suppressWarnings(suppressPackageStartupMessages(library(RnBeads)))
suppressWarnings(suppressPackageStartupMessages(library(getopt)))

#all.opts<-names(rnb.options())
opt.class<-RnBeads:::OPTION.TYPES[-28]
all.opts<-names(opt.class)
#all.opts<-paste("--", all.opts, sep="")
#all.opts<-gsub("\\.([a-z])", "\\U\\1", all.opts, perl=TRUE)
all.opts<-gsub("\\.","-", all.opts)
#opt.class<-sapply(rnb.options(), class)

rnb.opt.spec<-data.frame(
		Long=all.opts, 
		Short=as.character(1:length(all.opts)), 
		Mask=c(1,2)[as.integer((opt.class=="logical"))+1], 
		Type=opt.class)

### automated xml file preparation
#xml.strings<-apply(rnb.opt.spec,1, function(row){
#			
#			opt.lab<-gsub("-", ".", row[1])
#			opt.def.val<-rnb.getOption(opt.lab)
#			opt.name<-gsub("-([0-9a-z])", "\\U\\1", row[1], perl=TRUE)
#			tf.opt<-"\t\t\t<option value=\"True\">True</option>\n\t\t\t<option value=\"False\">False</option>"
#			opt.lab<-paste(opt.lab, gsub("\\."," ", row[4]), sep=", ")
#			if(row[4]=="logical"){
#				opt.type<-'select'
#				if(!is.null(opt.def.val) && opt.def.val)
#					opt.def.val<-"1" else
#					opt.def.val<-"0"
#				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)
#			}else{
#				opt.type<-'text'
#				if(!is.null(opt.def.val) && opt.def.val!="")
#					opt.def.val<-paste(opt.def.val, collapse=",") else
#					opt.def.val<-""
#				string<-sprintf("\t\t<param name=\"%s\" type=\"%s\"  label=\"%s\" value=\"%s\"/>\n", opt.name, opt.type, opt.lab, opt.def.val)
#			}
#			string
#		})
#
#cat(xml.strings, sep="", file="automated.settings.xml.txt")
#
#opt.def.strings<-apply(rnb.opt.spec,1, function(row){
#			
#			opt.name<-gsub("-([0-9a-z])", "\\U\\1", row[1], perl=TRUE)
#			opt.long<-row[1]
#			opt.short<-row[2]
#			
#			if(row[4]=="logical"){
#				def.string<-sprintf("#if str( $options.%s ) == \"True\"\n\t--%s\n#end if\n", opt.name, opt.long)		
#			}else{
#				def.string<-sprintf("#if str( $options.%s ) != \"\"\n\t--%s=\"$options.%s\" \n#end if\n", opt.name, opt.long, opt.name)
#			}
#			def.string
#			
#		})
#cat(opt.def.strings, sep="", file="automated.option.assignments.txt")


rnb.opt.spec$Type<-gsub("\\.vector", "", rnb.opt.spec$Type)
rnb.opt.spec$Type<-gsub("numeric", "double", rnb.opt.spec$Type)
rnb.opt.spec<-rbind(data.frame(
				Long=c("data-type", "pheno", "idat-dir","idat-files","bed-files", "gs-report", "geo-series", "betas", "pvals","output-file", "report-dir"),
				Short=c("d","s","a","i","f","g","e","b","p","r","o"),
				Mask=c(1,2,2,2,2,2,2,2,2,1,1),
				Type=c("character","character","character","character","character","character","character","character","character","character", "character")),
				rnb.opt.spec)
		
opts<-getopt(as.matrix(rnb.opt.spec))
#opts<-getopt(as.matrix(rnb.opt.spec), opt=list("--data-type=idats","--report-dir=dir", "--idats=file1\tfile2"))
print(opts)

if(opts[["data-type"]]=="idatDir"){
   
	data.source<-list()
	data.type<-"idat.dir"
	data.source[["idat.dir"]]<-opts[["idat-dir"]]
	data.source[["sample.sheet"]]<-opts[["pheno"]]
   
}else if(opts[["data-type"]]=="idatFiles"){
	
	data.type<-"idat.dir"
	files<-strsplit(opts[["idat-files"]], "\t")[[1]]
	idat.dir<-mkdir(file.path(opts[["report-dir"]], "idats"))
	file.copy(as.list(files), idat.dir)
	data.source<-list()
	data.source[["idat.dir"]]<-idat.dir
	data.source[["sample.sheet"]]<-opts[["pheno"]]
	
	
}else if(opts[["data-type"]]=="GS.report"){

	data.type<-"GS.report"
	data.source<-opts[["gs-report"]]
	
}else if(opts[["data-type"]]=="GEO"){
	
	data.type<-"GEO"
	data.source<-opts[["geo-series"]]
	
}else if(opts[["data-type"]]=="data.files"){
	
	data.type<-"GEO"
	data.source<-opts[["geo-series"]]
	
}else if(opts[["data-type"]]=="data.files"){
	
	data.type<-"data.files"
	data.source<-c(opts[["pheno"]], opts[["betas"]])
	if(!is.null(opts[["pvals"]]))
		data.source<-c(data.source, opts[["pvals"]])
		
}else if(opts[["data-type"]]=="beds"){

	data.type<-"idat.dir"
	files<-strsplit(opts[["bed-files"]], "\t")[[1]]
	bed.dir<-mkdir(file.path(opts[["report-dir"]], "beds"))
	file.copy(as.list(files), bed.dir)
	data.source<-list()
	data.source[["bed.dir"]]<-bed.dir
	data.source[["sample.sheet"]]<-opts[["pheno"]]

}

dump<-sapply(names(opt.class), function(on){
	getoptname<-gsub("-", "\\.","-",on)
	if(getoptname %in% names(opts)){
		if(opt.class[on]=="logical"){
			ov<-TRUE
		}else if(opt.class %in% c("character","character.vector")){
			ov<-opts[[getoptname]]
			ov<-gsub("\"", "", ov)
			if(opt.class=="character.vector"){
				ov<-as.character(strsplit(ov,","))
			}
			
		}else if(opt.class %in% c("integer","numeric","integer.vector","numeric.vector")){
			ov<-opts[[getoptname]]
			ov<-gsub("\"", "", ov)
			if(opt.class %in% c("integer.vector","numeric.vector")){
				ov<-as.character(strsplit(ov,","))
			}
		}
		eval(parse(text=sprintf("rnb.options(%s=ov)",on)))
	}
})


logical.opts<-names(opt.class[opt.class=="logical"])
logical.opts.false<-logical.opts[!logical.opts %in% gsub("-",".",names(opts))]

dump<-sapply(logical.opts.false, function(on){
			eval(parse(text=sprintf("rnb.options(%s=FALSE)",on)))	
		})

#report.out.dir<-sprintf("%s_rnbReport", tempdir())
report.out.dir<-opts[["report-dir"]]
suppressMessages(suppressWarnings(rnb.run.analysis(data.source=data.source, dir.report=report.out.dir, data.type=data.type)))

#file.symlink(paste(report.out.dir, "index.html", sep="/"), opts[["output-file"]]) 

outfile<-file(opts[["output-file"]],open="w")
cat(sprintf("<html>\n<body>\n<a href=\"index.html\">RnBeads report</a>\n</body>\n</html>",report.out.dir),file=outfile)
close(outfile)

sink(file=NULL)
flush(msg.file)
close(msg.file)