Skip to content

Commit

Permalink
Added new exported function IsICAMSCatalog
Browse files Browse the repository at this point in the history
  • Loading branch information
jnh01 committed Jan 13, 2022
1 parent 725602b commit ffba283
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export(GetFreebayesVAF)
export(GetMutectVAF)
export(GetPCAWGConsensusVAF)
export(GetStrelkaVAF)
export(IsICAMSCatalog)
export(MutectVCFFilesToCatalog)
export(MutectVCFFilesToCatalogAndPlotToPdf)
export(MutectVCFFilesToZipFile)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* Updated `catalog.row.order$ID166` and `catalog.row.headers$ID166`
for genic-intergenic indel catalog (ID166).

## Added
* Added new exported function `IsICAMSCatalog`.

# 3.0.4
## Changed
* Changed the legend of ID166 plot from `Nongenic region` to `Intergenic region`.
Expand Down
54 changes: 54 additions & 0 deletions R/infer_catalog_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,60 @@ InferCatalogClassString <- function(object) {
return(paste0(prefix, "Catalog"))
}

#' Check whether an R object contains one of the ICAMS catalog classes
#'
#' @param object An R object.
#'
#' @return A logical value.
#'
#' @export
#'
#' @examples
#' # Create a matrix with all values being 1
#' object <- matrix(1, nrow = 96, ncol = 1,
#' dimnames = list(catalog.row.order$SBS96))
#' IsICAMSCatalog(object) # FALSE
#'
#' # Use as.catalog to add class attribute to object
#' catalog <- as.catalog(object)
#' IsICAMSCatalog(catalog) # TRUE
IsICAMSCatalog <- function(object) {
supported.num.rows <-
c(96, 192, 1536, 78, 136, 144, 83, 166, 1697)
supported.type <-
c(paste0("SBS", c(96, 192, 1536)),
paste0("DBS", c(78, 136, 144)),
"ID", "ID166", "COMPOSITE")
supported.classes <-
paste0(supported.type, "Catalog")

# Change "IDCatalog" class to "IndelCatalog"
supported.classes[supported.classes == "IDCatalog"] <-
"IndelCatalog"

if (!nrow(object) %in% supported.num.rows) {
return(FALSE)
}

if (!inherits(x = object, what = supported.classes)) {
return(FALSE)
}

if (!is.null(rownames(object))) {
index <- which(nrow(object) == supported.num.rows)
type <- supported.type[index]
if (all(rownames(object) == ICAMS::catalog.row.order[[type]])) {
return(TRUE)
} else {
message("The rownames of the input object do not match the catalog row ",
"order used in ICAMS exactly. See ICAMS::catalog.row.order for details")
return(FALSE)
}
}

return(TRUE)
}



## Convert external catalog files with 96 rows into ICAMS internal catalog format.
Expand Down
Binary file modified data-raw/ICAMS_3.0.5.pdf
Binary file not shown.
27 changes: 27 additions & 0 deletions man/IsICAMSCatalog.Rd

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

40 changes: 40 additions & 0 deletions tests/testthat/test_IsICAMSCatalog.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
context("Test IsICAMSCatalog function")

test_that("IsICAMSCatalog function", {
dir <- c(system.file("extdata/Mutect-vcf",
package = "ICAMS"))
catalogs <-
VCFsToZipFile(dir,
zipfile = file.path(tempdir(), "test.zip"),
ref.genome = "hg19",
variant.caller = "mutect",
region = "genome",
base.filename = "Mutect")
cat.sbs96 <- catalogs$catSBS96
dim(cat.sbs96)
expect_true(IsICAMSCatalog(cat.sbs96))

# Test subsetting the catalog
test <- cat.sbs96[, 1, drop = FALSE]
dim(test)
expect_true(IsICAMSCatalog(test))

# Test a matrix with incorrect number of rows
mat1 <- matrix(data = 1, nrow = 97)
expect_false(IsICAMSCatalog(mat1))

# Test a matrix with correct number of rows but incorrect class
mat2 <- matrix(data = 1, nrow = 96)
expect_false(IsICAMSCatalog(mat2))

# Test a matrix with correct number of rows and class, but incorrect
# rownames
mat3 <- matrix(data = 1, nrow = 96)
mat3 <- as.catalog(mat3, infer.rownames = TRUE)
rownames(mat3) <- paste0("test", 1:96)

result <-
expect_message(IsICAMSCatalog(mat3),
"The rownames of the input object do not match the catalog row order used in ICAMS exactly.")
expect_false(result)
})

0 comments on commit ffba283

Please sign in to comment.