-
Notifications
You must be signed in to change notification settings - Fork 2
/
manuscript_workflow.R
346 lines (272 loc) · 12.7 KB
/
manuscript_workflow.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
library(tidyverse)
library(toxEval)
library(openxlsx)
path_to_data <- Sys.getenv("PASSIVE_PATH")
options(dplyr.summarise.inform = FALSE)
# shiny::runApp("apps/Mixture_Exploration/")
# Data setup is done in drake plan
# It's better to run separately, and shouldn't
# be needed to run this script
source(file = "read_chemicalSummary.R")
unique(tox_list$chem_data$CAS)[which(!(unique(tox_list$chem_data$CAS)) %in% tox_list$chem_info$CAS)]
tox_list$exclusions <- tox_list$exclusions %>%
filter(!is.na(CAS) & !is.na(endPoint))
###################################################
# Mixtures stuff:
EAR_thresh <- 0.001
site_thresh_percent <- 10
n_sites <- 7
TQ_thresh <- 0.1
source(file = "R/mixtures/mix_script.R")
source(file = "R/mixtures/prepare_mixture_data.R")
mix_df <- get_final_mixtures(chemicalSummary,
EAR_thresh,
site_thresh_percent, tox_list)
dir.create("R/mixtures/out", showWarnings = FALSE)
saveRDS(mix_df,"R/mixtures/out/mixtures_table.rds")
wb <- create_Excel_wb_mix(mix_df)
saveWorkbook(wb, file.path(Sys.getenv("PASSIVE_PATH"),"Tables",
"Mixtures.xlsx"), overwrite = TRUE)
################################################
# Figure 1 = Map
# Figure 2 = triple_graph.pdf
# Figure 3 = new_stack_w_table.pdf
rmarkdown::render("manuscript_figures.Rmd",
output_dir = file.path(Sys.getenv("PASSIVE_PATH"),
"Figures"))
# Figure SI A-D =
# Run the stacked_supplement_w_captions.Rmd file
rmarkdown::render("stacked_supplement_w_captions.Rmd",
output_dir = file.path(Sys.getenv("PASSIVE_PATH"),
"Supplemental"))
# This graph includes chemicals measured below detection levels...
# which is why it is longer and there appear to be more endpoints
# than the numbers we wrote in the text (those were all for detected chemicals)
# Create SI tables:
df_assays <- chemicalSummary %>%
select(endPoint) %>%
distinct() %>%
left_join(select(end_point_info,
endPoint = assay_component_endpoint_name,
source = assay_source_long_name), by="endPoint") %>%
rename(`ToxCast Assay` = endPoint,
`Assay Source` = source)
assays_left <- chemicalSummary %>%
select(CAS, endPoint) %>%
distinct() %>%
group_by(CAS) %>%
summarize(`Assays in study` = length(unique(endPoint)))
AOP_crosswalk <- data.table::fread(file.path(path_to_data, "data/data_for_git_repo/raw", "AOP_crosswalk.csv"), data.table = FALSE)
AOP_sup <- AOP_crosswalk %>%
select(AOP_shortname, `AOP #`, `AOP Title`, `KE#`, `Key Event Name`) %>%
distinct() %>%
filter(AOP_shortname != "")
library(ToxMixtures)
all_join <- join_everything_fnx(chemicalSummary) %>%
separate(genes, sep = ",", into = letters[1:5]) %>%
pivot_longer(letters[1:5], "names", values_to = "Gene") %>%
select(-names) %>%
filter(!is.na(Gene),
Gene != "") %>%
mutate(AOPs = ifelse(AOPs == "", "No", "Yes")) %>%
left_join(select(ToxMixtures::david_full,
GeneSymbol,
David = SpeciesGeneSymbol),
by = c("Gene"="GeneSymbol")) %>%
mutate(David = ifelse(is.na(David), "No", "Yes"))
AOP_pan <- chemicalSummary %>%
select(chnm, endPoint) %>%
distinct() %>%
left_join(all_join, by = "endPoint") %>%
select(Gene, chnm, endPoint, AOPs, David) %>%
distinct() %>%
arrange(Gene) %>%
mutate(Gene = ifelse(is.na(Gene), "No gene association", Gene))
#Run ECOTOX analysis
suppressMessages(source("R/Analyze/ECOTOX_workflow.R"))
#Combine EAR and TQ-based chem priorities
source("R/Analyze/combine_TQ_EAR_chem_priorities.R")
################################################
#Create table 2: summary of priority chemicals
source("R/report/chem_priority_summary_Table2.R")
table_2 <- get_table_2()
names(table_2) <- c("Chemical use class","Chemical name","Sites monitored","Individual","Mixtures"," ","Group 1","Group 2")
ms_tables_wb <- loadWorkbook(file.path(path_to_data,"Tables","Manuscript_tables.xlsx"))
writeData(wb = ms_tables_wb,sheet = "Table 2. Chemical Summary",x = table_2,startCol = 1,startRow = 4)
saveWorkbook(wb = ms_tables_wb,file = file.path(path_to_data,"Tables","Manuscript_tables.xlsx"),overwrite = TRUE)
################################################
# Venn diagram
eco_list <- create_toxEval(file.path(path_to_data, "data/toxEval input file/","passive_benchmarks_all.xlsx"))
eco_list$chem_info <- eco_list$chem_info %>%
rename(Chemical = chnm)
summary_conc <- get_concentration_summary(tox_list)
summary_eco <- get_chemical_summary(eco_list)
chmn_df <- tox_list$chem_info %>%
select(CAS, chnm) %>%
distinct()
summarize_dets <- bind_rows(select(chemicalSummary, Class, chnm, CAS, EAR) %>%
mutate(cat = "Tox",
chnm = as.character(chnm)),
select(summary_conc, Class, chnm, CAS, EAR)%>%
mutate(cat = "Conc",
chnm = as.character(chnm)),
select(summary_eco, Class, chnm, CAS, EAR)%>%
mutate(cat = "Eco",
chnm = as.character(chnm))) %>%
group_by(CAS, Class, cat) %>%
summarize(detect = any(EAR > 0)) %>%
pivot_wider(names_from = cat,
values_from = detect) %>%
mutate(in_both = Eco + Tox == 2,
in_neither = is.na(Eco) & is.na(Tox),
Tox = as.logical(Tox),
Eco = as.logical(Eco),
Conc = as.logical(Conc)) %>%
ungroup() %>%
left_join(chmn_df, by = "CAS")
no_dets <- summarize_dets %>%
filter(!Conc) %>%
select(Class, chnm) %>%
arrange(Class)
no_det_not_in_db <- summarize_dets %>%
filter(!Conc & in_neither) %>%
select(Class, chnm) %>%
arrange(Class)
no_det_in_Tox <- summarize_dets %>%
filter(!Conc & !Tox & is.na(Eco)) %>%
select(Class, chnm) %>%
arrange(Class)
no_det_in_Eco <- summarize_dets %>%
filter(!Conc & is.na(Tox) & !Eco) %>%
select(Class, chnm) %>%
arrange(Class)
no_det_in_both <- summarize_dets %>%
filter(!Conc & !in_neither) %>%
select(Class, chnm) %>%
arrange(Class)
det_in_both <- summarize_dets %>%
filter(Conc & in_both) %>%
select(Class, chnm) %>%
arrange(Class)
det_in_neither <- summarize_dets %>%
filter(Conc & in_neither) %>%
select(Class, chnm) %>%
arrange(Class)
det_only_tox <- summarize_dets %>%
filter(Conc & Tox & is.na(Eco)) %>%
select(Class, chnm) %>%
arrange(Class)
det_only_eco <- summarize_dets %>%
filter(Conc & is.na(Tox) & Eco) %>%
select(Class, chnm) %>%
arrange(Class)
library(openxlsx)
wb = createWorkbook()
addWorksheet(wb, "Venn")
writeData(wb = wb, startRow = 1, startCol = 1,
x = "Not Detected", sheet = "Venn")
writeData(wb = wb, startRow = 2, startCol = 1,
x = no_dets, sheet = "Venn")
writeData(wb = wb, startRow = 1, startCol = 3,
x = "Detected", sheet = "Venn")
writeData(wb = wb, startRow = 2, startCol = 3,
x = det_in_neither, sheet = "Venn")
writeData(wb = wb, startRow = 1, startCol = 5,
x = "Detected ToxCast", sheet = "Venn")
writeData(wb = wb, startRow = 2, startCol = 5,
x = det_only_tox, sheet = "Venn")
writeData(wb = wb, startRow = 1, startCol = 7,
x = "Detected Both", sheet = "Venn")
writeData(wb = wb, startRow = 2, startCol = 7,
x = det_in_both, sheet = "Venn")
writeData(wb = wb, startRow = 1, startCol = 9,
x = "Detected Ecotox", sheet = "Venn")
writeData(wb = wb, startRow = 2, startCol = 9,
x = det_only_eco, sheet = "Venn")
saveWorkbook(wb, file = file.path(Sys.getenv("PASSIVE_PATH"),
"Dan_V_2020_06_19/venn_diagram_table_data.xlsx"), overwrite = TRUE)
################################################
# Create the supplemental:
source(file = "R/analyze/update_SI_2.R")
tab_names <- c("SI-1 Site Table",
"SI-2 Chemical Table",
"SI-3 ToxCast Assays",
"SI-4 ToxCast Exclusions",
"SI-5 ECOTOX data",
"SI-6 ECOTOX summary",
"SI-7 AOP",
"SI-8 AOP and DAVID"
)
captions <- c("Table SI-1: Sampling locations and primary land cover characteristics for Great Lakes tributaries sampled by passive samplers, 2010-2014.",
"Table SI-2: Chemicals, prescribed chemical use, passive sampler type, sampling rate, ToxCast assay information, detection levels, and summary statistics from resulting data for Great Lakes tributaries, 2010-2014",
"Table SI-3: Toxcast assays included in data analysis for organic chemicals detected in Great Lakes tributary passive samplers, 2010-2014",
"Table SI-4: ToxCast assays excluded from analysis due low quality dose-response curves based on anomalous values or lack of response ",
"Table SI-5: Descriptive information for bioassay endpoints from the ECOTOX knowledgebase included in data analysis for organic chemicals detected in Great Lakes tributary passive samplers, 2010-2014",
"Table SI-6: Bioassay endpoint information for individual chemicals from the ECOTOX knowledgebase included in data analysis for organic chemicals detected in Great Lakes tributary passive samplers, 2010-2014. [\U003BCg/L: Units for min endpiont, max endpoint, and 5th endpoint percentile; chnm, chemical name used in the current study; Chemical.name, chemical name retrieved from ECOTOX Knowledgebase.]",
"Table SI-7: Adverse outcome pathway (AOP) identification and key event information from the AOP Knowledgebase for AOPs relevant to organic chemicals detected in Great Lakes tributary passive samplers, 2010-2014",
"Table SI-8: ToxCast endpoints, gene targets, and association with adverse outcome pathways and gene ontology pathways (Panther) for individual chemicals from the ECOTOX knowledgebase included in data analysis for organic chemicals detected in Great Lakes tributary passive samplers, 2010-2014")
wb <- update_table_2(path_to_data, tab_names[2])
sites <- tox_list$chem_site %>%
rename(`Site ID on Map` = map_nm,
`USGS Site ID` = SiteID,
`Site Name (Full)` = Fullname)
deployments <- readxl::read_xlsx(file.path(Sys.getenv("PASSIVE_PATH"),
"Supplemental/deployments.xlsx"))
# SI-1: Site Table
addWorksheet(wb, tab_names[1])
header_st <- createStyle(textDecoration = "Bold")
writeData(wb = wb, sheet = tab_names[1], colNames = FALSE, rowNames = FALSE,
x = captions[1])
writeData(wb = wb, sheet = tab_names[1], startRow = 3,
x = sites[,1:5], headerStyle = header_st)
writeData(wb = wb, sheet = tab_names[1],
startRow = 2, startCol = 6,
x = deployments, headerStyle = header_st)
writeData(wb = wb, sheet = tab_names[1],
startRow = 3, startCol = 8,
x = sites[,6:10], headerStyle = header_st)
worksheetOrder(wb) <- c(2,1)
#SI-2: Chemical Table
# See update_table_2
# SI-3: ToxCast Assays
addWorksheet(wb, tab_names[3])
writeData(wb = wb, sheet = tab_names[3], colNames = FALSE, rowNames = FALSE,
x = captions[3])
writeData(wb = wb, sheet = tab_names[3], startRow = 3,
x = df_assays, headerStyle = header_st)
# SI-4: Exclusions
addWorksheet(wb, tab_names[4])
writeData(wb = wb, sheet = tab_names[4], colNames = FALSE, rowNames = FALSE,
x = captions[4])
writeData(wb = wb, sheet = tab_names[4], startRow = 3,
x = rename(select(tox_list$exclusions, -Chemical),
`ToxCast Assay` = endPoint, Chemical = chnm), headerStyle = header_st)
#SI-5 ECOTOX data
addWorksheet(wb, tab_names[5])
writeData(wb = wb, sheet = tab_names[5], colNames = FALSE, rowNames = FALSE,
x = captions[5])
writeData(wb = wb, sheet = tab_names[5], startRow = 3,
x = tox_fw)
#SI-6 ECOTOX summary stats
addWorksheet(wb, tab_names[6])
writeData(wb = wb, sheet = tab_names[6], colNames = FALSE, rowNames = FALSE,
x = captions[6])
writeData(wb = wb, sheet = tab_names[6], startRow = 3,
x = tox_stats)
#SI-7 AOP crosswalk
addWorksheet(wb, tab_names[7])
writeData(wb = wb, sheet = tab_names[7], colNames = FALSE, rowNames = FALSE,
x = captions[7])
writeData(wb = wb, sheet = tab_names[7], startRow = 3,
x = AOP_sup,
headerStyle = header_st)
#SI-8 AOP and Panther
addWorksheet(wb, tab_names[8])
writeData(wb = wb, sheet = tab_names[8], colNames = FALSE, rowNames = FALSE,
x = captions[8])
writeData(wb = wb, sheet = tab_names[8], startRow = 3,
x = AOP_pan,
headerStyle = header_st)
# Save the whole thing:
saveWorkbook(wb, file.path(Sys.getenv("PASSIVE_PATH"),"Supplemental",
"Supplemental_32.xlsx"), overwrite = TRUE)