Skip to content

Commit

Permalink
feat: NULL width by default, with checks
Browse files Browse the repository at this point in the history
  • Loading branch information
js2264 committed Jul 11, 2024
1 parent 8a577d5 commit 48d7a97
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
10 changes: 6 additions & 4 deletions R/CoverageExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ setMethod(
"CoverageExperiment",
signature(tracks = "BigWigFileList", features = "GRangesList"),
function(
tracks, features, width,
tracks, features, width = NULL,
center = FALSE, scale = FALSE,
ignore.strand = TRUE,
window = 1,
Expand All @@ -115,7 +115,8 @@ setMethod(
## Extend and filter features
tracks <- .set_seqinfo_bwfl(tracks)
si <- seqinfo(tracks[[1]])
features <- lapply(features, .resize_granges, width = width, seqinfo = si)
if (is.null(width)) .check_granges_widths(features)
features <- lapply(features, .resize_granges, width, si)

## Prepare cData and rData
cData <- data.frame(
Expand Down Expand Up @@ -274,7 +275,7 @@ setMethod(
"CoverageExperiment",
signature(tracks = "list", features = "GRangesList"),
function(
tracks, features, width,
tracks, features, width = NULL,
center = FALSE, scale = FALSE,
ignore.strand = TRUE,
window = 1,
Expand All @@ -287,7 +288,8 @@ setMethod(
## Extend and filter features
tracks <- .set_seqinfo(tracks)
si <- seqinfo(tracks[[1]])[[1]]
features <- lapply(features, .resize_granges, width = width, seqinfo = si)
if (is.null(width)) .check_granges_widths(features)
features <- lapply(features, .resize_granges, width, si)

## Prepare cData and rData
cData <- data.frame(
Expand Down
23 changes: 18 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,25 @@
return(tracks)
}

.resize_granges <- function(gr, width, seqinfo) {
.resize_granges <- function(gr, w, seqinfo) {
GenomeInfoDb::seqlevels(gr, pruning.mode = "coarse") <- GenomeInfoDb::seqlevels(seqinfo)
GenomeInfoDb::seqinfo(gr) <- seqinfo
gr <- suppressWarnings(resize(gr, fix = 'center', width = width))
w <- width
if (is.null(w)) {
w <- width(gr) |> unlist() |> unique()
}
else {
gr <- suppressWarnings(resize(gr, fix = 'center', width = w))
}
gr <- trim(gr)
gr <- gr[width(gr) == w]
sort(gr)
gr
}

.check_granges_widths <- function(features) {
ws <- lapply(features, width) |> unlist() |> unique()
if (length(ws) > 1)
stop("Input GRanges do not all have the same width.
Use resize() to fix width prior to coverage data extraction.")
}

#' @importFrom IRanges NumericList
Expand All @@ -58,7 +69,9 @@
m[which.flip, ] <- rev(m[which.flip, ])
m <- as.matrix(m)
}
m <- t(scale(t(m), center = center, scale = scale))
if (any(c(center, scale))) {
m <- t(scale(t(m), center = center, scale = scale))
}
m
}

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ CE |>
filter(track %in% c('MNase', 'PolII')) |>
filter(features == 'TSSs') |>
aggregate() |>
ggplot(aes(x = coord, y = mean)) +
ggplot(aes(x = coord, y = mean, ymin = ci_low, ymax = ci_high, fill = track, col = track)) +
geom_ribbon(aes(ymin = ci_low, ymax = ci_high, fill = track), alpha = 0.2) +
geom_line(aes(col = track)) +
facet_grid(track ~ ., scales = "free") +
Expand All @@ -66,7 +66,7 @@ CE |>
## Plot coverage over a single locus

```r
CoverageExperiment(tracks, GRanges("II:450001-455000"), width = 5000) |>
CoverageExperiment(tracks, GRanges("II:450001-455000")) |>
expand() |>
ggplot(aes(x = coord, y = coverage)) +
geom_col(aes(fill = track, col = track)) +
Expand Down
4 changes: 2 additions & 2 deletions man/CoverageExperiment.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/reexports.Rd

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

2 changes: 2 additions & 0 deletions tests/testthat/test-compute-filter-import-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test_that("other CoverageExperiment methods work", {
x <- BigWigFile(system.file("extdata", "RNA.fwd.bw", package = "tidyCoverage"))
y <- import(system.file("extdata", "TSSs.bed", package = "tidyCoverage"))[1:200]
expect_s4_class(CoverageExperiment(x, y, width = 100), "CoverageExperiment")
expect_s4_class(CoverageExperiment(x, y), "CoverageExperiment")

# ~~~~~~~~ BigWigFile + GRangesList
y <- GRangesList(tss = y, tss2 = y)
Expand Down Expand Up @@ -74,6 +75,7 @@ test_that("other CoverageExperiment methods work", {
x <- import(system.file("extdata", "RNA.fwd.bw", package = "tidyCoverage"), as = 'Rle')
y <- import(system.file("extdata", "TSSs.bed", package = "tidyCoverage"))
expect_s4_class(CoverageExperiment(x, y, width = 100), "CoverageExperiment")
expect_s4_class(CoverageExperiment(x, y), "CoverageExperiment")

# ~~~~~~~~ RleList + GRangesList
y <- GRangesList(tss = y)
Expand Down

0 comments on commit 48d7a97

Please sign in to comment.