diff lib.r @ 3:9f1e18bc8ce3 draft

planemo upload commit ceb25d29a013b58d3476323f202276e7c876648a-dirty
author lecorguille
date Thu, 28 Jul 2016 11:03:04 -0400
parents
children 9fa5856f6184
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib.r	Thu Jul 28 11:03:04 2016 -0400
@@ -0,0 +1,96 @@
+##
+## This function launch IPO functions to get the best parameters for xcmsSet
+## A sample among the whole dataset is used to save time
+##
+ipo4xcmsSet = function(directory, parametersOutput, listArguments, samplebyclass=4) {
+    setwd(directory)
+    #@TODO: check the behaviour of direcory = "." ?
+    files = list.files(".", recursive=T)  # "KO/ko15.CDF" "KO/ko16.CDF" "WT/wt15.CDF" "WT/wt16.CDF"
+    files_classes = basename(dirname(files))    # "KO", "KO", "WT", "WT"
+    
+    mzmlfile = files
+    if (samplebyclass > 0) {
+        #random selection of N files for IPO in each class
+        classes<-unique(basename(dirname(files)))
+        mzmlfile = NULL
+        for (class_i in classes){
+            files_class_i = files[files_classes==class_i]
+            if (samplebyclass > length(files_class_i)) {
+                mzmlfile = c(mzmlfile, files_class_i)
+            } else {
+                mzmlfile = c(mzmlfile,sample(files_class_i,samplebyclass))
+            }
+        }
+    }
+    #TODO: else, must we keep the RData to been use directly by group?
+
+    cat("\t\tSamples used:\n")
+    print(mzmlfile)
+
+    paramsPP <- getDefaultXcmsSetStartingParams(listArguments[["method"]]) #load default parameters of IPO
+
+    #user defined new parameters
+    paramsPP$ppm <- listArguments[["ppm"]]
+    paramsPP$min_peakwidth <- listArguments[["min_peakwidth"]]
+    paramsPP$max_peakwidth <- listArguments[["max_peakwidth"]]
+    paramsPP$nSlaves <- listArguments[["nSlaves"]]
+    
+    #paramsPP$profparam <- list(step=0.005) #not yet used by IPO have to think of it for futur improvement
+    resultPPpos <- optimizeXcmsSet(mzmlfile, paramsPP, subdir="IPO_results") #some images generated by IPO
+    write.table(resultPPpos$best_settings$parameters, file=parametersOutput, sep="\t",row.names=FALSE) #can be read by user
+
+}
+
+
+
+
+##
+## This function check if xcms will found all the files
+##
+#@author Gildas Le Corguille lecorguille@sb-roscoff.fr ABiMS TEAM
+checkFilesCompatibilityWithXcms <- function(directory) {
+  cat("Checking files filenames compatibilities with xmcs...\n")
+  # WHAT XCMS WILL FIND
+  filepattern <- c("[Cc][Dd][Ff]", "[Nn][Cc]", "([Mm][Zz])?[Xx][Mm][Ll]","[Mm][Zz][Dd][Aa][Tt][Aa]", "[Mm][Zz][Mm][Ll]")
+  filepattern <- paste(paste("\\.", filepattern, "$", sep = ""),collapse = "|")
+  info <- file.info(directory)
+  listed <- list.files(directory[info$isdir], pattern = filepattern,recursive = TRUE, full.names = TRUE)
+  files <- c(directory[!info$isdir], listed)
+  files_abs <- file.path(getwd(), files)
+  exists <- file.exists(files_abs)
+  files[exists] <- files_abs[exists]
+  files[exists] <- sub("//","/",files[exists])
+
+  # WHAT IS ON THE FILESYSTEM
+  filesystem_filepaths=system(paste("find $PWD/",directory," -not -name '\\.*' -not -path '*conda-env*' -type f -name \"*\"", sep=""), intern=T)
+  filesystem_filepaths=filesystem_filepaths[grep(filepattern, filesystem_filepaths, perl=T)]
+
+  # COMPARISON
+  if (!is.na(table(filesystem_filepaths %in% files)["FALSE"])) { 
+    write("\n\nERROR: List of the files which will not be imported by xcmsSet",stderr())
+    write(filesystem_filepaths[!(filesystem_filepaths %in% files)],stderr())
+    stop("\n\nERROR: One or more of your files will not be import by xcmsSet. It may due to bad characters in their filenames.")
+
+  }
+}
+
+
+
+##
+## This function check if XML contains special caracters. It also checks integrity and completness.
+##
+#@author Misharl Monsoor misharl.monsoor@sb-roscoff.fr ABiMS TEAM
+checkXmlStructure <- function (directory) {
+  cat("Checking XML structure...\n")
+
+  cmd=paste("IFS=$'\n'; for xml in $(find",directory,"-not -name '\\.*' -not -path '*conda-env*' -type f -iname '*.*ml*'); do if [ $(xmllint --nonet --noout \"$xml\" 2> /dev/null; echo $?) -gt 0 ]; then echo $xml;fi; done;")
+  capture=system(cmd,intern=TRUE)
+
+  if (length(capture)>0){
+    #message=paste("The following mzXML or mzML file is incorrect, please check these files first:",capture)
+    write("\n\nERROR: The following mzXML or mzML file(s) are incorrect, please check these files first:", stderr())
+    write(capture, stderr())
+    stop("ERROR: xcmsSet cannot continue with incorrect mzXML or mzML files")
+  }
+   
+}