Mercurial > repos > computational-metabolomics > mspurity_combineannotations
comparison createDatabase.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(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 | |
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 pa@fileList <- filepaths[match(names(pa@fileList), galaxy_names)] | |
23 names(pa@fileList) <- nsave | |
24 | |
25 pa@puritydf$filename <- basename(pa@fileList[match(pa@puritydf$filename, old_filenames)]) | |
26 pa@grped_df$filename <- basename(pa@fileList[match(pa@grped_df$filename, old_filenames)]) | |
27 } | |
28 | |
29 | |
30 if (!all(basename(pa@fileList) == basename(xset@filepaths))) { | |
31 if (!all(names(pa@fileList) == basename(xset@filepaths))) { | |
32 print("FILELISTS DO NOT MATCH") | |
33 message("FILELISTS DO NOT MATCH") | |
34 quit(status = 1) | |
35 }else{ | |
36 xset@filepaths <- unname(pa@fileList) | |
37 } | |
38 } | |
39 | |
40 print(xset@phenoData) | |
41 print(xset@filepaths) | |
42 | |
43 return(list(pa, xset)) | |
44 } | |
45 | |
46 | |
47 | |
48 | |
49 option_list <- list( | |
50 make_option(c("-o", "--outDir"), type = "character"), | |
51 make_option("--pa", type = "character"), | |
52 make_option("--xset_xa", type = "character"), | |
53 make_option("--xcms_camera_option", type = "character"), | |
54 make_option("--eic", action = "store_true"), | |
55 make_option("--cores", default = 4), | |
56 make_option("--mzML_files", type = "character"), | |
57 make_option("--galaxy_names", type = "character"), | |
58 make_option("--grpPeaklist", type = "character") | |
59 ) | |
60 | |
61 | |
62 # store options | |
63 opt <- parse_args(OptionParser(option_list = option_list)) | |
64 print(opt) | |
65 | |
66 loadRData <- function(rdata_path, name) { | |
67 #loads an RData file, and returns the named xset object if it is there | |
68 load(rdata_path) | |
69 return(get(ls()[ls() %in% name])) | |
70 } | |
71 | |
72 getxcmsSetObject <- function(xobject) { | |
73 # XCMS 1.x | |
74 if (class(xobject) == "xcmsSet") | |
75 return(xobject) | |
76 # XCMS 3.x | |
77 if (class(xobject) == "XCMSnExp") { | |
78 # Get the legacy xcmsSet object | |
79 suppressWarnings(xset <- as(xobject, "xcmsSet")) | |
80 xcms::sampclass(xset) <- xset@phenoData$sample_group | |
81 return(xset) | |
82 } | |
83 } | |
84 | |
85 | |
86 print(paste("pa", opt$pa)) | |
87 print(opt$xset) | |
88 | |
89 print(opt$xcms_camera_option) | |
90 # Requires | |
91 pa <- loadRData(opt$pa, "pa") | |
92 | |
93 | |
94 print(pa@fileList) | |
95 | |
96 # Missing list element causes failures (should be updated | |
97 # in msPurity R package for future releases) | |
98 if (!exists("allfrag", where = pa@filter_frag_params)) { | |
99 pa@filter_frag_params$allfrag <- FALSE | |
100 } | |
101 | |
102 if (opt$xcms_camera_option == "xcms") { | |
103 | |
104 xset <- loadRData(opt$xset, c("xset", "xdata")) | |
105 xset <- getxcmsSetObject(xset) | |
106 fix <- xset_pa_filename_fix(opt, pa, xset) | |
107 pa <- fix[[1]] | |
108 xset <- fix[[2]] | |
109 xa <- NULL | |
110 }else{ | |
111 | |
112 xa <- loadRData(opt$xset, "xa") | |
113 fix <- xset_pa_filename_fix(opt, pa, xa@xcmsSet) | |
114 pa <- fix[[1]] | |
115 xa@xcmsSet <- fix[[2]] | |
116 xset <- NULL | |
117 } | |
118 | |
119 | |
120 if (is.null(opt$grpPeaklist)) { | |
121 grpPeaklist <- NA | |
122 }else{ | |
123 grpPeaklist <- opt$grpPeaklist | |
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( | |
157 plyr::ddply(data.frame(xset@peaks), ~ sample, convert2Raw, xset = xset)) | |
158 | |
159 # Saves the EICS into the previously created database | |
160 px <- msPurity::purityX(xset, | |
161 saveEIC = TRUE, | |
162 cores = 1, | |
163 sqlitePth = dbPth, | |
164 rtrawColumns = TRUE) | |
165 | |
166 } | |
167 | |
168 closeAllConnections() |