Skip to content

Commit 3e87c41

Browse files
authored
Merge pull request #335 from USEPA/162-update-tcplLoadData-to-support-chem-queries
162 update tcplLoadData to support chemical queries
2 parents 9e0a8ec + 8920b8a commit 3e87c41

File tree

6 files changed

+107
-6
lines changed

6 files changed

+107
-6
lines changed

Diff for: R/chemQueries.R

+25-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
# .ChemQ: Create tcplLoadChem query string
33
#-------------------------------------------------------------------------------
44

5+
#' @title Function to support queries by chemical
6+
#' @note This function is not exported and not intended to be used by the user.
7+
#'
8+
#' @param field Character, the field to query on
9+
#' @param val Vector of values to subset on
10+
#' @param exact Logical, should chemical names be considered exact?
11+
#'
12+
#' @description
13+
#' \code{.ChemQ} creates tcplLoadChem query string
14+
#'
15+
#' @seealso \code{\link{tcplLoadData}}
16+
#' @seealso \code{\link{tcplLoadChem}}
517
.ChemQ <- function(field, val, exact) {
618

719
qformat <-
@@ -29,11 +41,11 @@
2941
if (exact) {
3042
qformat <- paste(qformat, "chnm IN (%s);")
3143
val <- paste0("\"", val, "\"", collapse = ",")
32-
qstring <- sprintf(qformat, val, val)
44+
qstring <- sprintf(qformat, val)
3345
} else {
3446
qformat <- paste(qformat, "chnm RLIKE %s;")
3547
val <- paste0("\"", paste(val, collapse = "|"), "\"")
36-
qstring <- sprintf(qformat, val, val)
48+
qstring <- sprintf(qformat, val)
3749
}
3850
} else if (nfld == 'chem.only') {
3951
qstring <- "
@@ -64,6 +76,16 @@
6476
# .ChemListQ: Create tcplLoadChemList query string
6577
#-------------------------------------------------------------------------------
6678

79+
#' @title Function to support queries by chemical
80+
#' @note This function is not exported and not intended to be used by the user.
81+
#'
82+
#' @param field Character, the field to query on
83+
#' @param val Vector of values to subset on
84+
#'
85+
#' @description
86+
#' \code{.ChemListQ} creates tcplLoadChemList query string
87+
#'
88+
#' @seealso \code{\link{tcplLoadChemList}}
6789
.ChemListQ <- function(field, val) {
6890

6991
qformat <- "SELECT * FROM chemical_lists"
@@ -90,5 +112,4 @@
90112

91113
#-------------------------------------------------------------------------------
92114
# END .ChemListQ
93-
#-------------------------------------------------------------------------------
94-
115+
#-------------------------------------------------------------------------------

Diff for: R/tcplLoadChem.R

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
tcplLoadChem <- function(field = NULL, val = NULL, exact = TRUE,
5050
include.spid = TRUE) {
5151

52+
if (length(field) > 1) stop("'field' must be of length 1.")
53+
5254
if (getOption("TCPL_DRVR") == "API") {
5355
if (is.null(field) || tolower(field) != "spid") stop("When drvr option is set to 'API', only 'spid' is a valid 'field' value.")
5456
if (!exact) exact <- TRUE

Diff for: R/tcplLoadData.R

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#' the same order as 'fld'.
1616
#' @param add.fld Boolean if true we want to return
1717
#' the additional parameters fit with tcplfit2
18+
#' @param exact Logical, passed to tcplLoadChem -- should chemical names be
19+
#' considered exact?
1820
#'
1921
#' @details
2022
#' The data type can be either 'mc' for mutliple concentration data, or 'sc'
@@ -71,6 +73,12 @@
7173
#' ## Load level 0 data where the well type is "t" and the concentration
7274
#' ## index is 3 or 4
7375
#' tcplLoadData(lvl = 1, fld = c("wllt", "cndx"), val = list("t", c(3:4)))
76+
#'
77+
#' ## Load level 4 data using a chemical name
78+
#' tcplLoadData(lvl = 4, fld = "chnm", val = "Bisphenol A")
79+
#'
80+
#' ## Load level 3 data using a partial chemical name
81+
#' tcplLoadData(lvl = 3, fld = "chnm", val = "phenol", exact = FALSE)
7482
#' }
7583
#' @return A data.table containing data for the given fields.
7684
#'
@@ -82,7 +90,7 @@
8290
#' @importFrom rlang exec sym
8391
#' @export
8492

85-
tcplLoadData <- function(lvl, fld = NULL, val = NULL, type = "mc", add.fld = TRUE) {
93+
tcplLoadData <- function(lvl, fld = NULL, val = NULL, type = "mc", add.fld = TRUE, exact = TRUE) {
8694
#variable binding
8795
model <- model_param <- model_val <- NULL
8896
hit_param <- hit_val <- sc_vignette <- mc_vignette <- NULL
@@ -230,6 +238,12 @@ tcplLoadData <- function(lvl, fld = NULL, val = NULL, type = "mc", add.fld = TRU
230238
# add.fld is not possible if invitrodb version less than 4
231239
if (!check_tcpl_db_schema()) add.fld <- FALSE
232240

241+
if (any(fld %in% c("chid", "casn", "chnm", "dsstox_substance_id", "code"))) {
242+
chem <- tcplLoadChem(field = fld, val = val, exact = exact)
243+
fld <- "spid"
244+
val <- chem$spid
245+
}
246+
233247
table <- paste0(type, lvl)
234248
tbls_joins <- case_when(
235249
table == "sc0" ~ list(tbls = "sc0",

Diff for: man/dot-ChemListQ.Rd

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: man/dot-ChemQ.Rd

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: man/tcplLoadData.Rd

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)