annotate XSeekerPreparator.R @ 12:bdb2878ee189 draft

" master branch Updating"
author lain
date Wed, 07 Apr 2021 13:05:36 +0000
parents f4fc4a0f41e2
children 26f01380145d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
2
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
3 TOOL_NAME <- "XSeekerPreparator"
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
4 VERSION <- "1.2.0"
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
5
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
6 OUTPUT_SPECIFIC_TOOL <- "XSeeker_Galaxy"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
7
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
8 ENRICHED_RDATA_VERSION <- paste("1.1.2", OUTPUT_SPECIFIC_TOOL, sep="-")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
9 ENRICHED_RDATA_DOC <- sprintf("
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
10 Welcome to the enriched <Version %s> of the output of CAMERA/xcms.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
11 This doc was generated by the tool: %s - Version %s
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
12 To show the different variables contained in this rdata, type:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
13 - `load('this_rdata.rdata', rdata_env <- new.env())`
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
14 - `names(rdata_env)`
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
15
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
16 Sections
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
17 ######
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
18
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
19
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
20 This tools helpers
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
21 ------
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
22 The version number is somewhat special because the evolution of the
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
23 rdata's format is non-linear.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
24 There may be different branches, each evolving separatly.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
25 To reflect these branches's diversions, there may be a prepended
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
26 branch name following this format:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
27 major.minor.patch-branch_name
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
28 Like this, we can process rdata with the same tool, and output
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
29 rdata formated differently, for each tool.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
30
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
31
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
32 - enriched_rdata:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
33 - Description: flag created by that tool to tell it was enriched.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
34 - Retrieval method: enriched_rdata <- TRUE
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
35
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
36 - enriched_rdata_version:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
37 - Description: A flag created by that tool to tell which version of
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
38 this tool has enriched the rdata.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
39 - Retrieval method: enriched_rdata_version <- sprintf(\"%s\", ENRICHED_RDATA_VERSION)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
40
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
41 - enriched_rdata_doc:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
42 - Description: Contains the documentation string.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
43
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
44 Data from original mzxml file
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
45 ------
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
46 - tic:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
47 - Description: Those are the tic values from the original mzxml
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
48 file, extracted using xcms 2.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
49 - Retrieval method: xcms::xcmsRaw('original_file.mzxml')@tic
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
50 - xcms version: 2.0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
51
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
52 - mz:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
53 - Description: Those are the m/z values from the original mzxml
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
54 file, extracted using xcms 2.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
55 - Retrieval method: xcms::xcmsRaw('original_file.mzxml')@env$mz
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
56 - xcms version: 2.0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
57
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
58 - scanindex:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
59 - Description: Those are the scanindex values from the original mzxml
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
60 file, extracted using xcms 2.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
61 - Retrieval method: xcms::xcmsRaw('original_file.mzxml')@scanindex
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
62 - xcms version: 2.0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
63
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
64 - scantime:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
65 - Description: Those are the scantime values from the original mzxml
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
66 file, extracted using xcms 2.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
67 - Retrieval method: xcms::xcmsRaw('original_file.mzxml')@scantime
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
68 - xcms version: 2.0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
69
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
70 - intensity:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
71 - Description: Those are the intensity values from the original mzxml
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
72 file, extracted using xcms 2.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
73 - Retrieval method: xcms::xcmsRaw('original_file.mzxml')@env$intensity
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
74 - xcms version: 2.0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
75
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
76 - polarity:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
77 - Description: Those are the polarity values from the original mzxml
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
78 file, extracted using xcms 2.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
79 - Retrieval method: as.character(xcms::xcmsRaw('original_file.mzxml')@polarity[[1]])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
80 - xcms version: 2.0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
81
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
82 Data taken from incoming rdata
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
83 ------
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
84 - variableMetadata:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
85 - Description: Unmodified copy of variableMetadata from incoming rdata.
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
86 - Retrieval method: rdata_file$variableMetadata
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
87
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
88 - process_params:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
89 - Description: Those are the processing parameters values from the
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
90 curent rdata. They have been simplified to allow easy access like:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
91 for (params in process_params) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
92 if (params[[\"xfunction\"]] == \"annotatediff\") {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
93 process_peak_picking_params(params)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
94 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
95 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
96 - Retrieval method:
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
97 ## just he same list, but simplified
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
98 process_params <- list()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
99 for (list_name in names(rdata_file$listOFlistArguments)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
100 param_list <- list()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
101 for (param_name in names(rdata_file$listOFlistArguments[[list_name]])) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
102 param_list[[param_name]] <- rdata_file$listOFlistArguments[[list_name]][[param_name]]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
103 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
104 process_params[[length(process_params)+1]] <- param_list
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
105 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
106 ", ENRICHED_RDATA_VERSION, TOOL_NAME, VERSION, ENRICHED_RDATA_VERSION)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
107
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
108
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
109
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
110 get_models <- function(path) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
111 if (is.null(path)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
112 stop("No models to define the database schema")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
113 } else {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
114 message(sprintf("Loading models from %s", path))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
115 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
116 ## galaxy mangles the "@" to a "__at__"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
117 if (substr(path, 1, 9) == "git__at__") {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
118 path <- sub("^git__at__", "git@", path, perl=TRUE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
119 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
120 if (
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
121 substr(path, 1, 4) == "git@"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
122 || substr(path, length(path)-4, 4) == ".git"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
123 ) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
124 return (get_models_from_git(path))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
125 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
126 if (substr(path, 1, 4) == "http") {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
127 return (get_models_from_url(path))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
128 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
129 return (source(path)$value)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
130 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
131
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
132 get_models_from_git <- function (url, target_file="models.R", rm=TRUE) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
133 tmp <- tempdir()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
134 message(sprintf("Cloning %s", url))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
135 system2("git", c("clone", url, tmp))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
136 result <- search_tree(file.path(tmp, dir), target_file)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
137 if (!is.null(result)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
138 models <- source(result)$value
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
139 if (rm) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
140 unlink(tmp, recursive=TRUE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
141 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
142 return (models)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
143 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
144 if (rm) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
145 unlink(tmp, recursive=TRUE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
146 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
147 stop(sprintf(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
148 "Could not find any file named \"%s\" in this repo",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
149 target_file
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
150 ))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
151 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
152
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
153 get_models_from_url <- function (url, target_file="models.R", rm=TRUE) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
154 tmp <- tempdir()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
155 message(sprintf("Downloading %s", url))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
156 result <- file.path(tmp, target_file)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
157 if (download.file(url, destfile=result) == 0) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
158 models <- source(result)$value
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
159 if (rm) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
160 unlink(tmp, recursive=TRUE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
161 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
162 return (models)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
163 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
164 if (rm) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
165 unlink(tmp, recursive=TRUE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
166 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
167 stop("Could not download any file at this adress.")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
168 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
169
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
170 search_tree <- function(path, target) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
171 target <- tolower(target)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
172 for (file in list.files(path)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
173 if (is.dir(file)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
174 result <- search_tree(file.path(path, file), target)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
175 if (!is.null(result)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
176 return (result)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
177 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
178 } else if (tolower(file) == target) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
179 return (file.path(path, file))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
180 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
181 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
182 return (NULL)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
183 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
184
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
185 create_database <- function(orm) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
186 orm$recreate_database(no_exists=FALSE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
187 set_database_version(orm, "created")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
188 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
189
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
190 insert_adducts <- function(orm) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
191 message("Creating adducts...")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
192 adducts <- list(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
193 list("[M-H2O-H]-",1,-1,-48.992020312000001069,1,0,0.5,"H0","H1O3"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
194 list("[M-H-Cl+O]-",1,-1,-19.981214542000000022,2,0,0.5,"O1","H1Cl1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
195 list("[M-Cl+O]-",1,-1,-18.973389510000000512,3,0,0.5,"O1","Cl1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
196 list("[M-3H]3-",1,-3,-3.0218293560000000219,4,0,1.0,"H0","H3"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
197 list("[2M-3H]3-",2,-3,-3.0218293560000000219,4,0,0.5,"H0","H3"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
198 list("[3M-3H]3-",3,-3,-3.0218293560000000219,4,0,0.5,"H0","H3"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
199 list("[M-2H]2-",1,-2,-2.0145529039999998666,5,0,1.0,"H0","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
200 list("[2M-2H]2-",2,-2,-2.0145529039999998666,5,0,0.5,"H0","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
201 list("[3M-2H]2-",3,-2,-2.0145529039999998666,5,0,0.5,"H0","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
202 list("[M-H]-",1,-1,-1.0072764519999999333,6,1,1.0,"H0","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
203 list("[2M-H]-",2,-1,-1.0072764519999999333,6,0,0.5,"H0","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
204 list("[3M-H]-",3,-1,-1.0072764519999999333,6,0,0.5,"H0","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
205 list("[M]+",1,1,-0.00054858000000000000945,7,1,1.0,"H0","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
206 list("[M]-",1,-1,0.00054858000000000000945,8,1,1.0,"H0","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
207 list("[M+H]+",1,1,1.0072764519999999333,9,1,1.0,"H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
208 list("[2M+H]+",2,1,1.0072764519999999333,9,0,0.5,"H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
209 list("[3M+H]+",3,1,1.0072764519999999333,9,0,0.25,"H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
210 list("[M+2H]2+",1,2,2.0145529039999998666,10,0,0.75,"H2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
211 list("[2M+2H]2+",2,2,2.0145529039999998666,10,0,0.5,"H2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
212 list("[3M+2H]2+",3,2,2.0145529039999998666,10,0,0.25,"H2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
213 list("[M+3H]3+",1,3,3.0218293560000000219,11,0,0.75,"H3","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
214 list("[2M+3H]3+",2,3,3.0218293560000000219,11,0,0.5,"H3","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
215 list("[3M+3H]3+",3,3,3.0218293560000000219,11,0,0.25,"H3","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
216 list("[M-2H+NH4]-",1,-1,16.019272654000001665,12,0,0.25,"N1H4","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
217 list("[2M-2H+NH4]-",2,-1,16.019272654000001665,12,0,0.0,"N1H4","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
218 list("[3M-2H+NH4]-",3,-1,16.019272654000001665,12,0,0.25,"N1H4","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
219 list("[M+NH4]+",1,1,18.033825558000000199,13,1,1.0,"N1H4","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
220 list("[2M+NH4]+",2,1,18.033825558000000199,13,0,0.5,"N1H4","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
221 list("[3M+NH4]+",3,1,18.033825558000000199,13,0,0.25,"N1H4","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
222 list("[M+H+NH4]2+",1,2,19.041102009999999467,14,0,0.5,"N1H5","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
223 list("[2M+H+NH4]2+",2,2,19.041102009999999467,14,0,0.5,"N1H5","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
224 list("[3M+H+NH4]2+",3,2,19.041102009999999467,14,0,0.25,"N1H5","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
225 list("[M+Na-2H]-",1,-1,20.974668176000001551,15,0,0.75,"Na1","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
226 list("[2M-2H+Na]-",2,-1,20.974668176000001551,15,0,0.25,"Na1","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
227 list("[3M-2H+Na]-",3,-1,20.974668176000001551,15,0,0.25,"Na1","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
228 list("[M+Na]+",1,1,22.989221080000000086,16,1,1.0,"Na1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
229 list("[2M+Na]+",2,1,22.989221080000000086,16,0,0.5,"Na1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
230 list("[3M+Na]+",3,1,22.989221080000000086,16,0,0.25,"Na1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
231 list("[M+H+Na]2+",1,2,23.996497531999999353,17,0,0.5,"Na1H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
232 list("[2M+H+Na]2+",2,2,23.996497531999999353,17,0,0.5,"Na1H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
233 list("[3M+H+Na]2+",3,2,23.996497531999999353,17,0,0.25,"Na1H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
234 list("[M+2H+Na]3+",1,3,25.003773983999998619,18,0,0.25,"H2Na1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
235 list("[M+CH3OH+H]+",1,1,33.033491200000000276,19,0,0.25,"C1O1H5","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
236 list("[M-H+Cl]2-",1,-2,33.962124838000001148,20,0,1.0,"Cl1","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
237 list("[2M-H+Cl]2-",2,-2,33.962124838000001148,20,0,0.5,"Cl1","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
238 list("[3M-H+Cl]2-",3,-2,33.962124838000001148,20,0,0.5,"Cl1","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
239 list("[M+Cl]-",1,-1,34.969401290000000416,21,1,1.0,"Cl1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
240 list("[2M+Cl]-",2,-1,34.969401290000000416,21,0,0.5,"Cl1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
241 list("[3M+Cl]-",3,-1,34.969401290000000416,21,0,0.5,"Cl1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
242 list("[M+K-2H]-",1,-1,36.948605415999999479,22,0,0.5,"K1","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
243 list("[2M-2H+K]-",2,-1,36.948605415999999479,22,0,0.0,"K1","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
244 list("[3M-2H+K]-",3,-1,36.948605415999999479,22,0,0.0,"K1","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
245 list("[M+K]+",1,1,38.963158319999998013,23,1,1.0,"K1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
246 list("[2M+K]+",2,1,38.963158319999998013,23,0,0.5,"K1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
247 list("[3M+K]+",3,1,38.963158319999998013,23,0,0.25,"K1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
248 list("[M+H+K]2+",1,2,39.970434771999997281,24,0,0.5,"K1H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
249 list("[2M+H+K]2+",2,2,39.970434771999997281,24,0,0.5,"K1H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
250 list("[3M+H+K]2+",3,2,39.970434771999997281,24,0,0.25,"K1H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
251 list("[M+ACN+H]+",1,1,42.033825557999996646,25,0,0.25,"C2H4N1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
252 list("[2M+ACN+H]+",2,1,42.033825557999996646,25,0,0.25,"C2H4N1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
253 list("[M+2Na-H]+",1,1,44.971165708000000902,26,0,0.5,"Na2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
254 list("[2M+2Na-H]+",2,1,44.971165708000000902,26,0,0.25,"Na2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
255 list("[3M+2Na-H]+",3,1,44.971165708000000902,26,0,0.25,"Na2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
256 list("[2M+FA-H]-",2,-1,44.998202851999998586,27,0,0.25,"C1O2H2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
257 list("[M+FA-H]-",1,-1,44.998202851999998586,27,0,0.5,"C1O2H2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
258 list("[M+2Na]2+",1,2,45.978442160000000172,28,0,0.5,"Na2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
259 list("[2M+2Na]2+",2,2,45.978442160000000172,28,0,0.5,"Na2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
260 list("[3M+2Na]2+",3,2,45.978442160000000172,28,0,0.25,"Na2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
261 list("[M+H+2Na]3+",1,3,46.985718611999999438,29,0,0.25,"H1Na2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
262 list("[M+H+FA]+",1,1,47.012755755999997122,30,0,0.25,"C1O2H3","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
263 list("[M+Hac-H]-",1,-1,59.013852915999997607,31,0,0.25,"C2O2H4","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
264 list("[2M+Hac-H]-",2,-1,59.013852915999997607,31,0,0.25,"C2O2H4","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
265 list("[M+IsoProp+H]+",1,1,61.064791327999998317,32,0,0.25,"C3H9O1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
266 list("[M+Na+K]2+",1,2,61.9523793999999981,33,0,0.5,"Na1K1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
267 list("[2M+Na+K]2+",2,2,61.9523793999999981,33,0,0.5,"Na1K1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
268 list("[3M+Na+K]2+",3,2,61.9523793999999981,33,0,0.25,"Na1K1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
269 list("[M+NO3]-",1,-1,61.988366450000000895,34,0,0.5,"N1O3","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
270 list("[M+ACN+Na]+",1,1,64.015770185999997464,35,0,0.25,"C2H3N1Na1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
271 list("[2M+ACN+Na]+",2,1,64.015770185999997464,35,0,0.25,"C2H3N1Na1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
272 list("[M+NH4+FA]+",1,1,64.039304861999994502,36,0,0.25,"N1C1O2H6","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
273 list("[M-2H+Na+FA]-",1,-1,66.980147479999999405,37,0,0.5,"NaC1O2H2","H2"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
274 list("[M+3Na]3+",1,3,68.967663239999993153,38,0,0.25,"Na3","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
275 list("[M+Na+FA]+",1,1,68.99470038399999794,39,0,0.25,"Na1C1O2H2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
276 list("[M+2Cl]2-",1,-2,69.938802580000000832,40,0,1.0,"Cl2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
277 list("[2M+2Cl]2-",2,-2,69.938802580000000832,40,0,0.5,"Cl2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
278 list("[3M+2Cl]2-",3,-2,69.938802580000000832,40,0,0.5,"Cl2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
279 list("[M+2K-H]+",1,1,76.919040187999996758,41,0,0.5,"K2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
280 list("[2M+2K-H]+",2,1,76.919040187999996758,41,0,0.25,"K2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
281 list("[3M+2K-H]+",3,1,76.919040187999996758,41,0,0.25,"K2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
282 list("[M+2K]2+",1,2,77.926316639999996028,42,0,0.5,"K2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
283 list("[2M+2K]2+",2,2,77.926316639999996028,42,0,0.5,"K2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
284 list("[3M+2K]2+",3,2,77.926316639999996028,42,0,0.25,"K2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
285 list("[M+Br]-",1,-1,78.918886479999997619,43,1,1.0,"Br1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
286 list("[M+Cl+FA]-",1,-1,80.974880593999998268,44,0,0.5,"Cl1C1O2H2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
287 list("[M+AcNa-H]-",1,-1,80.995797543999998426,45,0,0.25,"C2H3Na1O2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
288 list("[M+2ACN+2H]2+",1,2,84.067651115999993292,46,0,0.25,"C4H8N2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
289 list("[M+K+FA]+",1,1,84.968637623999995868,47,0,0.25,"K1C1O2H2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
290 list("[M+Cl+Na+FA-H]-",1,-1,102.95682522200000619,48,0,0.5,"Cl1Na1C1O2H2","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
291 list("[2M+3H2O+2H]+",2,1,104.03153939599999944,49,0,0.25,"H8O6","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
292 list("[M+TFA-H]-",1,-1,112.98558742000000165,50,0,0.5,"C2F3O2H1","H1"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
293 list("[M+H+TFA]+",1,1,115.00014032400000019,51,0,0.25,"C2F3O2H2","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
294 list("[M+3ACN+2H]2+",1,2,125.09420022199999778,52,0,0.25,"C6H11N3","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
295 list("[M+NH4+TFA]+",1,1,132.02668943000000468,53,0,0.25,"N1C2F3O2H5","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
296 list("[M+Na+TFA]+",1,1,136.98208495200000811,54,0,0.25,"Na1C2F3O2H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
297 list("[M+Cl+TFA]-",1,-1,148.96226516199999423,55,0,0.5,"Cl1C2F3O2H1","H0"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
298 list("[M+K+TFA]+",1,1,152.95602219200000604,56,0,0.25,"K1C2F3O2H1","H0")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
299 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
300 dummy_adduct <- orm$adduct()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
301 for (adduct in adducts) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
302 i <- 0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
303 dummy_adduct$set_name(adduct[[i <- i+1]])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
304 dummy_adduct$set_multi(adduct[[i <- i+1]])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
305 dummy_adduct$set_charge(adduct[[i <- i+1]])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
306 dummy_adduct$set_mass(adduct[[i <- i+1]])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
307 dummy_adduct$set_oidscore(adduct[[i <- i+1]])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
308 dummy_adduct$set_quasi(adduct[[i <- i+1]])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
309 dummy_adduct$set_ips(adduct[[i <- i+1]])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
310 dummy_adduct$set_formula_add(adduct[[i <- i+1]])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
311 dummy_adduct$set_formula_ded(adduct[[i <- i+1]])
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
312 invisible(dummy_adduct$save())
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
313 dummy_adduct$clear(unset_id=TRUE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
314 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
315 message("Adducts created")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
316 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
317
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
318 insert_base_data <- function(orm, path, archetype=FALSE) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
319 if (archetype) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
320 ## not implemented yet
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
321 return ()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
322 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
323 base_data <- readLines(path)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
324 for (sql in strsplit(paste(base_data, collapse=" "), ";")[[1]]) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
325 orm$execute(sql)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
326 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
327 set_database_version(orm, "enriched")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
328 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
329
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
330 insert_compounds <- function(orm, compounds_path) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
331 compounds <- read.csv(file=compounds_path, sep="\t")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
332 if (is.null(compounds <- translate_compounds(compounds))) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
333 stop("Could not find asked compound's attributes in csv file.")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
334 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
335 dummy_compound <- orm$compound()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
336 compound_list <- list()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
337 for (i in seq_len(nrow(compounds))) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
338 dummy_compound$set_mz(compounds[i, "mz"])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
339 dummy_compound$set_name(compounds[i, "name"])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
340 dummy_compound$set_common_name(compounds[i, "common_name"])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
341 dummy_compound$set_formula(compounds[i, "formula"])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
342 compound_list[[length(compound_list)+1]] <- as.list(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
343 dummy_compound,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
344 c("mz", "name", "common_name", "formula")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
345 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
346 dummy_compound$clear(unset_id=TRUE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
347 }
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
348 invisible(dummy_compound$save(bulk=compound_list))
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
349 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
350
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
351 translate_compounds <- function(compounds) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
352 recognized_headers <- list(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
353 c("HMDB_ID", "MzBank", "X.M.H..", "X.M.H...1", "MetName", "ChemFormula", "INChIkey")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
354 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
355 header_translators <- list(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
356 hmdb_header_translator
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
357 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
358 for (index in seq_along(recognized_headers)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
359 headers <- recognized_headers[[index]]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
360 if (identical(colnames(compounds), headers)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
361 return (header_translators[[index]](compounds))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
362 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
363 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
364 if (is.null(translator <- guess_translator(colnames(compounds)))) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
365 return (NULL)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
366 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
367 return (csv_header_translator(translator, compounds))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
368 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
369
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
370 guess_translator <- function(header) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
371 result <- list(
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
372 # HMDB_ID=NULL,
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
373 mz=NULL,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
374 name=NULL,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
375 common_name=NULL,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
376 formula=NULL,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
377 # inchi_key=NULL
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
378 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
379 asked_cols <- names(result)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
380 for (asked_col in asked_cols) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
381 for (col in header) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
382 if ((twisted <- tolower(col)) == asked_col
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
383 || gsub("-", "_", twisted) == asked_col
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
384 || gsub(" ", "_", twisted) == asked_col
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
385 || tolower(gsub("(.)([A-Z])", "\\1_\\2", col)) == asked_col
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
386 ) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
387 result[[asked_col]] <- col
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
388 next
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
389 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
390 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
391 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
392 if (any(mapply(is.null, result))) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
393 return (NULL)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
394 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
395 return (result)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
396 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
397
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
398 hmdb_header_translator <- function(compounds) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
399 return (csv_header_translator(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
400 list(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
401 HMDB_ID="HMDB_ID",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
402 mz="MzBank",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
403 name="MetName",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
404 common_name="MetName",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
405 formula="ChemFormula",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
406 inchi_key="INChIkey"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
407 ), compounds
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
408 ))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
409 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
410
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
411 csv_header_translator <- function(translation_table, csv) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
412 header_names <- names(translation_table)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
413 result <- data.frame(1:nrow(csv))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
414 for (i in seq_along(header_names)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
415 result[, header_names[[i]]] <- csv[, translation_table[[i]]]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
416 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
417 result[, "mz"] <- as.numeric(result[, "mz"])
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
418 return (result)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
419 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
420
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
421 set_database_version <- function(orm, version) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
422 orm$set_tag(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
423 version,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
424 tag_name="database_version",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
425 tag_table_name="XSeeker_tagging_table"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
426 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
427 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
428
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
429 process_rdata <- function(orm, rdata, options) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
430 mzml_tmp_dir <- gather_mzml_files(rdata)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
431 samples <- names(rdata$singlefile)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
432 if (!is.null(options$samples)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
433 samples <- samples[options$samples %in% samples]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
434 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
435 show_percent <- (
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
436 is.null(options$`not-show-percent`)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
437 || options$`not-show-percent` == FALSE
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
438 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
439 error <- tryCatch({
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
440 process_sample_list(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
441 orm, rdata, samples,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
442 show_percent=show_percent
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
443 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
444 NULL
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
445 }, error=function(e) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
446 message(e)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
447 e
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
448 })
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
449 if (!is.null(mzml_tmp_dir)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
450 unlink(mzml_tmp_dir, recursive=TRUE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
451 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
452 if (!is.null(error)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
453 stop(error)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
454 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
455 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
456
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
457 gather_mzml_files <- function(rdata) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
458 if (is.null(rdata$singlefile)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
459 message("Extracting mxml files")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
460 tmp <- tempdir()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
461 rdata$singlefile <- utils::unzip(rdata$zipfile, exdir=tmp)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
462 names(rdata$singlefile) <- tools::file_path_sans_ext(basename(rdata$singlefile))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
463 message("Extracted")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
464 return (tmp)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
465 } else {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
466 message(sprintf("Not a zip file, loading files directly from path: %s", paste(rdata$singlefile, collapse=" ; ")))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
467 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
468 return (NULL)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
469 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
470
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
471 process_sample_list <- function(orm, radta, sample_names, show_percent) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
472 file_grouping_var <- find_grouping_var(rdata$variableMetadata)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
473 message("Processing samples.")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
474 message(sprintf("File grouping variable: %s", file_grouping_var))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
475 if(is.null(file_grouping_var)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
476 stop("Malformed variableMetada.")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
477 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
478
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
479 context <- new.env()
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
480 context$samples <- list()
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
481 context$peaks <- rdata$xa@xcmsSet@peaks
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
482 context$groupidx <- rdata$xa@xcmsSet@groupidx
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
483 xcms_set <- rdata$xa@xcmsSet
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
484 singlefile <- rdata$singlefile
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
485 process_arg_list <- rdata$listOFlistArguments
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
486 var_meta <- rdata$variableMetadata
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
487
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
488 process_params <- list()
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
489 if (is.null(process_arg_list)) {
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
490 histories <- list()
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
491 for (history in xcms_set@.processHistory) {
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
492 if (
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
493 class(history@param) == "CentWaveParam"
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
494 && history@type == "Peak detection"
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
495 ) {
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
496 params <- history@param
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
497 process_params <- list(list(
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
498 xfunction="annotatediff",
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
499 ppm=params@ppm,
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
500 peakwidth=sprintf("%s - %s", params@peakwidth[[1]], params@peakwidth[[2]]),
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
501 snthresh=params@snthresh,
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
502 prefilterStep=params@prefilter[[1]],
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
503 prefilterLevel=params@prefilter[[2]],
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
504 mzdiff=params@mzdiff,
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
505 fitgauss=params@fitgauss,
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
506 noise=params@noise,
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
507 mzCenterFun=params@mzCenterFun,
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
508 integrate=params@integrate,
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
509 firstBaselineCheck=params@firstBaselineCheck,
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
510 snthreshIsoROIs=!identical(params@roiScales, numeric(0))
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
511 ))
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
512 break
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
513 }
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
514 }
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
515 } else {
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
516 for (list_name in names(process_arg_list)) {
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
517 param_list <- list()
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
518 for (param_name in names(process_arg_list[[list_name]])) {
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
519 param_list[[param_name]] <- process_arg_list[[list_name]][[param_name]]
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
520 }
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
521 process_params[[length(process_params)+1]] <- param_list
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
522 }
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
523 }
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
524
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
525 message("Parameters from previous processes extracted.")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
526
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
527
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
528 indices <- as.numeric(unique(var_meta[, file_grouping_var]))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
529 smol_xcms_set <- orm$smol_xcms_set()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
530 mz_tab_info <- new.env()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
531 g <- xcms::groups(xcms_set)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
532 mz_tab_info$group_length <- nrow(g)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
533 mz_tab_info$dataset_path <- xcms::filepaths(xcms_set)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
534 mz_tab_info$sampnames <- xcms::sampnames(xcms_set)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
535 mz_tab_info$sampclass <- xcms::sampclass(xcms_set)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
536 mz_tab_info$rtmed <- g[,"rtmed"]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
537 mz_tab_info$mzmed <- g[,"mzmed"]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
538 mz_tab_info$smallmolecule_abundance_assay <- xcms::groupval(xcms_set, value="into")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
539 blogified <- blob::blob(fst::compress_fst(serialize(mz_tab_info, NULL), compression=100))
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
540 rm(mz_tab_info)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
541
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
542 invisible(smol_xcms_set$set_raw(blogified)$save())
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
543 smol_xcms_set_id <- smol_xcms_set$get_id()
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
544 rm(smol_xcms_set)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
545
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
546 for (no in indices) {
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
547 sample_name <- names(singlefile)[[no]]
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
548 sample_path <- singlefile[[no]]
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
549 if (
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
550 is.na(no)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
551 || is.null(sample_path)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
552 || !(sample_name %in% sample_names)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
553 ) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
554 next
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
555 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
556 env <- new.env()
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
557 ms_file <- xcms::xcmsRaw(sample_path)
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
558 env$tic <- ms_file@tic
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
559 env$mz <- ms_file@env$mz
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
560 env$scanindex <- ms_file@scanindex
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
561 env$scantime <- ms_file@scantime * 60
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
562 env$intensity <- ms_file@env$intensity
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
563 env$polarity <- as.character(ms_file@polarity[[1]])
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
564
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
565 ## Again, ms file is huge, so we get rid of it quickly.
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
566 rm(ms_file)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
567
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
568 env$sample_name <- sample_name
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
569 env$dataset_path <- sample_path
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
570 env$process_params <- process_params
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
571 env$enriched_rdata <- TRUE
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
572 env$enriched_rdata_version <- ENRICHED_RDATA_VERSION
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
573 env$tool_name <- TOOL_NAME
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
574 env$enriched_rdata_doc <- ENRICHED_RDATA_DOC
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
575 sample <- add_sample_to_database(orm, env, context, smol_xcms_set_id)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
576 rm (env)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
577 context$samples[no] <- sample$get_id()
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
578 rm (sample)
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
579 }
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
580 context$clusters <- list()
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
581 context$show_percent <- show_percent
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
582 context$cluster_mean_rt_abundance <- list()
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
583 context$central_feature <- list()
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
584 context$adducts <- list()
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
585 load_variable_metadata(orm, var_meta, context)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
586 clusters <- context$clusters
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
587 rm(context)
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
588 message("Features enrichment")
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
589 complete_features(orm, clusters, show_percent)
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
590 message("Features enrichment done.")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
591 return (NULL)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
592 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
593
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
594 find_grouping_var <- function(var_meta) {
6
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
595 known_colnames = c(
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
596 "name", "namecustom", "mz", "mzmin", "mzmax",
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
597 "rt", "rtmin", "rtmax", "npeaks", "isotopes", "adduct", "pcgroup"
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
598 )
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
599 col_names <- colnames(var_meta)
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
600 classes = list()
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
601 for (name in col_names) {
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
602 if (!(name %in% known_colnames)) {
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
603 classes[[length(classes)+1]] = name
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
604 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
605 }
6
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
606 if (length(classes) > 1) {
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
607 stop(sprintf("Only one class expected in the variable metadata. Found %d .", length(classes)))
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
608 }
7
5be8a770a11d " master branch Updating"
lain
parents: 6
diff changeset
609 if (length(classes) == 0) {
6
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
610 stop("Could not find any class column in your variableMetadata.")
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
611 }
207e36770d18 " master branch Updating"
lain
parents: 0
diff changeset
612 return (classes[[1]])
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
613 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
614
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
615 add_sample_to_database <- function(orm, env, context, smol_xcms_set_id) {
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
616 message(sprintf("Processing sample %s", env$sample_name))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
617 sample <- (
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
618 orm$sample()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
619 $set_name(env$sample_name)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
620 $set_path(env$dataset_path)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
621 $set_kind("enriched_rdata")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
622 $set_polarity(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
623 if (is.null(env$polarity) || identical(env$polarity, character(0))) ""
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
624 else env$polarity
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
625 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
626 $set_raw(blob::blob(fst::compress_fst(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
627 serialize(env, NULL),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
628 compression=100
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
629 )))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
630 )
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
631 sample[["smol_xcms_set_id"]] <- smol_xcms_set_id
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
632 sample$modified__[["smol_xcms_set_id"]] <- smol_xcms_set_id
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
633 sample <- sample$save()
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
634 load_process_params(orm, sample, env$process_params)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
635 message(sprintf("Sample %s inserted.", env$sample_name))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
636 return (sample)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
637 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
638
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
639
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
640 load_variable_metadata <- function(orm, var_meta, context) {
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
641 all_clusters <- orm$cluster()$all()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
642
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
643 next_feature_id <- get_next_id(orm$feature()$all(), "featureID") + 1
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
644 next_cluster_id <- 0
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
645 next_pc_group <- get_next_id(all_clusters, "pc_group")
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
646 next_align_group <- get_next_id(all_clusters, "align_group") + 1
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
647 message("Extracting features")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
648 invisible(create_features(
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
649 orm, var_meta, context,
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
650 next_feature_id, next_cluster_id,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
651 next_pc_group, next_align_group
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
652 ))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
653 message("Extracting features done.")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
654 return (NULL)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
655 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
656
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
657 get_next_id <- function(models, attribute) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
658 if ((id <- models$max(attribute)) == Inf || id == -Inf) {
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
659 return (0)
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
660 }
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
661 return (id)
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
662 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
663
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
664 create_features <- function(
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
665 orm, var_meta, context,
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
666 next_feature_id, next_cluster_id,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
667 next_pc_group, next_align_group
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
668 ) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
669 field_names <- as.list(names(orm$feature()$fields__))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
670 field_names[field_names=="id"] <- NULL
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
671
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
672 features <- list()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
673 dummy_feature <- orm$feature()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
674
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
675 if (show_percent <- context$show_percent) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
676 percent <- -1
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
677 total <- nrow(var_meta)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
678 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
679 for (row in seq_len(nrow(var_meta))) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
680 if (show_percent && (row / total) * 100 > percent) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
681 percent <- percent + 1
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
682 message("\r", sprintf("\r%d %%", percent), appendLF=FALSE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
683 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
684
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
685 curent_var_meta <- var_meta[row, ]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
686
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
687
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
688 set_feature_fields_from_var_meta(dummy_feature, curent_var_meta)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
689
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
690 dummy_feature$set_featureID(next_feature_id)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
691 next_feature_id <- next_feature_id + 1
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
692 fake_iso <- dummy_feature$get_iso()
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
693 iso <- extract_iso(fake_iso)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
694 clusterID <- extract_clusterID(fake_iso, next_cluster_id)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
695 context$clusterID <- clusterID
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
696 dummy_feature$set_iso(iso)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
697
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
698
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
699 peak_list <- context$peaks[context$groupidx[[row]], ]
10
bb9ebd6365ea " master branch Updating"
lain
parents: 9
diff changeset
700 if (! ("matrix" %in% class(peak_list))) {
bb9ebd6365ea " master branch Updating"
lain
parents: 9
diff changeset
701 peak_list <- matrix(peak_list, nrow=1, ncol=length(peak_list), dimnames=list(c(), names(peak_list)))
bb9ebd6365ea " master branch Updating"
lain
parents: 9
diff changeset
702 }
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
703
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
704 clusterID <- as.character(clusterID)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
705 if (is.null(context$central_feature[[clusterID]])) {
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
706 int_o <- extract_peak_var(peak_list, "into")
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
707 context$central_feature[[clusterID]] <- (
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
708 peak_list[peak_list[, "into"] == int_o,]["sample"]
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
709 )
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
710 }
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
711
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
712 sample_peak_list <- peak_list[as.integer(peak_list[, "sample"]) == context$central_feature[[clusterID]], , drop=FALSE]
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
713 if (!identical(sample_peak_list, numeric(0)) && !is.null(nrow(sample_peak_list)) && nrow(sample_peak_list) != 0) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
714 if (!is.na(int_o <- extract_peak_var(sample_peak_list, "into"))) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
715 dummy_feature$set_int_o(int_o)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
716 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
717 if (!is.na(int_b <- extract_peak_var(sample_peak_list, "intb"))) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
718 dummy_feature$set_int_b(int_b)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
719 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
720 if (!is.na(max_o <- extract_peak_var(sample_peak_list, "maxo"))) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
721 dummy_feature$set_max_o(max_o)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
722 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
723 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
724 create_associated_cluster(
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
725 orm,
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
726 context$central_feature[[clusterID]],
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
727 dummy_feature, clusterID,
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
728 context, curent_var_meta, next_pc_group,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
729 next_align_group
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
730 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
731 next_align_group <- next_align_group + 1
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
732 features[[length(features)+1]] <- as.list(dummy_feature, field_names)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
733 dummy_feature$clear()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
734 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
735 message("")## +\n for previous message
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
736 message("Saving features")
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
737 rm(var_meta)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
738 invisible(dummy_feature$save(bulk=features))
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
739 message("Saved.")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
740 return (context$clusters)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
741 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
742
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
743 extract_peak_var <- function(peak_list, var_name, selector=max) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
744 value <- peak_list[, var_name]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
745 names(value) <- NULL
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
746 return (selector(value))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
747 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
748
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
749 set_feature_fields_from_var_meta <- function(feature, var_meta) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
750 if (!is.null(mz <- var_meta[["mz"]]) && !is.na(mz)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
751 feature$set_mz(mz)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
752 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
753 if (!is.null(mzmin <- var_meta[["mzmin"]]) && !is.na(mzmin)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
754 feature$set_mz_min(mzmin)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
755 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
756 if (!is.null(mzmax <- var_meta[["mzmax"]]) && !is.na(mzmax)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
757 feature$set_mz_max(mzmax)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
758 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
759 if (!is.null(rt <- var_meta[["rt"]]) && !is.na(rt)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
760 feature$set_rt(rt)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
761 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
762 if (!is.null(rtmin <- var_meta[["rtmin"]]) && !is.na(rtmin)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
763 feature$set_rt_min(rtmin)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
764 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
765 if (!is.null(rtmax <- var_meta[["rtmax"]]) && !is.na(rtmax)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
766 feature$set_rt_max(rtmax)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
767 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
768 if (!is.null(isotopes <- var_meta[["isotopes"]]) && !is.na(isotopes)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
769 feature$set_iso(isotopes)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
770 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
771 return (feature)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
772 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
773
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
774 extract_iso <- function(weird_data) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
775 if (grepl("^\\[\\d+\\]", weird_data)[[1]]) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
776 return (sub("^\\[\\d+\\]", "", weird_data, perl=TRUE))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
777 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
778 return (weird_data)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
779 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
780
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
781 extract_clusterID <- function(weird_data, next_cluster_id){
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
782 if (grepl("^\\[\\d+\\]", weird_data)[[1]]) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
783 clusterID <- stringr::str_extract(weird_data, "^\\[\\d+\\]")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
784 clusterID <- as.numeric(stringr::str_extract(clusterID, "\\d+"))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
785 } else {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
786 clusterID <- 0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
787 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
788 return (clusterID + next_cluster_id)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
789 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
790
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
791 create_associated_cluster <- function(
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
792 orm,
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
793 sample_no, feature, clusterID,
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
794 context, curent_var_meta, next_pc_group, next_align_group
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
795 ) {
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
796 clusterID <- as.character(clusterID)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
797 if (is.null(cluster <- context$clusters[[clusterID]])) {
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
798 pcgroup <- as.numeric(curent_var_meta[["pcgroup"]])
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
799 adduct_name <- as.character(curent_var_meta[["adduct"]])
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
800 annotation <- curent_var_meta[["isotopes"]]
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
801 cluster <- context$clusters[[clusterID]] <- orm$cluster(
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
802 pc_group=pcgroup + next_pc_group,
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
803 # adduct=adduct,
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
804 align_group=next_align_group,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
805 # curent_group=curent_group,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
806 clusterID=context$clusterID,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
807 annotation=annotation
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
808 )
12
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
809 if (is.null(adduct <- context$adducts[[adduct_name]])) {
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
810 context$adducts[[adduct_name]] <- orm$adduct()$load_by(name=adduct_name)$first()
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
811 if (is.null(adduct <- context$adducts[[adduct_name]])) {
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
812 adduct <- context$adducts[[adduct_name]] <- orm$adduct(name=adduct_name, charge=0)
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
813 adduct$save()
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
814 }
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
815 }
bdb2878ee189 " master branch Updating"
lain
parents: 11
diff changeset
816 cluster$set_adduct(adduct)
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
817 ## Crappy hack to assign sample id to cluster without loading the sample.
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
818 ## Samples are too big (their sample$env) and slows the process, and eat all the menory
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
819 ## so we dont't want to load them.
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
820 cluster[["sample_id"]] <- context$samples[sample_no][[1]]
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
821 cluster$modified__[["sample_id"]] <- cluster[["sample_id"]]
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
822 } else {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
823 if (context$clusterID != 0 && cluster$get_clusterID() == 0) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
824 cluster$set_clusterID(context$clusterID)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
825 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
826 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
827 cluster$save()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
828 feature$set_cluster(cluster)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
829 return (feature)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
830 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
831
11
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
832 complete_features <- function(orm, clusters, show_percent) {
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
833 total <- length(clusters)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
834 percent <- -1
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
835 i <- 0
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
836 for (cluster in clusters) {
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
837 i <- i+1
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
838 if (show_percent && (i / total) * 100 > percent) {
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
839 percent <- percent + 1
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
840 message("\r", sprintf("\r%d %%", percent), appendLF=FALSE)
f4fc4a0f41e2 " master branch Updating"
lain
parents: 10
diff changeset
841 }
0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
842 features <- orm$feature()$load_by(cluster_id=cluster$get_id())
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
843 if (features$any()) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
844 if (!is.null(rt <- features$mean("rt"))) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
845 cluster$set_mean_rt(rt)$save()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
846 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
847 features_df <- as.data.frame(features)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
848 central_feature <- features_df[grepl("^\\[M\\]", features_df[, "iso"]), ]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
849 central_feature_into <- central_feature[["int_o"]]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
850 if (!identical(central_feature_into, numeric(0)) && central_feature_into != 0) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
851 for (feature in as.vector(features)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
852 feature$set_abundance(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
853 feature$get_int_o() / central_feature_into * 100
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
854 )$save()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
855 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
856 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
857 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
858 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
859 return (NULL)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
860 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
861
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
862 load_process_params <- function(orm, sample, params) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
863 for (param_list in params) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
864 if (is.null(param_list[["xfunction"]])) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
865 next
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
866 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
867 if (param_list[["xfunction"]] == "annotatediff") {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
868 load_process_params_peak_picking(orm, sample, param_list)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
869 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
870 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
871 return (sample)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
872 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
873
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
874 load_process_params_peak_picking <- function(orm, sample, peak_picking_params) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
875 return (add_sample_process_parameters(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
876 params=peak_picking_params,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
877 params_translation=list(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
878 ppm="ppm",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
879 maxcharge="maxCharge",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
880 maxiso="maxIso"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
881 ),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
882 param_model_generator=orm$peak_picking_parameters,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
883 sample_param_setter=sample$set_peak_picking_parameters
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
884 ))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
885 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
886
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
887 add_sample_process_parameters <- function(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
888 params,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
889 params_translation,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
890 param_model_generator,
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
891 sample_param_setter
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
892 ) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
893 model_params <- list()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
894 for (rdata_param_name in names(params_translation)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
895 database_param_name <- params_translation[[rdata_param_name]]
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
896 if (is.null(rdata_param <- params[[rdata_param_name]])) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
897 next
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
898 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
899 model_params[[database_param_name]] <- rdata_param
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
900 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
901 params_models <- do.call(param_model_generator()$load_by, model_params)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
902 if (params_models$any()) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
903 params_model <- params_models$first()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
904 } else {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
905 params_model <- do.call(param_model_generator, model_params)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
906 params_model$save()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
907 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
908 return (sample_param_setter(params_model)$save())
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
909 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
910
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
911
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
912 library(optparse)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
913
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
914 option_list <- list(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
915 optparse::make_option(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
916 c("-v", "--version"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
917 action="store_true",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
918 help="Display this tool's version and exits"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
919 ),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
920 optparse::make_option(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
921 c("-i", "--input"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
922 type="character",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
923 help="The rdata path to import in XSeeker"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
924 ),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
925 optparse::make_option(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
926 c("-s", "--samples"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
927 type="character",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
928 help="Samples to visualise in XSeeker"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
929 ),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
930 optparse::make_option(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
931 c("-B", "--archetype"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
932 type="character",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
933 help="The name of the base database"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
934 ),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
935 optparse::make_option(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
936 c("-b", "--database"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
937 type="character",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
938 help="The base database's path"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
939 ),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
940 optparse::make_option(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
941 c("-c", "--compounds-csv"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
942 type="character",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
943 help="The csv containing compounds"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
944 ),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
945 optparse::make_option(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
946 c("-m", "--models"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
947 type="character",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
948 help="The path or url (must begin with http[s]:// or git@) to the database's models"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
949 ),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
950 optparse::make_option(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
951 c("-o", "--output"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
952 type="character",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
953 help="The path where to output sqlite"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
954 ),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
955 optparse::make_option(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
956 c("-P", "--not-show-percent"),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
957 action="store_true",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
958 help="Flag not to show the percents",
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
959 default=FALSE
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
960 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
961 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
962
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
963 options(error=function(){traceback(3)})
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
964
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
965 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
966 args <- parse_args(parser, positional_arguments=0)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
967
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
968 err_code <- 0
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
969
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
970 if (!is.null(args$options$version)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
971 message(sprintf("%s %s", TOOL_NAME, VERSION))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
972 quit()
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
973 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
974
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
975 models <- get_models(args$options$models)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
976 orm <- DBModelR::ORM(
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
977 connection_params=list(dbname=args$options$output),
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
978 dbms="SQLite"
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
979 )
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
980
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
981 invisible(orm$models(models))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
982 invisible(create_database(orm))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
983
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
984 message("Database model created")
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
985
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
986 insert_adducts(orm)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
987
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
988 if (!is.null(args$options$database)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
989 insert_base_data(orm, args$options$database)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
990 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
991 message(sprintf("Base data inserted using %s.", args$options$database))
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
992
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
993 if (!is.null(args$options$archetype)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
994 insert_base_data(orm, args$options$archetype, archetype=TRUE)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
995 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
996 if (!is.null(args$options$`compounds-csv`)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
997 insert_compounds(orm, args$options$`compounds-csv`)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
998 }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
999
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1000 # if (!is.null(args$options$rdata)) {
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1001 # load_rdata_in_base(args$options$rdata, args$options$samples, args$options$`not-show-percent`)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1002 # }
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1003
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1004
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1005 load(args$options$input, rdata <- new.env())
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1006
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1007 process_rdata(orm, rdata, args$options)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1008
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1009 quit(status=err_code)
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1010
a174cbbb12dd " master branch Updating"
lain
parents:
diff changeset
1011