-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from pepijn-devries/work-in-progress
Added test to compare online search with results from local search
- Loading branch information
Showing
8 changed files
with
154 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Package: ECOTOXr | ||
Type: Package | ||
Title: Download and Extract Data from US EPA's ECOTOX Database | ||
Version: 1.0.9 | ||
Date: 2024-01-07 | ||
Version: 1.1.0 | ||
Date: 2024-02-10 | ||
Authors@R: c(person("Pepijn", "de Vries", role = c("aut", "cre", "dtc"), | ||
email = "[email protected]", | ||
comment = c(ORCID = "0000-0002-7961-6646"))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
library(dplyr) | ||
|
||
check_db <- function() { | ||
if (!check_ecotox_availability()) { | ||
skip("ECOTOX database not available") | ||
} | ||
} | ||
|
||
test_that("Online and local search yield the same results", { | ||
check_db() | ||
skip_if_offline() | ||
expect_true({ | ||
load(file.path(testthat::test_path(), "test_data", "insecticides.rdata")) | ||
insecticides$cas <- format(as.cas(insecticides$cas), hyphenate = FALSE) | ||
unit_conversion <- | ||
data.frame(what = c(rep("mass", 8), rep("volume", 2)), | ||
unit = c("pg", "ng", "ug", "mg", "g", "nmol", "umol", "mmol", "L", "m3"), | ||
conversion = 10^c(-12, -9, -6, -3, 0, -9, -6, -3, 0, 3)) | ||
|
||
insecticedes_search <- search_ecotox( | ||
list( | ||
test_cas = list(terms = insecticides$cas, method = "exact"), | ||
endpoint = list(terms = c("EC50", "LC50"), method = "contains"), | ||
latin_name = list(terms = "Daphnia magna", method = "exact"), | ||
effect = list(terms = c("ITX", "MOR"), method = "contains") | ||
), | ||
c(list_ecotox_fields(), "results.obs_duration_mean", "results.obs_duration_unit", | ||
"results.result_id")) |> | ||
mutate( | ||
duration_corr = case_match( | ||
obs_duration_unit, "d" ~ 1, "h" ~ 1/24, "mi" ~ 1/(60*24), "NR" ~ NA, "wk" ~ 7), | ||
duration_corr = suppressWarnings(as.numeric(obs_duration_mean)*duration_corr), | ||
test_cas = as.character(ECOTOXr::as.cas(test_cas)), | ||
conc1_mean = suppressWarnings({as.numeric(gsub("[*]", "", conc1_mean))}) | ||
) |> | ||
filter(duration_corr == 2 & conc1_mean != "NR" & !grepl("org", conc1_unit)) |> | ||
left_join(insecticides |> distinct(), c(test_cas = "cas")) |> | ||
mutate( | ||
conc1_unit_fix = trimws(gsub("AI", "", conc1_unit)), | ||
conc1_unit_fix = | ||
case_match( | ||
conc1_unit_fix, | ||
"mM" ~ "mmol/L", | ||
"uM" ~ "umol/L", | ||
"nM" ~ "nmol/L", | ||
"mg/kg" ~ "mg/L", | ||
"ppm" ~ "mg/L", | ||
"ppb" ~ "ug/L", | ||
"ppt" ~ "ng/L", | ||
.default = conc1_unit_fix), | ||
conc1_conversion_factor = { | ||
do.call(rbind, strsplit(conc1_unit_fix, "/")) |> | ||
as.data.frame() |> | ||
rename_with(~c("mass", "volume")) |> | ||
left_join(unit_conversion, c(mass = "unit")) |> | ||
rename(mass_conversion = "conversion") |> | ||
mutate( | ||
## If mass is reported as volume (1 case) use specific gravity to convert to actual mass | ||
mass_conversion = ifelse(mass == "ul" & test_cas == "333-41-5", 1.117e-6, mass_conversion) | ||
) |> | ||
left_join(unit_conversion, c(volume = "unit")) |> | ||
rename(volume_conversion = "conversion") |> | ||
mutate(molar_conversion = ifelse(grepl("mol", mass), molweight, 1), | ||
total_conversion = molar_conversion*mass_conversion/volume_conversion) |> | ||
pull(total_conversion) | ||
}, | ||
conc1_ug_l = 1e6*conc1_mean*conc1_conversion_factor | ||
) |> | ||
suppressWarnings() |> | ||
suppressMessages() | ||
|
||
websearch <- list_ecotox_web_fields( | ||
txAdvancedChemicalEntries = paste(insecticides$cas, | ||
collapse = "\r\n"), | ||
RBCHEMSEARCHTYPE = "EXACT", | ||
txAdvancedSpecEntries = "daphnia magna", | ||
RBSPECSEARCHTYPE = "EXACT", | ||
cbResultsGroup12a = "LC50", | ||
cbResultsGroup13a = "EC50", | ||
cbResultsGroup6 = "MOR", | ||
cbResultsGroup7c = "ITX", | ||
txExposureDurationStd = "2", | ||
cbResult_number = "Result Number") | ||
|
||
websearch <- suppressWarnings(websearch_ecotox(websearch)) | ||
websearch <- websearch$`Aquatic-Export` |> | ||
dplyr::filter(!is.na(`Conc 1 Mean (Standardized)`) & | ||
`Conc 1 Units (Standardized)` == "AI mg/L") |> | ||
select(result_id = "Result Number", | ||
conc1_ug_l = "Conc 1 Mean (Standardized)", | ||
test_cas = "CAS Number") |> | ||
mutate(test_cas = as.character(as.cas(test_cas)), ## hyphenate the CAS numbers | ||
conc1_ug_l = 1e3*conc1_ug_l) | ||
conc_check <- | ||
full_join( | ||
websearch |> | ||
select(web_conc = "conc1_ug_l", "result_id"), | ||
insecticedes_search |> | ||
select(local_conc = "conc1_ug_l", "result_id"), | ||
by = "result_id" | ||
) |> | ||
mutate( | ||
diff = 1 - web_conc/local_conc, | ||
check = diff < 1e-3 | ||
) | ||
result <- | ||
## Number of records differ no more than 2 | ||
(abs(nrow(websearch) - nrow(insecticedes_search)) <= 2) && | ||
## Retrieved websearch cas numbers are also in local cas numbers | ||
all(websearch$test_cas %in% insecticedes_search$test_cas) && | ||
## Retrieved local cas numbers are also in websearch cas numbers | ||
all(insecticedes_search$test_cas %in% websearch$test_cas) && | ||
## concentrations are identical | ||
all(na.omit(conc_check$check)) | ||
result | ||
}) | ||
}) |