diff createDatabase.R @ 6:52d48bcd3608 draft

"planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 2579c8746819670348c378f86116f83703c493eb"
author computational-metabolomics
date Thu, 04 Mar 2021 12:29:17 +0000
parents 8442dd3d2136
children e98ae5c1f4e7
line wrap: on
line diff
--- a/createDatabase.R	Fri Nov 13 09:55:01 2020 +0000
+++ b/createDatabase.R	Thu Mar 04 12:29:17 2021 +0000
@@ -3,19 +3,18 @@
 library(xcms)
 library(CAMERA)
 print(sessionInfo())
-print('CREATING DATABASE')
+print("CREATING DATABASE")
 
-xset_pa_filename_fix <- function(opt, pa, xset){
+xset_pa_filename_fix <- function(opt, pa, xset) {
 
-  if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)){
+  if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)) {
     # NOTE: Relies on the pa@fileList having the names of files given as 'names' of the variables
     # needs to be done due to Galaxy moving the files around and screwing up any links to files
 
-    filepaths <- trimws(strsplit(opt$mzML_files, ',')[[1]])
+    filepaths <- trimws(strsplit(opt$mzML_files, ",")[[1]])
     filepaths <- filepaths[filepaths != ""]
-    new_names <- basename(filepaths)
 
-    galaxy_names <- trimws(strsplit(opt$galaxy_names, ',')[[1]])
+    galaxy_names <- trimws(strsplit(opt$galaxy_names, ",")[[1]])
     galaxy_names <- galaxy_names[galaxy_names != ""]
 
     nsave <- names(pa@fileList)
@@ -28,10 +27,10 @@
   }
 
 
- if(!all(basename(pa@fileList)==basename(xset@filepaths))){
-    if(!all(names(pa@fileList)==basename(xset@filepaths))){
-       print('FILELISTS DO NOT MATCH')
-       message('FILELISTS DO NOT MATCH')
+ if (!all(basename(pa@fileList) == basename(xset@filepaths))) {
+    if (!all(names(pa@fileList) == basename(xset@filepaths))) {
+       print("FILELISTS DO NOT MATCH")
+       message("FILELISTS DO NOT MATCH")
        quit(status = 1)
     }else{
       xset@filepaths <- unname(pa@fileList)
@@ -48,23 +47,23 @@
 
 
 option_list <- list(
-  make_option(c("-o", "--outDir"), type="character"),
-  make_option("--pa", type="character"),
-  make_option("--xset_xa", type="character"),
-  make_option("--xcms_camera_option", type="character"),
-  make_option("--eic", action="store_true"),
-  make_option("--cores", default=4),
-  make_option("--mzML_files", type="character"),
-  make_option("--galaxy_names", type="character"),
-  make_option("--grpPeaklist", type="character")
+  make_option(c("-o", "--outDir"), type = "character"),
+  make_option("--pa", type = "character"),
+  make_option("--xset_xa", type = "character"),
+  make_option("--xcms_camera_option", type = "character"),
+  make_option("--eic", action = "store_true"),
+  make_option("--cores", default = 4),
+  make_option("--mzML_files", type = "character"),
+  make_option("--galaxy_names", type = "character"),
+  make_option("--grpPeaklist", type = "character")
 )
 
 
 # store options
-opt<- parse_args(OptionParser(option_list=option_list))
+opt <- parse_args(OptionParser(option_list = option_list))
 print(opt)
 
-loadRData <- function(rdata_path, name){
+loadRData <- function(rdata_path, name) {
 #loads an RData file, and returns the named xset object if it is there
     load(rdata_path)
     return(get(ls()[ls() %in% name]))
@@ -73,36 +72,36 @@
 getxcmsSetObject <- function(xobject) {
     # XCMS 1.x
     if (class(xobject) == "xcmsSet")
-        return (xobject)
+        return(xobject)
     # XCMS 3.x
     if (class(xobject) == "XCMSnExp") {
         # Get the legacy xcmsSet object
-        suppressWarnings(xset <- as(xobject, 'xcmsSet'))
-        sampclass(xset) <- xset@phenoData$sample_group
-        return (xset)
+        suppressWarnings(xset <- as(xobject, "xcmsSet"))
+        xcms::sampclass(xset) <- xset@phenoData$sample_group
+        return(xset)
     }
 }
 
 
-print(paste('pa', opt$pa))
+print(paste("pa", opt$pa))
 print(opt$xset)
 
 print(opt$xcms_camera_option)
 # Requires
-pa <- loadRData(opt$pa, 'pa')
+pa <- loadRData(opt$pa, "pa")
 
 
 print(pa@fileList)
 
 # Missing list element causes failures (should be updated
 # in msPurity R package for future releases)
-if (!exists('allfrag', where=pa@filter_frag_params)){
+if (!exists("allfrag", where = pa@filter_frag_params)) {
     pa@filter_frag_params$allfrag <- FALSE
 }
 
-if (opt$xcms_camera_option=='xcms'){
+if (opt$xcms_camera_option == "xcms") {
 
-  xset <- loadRData(opt$xset, c('xset','xdata'))
+  xset <- loadRData(opt$xset, c("xset", "xdata"))
   xset <- getxcmsSetObject(xset)
   fix <- xset_pa_filename_fix(opt, pa, xset)
   pa <- fix[[1]]
@@ -110,7 +109,7 @@
   xa <- NULL
 }else{
 
-  xa <- loadRData(opt$xset, 'xa')
+  xa <- loadRData(opt$xset, "xa")
   fix <- xset_pa_filename_fix(opt, pa, xa@xcmsSet)
   pa <- fix[[1]]
   xa@xcmsSet <- fix[[2]]
@@ -118,36 +117,33 @@
 }
 
 
-
-if(is.null(opt$grpPeaklist)){
-  grpPeaklist = NA
+if (is.null(opt$grpPeaklist)) {
+  grpPeaklist <- NA
 }else{
-  grpPeaklist = opt$grpPeaklist
+  grpPeaklist <- opt$grpPeaklist
 }
 
-
-
 dbPth <- msPurity::createDatabase(pa,
-                                   xset=xset,
-                                   xsa=xa,
-                                   outDir=opt$outDir,
-                                   grpPeaklist=grpPeaklist,
-                                   dbName='createDatabase_output.sqlite'
+                                   xset = xset,
+                                   xsa = xa,
+                                   outDir = opt$outDir,
+                                   grpPeaklist = grpPeaklist,
+                                   dbName = "createDatabase_output.sqlite"
 )
 
 
 
 
 
-if (!is.null(opt$eic)){
+if (!is.null(opt$eic)) {
 
-  if (is.null(xset)){
+  if (is.null(xset)) {
       xset <- xa@xcmsSet
   }
   # previous check should have matched filelists together
   xset@filepaths <- unname(pa@fileList)
 
-  convert2Raw <- function(x, xset){
+  convert2Raw <- function(x, xset) {
     sid <- unique(x$sample)
     # for each file get list of peaks
     x$rt_raw <- xset@rt$raw[[sid]][match(x$rt, xset@rt$corrected[[sid]])]
@@ -157,13 +153,14 @@
 
   }
 
-  xset@peaks <- as.matrix(plyr::ddply(data.frame(xset@peaks), ~ sample, convert2Raw, xset=xset))
+  xset@peaks <- as.matrix(
+    plyr::ddply(data.frame(xset@peaks), ~ sample, convert2Raw, xset = xset))
 
   # Saves the EICS into the previously created database
   px <- msPurity::purityX(xset,
                           saveEIC = TRUE,
-                          cores=1,
-                          sqlitePth=dbPth,
+                          cores = 1,
+                          sqlitePth = dbPth,
                           rtrawColumns = TRUE)
 
 }