view lib.r @ 1:508ab373b524 draft

planemo upload commit 2c68962f28b58980db71ac15b6a3e75bbcf7885b-dirty
author lecorguille
date Mon, 25 Jul 2016 09:49:16 -0400
parents b2032600d98f
children
line wrap: on
line source

##
## 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")
  }
   
}