comparison frag4feature.R @ 5:f2683ec717fe draft default tip

planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit a164f06c09dc1614c2909c247ebf390aab433527-dirty
author tomnl
date Wed, 18 Sep 2019 05:46:09 -0400
parents 769ec2496d14
children
comparison
equal deleted inserted replaced
4:769ec2496d14 5:f2683ec717fe
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]])
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
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("--mostIntense", 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("--use_group", action="store_true")
64 )
65
66 # store options
67 opt<- parse_args(OptionParser(option_list=option_list))
68
69 print(opt)
70
71 loadRData <- function(rdata_path, name){
72 #loads an RData file, and returns the named xset object if it is there
73 load(rdata_path)
74 return(get(ls()[ls() %in% name]))
75 }
76
77 # This function retrieve a xset like object
78 #@author Gildas Le Corguille lecorguille@sb-roscoff.fr
79 getxcmsSetObject <- function(xobject) {
80 # XCMS 1.x
81 if (class(xobject) == "xcmsSet")
82 return (xobject)
83 # XCMS 3.x
84 if (class(xobject) == "XCMSnExp") {
85 # Get the legacy xcmsSet object
86 suppressWarnings(xset <- as(xobject, 'xcmsSet'))
87 sampclass(xset) <- xset@phenoData$sample_group
88 return (xset)
89 }
90 }
91
92 # Requires
93 pa <- loadRData(opt$pa, 'pa')
94 xset <- loadRData(opt$xset, c('xset','xdata'))
95 xset <- getxcmsSetObject(xset)
96
97 pa@cores <- opt$cores
98
99 print(pa@fileList)
100 print(xset@filepaths)
101
102 if(is.null(opt$mostIntense)){
103 mostIntense = FALSE
104 }else{
105 mostIntense = TRUE
106 }
107
108 if(is.null(opt$convert2RawRT)){
109 convert2RawRT = FALSE
110 }else{
111 convert2RawRT= TRUE
112 }
113
114 if(is.null(opt$createDB)){
115 createDB = FALSE
116 }else{
117 createDB = TRUE
118 }
119
120 if(is.null(opt$use_group)){
121 fix <- xset_pa_filename_fix(opt, pa, xset)
122 pa <- fix[[1]]
123 xset <- fix[[2]]
124 use_group=FALSE
125 }else{
126 # if are only aligning to the group not eah file we do not need to align the files between the xset and pa object
127 print('use_group')
128 fix <- xset_pa_filename_fix(opt, pa)
129 pa <- fix[[1]]
130 use_group=TRUE
131 }
132
133
134 if(is.null(opt$grp_peaklist)){
135 grp_peaklist = NA
136 }else{
137 grp_peaklist = opt$grp_peaklist
138 }
139
140 print(pa@fileList)
141 print(names(pa@fileList))
142 print(xset@filepaths)
143 saveRDS(pa, 'test_pa.rds')
144
145 pa <- msPurity::frag4feature(pa=pa, xset=xset, ppm=opt$ppm, plim=opt$plim,
146 intense=opt$mostIntense, convert2RawRT=convert2RawRT,
147 db_name='alldata.sqlite', out_dir=opt$out_dir, grp_peaklist=grp_peaklist,
148 create_db=createDB, use_group=use_group)
149
150 save(pa, file=file.path(opt$out_dir, 'frag4feature.RData'))
151
152 print(head(pa@grped_df))
153 write.table(pa@grped_df, file.path(opt$out_dir, 'frag4feature.tsv'), row.names=FALSE, sep='\t')