Mercurial > repos > tomnl > msp_split
comparison frag4feature.R @ 2:89f33758ad22 draft
planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit f79fa34772bbab836d89cf8bad52d49285409a98
| author | tomnl |
|---|---|
| date | Thu, 14 Jun 2018 09:09:58 -0400 |
| parents | |
| children | a91e186cf294 |
comparison
equal
deleted
inserted
replaced
| 1:0db4c33f5f5b | 2:89f33758ad22 |
|---|---|
| 1 library(optparse) | |
| 2 library(msPurity) | |
| 3 library(xcms) | |
| 4 print(sessionInfo()) | |
| 5 | |
| 6 xset_pa_filename_fix <- function(opt, pa, xset){ | |
| 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]]) | |
| 14 filepaths <- filepaths[filepaths != ""] | |
| 15 new_names <- basename(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 print(xset@filepaths) | |
| 31 | |
| 32 if(!all(basename(pa@fileList)==basename(xset@filepaths))){ | |
| 33 if(!all(names(pa@fileList)==basename(xset@filepaths))){ | |
| 34 print('FILELISTS DO NOT MATCH') | |
| 35 message('FILELISTS DO NOT MATCH') | |
| 36 quit(status = 1) | |
| 37 }else{ | |
| 38 xset@filepaths <- unname(pa@fileList) | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 | |
| 43 return(list(pa, xset)) | |
| 44 } | |
| 45 | |
| 46 | |
| 47 option_list <- list( | |
| 48 make_option(c("-o", "--out_dir"), type="character"), | |
| 49 make_option("--pa", type="character"), | |
| 50 make_option("--xset", type="character"), | |
| 51 make_option("--ppm", default=10), | |
| 52 make_option("--plim", default=0.0), | |
| 53 make_option("--convert2RawRT", action="store_true"), | |
| 54 make_option("--mostIntense", action="store_true"), | |
| 55 make_option("--createDB", action="store_true"), | |
| 56 make_option("--cores", default=4), | |
| 57 make_option("--mzML_files", type="character"), | |
| 58 make_option("--galaxy_names", type="character"), | |
| 59 make_option("--grp_peaklist", type="character") | |
| 60 ) | |
| 61 | |
| 62 # store options | |
| 63 opt<- parse_args(OptionParser(option_list=option_list)) | |
| 64 | |
| 65 loadRData <- function(rdata_path, name){ | |
| 66 #loads an RData file, and returns the named xset object if it is there | |
| 67 load(rdata_path) | |
| 68 return(get(ls()[ls() %in% name])) | |
| 69 } | |
| 70 | |
| 71 # This function retrieve a xset like object | |
| 72 #@author Gildas Le Corguille lecorguille@sb-roscoff.fr | |
| 73 getxcmsSetObject <- function(xobject) { | |
| 74 # XCMS 1.x | |
| 75 if (class(xobject) == "xcmsSet") | |
| 76 return (xobject) | |
| 77 # XCMS 3.x | |
| 78 if (class(xobject) == "XCMSnExp") { | |
| 79 # Get the legacy xcmsSet object | |
| 80 suppressWarnings(xset <- as(xobject, 'xcmsSet')) | |
| 81 sampclass(xset) <- xset@phenoData$sample_group | |
| 82 return (xset) | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 # Requires | |
| 87 pa <- loadRData(opt$pa, 'pa') | |
| 88 xset <- loadRData(opt$xset, c('xset','xdata')) | |
| 89 xset <- getxcmsSetObject(xset) | |
| 90 | |
| 91 pa@cores <- opt$cores | |
| 92 | |
| 93 print(pa@fileList) | |
| 94 print(xset@filepaths) | |
| 95 | |
| 96 if(is.null(opt$mostIntense)){ | |
| 97 mostIntense = FALSE | |
| 98 }else{ | |
| 99 mostIntense = TRUE | |
| 100 } | |
| 101 | |
| 102 if(is.null(opt$convert2RawRT)){ | |
| 103 convert2RawRT = FALSE | |
| 104 }else{ | |
| 105 convert2RawRT= TRUE | |
| 106 } | |
| 107 | |
| 108 if(is.null(opt$createDB)){ | |
| 109 createDB = FALSE | |
| 110 }else{ | |
| 111 createDB = TRUE | |
| 112 } | |
| 113 | |
| 114 | |
| 115 fix <- xset_pa_filename_fix(opt, pa, xset) | |
| 116 pa <- fix[[1]] | |
| 117 xset <- fix[[2]] | |
| 118 | |
| 119 if(is.null(opt$grp_peaklist)){ | |
| 120 grp_peaklist = NA | |
| 121 }else{ | |
| 122 grp_peaklist = opt$grp_peaklist | |
| 123 } | |
| 124 | |
| 125 print(pa@fileList) | |
| 126 print(names(pa@fileList)) | |
| 127 print(xset@filepaths) | |
| 128 saveRDS(pa, 'test_pa.rds') | |
| 129 | |
| 130 pa <- msPurity::frag4feature(pa=pa, xset=xset, ppm=opt$ppm, plim=opt$plim, | |
| 131 intense=opt$mostIntense, convert2RawRT=convert2RawRT, | |
| 132 db_name='alldata.sqlite', out_dir=opt$out_dir, grp_peaklist=grp_peaklist, | |
| 133 create_db=createDB) | |
| 134 | |
| 135 save(pa, file=file.path(opt$out_dir, 'frag4feature.RData')) | |
| 136 | |
| 137 print(head(pa@grped_df)) | |
| 138 write.table(pa@grped_df, file.path(opt$out_dir, 'frag4feature.tsv'), row.names=FALSE, sep='\t') |
