Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

231 add data retrieval via api vignette #260

Merged
merged 36 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
84c2c91
231 add initial api data retrieval vignette with json api responses
cthunes Apr 17, 2024
d2c6998
231 added tables to vignette and other small updates
cthunes Apr 18, 2024
0f11ff3
235 refactored tcplPlot + utils to support API plotting
cthunes May 13, 2024
0424f9e
247+248 fixed wllt with sc and obscure well types bug
cthunes May 15, 2024
e28322f
Resolve newfound order bug with single conc plotting
cthunes May 15, 2024
4f25d60
206 improve cutoff direction for sc, wllt output fix for length > 1
cthunes May 17, 2024
e16ae5c
moved away from using tcplPrepOtpt with API plotting
cthunes May 17, 2024
b2326c2
limit wllt check to non-API usage, add back output for single wllt
cthunes May 17, 2024
e94c1d3
231 add initial api data retrieval vignette with json api responses
cthunes Apr 17, 2024
242b338
231 added tables to vignette and other small updates
cthunes Apr 18, 2024
43c28c6
Merge branch '231-add-data-retrieval-via-API-vignette' of https://git…
cthunes May 21, 2024
66aeaa7
Update tcplPlotUtils.R
cthunes May 23, 2024
1c3131c
231 add rest of tcplLoadXxxx functions and plotting examples
cthunes May 23, 2024
b347703
235 fixes to support standalone-compare API plotting
cthunes May 23, 2024
6b41516
235 Fix unnest to include flags, exclude m4id, add is.null(flag) check
cthunes May 24, 2024
04a2c33
231 added standalone plot image
cthunes May 24, 2024
1a703f1
218 update tcplConfDefault to use API
cthunes May 24, 2024
1bd093d
231 added tcplLoadChem section and improved some text
cthunes May 24, 2024
ff8d7cb
235 add if flags == TRUE
cthunes May 24, 2024
c7bc7e6
235 added tidyr unnest import and ran devtools::document
cthunes May 24, 2024
a7dda6a
Added sc1 method bval.nwlls.med
Kelly-Carstens-EPA May 24, 2024
986facd
fixed order bug, now with use in list or non-list 'val'
cthunes Jun 4, 2024
216e7d4
Remove line for ccdR fix, to be merged to dev soon
cthunes Jun 5, 2024
d613ecd
231 Fixed some wording/grammar issues
cthunes Jun 10, 2024
bc90593
Merge pull request #257 from USEPA/235-247-248-206-API-plotting-and-b…
cthunes Jun 10, 2024
1bece41
Added sc1 method description for bval.nwlls.med
Kelly-Carstens-EPA Jun 17, 2024
684216b
Merge pull request #261 from USEPA/258-add-sc1-method-for-bval-by-wllt
Kelly-Carstens-EPA Jun 17, 2024
9307ad1
231 add initial api data retrieval vignette with json api responses
cthunes Apr 17, 2024
003e8de
231 added tables to vignette and other small updates
cthunes Apr 18, 2024
e8d2ebc
231 add rest of tcplLoadXxxx functions and plotting examples
cthunes May 23, 2024
6db687b
231 added standalone plot image
cthunes May 24, 2024
b21f280
218 update tcplConfDefault to use API
cthunes May 24, 2024
8b1e560
231 added tcplLoadChem section and improved some text
cthunes May 24, 2024
e100ccf
231 Fixed some wording/grammar issues
cthunes Jun 10, 2024
a945cce
Merge branch '231-add-data-retrieval-via-API-vignette' of https://git…
cthunes Jun 17, 2024
ca9b7cc
231 updates from review
cthunes Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ Suggests:
kableExtra,
colorspace,
magrittr,
vdiffr
vdiffr,
httptest
License: MIT + file LICENSE
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
VignetteBuilder: knitr
Encoding: UTF-8
Config/testthat/edition: 3
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ importFrom(tcplfit2,tcplhit2_core)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,pivot_wider)
importFrom(tidyr,separate_wider_delim)
importFrom(tidyr,unnest)
importFrom(tidyr,unnest_longer)
importFrom(utils,data)
importFrom(utils,read.csv)
Expand Down
10 changes: 10 additions & 0 deletions R/sc1_mthds.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#' \item{bval.apid.tn.med}{Calculate the baseline value (bval) as the plate-wise median,
#' by assay plate ID (apid), of the raw values (rval) for test compound wells (wllt = t)
#' and neutral control wells (wllt = n).}
#' \item{bval.nwlls.med}{Calculate the baseline value (bval) as the median of the raw values
#' (rval) for neutral control wells (wllt = n) by assay endpoint id (aeid).}
#' }
#' }
#'
Expand Down Expand Up @@ -131,6 +133,14 @@ sc1_mthds <- function() {
by = list(aeid, apid)])
list(e1)

},
bval.nwlls.med = function(aeids) {

e1 <- bquote(dat[J(.(aeids)),
bval := median(rval[wllt == "n"], na.rm = TRUE),
by = list(aeid)])
list(e1)

},

pval.apid.pwlls.med = function(aeids) {
Expand Down
4 changes: 2 additions & 2 deletions R/tcplConfDefault.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

tcplConfDefault <- function () {

TCPLlite <- file.path(system.file(package = "tcpl"), "csv")
tcplConf(db = TCPLlite, user = NA, host = NA, drvr = "tcplLite")
tcpl_key <- "01cbaf22-904f-11ee-954e-325096b39f47"
tcplConf(db = NA, user = NA, pass = tcpl_key, host = NULL, drvr = "API")

}

Expand Down
205 changes: 129 additions & 76 deletions R/tcplPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare.
list2env(validated_vars, envir = environment())

# check_tcpl_db_schema is a user-defined function found in v3_schema_functions.R file
if (check_tcpl_db_schema() | !is.null(dat)) {
if (check_tcpl_db_schema() | !is.null(dat) | getOption("TCPL_DRVR") == "API") {
# check if user supplied data. If not, load from db connection
if(is.null(dat)){
dat <- tcplPlotLoadData(lvl = lvl, fld = fld, val = val, type = type,flags = flags, compare = FALSE) #code defined in tcplPlotUtils.R
Expand Down Expand Up @@ -114,7 +114,7 @@ tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare.
nrow = ncol = 1
}
# error message for output="console" and multi=FALSE to avoid multiple plots in console
if(nrow(dat) > 1 && output == "console" && multi == FALSE) stop("More than 1 concentration series returned for given field/val combination. Set output to pdf or reduce the number of curves to 1. Current number of curves: ", nrow(input))
if(nrow(dat[compare == FALSE]) != 1 && output == "console" && multi == FALSE) stop("More than 1 concentration series returned for given field/val combination. Set output to pdf or reduce the number of curves to 1. Current number of curves: ", nrow(dat))
if(is.null(nrow)){
nrow <- ifelse(verbose,2,2)
}
Expand All @@ -132,7 +132,7 @@ tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare.
# tcplPlotlyplot is the user-defined function found in tcplPlot.R file used to connect tcpl and plotly packages
# tcplggplot is the user-defined function found in tcplPlot.R file used to connect tcpl and ggplot2 packages
return(tcplPlotlyPlot(dat, lvl)),
return(ggsave(filename=paste0(fileprefix,"_",paste0(dat$m4id, collapse = "_"),".",output),
return(ggsave(filename=paste0(fileprefix,"_",paste0(ifelse(type=="mc",dat$m4id,dat$s2id), collapse = "_"),".",output),
plot= if(is.null(compare.val)) tcplggplot(dat,verbose = verbose, lvl = lvl, flags = flags, yrange = yrange) else tcplggplotCompare(dat[compare == FALSE],dat[compare == TRUE],verbose = verbose, lvl = lvl, flags = flags, yrange = yrange), width = 7, height = 5, dpi=dpi))
)
} else {
Expand Down Expand Up @@ -399,6 +399,22 @@ tcplPlotlyPlot <- function(dat, lvl = 5){
}
}

} else if (lvl == 2) { #single conc
# main data
if (!is.null(dat$coff) && dat$max_med < 0) {
dat$coff <- dat$coff * -1
}
if (!is.null(dat$coff) && !is.null(dat$hitc) && dat$hitc < 0) {
dat$coff <- dat$coff * -1
}

# compare data
if (!is.null(compare.dat$coff) && compare.dat$max_med < 0) {
compare.dat$coff <- compare.dat$coff * -1
}
if (!is.null(compare.dat$coff) && !is.null(compare.dat$hitc) && compare.dat$hitc < 0) {
compare.dat$coff <- compare.dat$coff * -1
}
}

# function for truncating decimals
Expand Down Expand Up @@ -500,7 +516,7 @@ tcplPlotlyPlot <- function(dat, lvl = 5){
inherit = FALSE,
hoverinfo = "text",
text = ~ paste(
"</br>", paste0("Cut Off B (", specify_decimal(dat$coff,2), ")")
"</br>", paste0("Cut Off B (", specify_decimal(compare.dat$coff,2), ")")
)
)
}
Expand Down Expand Up @@ -831,37 +847,47 @@ tcplggplot <- function(dat, lvl = 5, verbose = FALSE, flags = FALSE, yrange = c(
range()

# check if model_type is 3 or 4, which means an override method was assigned
if (lvl == 5 && dat$model_type == 3) { # gain direction
# leave coff but bmr should flip if top is negative
if (!is.null(dat$top) && !is.na(dat$top) && !is.null(dat$bmr)) {
if (dat$top < 0) {
dat$bmr <- dat$bmr * -1
if (lvl == 5) {
if (dat$model_type == 3) { # gain direction
# leave coff but bmr should flip if top is negative
if (!is.null(dat$top) && !is.na(dat$top) && !is.null(dat$bmr)) {
if (dat$top < 0) {
dat$bmr <- dat$bmr * -1
}
}
} else if (dat$model_type == 4) { # loss direction
# coff and bmr(if top < 0) should be negative
if (!is.null(dat$top) && !is.null(dat$coff) && !is.na(dat$top) && !is.null(dat$bmr)) {
dat$coff <- dat$coff * -1
if (dat$top < 0) {
dat$bmr <- dat$bmr * -1
}
}
} else { # bidirectional
# check if winning model has negative top. If so coff,bmr should be negative
if (!is.null(dat$top) && !is.null(dat$coff) && !is.na(dat$top) && !is.null(dat$bmr)) {
if (dat$top < 0) {
dat$coff <- dat$coff * -1
dat$bmr <- dat$bmr * -1
}
}
}
} else if (lvl == 5 && dat$model_type == 4) { # loss direction
# coff and bmr(if top < 0) should be negative
if (!is.null(dat$top) && !is.null(dat$coff) && !is.na(dat$top) && !is.null(dat$bmr)) {
} else { #single conc
if (!is.null(dat$coff) && dat$max_med < 0) {
dat$coff <- dat$coff * -1
if (dat$top < 0) {
dat$bmr <- dat$bmr * -1
}
}
} else { # bidirectional
# check if winning model has negative top. If so coff,bmr should be negative
if (!is.null(dat$top) && !is.null(dat$coff) && !is.na(dat$top) && !is.null(dat$bmr)) {
if (dat$top < 0) {
dat$coff <- dat$coff * -1
dat$bmr <- dat$bmr * -1
}
if (!is.null(dat$coff) && !is.null(dat$hitc) && dat$hitc < 0) {
dat$coff <- dat$coff * -1
}
}


# check if ac50 is null and assign NA if it is
dat$ac50 <- ifelse(is.null(dat$ac50), NA, dat$ac50)

# check if dtxsid is NA, pull wllt in from lvl 3
if (is.na(dat$dsstox_substance_id) | is.na(dat$chnm)) {
wllt <- unique(tcplLoadData(lvl = 0, fld = list("spid","acid"),
if (getOption("TCPL_DRVR") != "API" && (is.na(dat$dsstox_substance_id) | is.na(dat$chnm))) {
wllt <- unique(tcplLoadData(type = ifelse(lvl == 2, "sc", "mc"), lvl = 0, fld = list("spid","acid"),
list(dat$spid, tcplLoadAcid(fld = "aeid", val = dat$aeid)$acid))$wllt)
if (length(wllt) == 1) {
if (wllt == 'c' | wllt == 'p') {
Expand All @@ -885,13 +911,18 @@ tcplggplot <- function(dat, lvl = 5, verbose = FALSE, flags = FALSE, yrange = c(
dat$chnm <- ""
}
else {
data$dsstox_substance_id <- paste0("Well type: ", wllt)
data$chnm <- ""
dat$dsstox_substance_id <- paste0("Well type: ", wllt)
dat$chnm <- ""
}
}
else {
warning(paste0("wllt for SPID: ", dat$spid, " is missing or length > 1.
if (length(wllt) > 1) {
dat$dsstox_substance_id <- paste0("Well type: ", paste(wllt, collapse = ", "))
dat$chnm <- ""
} else {
warning(paste0("wllt for SPID: ", dat$spid, " is missing.
Leaving dsstox_substance_id and chnm as NA."))
}
}
}

Expand All @@ -915,7 +946,7 @@ tcplggplot <- function(dat, lvl = 5, verbose = FALSE, flags = FALSE, yrange = c(
if (lvl == 2) {
gg <- ggplot(l3_dat, aes(x = conc)) +
geom_hline(aes(yintercept = dat$max_med, linetype = "Max Median"), color="red") +
geom_hline(aes(yintercept = ifelse(dat$max_med >= 0, dat$coff, dat$coff * -1), linetype="Cutoff"), color="blue") +
geom_hline(aes(yintercept = dat$coff, linetype="Cutoff"), color="blue") +
geom_point(aes(y = resp)) +
scale_x_continuous(limits = l3_range, trans = ifelse(0 %in% l3_dat$conc,"identity","log10")) +
scale_y_continuous(limits = yrange) +
Expand Down Expand Up @@ -1093,62 +1124,77 @@ tcplggplotCompare <- function(dat, compare.dat, lvl = 5, verbose = FALSE, flags

if (dat$conc_unit != compare.dat$conc_unit || dat$normalized_data_type != compare.dat$normalized_data_type) stop("Units do not match.")

# main data
# check if model_type is 3 or 4, which means an override method was assigned
if (lvl == 5 && dat$model_type == 3) { # gain direction
# leave coff but bmr should flip if top is negative
if (!is.null(dat$top) && !is.na(dat$top) && !is.null(dat$bmr)) {
if (dat$top < 0) {
dat$bmr <- dat$bmr * -1
}
}
} else if (lvl == 5 && dat$model_type == 4) { # loss direction
# coff and bmr(if top < 0) should be negative
if (!is.null(dat$top) && !is.null(dat$coff) && !is.na(dat$top) && !is.null(dat$bmr)) {
dat$coff <- dat$coff * -1
if (dat$top < 0) {
dat$bmr <- dat$bmr * -1
if (lvl == 5) {
# main data
if (dat$model_type == 3) { # gain direction
# leave coff but bmr should flip if top is negative
if (!is.null(dat$top) && !is.na(dat$top) && !is.null(dat$bmr)) {
if (dat$top < 0) {
dat$bmr <- dat$bmr * -1
}
}
}
} else { # bidirectional
# check if winning model has negative top. If so coff,bmr should be negative
if (!is.null(dat$top) && !is.null(dat$coff) && !is.na(dat$top) && !is.null(dat$bmr)) {
if (dat$top < 0) {
} else if (dat$model_type == 4) { # loss direction
# coff and bmr(if top < 0) should be negative
if (!is.null(dat$top) && !is.null(dat$coff) && !is.na(dat$top) && !is.null(dat$bmr)) {
dat$coff <- dat$coff * -1
dat$bmr <- dat$bmr * -1
if (dat$top < 0) {
dat$bmr <- dat$bmr * -1
}
}
}
}

# compare data
# check if model_type is 3 or 4, which means an override method was assigned
if (lvl == 5 && compare.dat$model_type == 3) { # gain direction
# leave coff but bmr should flip if top is negative
if (!is.null(compare.dat$top) && !is.na(compare.dat$top) && !is.null(compare.dat$bmr)) {
if (compare.dat$top < 0) {
compare.dat$bmr <- compare.dat$bmr * -1
} else { # bidirectional
# check if winning model has negative top. If so coff,bmr should be negative
if (!is.null(dat$top) && !is.null(dat$coff) && !is.na(dat$top) && !is.null(dat$bmr)) {
if (dat$top < 0) {
dat$coff <- dat$coff * -1
dat$bmr <- dat$bmr * -1
}
}
}
} else if (lvl == 5 && compare.dat$model_type == 4) { # loss direction
# coff and bmr(if top < 0) should be negative
if (!is.null(compare.dat$top) && !is.null(compare.dat$coff) && !is.na(compare.dat$top) && !is.null(compare.dat$bmr)) {
compare.dat$coff <- compare.dat$coff * -1
if (compare.dat$top < 0) {
compare.dat$bmr <- compare.dat$bmr * -1

# compare data
if (compare.dat$model_type == 3) { # gain direction
# leave coff but bmr should flip if top is negative
if (!is.null(compare.dat$top) && !is.na(compare.dat$top) && !is.null(compare.dat$bmr)) {
if (compare.dat$top < 0) {
compare.dat$bmr <- compare.dat$bmr * -1
}
}
}
} else { # bidirectional
# check if winning model has negative top. If so coff,bmr should be negative
if (!is.null(compare.dat$top) && !is.null(compare.dat$coff) && !is.na(compare.dat$top) && !is.null(compare.dat$bmr)) {
if (compare.dat$top < 0) {
} else if (compare.dat$model_type == 4) { # loss direction
# coff and bmr(if top < 0) should be negative
if (!is.null(compare.dat$top) && !is.null(compare.dat$coff) && !is.na(compare.dat$top) && !is.null(compare.dat$bmr)) {
compare.dat$coff <- compare.dat$coff * -1
compare.dat$bmr <- compare.dat$bmr * -1
if (compare.dat$top < 0) {
compare.dat$bmr <- compare.dat$bmr * -1
}
}
} else { # bidirectional
# check if winning model has negative top. If so coff,bmr should be negative
if (!is.null(compare.dat$top) && !is.null(compare.dat$coff) && !is.na(compare.dat$top) && !is.null(compare.dat$bmr)) {
if (compare.dat$top < 0) {
compare.dat$coff <- compare.dat$coff * -1
compare.dat$bmr <- compare.dat$bmr * -1
}
}
}
} else { #single conc
# main data
if (!is.null(dat$coff) && dat$max_med < 0) {
dat$coff <- dat$coff * -1
}
if (!is.null(dat$coff) && !is.null(dat$hitc) && dat$hitc < 0) {
dat$coff <- dat$coff * -1
}

# compare data
if (!is.null(compare.dat$coff) && compare.dat$max_med < 0) {
compare.dat$coff <- compare.dat$coff * -1
}
if (!is.null(compare.dat$coff) && !is.null(compare.dat$hitc) && compare.dat$hitc < 0) {
compare.dat$coff <- compare.dat$coff * -1
}
}



# check if data is outside bounds of yrange. If so, expand yrange bounds
if (!identical(yrange, c(NA,NA))) {
yrange[1] <- min(dat$resp_min, dat$coff, yrange[1], unlist(dat$resp),
Expand All @@ -1160,7 +1206,7 @@ tcplggplotCompare <- function(dat, compare.dat, lvl = 5, verbose = FALSE, flags
check_wllt <- function(data) {
# check if dtxsid is NA, pull wllt in from lvl 3
if (is.na(data$dsstox_substance_id) | is.na(data$chnm)) {
wllt <- unique(tcplLoadData(lvl = 0, fld = list("spid","acid"),
wllt <- unique(tcplLoadData(type = ifelse(lvl == 2, "sc", "mc"), lvl = 0, fld = list("spid","acid"),
list(data$spid, tcplLoadAcid(fld = "aeid", val = data$aeid)$acid))$wllt)
if (length(wllt) == 1) {
if (wllt == 'c' | wllt == 'p') {
Expand Down Expand Up @@ -1189,14 +1235,21 @@ tcplggplotCompare <- function(dat, compare.dat, lvl = 5, verbose = FALSE, flags
}
}
else {
warning(paste0("wllt for SPID: ", data$spid, " is missing or length > 1.
if (length(wllt) > 1) {
data$dsstox_substance_id <- paste0("Well type: ", paste(wllt, collapse = ", "))
data$chnm <- ""
} else {
warning(paste0("wllt for SPID: ", data$spid, " is missing.
Leaving dsstox_substance_id and chnm as NA."))
}
}
}
return(data)
}
dat <- check_wllt(dat)
compare.dat <- check_wllt(compare.dat)
if (getOption("TCPL_DRVR") != "API") {
dat <- check_wllt(dat)
compare.dat <- check_wllt(compare.dat)
}

dat$winning_model_string <- paste0("Model A(", dat$modl, ")")
compare.dat$winning_model_string <- paste0("Model B(", compare.dat$modl, ")")
Expand Down
Loading