Mercurial > repos > vladimir-daric > ebio_deseq
view GetOptions.R @ 0:8c5de60b3c04 draft
Uploaded
author | vladimir-daric |
---|---|
date | Fri, 25 Apr 2014 05:05:39 -0400 |
parents | |
children |
line wrap: on
line source
### # Created on 25 sep. 2012 # @author: Alban Ott # @copyright: IGM ebio.igmors.u-psud.fr # @licence: GPL v3 # # Il est interdit de mettre ARGUMENT comme argument long # spec contains at least 4 columns, as many as 5 columns. # # column 1: long name of flag # # column 2: short name of flag # # column 3: argument flag. 0=without argument, 1=with argument # # column 4: mode of argument. one of "logical", "integer", "double", # "complex", "character", "" for arguments without value (return logical) # # column 5 (optional): description of ARGUMENT. # RetreiveAndDestroy=function(opt,root,stem,regexp,SearchNames,Out,isValue,NbFound,StockNames){ Bool=lapply(paste(root,SearchNames,stem,sep=""),grepl,opt) names(Bool)=StockNames Pos=lapply(Bool,which) names(Pos)=StockNames disable=c() for (i in StockNames){ nbmatch=length(Pos[[i]]) if(nbmatch>0){ NbFound[[i]]=NbFound[[i]]+nbmatch disable=c(disable,-1*Pos[[i]]) if(is.null(Out[[i]])){ if(isValue[[i]]!=0){ if(regexp=="next"){ Out[[i]]=opt[Pos[[i]]+1] disable=c(disable,-1*(Pos[[i]]+1)) }else{ Out[[i]]=sub(regexp,"\\1",opt[Pos[[i]]]) } }else{ Out[[i]]=TRUE } }else{ if(isValue[[i]]!=0){ if(regexp=="next"){ Out[[i]]=c(Out[[i]],opt[Pos[[i]]+1]) disable=c(disable,-1*(Pos[[i]]+1)) }else{ Out[[i]]=c(Out[[i]],sub(regexp,"\\1",opt[Pos[[i]]])) } }else{ Out[[i]]=c(Out[[i]],TRUE) } } } } if(length(disable)>0){ opt=opt[disable] } Out[["ARGUMENT"]]=list() Out[["ARGUMENT"]][["opt"]]=opt Out[["ARGUMENT"]][["NbFound"]]=NbFound return(Out) } getopt = function (spec=NULL,opt=commandArgs()) { FindArgs=which(opt=="--args") if(length(FindArgs)!=1){ stop(length(FindArgs)," --args found where 1 expected.",call.=F) } ExecName=sub("--file=","",opt[FindArgs-1]) if(FindArgs<length(opt)){ opt=opt[(FindArgs+1):length(opt)] }else{ opt="" } min.columns=5 colNames=c("LongName","ShortName","Flag","Mod","Default") max.columns=6 DimSpec=dim(spec) if(DimSpec[2]>min.columns){ colNames=c(colNames,"Description") } if(is.null(spec) | !is.matrix(spec) | (DimSpec[2]<min.columns | DimSpec[2]>max.columns)){ stop('argument "spec" is required and must be a matrix with 4|5 columns.',call.=F) } colnames(spec)=colNames spec=as.data.frame(spec,stringsAsFactors=F) #spec validation if(length(unique(c(spec$ShortName,"ARGUMENT","args")))!=DimSpec[1]+2 | length(unique(spec$LongName))!=DimSpec[1]){ stop('Long|Short names for flags must be unique (Long name : "ARGUMENT" and "args" forbidden).', "\n","List of duplicated :", "\n","Short: ",paste(spec$ShortName[duplicated(c(spec$ShortName,"ARGUMENT","args"))],collapse=" "), "\n","Long: ",paste(spec$ShortName[duplicated(spec$LongName)],collapse=" "),call.=F) } if(length(which(nchar(spec$ShortName)>1))!=0){ stop('Short names flags can\'t be longer than 1 character.') } #initialize Out=list() Short2Long=list() NbFound=list() isValue=list() for (i in 1:DimSpec[1]){ Short2Long[[spec$ShortName[i]]]=spec$LongName[i] NbFound[[spec$LongName[i]]]=0 isValue[[spec$LongName[i]]]=spec$Flag[i] } #Map, retreive and suppress ARGUMENTs and arguments #Value ARGUMENT --example=value Out=RetreiveAndDestroy(opt,"^--","=.+$",".+=(.+)$",spec$LongName,Out,isValue,NbFound,spec$LongName) opt=Out[["ARGUMENT"]][["opt"]] NbFound=Out[["ARGUMENT"]][["NbFound"]] Out[["ARGUMENT"]]=NULL #boolean ARGUMENT --example Out=RetreiveAndDestroy(opt,"^--","$","$",spec$LongName,Out,isValue,NbFound,spec$LongName) opt=Out[["ARGUMENT"]][["opt"]] NbFound=Out[["ARGUMENT"]][["NbFound"]] Out[["ARGUMENT"]]=NULL #short name ARGUMENT -t value OR boolean -t Out=RetreiveAndDestroy(opt,"^-","$","next",spec$ShortName,Out,isValue,NbFound,spec$LongName) opt=Out[["ARGUMENT"]][["opt"]] NbFound=Out[["ARGUMENT"]][["NbFound"]] Out[["ARGUMENT"]]=NULL #Warn about non mapped ARGUMENTs if(length(opt)>0){ PosUnkArg=which(grepl("^-",opt)) if(length(PosUnkArg)){ message("Error, argument unreconized :","\n",paste(opt[PosUnkArg],collapse="\n"),"\n\n") } if(length(PosUnkArg)>0){ opt=opt[PosUnkArg*-1] } } #Arguments Out[["ARGUMENT"]]=opt #Validation of ARGUMENTs for(i in 1:DimSpec[1]){ if(spec$Flag[i]=="0"){#verify boolean arguments NbValue=length(Out[[spec$LongName[i]]]) if(NbValue>1){ message("Warning : ",spec$LongName[i]," found ",NbValue," times") } } if(length(Out[[spec$LongName[i]]])==0){ Out[[spec$LongName[i]]]=spec$Default[i] } library("methods") Out[[spec$LongName[i]]]=as(Out[[spec$LongName[i]]],spec$Mod[i]) } return(Out) } # column 3: argument flag. 0=no argument, 1=required argument, 2=optional argument