Skip to content

Commit

Permalink
Allow to specify temp filenames in 'mhcnuggetsr_self_test'
Browse files Browse the repository at this point in the history
  • Loading branch information
richelbilderbeek committed Feb 27, 2021
1 parent c9e14b1 commit 1c24ab3
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 17 deletions.
25 changes: 20 additions & 5 deletions R/install_pip.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
#' Install pip.
#' @param python_script_filename name of a temporary Python
#' script file. Will be deleted at the end of the function
#' @return Nothing
#' @examples
#' \dontrun{
#' install_pip()
#' }
#' @author Richèl J.C. Bilderbeek
#' @export
install_pip <- function() {
script_filename <- tempfile()
install_pip <- function(
python_script_filename = file.path(
tmpdir = rappdirs::user_cache_dir(),
"temp_install_pip.py"
)
) {
dir.create(
dirname(python_script_filename),
showWarnings = FALSE,
recursive = TRUE
)

utils::download.file(
url = "https://bootstrap.pypa.io/get-pip.py",
destfile = script_filename,
destfile = python_script_filename,
quiet = TRUE
)
system2(
error_code <- system2(
reticulate::py_config()$python,
args = c(script_filename, "--user"),
args = c(python_script_filename, "--user"),
stdout = FALSE
)

file.remove(python_script_filename)

error_code
}
12 changes: 10 additions & 2 deletions R/mhcnuggetsr_self_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
#' @author Richèl J.C. Bilderbeek
#' @export
mhcnuggetsr_self_test <- function(
mhcnuggets_options = create_test_mhcnuggets_options()
mhcnuggets_options = create_test_mhcnuggets_options(),
peptides_path = create_temp_peptides_path(),
mhcnuggets_output_filename = mhcnuggetsr::create_temp_peptides_path(
fileext = ".csv"
),
verbose = FALSE
) {
df <- mhcnuggetsr::predict_ic50(
peptides = "AIAACAMLLV",
mhcnuggets_options = mhcnuggets_options
mhcnuggets_options = mhcnuggets_options,
peptides_path = peptides_path,
mhcnuggets_output_filename = mhcnuggets_output_filename,
verbose = verbose
)
testthat::expect_equal(df$ic50, 5578.77)
}
19 changes: 17 additions & 2 deletions R/predict_ic50.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,34 @@
predict_ic50 <- function(
mhcnuggets_options,
peptides,
peptides_path = create_temp_peptides_path()
peptides_path = create_temp_peptides_path(),
mhcnuggets_output_filename = mhcnuggetsr::create_temp_peptides_path(
fileext = ".csv"
),
verbose = FALSE
) {
if (any(nchar(peptides) > 15)) {
stop(
"'peptides' must have lengths of at most 15 amino acids. \n",
"Tip: use 'predict_ic50s' to get the IC50s for longer peptides"
)
}
dir.create(
path = dirname(peptides_path),
showWarnings = FALSE,
recursive = TRUE
)

# Write the peptides to file
writeLines(text = peptides, con = peptides_path)
testthat::expect_true(file.exists(peptides_path))
ic50 <- mhcnuggetsr::predict_ic50_from_file(
mhcnuggets_options = mhcnuggets_options,
peptides_path = peptides_path
peptides_path = peptides_path,
mhcnuggets_output_filename = mhcnuggets_output_filename,
verbose = verbose
)
testthat::expect_true(file.exists(peptides_path))
file.remove(peptides_path)
ic50
}
56 changes: 53 additions & 3 deletions R/predict_ic50_from_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ predict_ic50_from_file <- function(
peptides_path,
mhcnuggets_output_filename = mhcnuggetsr::create_temp_peptides_path(
fileext = ".csv"
)
),
verbose = FALSE
) {
if (!file.exists(peptides_path)) {
stop(
Expand All @@ -39,6 +40,14 @@ predict_ic50_from_file <- function(
stop("'peptides' must have lengths of at most 15")
}
mhcnuggetsr::check_mhcnuggets_options(mhcnuggets_options)
if (peptides_path == mhcnuggets_output_filename) {
stop(
"'peptides_path' and 'mhcnuggets_output_filename' must be different, \n",
"because 'mhcnuggets_output_filename' is a temporary file that will \n",
"be deleted. \n",
"peptides_path: ", peptides_path
)
}

if (is.na(mhcnuggets_options$mhc_class)) {
if (mhcnuggets_options$mhc %in% mhcnuggetsr::get_mhc_1_haplotypes()) {
Expand All @@ -52,21 +61,62 @@ predict_ic50_from_file <- function(
}
testthat::expect_true(mhcnuggets_options$mhc_class %in% c("I", "II"))

dir.create(
path = dirname(mhcnuggets_output_filename),
showWarnings = FALSE,
recursive = TRUE
)

# Up to full path
peptides_path <- normalizePath(peptides_path)
testthat::expect_true(file.exists(peptides_path))
mhcnuggets_output_filename <- normalizePath(
mhcnuggets_output_filename,
mustWork = FALSE
)
testthat::expect_true(peptides_path != mhcnuggets_output_filename)

if (verbose) {
message(
"Calling MHCnuggets, with:\n",
"class: ", mhcnuggets_options$mhc_class,"\n",
"peptides_path: ", peptides_path,"\n",
"mhc: ", mhcnuggets_options$mhc,"\n",
"ba_models: ", mhcnuggets_options$ba_models,"\n",
"output: ", mhcnuggets_output_filename,"\n"
)
}

module <- reticulate::import_from_path(module = "mhcnuggets")
suppressMessages(
if (verbose) {
module$src$predict$predict(
class_ = mhcnuggets_options$mhc_class,
peptides_path = peptides_path,
mhc = mhcnuggets_options$mhc,
ba_models = mhcnuggets_options$ba_models,
output = mhcnuggets_output_filename
)
)
} else {
suppressWarnings(
suppressMessages(
module$src$predict$predict(
class_ = mhcnuggets_options$mhc_class,
peptides_path = peptides_path,
mhc = mhcnuggets_options$mhc,
ba_models = mhcnuggets_options$ba_models,
output = mhcnuggets_output_filename
)
)
)
}
testthat::expect_true(file.exists(peptides_path))

df <- tibble::as_tibble(utils::read.csv(mhcnuggets_output_filename))
df$peptide <- as.character(df$peptide)

file.remove(mhcnuggets_output_filename)

testthat::expect_true(file.exists(peptides_path))

df
}
9 changes: 8 additions & 1 deletion man/install_pip.Rd

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

11 changes: 10 additions & 1 deletion man/mhcnuggetsr_self_test.Rd

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

6 changes: 5 additions & 1 deletion man/predict_ic50.Rd

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

5 changes: 4 additions & 1 deletion man/predict_ic50_from_file.Rd

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

28 changes: 27 additions & 1 deletion tests/testthat/test-mhcnuggetsr_self_test.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
test_that("use", {
test_that("default use", {
if (is_mhcnuggets_installed()) {
expect_silent(mhcnuggetsr_self_test())
} else {
expect_error(mhcnuggetsr_self_test())
}
})

test_that("verbosity", {
if (is_mhcnuggets_installed()) {
expect_message(mhcnuggetsr_self_test(verbose = TRUE))
} else {
expect_message(mhcnuggetsr_self_test(verbose = TRUE))
}
})

test_that("detailed use", {
if (!is_mhcnuggets_installed()) return()

mhcnuggets_options <- create_test_mhcnuggets_options()
peptides_path <- create_temp_peptides_path()
mhcnuggets_output_filename = mhcnuggetsr::create_temp_peptides_path(
fileext = ".csv"
)
expect_silent(
mhcnuggetsr_self_test(
mhcnuggets_options = mhcnuggets_options,
peptides_path = peptides_path,
mhcnuggets_output_filename = mhcnuggets_output_filename,
verbose = FALSE
)
)
})
14 changes: 14 additions & 0 deletions tests/testthat/test-predict_ic50_from_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,17 @@ test_that("abuse, too long peptide", {
"'peptides' must have lengths of at most 15"
)
})

test_that("abuse, too long peptide", {
peptides_path <- tempfile()
writeLines(text = "CCCCCCCCCCCCCCC", con = peptides_path)
expect_error(
predict_ic50_from_file(
peptides_path = peptides_path,
mhcnuggets_options = create_test_mhcnuggets_options(),
mhcnuggets_output_filename = peptides_path
),
"'peptides_path' and 'mhcnuggets_output_filename' must be different"
)
file.remove(peptides_path)
})

0 comments on commit 1c24ab3

Please sign in to comment.