| 4 | 1 #!/usr/bin/env Rscript | 
|  | 2 # setup R error handling to go to stderr | 
|  | 3 options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } ) | 
| 2 | 4 | 
| 4 | 5 # we need that to not crash galaxy with an UTF8 error on German LC settings. | 
|  | 6 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | 
|  | 7 | 
|  | 8 library("optparse") | 
| 2 | 9 | 
| 4 | 10 ##### Read options | 
|  | 11 option_list=list( | 
|  | 12 		make_option("--id",type="character",default=NULL,help="GSE ID from GEO databse (required)"), | 
|  | 13 		make_option("--transformation",type="character",default=NULL,help="log2 transformation (required)"), | 
|  | 14 		make_option("--data",type="character",default=NULL,help="A table containing the expression data"), | 
|  | 15 		make_option("--rdata",type="character",default="NULL",help="rdata object containing eset object"), | 
|  | 16 		make_option("--conditions",type="character",default=NULL,help="Text file summarizing conditions of the experiment") | 
|  | 17 | 
|  | 18 ); | 
| 2 | 19 | 
| 4 | 20 opt_parser = OptionParser(option_list=option_list); | 
|  | 21 opt = parse_args(opt_parser); | 
|  | 22 | 
|  | 23 if(is.null(opt$id)){ | 
|  | 24 	print_help(opt_parser) | 
|  | 25 	stop("GEOdata id required.", call.=FALSE) | 
|  | 26 } | 
| 2 | 27 | 
| 4 | 28 #loading libraries | 
|  | 29 suppressPackageStartupMessages(require(GEOquery)) | 
|  | 30 | 
|  | 31 GEOQueryID=opt$id | 
|  | 32 GEOQueryData=opt$data | 
|  | 33 GEOQueryRData=opt$rdata | 
|  | 34 conditionFile=opt$conditions | 
|  | 35 transformation=opt$transformation | 
|  | 36 | 
|  | 37 data1=getGEO(GEOQueryID) | 
| 2 | 38 eset=data1[[1]] | 
|  | 39 | 
|  | 40 #check if datas are in log2 space | 
|  | 41 normalization<-function(data){ | 
|  | 42 	ex <- exprs(data) | 
|  | 43 	qx <- as.numeric(quantile(ex, c(0., 0.25, 0.5, 0.75, 0.99, 1.0), na.rm=T)) | 
|  | 44 	LogC <- (qx[5] > 100) || | 
|  | 45 			(qx[6]-qx[1] > 50 && qx[2] > 0) || | 
|  | 46 			(qx[2] > 0 && qx[2] < 1 && qx[4] > 1 && qx[4] < 2) | 
|  | 47 	if (LogC) { ex[which(ex <= 0)] <- NaN | 
|  | 48 		return (log2(ex)) } else { | 
|  | 49 		return (ex) | 
|  | 50 	} | 
|  | 51 } | 
|  | 52 | 
|  | 53 if (transformation=="auto"){ | 
|  | 54 	exprs(eset)=normalization(eset) | 
|  | 55 } else if (transformation=="yes"){ | 
|  | 56 	exprs(eset)=log2(exprs(eset)) | 
|  | 57 } | 
|  | 58 | 
|  | 59 matrixData=exprs(eset) | 
|  | 60 write.table(matrixData,col.names=NA,row.names=TRUE,sep="\t",file=GEOQueryData) | 
|  | 61 | 
|  | 62 #Construcion of condition file | 
|  | 63 #if there is data in "source_name_ch1" field, we keep this data as a condition | 
|  | 64 #else we keep the "description" field data. | 
|  | 65 if (length(unique(tolower(pData(data1[[1]])["source_name_ch1"][,1])))>1) | 
|  | 66 { | 
|  | 67 	conditions=pData(data1[[1]])["source_name_ch1"] | 
|  | 68 	description=paste0(as.vector(pData(data1[[1]])["geo_accession"][,1]), " ",as.vector(pData(data1[[1]])["title"][,1]), " ", as.vector(conditions[,1])) | 
|  | 69 }	else | 
|  | 70 { | 
|  | 71 	conditions=pData(data1[[1]])["description"] | 
|  | 72 	description=paste0(as.vector(pData(data1[[1]])["geo_accession"][,1]), " ",as.vector(pData(data1[[1]])["title"][,1]), " ", as.vector(conditions[,1])) | 
|  | 73 } | 
|  | 74 | 
|  | 75 conditions[,1]=tolower(conditions[,1]) | 
|  | 76 pData(eset)["source_name_ch1"]=conditions | 
|  | 77 | 
|  | 78 write.table(cbind(conditions,description),quote = FALSE,col.names = FALSE, row.names=TRUE,file=conditionFile,sep="\t") | 
|  | 79 save(eset,conditions,file=GEOQueryRData) |