Mercurial > repos > computational-metabolomics > mspurity_combineannotations
comparison frag4feature.R @ 0:de93f6aac760 draft default tip
"planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 2579c8746819670348c378f86116f83703c493eb"
| author | computational-metabolomics |
|---|---|
| date | Thu, 04 Mar 2021 12:21:39 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:de93f6aac760 |
|---|---|
| 1 library(optparse) | |
| 2 library(msPurity) | |
| 3 library(xcms) | |
| 4 print(sessionInfo()) | |
| 5 | |
| 6 xset_pa_filename_fix <- function(opt, pa, xset=NULL) { | |
| 7 | |
| 8 | |
| 9 if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)) { | |
| 10 # NOTE: Relies on the pa@fileList having the names of files given as 'names' of the variables | |
| 11 # needs to be done due to Galaxy moving the files around and screwing up any links to files | |
| 12 | |
| 13 filepaths <- trimws(strsplit(opt$mzML_files, ",")[[1]]) # nolint | |
| 14 | |
| 15 filepaths <- filepaths[filepaths != ""] | |
| 16 | |
| 17 galaxy_names <- trimws(strsplit(opt$galaxy_names, ",")[[1]]) | |
| 18 galaxy_names <- galaxy_names[galaxy_names != ""] | |
| 19 | |
| 20 nsave <- names(pa@fileList) | |
| 21 old_filenames <- basename(pa@fileList) | |
| 22 | |
| 23 pa@fileList <- filepaths[match(names(pa@fileList), galaxy_names)] | |
| 24 names(pa@fileList) <- nsave | |
| 25 | |
| 26 pa@puritydf$filename <- basename(pa@fileList[match(pa@puritydf$filename, old_filenames)]) | |
| 27 pa@grped_df$filename <- basename(pa@fileList[match(pa@grped_df$filename, old_filenames)]) | |
| 28 } | |
| 29 print(pa@fileList) | |
| 30 | |
| 31 if (!is.null(xset)) { | |
| 32 | |
| 33 print(xset@filepaths) | |
| 34 | |
| 35 if (!all(basename(pa@fileList) == basename(xset@filepaths))) { | |
| 36 if (!all(names(pa@fileList) == basename(xset@filepaths))) { | |
| 37 print("FILELISTS DO NOT MATCH") | |
| 38 message("FILELISTS DO NOT MATCH") | |
| 39 quit(status = 1) | |
| 40 }else{ | |
| 41 xset@filepaths <- unname(pa@fileList) | |
| 42 } | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 return(list(pa, xset)) | |
| 47 } | |
| 48 | |
| 49 | |
| 50 option_list <- list( | |
| 51 make_option(c("-o", "--out_dir"), type = "character"), | |
| 52 make_option("--pa", type = "character"), | |
| 53 make_option("--xset", type = "character"), | |
| 54 make_option("--ppm", default = 10), | |
| 55 make_option("--plim", default = 0.0), | |
| 56 make_option("--convert2RawRT", action = "store_true"), | |
| 57 make_option("--intense", action = "store_true"), | |
| 58 make_option("--createDB", action = "store_true"), | |
| 59 make_option("--cores", default = 4), | |
| 60 make_option("--mzML_files", type = "character"), | |
| 61 make_option("--galaxy_names", type = "character"), | |
| 62 make_option("--grp_peaklist", type = "character"), | |
| 63 make_option("--useGroup", action = "store_true") | |
| 64 ) | |
| 65 | |
| 66 # store options | |
| 67 opt <- parse_args(OptionParser(option_list = option_list)) | |
| 68 print(opt) | |
| 69 | |
| 70 loadRData <- function(rdata_path, name) { | |
| 71 #loads an RData file, and returns the named xset object if it is there | |
| 72 load(rdata_path) | |
| 73 return(get(ls()[ls() %in% name])) | |
| 74 } | |
| 75 | |
| 76 # This function retrieve a xset like object | |
| 77 #@author Gildas Le Corguille lecorguille@sb-roscoff.fr | |
| 78 getxcmsSetObject <- function(xobject) { | |
| 79 # XCMS 1.x | |
| 80 if (class(xobject) == "xcmsSet") | |
| 81 return(xobject) | |
| 82 # XCMS 3.x | |
| 83 if (class(xobject) == "XCMSnExp") { | |
| 84 # Get the legacy xcmsSet object | |
| 85 suppressWarnings(xset <- as(xobject, "xcmsSet")) | |
| 86 sampclass(xset) <- xset@phenoData$sample_group | |
| 87 return(xset) | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 # Requires | |
| 92 pa <- loadRData(opt$pa, "pa") | |
| 93 xset <- loadRData(opt$xset, c("xset", "xdata")) | |
| 94 xset <- getxcmsSetObject(xset) | |
| 95 | |
| 96 pa@cores <- opt$cores | |
| 97 | |
| 98 print(pa@fileList) | |
| 99 print(xset@filepaths) | |
| 100 | |
| 101 if (is.null(opt$intense)) { | |
| 102 intense <- FALSE | |
| 103 }else{ | |
| 104 intense <- TRUE | |
| 105 } | |
| 106 | |
| 107 if (is.null(opt$convert2RawRT)) { | |
| 108 convert2RawRT <- FALSE | |
| 109 }else{ | |
| 110 convert2RawRT <- TRUE | |
| 111 } | |
| 112 | |
| 113 if (is.null(opt$createDB)) { | |
| 114 createDB <- FALSE | |
| 115 }else{ | |
| 116 createDB <- TRUE | |
| 117 } | |
| 118 | |
| 119 if (is.null(opt$useGroup)) { | |
| 120 fix <- xset_pa_filename_fix(opt, pa, xset) | |
| 121 pa <- fix[[1]] | |
| 122 xset <- fix[[2]] | |
| 123 useGroup <- FALSE | |
| 124 }else{ | |
| 125 # if are only aligning to the group not eah file we do not need to align the files between the xset and pa object | |
| 126 print("useGroup") | |
| 127 fix <- xset_pa_filename_fix(opt, pa) | |
| 128 pa <- fix[[1]] | |
| 129 useGroup <- TRUE | |
| 130 } | |
| 131 | |
| 132 | |
| 133 if (is.null(opt$grp_peaklist)) { | |
| 134 grp_peaklist <- NA | |
| 135 }else{ | |
| 136 grp_peaklist <- opt$grp_peaklist | |
| 137 } | |
| 138 print(useGroup) | |
| 139 | |
| 140 pa <- msPurity::frag4feature(pa = pa, | |
| 141 xset = xset, | |
| 142 ppm = opt$ppm, | |
| 143 plim = opt$plim, | |
| 144 intense = intense, | |
| 145 convert2RawRT = convert2RawRT, | |
| 146 db_name = "alldata.sqlite", | |
| 147 out_dir = opt$out_dir, | |
| 148 grp_peaklist = grp_peaklist, | |
| 149 create_db = createDB, | |
| 150 use_group = useGroup) | |
| 151 print(pa) | |
| 152 save(pa, file = file.path(opt$out_dir, "frag4feature_output.RData")) | |
| 153 | |
| 154 pa@grped_df$filename <- sapply(pa@grped_df$fileid, function(x) names(pa@fileList)[as.integer(x)]) | |
| 155 | |
| 156 print(head(pa@grped_df)) | |
| 157 write.table(pa@grped_df, file.path(opt$out_dir, "frag4feature_output.tsv"), row.names = FALSE, sep = "\t") |
