Skip to content

Commit

Permalink
fix: as.matrix returns sparse by default, use as(x, 'dgTMatrix') for …
Browse files Browse the repository at this point in the history
…sparse
  • Loading branch information
js2264 committed Jan 10, 2024
1 parent 5ab6f12 commit 97c8810
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
10 changes: 9 additions & 1 deletion R/coerce.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ setAs("HiCExperiment", "ContactMatrix", function(from) {
#' @name as

setAs("HiCExperiment", "matrix", function(from) {
as(from, "ContactMatrix") |> cm2matrix(sparse = FALSE)
})


#' @export
#' @name as

setAs("HiCExperiment", "dgTMatrix", function(from) {
as(from, "ContactMatrix") |> cm2matrix(sparse = TRUE)
})

Expand All @@ -109,7 +117,7 @@ setAs("HiCExperiment", "data.frame", function(from) {
#' @export
#' @name as

setMethod("as.matrix", "HiCExperiment", function(x, use.scores = "balanced", sparse = TRUE) {
setMethod("as.matrix", "HiCExperiment", function(x, use.scores = "balanced", sparse = FALSE) {
interactions(x) |> gi2cm(use.scores = use.scores) |> cm2matrix(sparse = sparse)
})

Expand Down
2 changes: 1 addition & 1 deletion man/as.Rd

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

16 changes: 13 additions & 3 deletions tests/testthat/test-HiCExperiment-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,23 @@ test_that("coerce works", {
expect_s4_class({
as(contacts_yeast, 'ContactMatrix')
}, 'ContactMatrix')
expect_true(is.matrix(base::as.matrix(as.matrix(contacts_yeast))))
expect_true({
is.matrix(as(contacts_yeast, 'matrix'))
})
expect_false({
is.matrix(as(contacts_yeast, 'dgTMatrix'))
})
expect_s4_class({
as(contacts_yeast, 'matrix')
as(contacts_yeast, 'dgTMatrix')
}, 'dgTMatrix')
expect_s4_class({
as.matrix(contacts_yeast)
as.matrix(contacts_yeast, sparse = TRUE)
}, 'dgTMatrix')
expect_true({
is.matrix(
as.matrix(contacts_yeast, sparse = FALSE)
)
})
expect_s3_class({
df <- as(contacts_yeast, 'data.frame')
}, 'data.frame')
Expand Down
3 changes: 2 additions & 1 deletion vignettes/HiCExperiment.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ The implemented `import()` methods allow one to import Hi-C matrix files in R as
`HiCExperiment` objects.

```{r import, eval = FALSE}
## Change <path/to/contact_matrix>.cool accordingly
hic <- import(
"path/to/contact_matrix.cool",
"<path/to/contact_matrix>.cool",
focus = "chr:start-end",
resolution = ...
)
Expand Down

0 comments on commit 97c8810

Please sign in to comment.