R/utilities.R

Defines functions .return_user_agent .return_library loadMetadata stopIfInvalidLibraries validateLibraries .validateLibrary .ilincsBaseUrl targetRename

Documented in .ilincsBaseUrl loadMetadata .return_library .return_user_agent stopIfInvalidLibraries targetRename validateLibraries .validateLibrary

#' Rename the Target-Related Columns
#'
#' This function is used to standardize the names
#' of the columns output at the end of the result.
#'
#' @param inputNames A character vector of input_names
#'
#' @return A character vector of new names
targetRename <- function(inputNames) {
    if ("treatment" %in% inputNames) {
        c(
            "TargetSignature", "Target", "TargetCellLine",
            "TargetTime", "Similarity", "SignatureDirection", "pValue"
        )
    } else {
        c(
            "TargetSignature", "Target", "TargetCellLine",
            "TargetTime", "TargetConcentration", "Similarity",
            "SignatureDirection", "pValue"
        )
    }
}

#' Parameterize the base URL for the iLINCS API
#'
#' @keywords internal
#' @return a fixed string URL
.ilincsBaseUrl <- function() {
    "https://www.ilincs.org/api"
}

#' Check if the library is valid
#'
#' @param lib a string of libraries
#'
#' @keywords internal
#' @return a boolean
.validateLibrary <- function(lib) {
    lib %in% c("CP", "KD", "OE")
}

#' Check if the libraries input are valid
#'
#' This function confirms that the library parameter
#' is one of the allowed ones.
#'
#' @param libs a character vector of libraries
#'
#' @keywords internal
#' @return a boolean
validateLibraries <- function(libs) {
    all(purrr::map_lgl(libs, .validateLibrary))
}

#' Stop if the libraries are invalid
#'
#' @param libs a character vector of libraries
#'
#' @keywords internal
#' @return a stop if the libraries are invalid
stopIfInvalidLibraries <- function(libs) {
    if (!validateLibraries(libs)) {
        stop("Both input and output libraries must be one of 'OE', 'KD', 'CP'")
    }
}

#' Load the correct metadata table
#'
#' @param lib a string. One of "OE", "KD" or "CP"
#'
#' @keywords internal
#' @return a tibble
loadMetadata <- function(lib) {
    if (lib == "OE") {
        oeMetadata
    } else if (lib == "KD") {
        kdMetadata
    } else if (lib == "CP") {
        cpMetadata
    } else {
        stop("Invalid library")
    }
}


#' Return the internal iLINCS Library ID for a given library
#'
#' @param lib A library name. Can be one of "OE", "KD" or "CP"
#'
#' @keywords internal
#' @return A string with the associated library ID
.return_library <- function(lib) {
    stopIfInvalidLibraries(lib)

    libMap <- c(
        OE = "LIB_11",
        KD = "LIB_6",
        CP = "LIB_5"
    )

    libMap[lib]
}

#' Return a string suitable as a User-Agent for the iLINCS API
#'
#' @keywords internal
#' @return a string
.return_user_agent <- function() {
    paste0("drugfindR/", utils::packageVersion("drugfindR"), "; https://github.com/CogDisResLab/drugfindR")
}
AliSajid/drugfindR documentation built on Jan. 29, 2025, 3:25 p.m.