comparison frag4feature.R @ 0:4b417094bf71 draft

planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit 0aa10df0ec1ed71601f932cfb11d7d4d4f620d80-dirty
author tomnl
date Wed, 02 May 2018 13:09:23 -0400
parents
children dd6a4d101f9a
comparison
equal deleted inserted replaced
-1:000000000000 0:4b417094bf71
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('heck')
126 print(pa@fileList)
127 print(names(pa@fileList))
128 print(xset@filepaths)
129 saveRDS(pa, 'test_pa.rds')
130
131 pa <- msPurity::frag4feature(pa=pa, xset=xset, ppm=opt$ppm, plim=opt$plim,
132 intense=opt$mostIntense, convert2RawRT=convert2RawRT,
133 db_name='alldata.sqlite', out_dir=opt$out_dir, grp_peaklist=grp_peaklist,
134 create_db=createDB)
135
136 save(pa, file=file.path(opt$out_dir, 'frag4feature.RData'))
137
138 print(head(pa@grped_df))
139 write.table(pa@grped_df, file.path(opt$out_dir, 'frag4feature.tsv'), row.names=FALSE, sep='\t')