Skip to content

Commit

Permalink
Add tests; fix R CMD checks
Browse files Browse the repository at this point in the history
  • Loading branch information
btmonier committed Jul 9, 2024
1 parent e643fff commit 0d91d63
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 9 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ Imports:
jsonlite,
methods,
patchwork,
rJava,
rlang,
scales,
tibble,
tools,
utils
VignetteBuilder: knitr
Suggests:
knitr,
rJava,
rmarkdown,
testthat (>= 3.0.0)
Config/testthat/edition: 3
10 changes: 10 additions & 0 deletions R/class_all_generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,13 @@ setGeneric("readSamples", function(object, ...) standardGeneric("readSamples"))
setGeneric("serverInfo", function(object, ...) standardGeneric("serverInfo"))


## ----
#' @importFrom GenomeInfoDb seqnames
NULL


## ----
#' @importFrom GenomeInfoDb seqnames<-
NULL


12 changes: 9 additions & 3 deletions R/class_phg_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,13 @@ setMethod(


## ----
#' @title
#' Return all contig IDs from metrics object
#'
#' @param x
#' A \code{PHGMetrics} object
#'
#' @return A vector of unique contig IDs
#' @importFrom GenomeInfoDb seqnames
#' @export
setMethod(
Expand Down Expand Up @@ -662,8 +666,10 @@ setMethod(


## ----
#' @title
#' Set Sequence Names for PHGMetrics Object
#'
#' @description
#' This method replaces old IDs with new IDs in the `PHGMetrics` object.
#' It ensures that both `metricAlign` and `metricGvcf` fields are updated
#' with the new sequence names provided in the `value` data frame.
Expand Down Expand Up @@ -711,16 +717,16 @@ setMethod(

# Helper function to replace IDs
replaceIds <- function(x, slot_name, field, value) {
metrics <- slot(x, slot_name)
metrics <- methods::slot(x, slot_name)
if (is.null(metrics) || length(metrics) == 0) return()
len <- if (is(metrics, "data.frame")) 1 else length(metrics)
for (i in seq_len(len)) {
oldIds <- as.character(metrics[[i]][[field]])
replacements <- setNames(value$new_id, value$old_id)
replacements <- stats::setNames(value$new_id, value$old_id)
newIds <- ifelse(oldIds %in% names(replacements), replacements[oldIds], oldIds)
metrics[[i]][[field]] <- newIds
}
slot(x, slot_name) <<- metrics
methods::slot(x, slot_name) <<- metrics
}

# Replace IDs in metricAlign if it's not NULL
Expand Down
4 changes: 2 additions & 2 deletions R/utils_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ metMessenger <- function(
# }
parseFormula <- function(formula) {
# Extract the terms object from the formula
termsObj <- terms(formula)
termsObj <- stats::terms(formula)

# Extract the response variables (LHS)
lhsVars <- all.vars(formula[[2]])
Expand All @@ -261,7 +261,7 @@ parseFormula <- function(formula) {
}

# Check if keywords are used with other variables in RHS
if ("ALL" %in% lhsVars && length(rhsVars) > 1) {
if (any(keywords %in% rhsVars) && length(rhsVars) > 1) {
rlang::abort(errorMsg)
}

Expand Down
2 changes: 1 addition & 1 deletion R/vis_plot_gvcf.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ plotGvcfFromMetrics <- function(df, formula, nRow, nCol, tag) {
filtGvcfMap <- GVCF_MAP[GVCF_MAP$id %in% lhsVars, ]
}

if (length(filtGvcfMap) == 0) {
if (nrow(filtGvcfMap) == 0) {
rlang::abort("No valid metrics found in left-hand side of equation")
}

Expand Down
17 changes: 17 additions & 0 deletions man/seqnames-PHGMetrics-method.Rd

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

4 changes: 2 additions & 2 deletions man/seqnames-set-PHGMetrics-method.Rd

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

24 changes: 24 additions & 0 deletions tests/testthat/test_class_phg_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ test_that("PHGMetrics general metric ID return and update tests", {
object = metricsIds(metTest),
expected = c("toy_anchors_s01", "new_gvcf_id")
)

# seqnames
expect_equal(
object = seqnames(metBase),
expected = c(
"1", "2", "3",
"Vu01", "Vu02", "Vu03", "Vu04", "Vu05",
"Vu06", "Vu07", "Vu08", "Vu09", "Vu10",
"Vu11", "contig_206", "contig_178"
)
)

metTest <- metBase
expect_error(object = seqnames(metTest) <- data.frame(old_id = "error"))
expect_error(object = seqnames(metTest) <- c("another", "error"))
seqnames(metTest) <- data.frame(
old_id = c("Vu01", "Vu03", "1"),
new_id = c("CHR_NEW_01", "CHR_NEW_03", "ALGN_CHR_01")
)
expect_true(all(c("CHR_NEW_01", "CHR_NEW_03", "ALGN_CHR_01") %in% seqnames(metTest)))
})


Expand Down Expand Up @@ -315,11 +335,15 @@ test_that("PHGMetrics general gVCF plot tests", {

metBase <- PHGMetrics(metricDir)
expect_true(is(plotGvcf(metBase, metBase$toy_gvcf_metrics), "ggplot"))
expect_error(is(plotGvcf(metBase, c(metBase$toy_gvcf_metrics, metBase$toy_anchors_s01)), "ggplot"))
expect_true(is(plotGvcf(metBase), "ggplot"))
expect_true(is(plotGvcf(metBase, f = ALL ~ ALL), "ggplot"))
expect_true(is(plotGvcf(metBase, f = ALL ~ Vu01 + Vu05), "ggplot"))
expect_true(is(plotGvcf(metBase, f = num_ns ~ Vu01 + Vu05), "ggplot"))
expect_error(is(plotGvcf(metBase, f = ALL + CORE ~ ALL + Vu02), "ggplot"))
expect_error(is(plotGvcf(metBase, f = CORE + num_ns ~ ALL + Vu02), "ggplot"))
expect_error(is(plotGvcf(metBase, f = num_nsss ~ ALL + Vu02), "ggplot"))
expect_error(is(plotGvcf(metBase, f = num_nsss ~ ALL), "ggplot"))

})

Expand Down

0 comments on commit 0d91d63

Please sign in to comment.