Skip to content

Commit

Permalink
refactor: shorten function lengths
Browse files Browse the repository at this point in the history
Signed-off-by: Ali Sajid Imami <395482+AliSajid@users.noreply.github.com>
  • Loading branch information
AliSajid committed Jun 17, 2024
1 parent 41124e4 commit ad13576
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 22 deletions.
7 changes: 1 addition & 6 deletions R/investigateSignature.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ investigateSignature <- function(
sourceCellLine = "NA",
sourceTime = "NA",
sourceConcentration = "NA") {
libs <- c("OE", "KD", "CP")

if (!outputLib %in% libs) {
stop("Output library must be one of 'OE', 'KD', 'CP'")
}

stopIfInvalidLibraries(outputLib)
if (missing(outputLib)) {
stop("Please specify an output library")
}
Expand Down
18 changes: 2 additions & 16 deletions R/investigateTarget.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,9 @@ investigateTarget <- function(
similarityThreshold = 0.321,
paired = TRUE, inputCellLines = NULL,
outputCellLines = NULL) {
libs <- c("OE", "KD", "CP")

if (!inputLib %in% libs || !outputLib %in% libs) {
stop("Both input and output libraries must be one of 'OE', 'KD', 'CP'")
}


if (inputLib == "OE") {
inputMetadata <- oeMetadata # nolint: object_usage_linter.
} else if (inputLib == "KD") {
inputMetadata <- kdMetadata # nolint: object_usage_linter.
} else if (inputLib == "CP") {
inputMetadata <- cpMetadata # nolint: object_usage_linter.
} else {
stop("Invalid inputLib")
}
stopIfInvalidLibraries(c(inputLib, outputLib))

inputMetadata <- loadMetadata(inputLib)

if (!is.null(inputCellLines)) {
filteredSignatureIds <- inputMetadata %>%
Expand Down
46 changes: 46 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,49 @@ targetRename <- function(inputNames) {
)
}
}

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

#' Check if the libraries input are valid
#'
#' @param libs a character vector of libraries
#'
#' @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
#'
#' @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"
#'
#' @return a tibble
loadMetadata <- function(lib) {
if (lib == "OE") {
oeMetadata
} else if (lib == "KD") {
kdMetadata
} else if (lib == "CP") {
cpMetadata
} else {
stop("Invalid library")
}
}
17 changes: 17 additions & 0 deletions man/dot-validateLibrary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/loadMetadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/stopIfInvalidLibraries.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/validateLibraries.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified tests/testthat/fixtures/exampleSignature.RDS
Binary file not shown.

0 comments on commit ad13576

Please sign in to comment.