Skip to content

Commit

Permalink
Merge pull request #63 from USEPA/61-rename-cctected-to-ctx
Browse files Browse the repository at this point in the history
61 rename cctected to ctx
  • Loading branch information
rachmaninoffpk authored Jun 27, 2024
2 parents 1860bec + 10a01a5 commit e6588c4
Show file tree
Hide file tree
Showing 107 changed files with 774 additions and 760 deletions.
16 changes: 9 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Type: Package
Package: ccdR
Title: Utilities for Interacting with the 'CCTE' APIs
Title: Utilities for Interacting with the 'CTX' APIs
Version: 1.0.0
Authors@R: c(
person("Paul", "Kruse", email = "[email protected]",
Expand All @@ -17,13 +17,15 @@ Authors@R: c(
person("Jason", "Brown", role = "ctb",
email = "[email protected]")
)
Description: Access chemical, hazard, and bioactivity data from the Center for
Computational Toxicology and Exposure ('CCTE') APIs
Description: Access chemical, hazard, bioactivity, and exposure data from the
Computational Toxicology and Exposure ('CTX') APIs
<https://api-ccte.epa.gov/docs/>. 'ccdR' was developed to streamline the
process of accessing the information available through the 'CCTE' APIs
without requiring prior knowledge of how to use APIs. All data is also
available on the CompTox Chemical Dashboard ('CCD')
<https://comptox.epa.gov/dashboard/>.
process of accessing the information available through the 'CTX' APIs
without requiring prior knowledge of how to use APIs. Most data is also
available on the CompTox Chemical Dashboard ('CCD')
<https://comptox.epa.gov/dashboard/> and other resources found at the
EPA Computational Toxicology and Exposure Online Resources
<https://www.epa.gov/comptox-tools>.
License: GPL (>= 3)
Imports:
cli,
Expand Down
8 changes: 5 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# Generated by roxygen2: do not edit by hand

S3method(print,ccte_credentials)
S3method(print,ctx_credentials)
export(ccdr_hide_api_key)
export(ccdr_show_api_key)
export(ccte_key)
export(chemical_contains)
export(chemical_contains_batch)
export(chemical_equal)
export(chemical_equal_batch)
export(chemical_starts_with)
export(chemical_starts_with_batch)
export(ctx_key)
export(get_all_assays)
export(get_all_public_chemical_lists)
export(get_annotation_by_aeid)
export(get_annotation_by_aeid_batch)
export(get_bioactivity_details)
export(get_bioactivity_details_batch)
export(get_bioactivity_endpoint_status)
export(get_bioactivity_summary)
export(get_bioactivity_summary_batch)
export(get_cancer_hazard)
Expand Down Expand Up @@ -61,6 +62,7 @@ export(get_genetox_summary)
export(get_genetox_summary_batch)
export(get_hazard_by_dtxsid)
export(get_hazard_by_dtxsid_batch)
export(get_hazard_endpoint_status)
export(get_human_hazard_by_dtxsid)
export(get_human_hazard_by_dtxsid_batch)
export(get_inchi)
Expand All @@ -81,7 +83,7 @@ export(get_skin_eye_hazard_batch)
export(get_smiles)
export(has_ccdr_option)
export(has_ccdr_options)
export(has_ccte_key)
export(has_ctx_key)
export(register_ccdr)
export(set_ccdr_option)
export(showing_key)
10 changes: 5 additions & 5 deletions R/attach.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
' ',
"CCTE's Terms of Service: ",
cli::col_blue(cli::style_italic(
cli::style_hyperlink('<https://api-ccte.epa.gov/docs/>', 'https://api-ccte.epa.gov/docs/')
cli::style_hyperlink('<https://www.epa.gov/comptox-tools/computational-toxicology-and-exposure-apis>', 'https://www.epa.gov/comptox-tools/computational-toxicology-and-exposure-apis')
))
)
cite <- paste0(
cli::col_green(cli::symbol$info),
' ',
'Please cite ', cli::col_blue('ccdr'), ' if you use it! Use `citation(\'ccdr\')` for details.'
'Please cite ', cli::col_blue('ccdR'), ' if you use it! Use `citation(\'ccdR\')` for details.'
)

rlang::inform(
Expand All @@ -32,11 +32,11 @@

bootstrap_ccdr <- function() {
set_ccdr_option(
'ccte' = structure(
'ctx' = structure(
list(

),
class = 'ccte_credentials'
class = 'ctx_credentials'
),
'display_api_key' = FALSE
)
Expand Down Expand Up @@ -68,7 +68,7 @@ bootstrap_ccdr <- function() {
} else {
if (!silent) packageStartupMessage("API key file found but no api entry.")
}
} else if ((ev <- Sys.getenv("CCTE_API_KEY")) != "") {
} else if ((ev <- Sys.getenv("CTX_API_KEY")) != "") {
.pkgenv[["api"]] <- ev
if (!silent) packageStartupMessage("Setting API key from environment variable.")
} else {
Expand Down
18 changes: 9 additions & 9 deletions R/bioactivity-APIs-batch.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @return A named list of data.frames containing bioactivity information for
#' the chemicals with DTXSID or assays with AEID matching the input parameter.
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Pull bioactivity details for multiple chemicals
#' dtxsid <- c('DTXSID7020182', 'DTXSID2021315')
#' batch_bioactivity <- get_bioactivity_details_batch(DTXSID = dtxsid)
Expand All @@ -28,8 +28,8 @@ get_bioactivity_details_batch <- function(DTXSID = NULL,
rate_limit = 0L,
verbose = FALSE){
if (is.null(API_key) || !is.character(API_key)){
if (has_ccte_key()) {
API_key <- ccte_key()
if (has_ctx_key()) {
API_key <- ctx_key()
if (verbose) {
message('Using stored API key!')
}
Expand Down Expand Up @@ -163,7 +163,7 @@ get_bioactivity_details_batch <- function(DTXSID = NULL,
#' @return A named list of data.frames containing bioactivity summary
#' information for the assays with AEID matching the input parameter.
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Get bioactivity summary for multiple aeids
#' aeids <- get_bioactivity_summary_batch(AEID = c(159, 160))

Expand All @@ -175,8 +175,8 @@ get_bioactivity_summary_batch <- function(AEID = NULL,
rate_limit = 0L,
verbose = FALSE){
if (is.null(API_key) || !is.character(API_key)){
if (has_ccte_key()) {
API_key <- ccte_key()
if (has_ctx_key()) {
API_key <- ctx_key()
if (verbose) {
message('Using stored API key!')
}
Expand Down Expand Up @@ -232,7 +232,7 @@ get_bioactivity_summary_batch <- function(AEID = NULL,
#' @return A named list of data.frames containing annotation information for the
#' assays with AEID matching the input parameter.
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Get annotations for multiple aeids
#' aeid_annotations <- get_annotation_by_aeid_batch(AEID = c(159, 160))

Expand All @@ -242,8 +242,8 @@ get_annotation_by_aeid_batch <- function(AEID = NULL,
rate_limit = 0L,
verbose = FALSE){
if (is.null(API_key) || !is.character(API_key)){
if (has_ccte_key()) {
API_key <- ccte_key()
if (has_ctx_key()) {
API_key <- ctx_key()
if (verbose) {
message('Using stored API key!')
}
Expand Down
72 changes: 28 additions & 44 deletions R/bioactivity-APIs.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @return A data.frame containing bioactivity information for the chemical or assay endpoint with
#' identifier matching the input parameter.
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Pull BPA bioactivity details
#' bpa <- get_bioactivity_details(DTXSID = 'DTXSID7020182')
#' # Pull assay bioactivity details
Expand All @@ -30,8 +30,8 @@ get_bioactivity_details <- function(DTXSID = NULL,
else if (length(which(!sapply(list(DTXSID, AEID, SPID, m4id), is.null))) > 1)
stop('Please input a value for only one of DTXSID, AEID, SPID, or m4id, but not multiple!')
else if (is.null(API_key)){
if (has_ccte_key()) {
API_key <- ccte_key()
if (has_ctx_key()) {
API_key <- ctx_key()
if (verbose) {
message('Using stored API key!')
}
Expand Down Expand Up @@ -76,11 +76,7 @@ get_bioactivity_details <- function(DTXSID = NULL,
stop('Please input an API_key!')
}
if(response$status_code == 200){
req_content <- httr::content(response, as = 'text', encoding = "UTF-8")
if (nchar(req_content) == 0){
return(data.table::data.table())
}
res <- jsonlite::fromJSON(req_content)
res <- jsonlite::fromJSON(httr::content(response, as = 'text', encoding = "UTF-8"))
if (!is.data.frame(res) & (length(res) != 0)){
for (i in 1:length(res)){
if (is.null(res[[i]])) res[[i]] <- NA # set any NULLs to NA
Expand All @@ -93,35 +89,9 @@ get_bioactivity_details <- function(DTXSID = NULL,
param_cols <- c('mc3Param', 'mc4Param', 'mc5Param', 'mc6Param')
col_index <- which(param_cols %in% names(res))
if (length(col_index) > 0){
# In some cases, columns are given by data.frames and we will not try to unnest these
non_df_cols <- param_cols[col_index][which(sapply(param_cols[col_index], function(t){!is.data.frame(res[[t]])}))]
if (length(non_df_cols) > 0)
# In some cases, columns will be NA (in the m4id cases) and we will not try to unnest these
col_index <- which(unname(!sapply(res[which(names(res) %in% non_df_cols)], is.na)))
if (length(col_index) > 0){
res <- tidyr::unnest_wider(data = res, col = param_cols[col_index])
}
res <- tidyr::unnest_wider(data = res, col = param_cols[col_index])
}


res_dt <- data.table::data.table(res)

# For columns which are lists of lists, we want to unnest the lists
# Check which columns that tend to return nested are in the data.table
nested_list_cols <- intersect(names(res_dt), c('resp', 'logc', 'flag', 'mc6MthdId'))

# Check which of these columns are lists of lists. Every entry must be a
# list in order to be unnested
nested_indices <- sapply(seq_along(nested_list_cols), function(t) {
all(sapply(as.data.frame(res_dt)[, nested_list_cols[[t]]], is.list))
})

# Unnest the columns which are nested lists
if (length(nested_indices) > 0 && any(nested_indices)){
res_temp <- tidyr::unnest(data = as.data.frame(res_dt), cols = nested_list_cols[nested_indices])
res_dt <- data.table::as.data.table(res_temp)
}

return(res_dt)
} else {
if (verbose){
Expand All @@ -141,7 +111,7 @@ get_bioactivity_details <- function(DTXSID = NULL,
#' @return A data.frame containing summary information corresponding to the
#' input AEID
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Pull an assay bioactivity summary
#' aeid_1386 <- get_bioactivity_summary(AEID = 1386)
get_bioactivity_summary <- function(AEID = NULL,
Expand All @@ -153,8 +123,8 @@ get_bioactivity_summary <- function(AEID = NULL,
if (is.null(AEID))
stop('Please input an AEID!')
else if (is.null(API_key)){
if (has_ccte_key()){
API_key <- ccte_key()
if (has_ctx_key()){
API_key <- ctx_key()
if (verbose) {
message('Using stored API key!')
}
Expand Down Expand Up @@ -208,16 +178,16 @@ get_bioactivity_summary <- function(AEID = NULL,
#'
#' @return A data.frame containing all the assays and associated information
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Retrieve all assays
#' assays <- get_all_assays()
get_all_assays <- function(API_key = NULL,
Server = bioactivity_api_server,
verbose = FALSE){

if (is.null(API_key)){
if (has_ccte_key()){
API_key <- ccte_key()
if (has_ctx_key()){
API_key <- ctx_key()
if (verbose) {
message('Using stored API key!')
}
Expand Down Expand Up @@ -258,7 +228,7 @@ get_all_assays <- function(API_key = NULL,
#' @return A data.frame containing the annotated assays corresponding to the
#' input AEID parameter
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Retrieve annotation for an assay
#' annotation <- get_annotation_by_aeid(AEID = 159)
get_annotation_by_aeid <- function(AEID = NULL,
Expand All @@ -268,8 +238,8 @@ get_annotation_by_aeid <- function(AEID = NULL,
if (is.null(AEID))
stop('Please input an AEID!')
else if (is.null(API_key)){
if (has_ccte_key()){
API_key <- ccte_key()
if (has_ctx_key()){
API_key <- ctx_key()
if (verbose) {
message('Using stored API key!')
}
Expand Down Expand Up @@ -313,3 +283,17 @@ get_annotation_by_aeid <- function(AEID = NULL,


}

#' Bioactivity API Endpoint status
#'
#' @return Status of Bioactivity API Endpoints
#' @export
#'
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' status <- get_bioactivity_endpoint_status()
#' print(status)

get_bioactivity_endpoint_status <- function(){
request <- httr::GET(url = paste0(bioactivity_api_server, "/health"))
return(request$status_code)
}
8 changes: 4 additions & 4 deletions R/ccdr_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' ccdr stores options as a named list in R's global options, i.e.
#' `getOption('ccdr')`. It currently stores two such options, one for CCTE
#' credentialing and one to supress private API information in the URLs printed
#' credentialing and one to suppress private API information in the URLs printed
#' to the screen when web queries are placed. For both of those, see
#' [register_ccdr()].
#'
Expand All @@ -28,7 +28,7 @@

#' @rdname ccdr_options
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Set ccdr options
#' set_ccdr_option('display_api_key' = FALSE)
set_ccdr_option <- function(...) {
Expand Down Expand Up @@ -62,7 +62,7 @@ set_ccdr_option <- function(...) {

#' @rdname ccdr_options
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Check if there are options registered to 'ccdr'
#' has_ccdr_options()

Expand All @@ -72,7 +72,7 @@ has_ccdr_options <- function() {

#' @rdname ccdr_options
#' @export
#' @examplesIf has_ccte_key() & is.na(ccte_key() == 'FAKE_KEY')
#' @examplesIf has_ctx_key() & is.na(ctx_key() == 'FAKE_KEY')
#' # Check if a specific option is registered for 'ccdr'
#' has_ccdr_option('display_api_key')

Expand Down
Loading

0 comments on commit e6588c4

Please sign in to comment.