Skip to content

Commit

Permalink
Function wrappers, new package data, new vignettes (#32)
Browse files Browse the repository at this point in the history
* Function wrappers, new package data, new vignettes

* More merge changes
  • Loading branch information
SkylarMarvel authored Feb 8, 2024
1 parent f9fca8e commit 4379f9f
Show file tree
Hide file tree
Showing 36 changed files with 1,493 additions and 1,371 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/*.Rcheck/

# RStudio files
*.Rproj
.Rproj.user/

# produced vignettes
Expand Down
7 changes: 7 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ License: MIT + file LICENSE
Suggests:
dplyr,
ggplot2,
ggpubr,
httk,
knitr,
readr,
readxl,
rmarkdown,
scales,
sf,
stringr,
testthat (>= 3.0.0),
tidyr
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports:
methods,
truncnorm
VignetteBuilder: knitr
Depends:
Expand Down
22 changes: 0 additions & 22 deletions GeoTox.Rproj

This file was deleted.

3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
export(calc_concentration_response)
export(calc_internal_dose)
export(calc_invitro_concentration)
export(extract_hill_params)
export(fit_hill)
export(sample_Css)
export(simulate_age)
export(simulate_exposure)
export(simulate_inhalation_rate)
export(simulate_obesity)
54 changes: 35 additions & 19 deletions R/calc_concentration_response.R
Original file line number Diff line number Diff line change
@@ -1,54 +1,70 @@
#' Calculate the mixture response from one of three different approaches:
#' IA, GCA ,or Hazard Quotient
#'
#' @param resp data frame with columns "tp", "tp.sd", "logAC50", "logAC50.sd",
#' "resp_max", "logc_min", "logc_max".
#' @param concentration concentration
#' @param C_invitro in vitro concentrations
#' @param hill_params output from `fit_hill()`
#' @param tp_b_mult upper bound multiplier for tp rtruncnorm
#' @param fixed if TRUE, sd = 0
#'
#' @description
#' Calculate the combined response of multiple chemicals. It calculates the
#' generalized concentration addition response, the independent action
#' response, and a hazard quotient
#'
#' @return data frame
#'
#' @export
calc_concentration_response <- function(
resp, concentration, tp_b_mult = 1.5, fixed = FALSE
C_invitro, hill_params, tp_b_mult = 1.5, fixed = FALSE
) {

# TODO make inputs more general
# TODO add input for linear/log concentration
# TODO maybe add option for ln(resp) or not
if (methods::is(C_invitro, "matrix")) {
.calc_concentration_response(C_invitro, hill_params, tp_b_mult, fixed)
} else {
mapply(
.calc_concentration_response,
C_invitro = C_invitro,
hill_params = list(hill_params),
tp_b_mult = tp_b_mult,
fixed = fixed,
SIMPLIFY = FALSE
)
}

}

.calc_concentration_response <- function(
C_invitro, hill_params, tp_b_mult, fixed
) {

interval <- c(-50,50)

# TODO value of b not consistent
# grep "tp.sim <-" ~/github/GeoToxMIE/*.R
tp <- t(sapply(1:nrow(concentration), function(x) {
tp <- t(sapply(1:nrow(C_invitro), function(x) {
truncnorm::rtruncnorm(
1,
a = 0,
b = resp$resp_max * tp_b_mult,
mean = resp$tp,
sd = if (fixed) 0 else resp$tp.sd
b = hill_params$resp_max * tp_b_mult,
mean = hill_params$tp,
sd = if (fixed) 0 else hill_params$tp.sd
)
}))

logAC50 <- t(sapply(1:nrow(concentration), function(x) {
logAC50 <- t(sapply(1:nrow(C_invitro), function(x) {
truncnorm::rtruncnorm(
1,
a = resp$logc_min - 2,
b = resp$logc_max + 0.5,
mean = resp$logAC50,
sd = if (fixed) 0 else resp$logAC50.sd
a = hill_params$logc_min - 2,
b = hill_params$logc_max + 0.5,
mean = hill_params$logAC50,
sd = if (fixed) 0 else hill_params$logAC50.sd
)
}))

GCA.eff <- IA.eff <- GCA.HQ.10 <- IA.HQ.10 <- rep(NA, nrow(concentration))
for (i in 1:nrow(concentration)) {
GCA.eff <- IA.eff <- GCA.HQ.10 <- IA.HQ.10 <- rep(NA, nrow(C_invitro))
for (i in 1:nrow(C_invitro)) {

C_i <- concentration[i, ]
C_i <- C_invitro[i, ]
tp_i <- tp[i, ]
AC50_i <- 10^logAC50[i, ]

Expand Down
35 changes: 32 additions & 3 deletions R/calc_internal_dose.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
#' Calculate internal chemical dose
#'
#' @description
#' Estimate the internal dose from inhalation of a chemical given inhalation rate, time, and body weight
#' Estimate the internal dose from inhalation of a chemical given inhalation
#' rate, time, and body weight
#'
#' @param C_ext ambient chemical concentration in \eqn{\frac{mg}{m^3}}
#' @param IR inhalation rate in \eqn{\frac{m^3}{day}}
#' @param time total time in \eqn{days}
#' @param BW body weight in \eqn{kg}
#' @param scaling scaling factor encompassing any required unit adjustments
#'
#' @details
#' TODO Additional details...
#' \deqn{D_{int} = \frac{C_{ext} \,\times\, IR \,\times\, time}{BW}}
#'
#' @return internal chemical dose in \eqn{\frac{mg}{kg}}
#'
#' @examples
#' n_chem <- 3
#' n_sample <- 5
#'
#' # Single population
#' C_ext <- matrix(runif(n_sample * n_chem), ncol = n_chem)
#' IR <- runif(n_sample)
#' calc_internal_dose(C_ext, IR)
#'
#' # Multiple populations
#' C_ext <- list(
#' "a" = matrix(runif(n_sample * n_chem), ncol = n_chem),
#' "b" = matrix(runif(n_sample * n_chem), ncol = n_chem)
#' )
#' IR <- list(runif(n_sample), runif(n_sample))
#' calc_internal_dose(C_ext, IR)
#'
#' @export
calc_internal_dose <- function(C_ext, IR, time = 1, BW = 1) {
calc_internal_dose <- function(C_ext, IR, time = 1, BW = 1, scaling = 1) {
# TODO How to handle inputs with different units?
# e.g. simulated inhalation rate is in m^3/(day * kg), so BW isn't needed
# TODO paper states t = 365 in section 2.3, also states that C_ss achieved
# in 1 day and repeated exposure accumulates additively. Computation done
# with t = 1, is that correct?
C_ext * IR * time / BW

if (methods::is(C_ext, "matrix")) {
.calc_internal_dose(C_ext, IR, time, BW, scaling)
} else {
mapply(.calc_internal_dose, C_ext, IR, time, BW, scaling, SIMPLIFY = FALSE)
}
}

.calc_internal_dose <- function(C_ext, IR, time, BW, scaling) {
C_ext * IR * time / BW * scaling
}
13 changes: 11 additions & 2 deletions R/calc_invitro_concentration.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@
#' TODO Additional details...
#' \deqn{C_{plasma} = C_{ss} \,\times\, D_{int}}
#'
#' TODO If C_ss is not provided, uses \emph{httk} to create...
#'
#' @return \emph{in vitro} equivalent plasma concentration in \eqn{\mu M}
#' @export
calc_invitro_concentration <- function(D_int, C_ss = NULL) {

if (is.null(C_ss)) {
# TODO add real-time computation of Css values
stop("real-time computation of Css values has not been implemented")
}

# TODO the current C_ss data passed into this for step 01-Sensitivity.R
# doesn't match the ages that were simulated?

if (methods::is(D_int, "matrix")) {
.calc_invitro_concentration(D_int, C_ss)
} else {
mapply(.calc_invitro_concentration, D_int, C_ss, SIMPLIFY = FALSE)
}
}

.calc_invitro_concentration <- function(D_int, C_ss) {
D_int * C_ss
}
28 changes: 13 additions & 15 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#' Geo Tox data
#'
#' A subset of data from the GeoToxMIE source scripts
#' Sample data for use in vignettes and function examples. See the
#' "package_data" vignette for details on how this data was gathered.
#'
#' @format A list with 4 items:
#' @format A list with items:
#' \describe{
#' \item{ice}{Description}
#' \item{MC_iter}{Description}
#' \item{cyp1a1}{Description}
#' \item{age}{Description}
#' \item{Css}{Description}
#' }
#' Css is a list with 5 items:
#' \describe{
#' \item{Css$age}{Description}
#' \item{Css$obesity}{Description}
#' \item{Css$httk}{Description}
#' \item{Css$'dose-resp'}{Description}
#' \item{Css$'ext-conc'}{Description}
#' \item{exposure}{2019 AirToxScreen exposure concentrations for a subset of
#' chemicals in North Carolina counties}
#' \item{dose_response}{A subset of data from InvitroDB for the
#' "LTEA_HepaRG_CYP1A1_up" assay}
#' \item{age}{County population estimates for 7/1/2019 in North Carolina}
#' \item{obesity}{CDC PLACES obesity data for North Carolina counties in
#' 2020}
#' \item{simulated_css}{Simulated steady-state plasma concentrations for
#' various age groups and obesity status combinations}
#' \item{boundaries}{County and state boundaries for North Carolina in 2019}
#' }
"geo_tox_data"
45 changes: 0 additions & 45 deletions R/extract_hill_params.R

This file was deleted.

Loading

0 comments on commit 4379f9f

Please sign in to comment.