comparison createDatabase.R @ 0:50eedf5ad217 draft

"planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit cb903cd93f9378cfb5eeb68512a54178dcea7bbc-dirty"
author computational-metabolomics
date Wed, 27 Nov 2019 13:35:22 -0500
parents
children 96fba93b4c8a
comparison
equal deleted inserted replaced
-1:000000000000 0:50eedf5ad217
1 library(msPurity)
2 library(optparse)
3 library(xcms)
4 library(CAMERA)
5 print(sessionInfo())
6 print('CREATING DATABASE')
7
8 xset_pa_filename_fix <- function(opt, pa, xset){
9
10 if (!is.null(opt$mzML_files) && !is.null(opt$galaxy_names)){
11 # NOTE: Relies on the pa@fileList having the names of files given as 'names' of the variables
12 # needs to be done due to Galaxy moving the files around and screwing up any links to files
13
14 filepaths <- trimws(strsplit(opt$mzML_files, ',')[[1]])
15 filepaths <- filepaths[filepaths != ""]
16 new_names <- basename(filepaths)
17
18 galaxy_names <- trimws(strsplit(opt$galaxy_names, ',')[[1]])
19 galaxy_names <- galaxy_names[galaxy_names != ""]
20
21 nsave <- names(pa@fileList)
22 old_filenames <- basename(pa@fileList)
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
30
31 if(!all(basename(pa@fileList)==basename(xset@filepaths))){
32 if(!all(names(pa@fileList)==basename(xset@filepaths))){
33 print('FILELISTS DO NOT MATCH')
34 message('FILELISTS DO NOT MATCH')
35 quit(status = 1)
36 }else{
37 xset@filepaths <- unname(pa@fileList)
38 }
39 }
40
41 print(xset@phenoData)
42 print(xset@filepaths)
43
44 return(list(pa, xset))
45 }
46
47
48
49
50 option_list <- list(
51 make_option(c("-o", "--outDir"), type="character"),
52 make_option("--pa", type="character"),
53 make_option("--xset_xa", type="character"),
54 make_option("--xcms_camera_option", type="character"),
55 make_option("--eic", 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("--grpPeaklist", type="character")
60 )
61
62
63 # store options
64 opt<- parse_args(OptionParser(option_list=option_list))
65 print(opt)
66
67 loadRData <- function(rdata_path, name){
68 #loads an RData file, and returns the named xset object if it is there
69 load(rdata_path)
70 return(get(ls()[ls() %in% name]))
71 }
72
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
87 print(paste('pa', opt$pa))
88 print(opt$xset)
89
90 print(opt$xcms_camera_option)
91 # Requires
92 pa <- loadRData(opt$pa, 'pa')
93
94
95 print(pa@fileList)
96
97
98
99 if (opt$xcms_camera_option=='xcms'){
100
101 xset <- loadRData(opt$xset, c('xset','xdata'))
102 xset <- getxcmsSetObject(xset)
103 fix <- xset_pa_filename_fix(opt, pa, xset)
104 pa <- fix[[1]]
105 xset <- fix[[2]]
106 xa <- NULL
107 }else{
108
109 xa <- loadRData(opt$xset, 'xa')
110 fix <- xset_pa_filename_fix(opt, pa, xa@xcmsSet)
111 pa <- fix[[1]]
112 xa@xcmsSet <- fix[[2]]
113 xset <- NULL
114 }
115
116
117
118 if(is.null(opt$grp_peaklist)){
119 grpPeaklist = NA
120 }else{
121 grpPeaklist = opt$grp_peaklist
122 }
123
124
125
126 dbPth <- msPurity::createDatabase(pa,
127 xset=xset,
128 xsa=xa,
129 outDir=opt$outDir,
130 grpPeaklist=grpPeaklist,
131 dbName='createDatabase_output.sqlite'
132 )
133
134
135
136
137
138 if (!is.null(opt$eic)){
139
140 if (is.null(xset)){
141 xset <- xa@xcmsSet
142 }
143 # previous check should have matched filelists together
144 xset@filepaths <- unname(pa@fileList)
145
146 convert2Raw <- function(x, xset){
147 sid <- unique(x$sample)
148 # for each file get list of peaks
149 x$rt_raw <- xset@rt$raw[[sid]][match(x$rt, xset@rt$corrected[[sid]])]
150 x$rtmin_raw <- xset@rt$raw[[sid]][match(x$rtmin, xset@rt$corrected[[sid]])]
151 x$rtmax_raw <- xset@rt$raw[[sid]][match(x$rtmax, xset@rt$corrected[[sid]])]
152 return(x)
153
154 }
155
156 xset@peaks <- as.matrix(plyr::ddply(data.frame(xset@peaks), ~ sample, convert2Raw, xset=xset))
157
158 # Saves the EICS into the previously created database
159 px <- msPurity::purityX(xset,
160 saveEIC = TRUE,
161 cores=1,
162 sqlitePth=dbPth,
163 rtrawColumns = TRUE)
164
165 }
166
167 closeAllConnections()