-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.vdoc.r
390 lines (355 loc) · 6.97 KB
/
.vdoc.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#| label: read files in R
#| message: false
#| warning: false
# Reading different file formats in R
# If rio package is not installed, install it first, otherwise skip this step.
# install.packages("rio")
library(rio)
# Reading xlsx file.
df <- import("inst/chemicals.xlsx")
df
#
#
#
#
#
#| label: write files in R
#| message: false
#| warning: false
#| echo: false
# Writing different file formats in R
export(df, "inst/chemicals.xlsx")
# Check the documentation for more details. Using ? before the function name will display the help page.
?import
?export
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#| label: installation
#| message: false
#| warning: false
#| eval: false
# If you haven't installed the remotes package, you can install it with:
install.packages("remotes")
# Then you can install the labtools package with:
remotes::install_github("QizhiSu/labtools")
#
#
#
#
#
#
#
#| label: cid extraction
#| message: false
#| warning: false
#| class: output
# Given that you have read in your data as df as shown in the Section 1, you can use the following function to extract the metadata from Pubchem:
library(labtools)
df_cid <- extract_cid(df, inchikey_col = 3, cas_col = 2, name_col = 1)
df_cid
#
#
#
#
#
#
#
#| label: manual CID assignment
#| message: false
#| warning: false
# Manually assign CID to the first row
df_cid[1, "CID"] <- 6184
# Just remember that CID is integer and not character. So don't do this:
# df_cid[1, "CID"] <- "6184"
#
#
#
#
#
#| label: meta extraction
#| message: false
#| warning: false
#| class-output: output
df_meta <- extract_meta(df_cid)
df_meta
# If you also want to retrieve CAS number, in case you haven't provided it in the input dataframe, you can use the following function, but it will be slower:
df_meta <- extract_meta(df_cid, cas = TRUE)
df_meta
# It is also possible to retrieve odour information from the Flavonet (https://www.flavornet.org/flavornet.html):
df_meta <- extract_meta(df_cid, cas = TRUE, flavornet = TRUE)
df_meta
# Once metadata are retrieved, you can export them into a new xlsx file.
export(df_meta, "inst/chemicals_meta.xlsx")
#
#
#
#
#
#
#
#| label: class extraction
#| message: false
#| warning: false
#| class: output
df_cla <- extract_classyfire(df_meta)
df_cla
#
#
#
#
#
#
#
#| eval: false
# If rCDK and Retip are not installed, you can install them with, otherwise skip this step.
install.packages("rCDK")
install.packages("Retip")
#
#
#
#| label: descriptor calculation
#| message: false
#| warning: false
library(Retip)
df_cd <- Retip::getCD(df_meta[, c("Name", "SMILES")])
df_cd[, 1:10] # show the first 10 columns
#
#
#
#
#
#
#
#
#
#| label: structure viewer
#| message: false
#| warning: false
# subset the dataframe to show only Name, CAS, CID, and SMILES
df2view <- df_meta[, c("Name", "CAS", "CID", "SMILES")]
df2view
# navigate_chem(df2view)
#
#
#
#
#
#
#
#
#
#
#
#
#
#| label: read MS-DIAL data and extract metadata
#| message: false
#| warning: false
# read MS-DIAL data
library(labtools)
df <- read_msdial("inst/gc_msdial.txt", keep_unknown = FALSE, keep_spectrum = FALSE, keep_mean_sd = FALSE)
#
#
#
#
#
#| label: extract metadata and chemical classes
#| message: false
#| warning: false
df_meta <- df[, 1:12] # exclude peak area columns
df_meta <- extract_cid(df_meta, inchikey_col = 9, name_col = 7)
df_meta <- extract_meta(df_meta, cas = TRUE, flavornet = TRUE)
df_meta <- extract_classyfire(df_meta)
#
#
#
#
#
#| label: calculate detection rate
df_meta$Comment <- gsub(";.*$", "", df_meta$Comment) # remove any notes in the Comment column
df_meta <- calculate_freq(df_meta, num_sample = 2, sep = ",")
df_meta[, 1:15]
#
#
#
#
#
#
#
#| label: sample code mapping
sam_code <- rio::import("inst/sample_code.xlsx")
sam_code
#
#
#
#
#
#
#
#
#| label: fcmsafety installation
#| eval: false
# remotes::install_github("QizhiSu/fcmsafety") # in case not installed
library(fcmsafety)
# export structural information for toxtree
export4toxtree(df_meta, cas_col = 19, name_col = 7, output = "inst/gc_for_toxtree.csv")
# then open Toxtree and run batch mode to calculate Cramer level
# load databases
load_databases()
df_meta <- assign_toxicity(df_meta, toxtree_result = "inst/gc_toxtree_results.csv")
df_meta
#
#
#
#
#
#
#
#| label: calculate molecular descriptors
#| message: false
#| warning: false
library(Retip)
# load reference compounds and extract metadata
ref <- rio::import("inst/reference.xlsx")
ref_meta <- extract_cid(ref, name_col = 1)
ref_meta <- extract_meta(ref_meta)
# calculate molecular descriptors for reference compounds.
ref_cd <- Retip::getCD(ref_meta)
# Since the reference compounds may not be changed that frequently, we can calculate the descriptors once and save them as a xlsx file.
rio::export(ref_cd, "inst/ref_cd.xlsx")
# In case you do not change reference compounds, Then, we can load the xlsx file and use it for subsequent analysis. Otherwise, it is better to do it from scratch.
ref_cd <- rio::import("inst/ref_cd.xlsx")
ref_cd[, 1:15]
# calculate molecular descriptors for sample compounds
df_cd <- Retip::getCD(df_meta[, c("ID", "Name", "SMILES", "Comment")])
df_cd[, 1:15]
#
#
#
#
#
#
#
#| label: select reference standard
# select reference standard for each target compound
# do not use response for the calculation
df_con <- select_std(std_md = ref_cd, std_res_col = FALSE, std_md1_col = 14, data_md = df_cd, data_md1_col = 5)
# use the top 50% of the molecular descriptors for the calculation
# df_con <- select_std(std_md = ref_cd, std_res_col = 3, std_md1_col = 14, data_md = df_cd, data_md1_col = 4, top_npct_md = 50)
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#| label: combine data
#| message: false
#| warning: false
library(dplyr)
# combining data
df_con <- left_join(df_con, df[, c(7, 13:ncol(df))], by = "Name")
#
#
#
#
#
#
#| label: keep peak areas
# load sample code mapping file
sam_code <- rio::import("inst/sample_code.xlsx")
sam_code
colnames(df_con)
# keep peak areas of identified sample and turn others into NA
df_con <- keep_area(df_con, sam_code = sam_code, start_col = 12, keep_bk = TRUE, keep_d8 = TRUE)
#
#
#
#
#
#| label: calculate concentration
# calculate concentration of each compound in each sample
sam_weight <- rio::import("inst/sample_weight.xlsx")
sam_weight
# concentration considering sample weight and final volumn
df_con <- calculate_con(df_con, sam_weight = sam_weight, start_col = 12)
# concentration without sample weight and final volumn adjustment
# df_con <- calculate_con(df_con, start_col = 12, sam_weight = NULL)
#
#
#
#
#
#| label: organize data
# organize data
# set NA to zero and not bind mean and sd
df_con <- organize_con(df_con, sam_code = sam_code, start_col = 12, digits = 2, na2zero = TRUE, bind_mean_sd = FALSE)
# do not set NA to zero and do not bind mean and sd
# df_con <- organize_con(df_con, sam_code = sam_code, start_col = 12, digits = 2, na2zero = FALSE, bind_mean_sd = FALSE)
# if you want to combine all data into one, then you can run
df_bind <- left_join(df_meta, select(df_con, -c("SMILES", "Comment")), by = c("ID", "Name"))
#
#
#
#
#