| ... | ... |
@@ -38,7 +38,8 @@ expand.CoverageExperiment <- function(data, ..., .name_repair = NULL) {
|
| 38 | 38 |
m <- assay(data, "coverage")[f, t][[1]] |> |
| 39 | 39 |
as.data.frame() |> |
| 40 | 40 |
dplyr::mutate( |
| 41 |
- track = t, features = f, |
|
| 41 |
+ track = factor(t, levels = tracks), |
|
| 42 |
+ features = factor(f, levels = features), |
|
| 42 | 43 |
chr = as.vector(seqnames(rr)), |
| 43 | 44 |
ranges = as.character(rr), |
| 44 | 45 |
strand = as.vector(strand(rr)) |
| ... | ... |
@@ -12,7 +12,7 @@ |
| 12 | 12 |
#' @aliases expand,CoverageExperiment-method |
| 13 | 13 |
#' @rdname expand |
| 14 | 14 |
#' |
| 15 |
-#' @param x a `CoverageExperiment` object |
|
| 15 |
+#' @param data a `CoverageExperiment` object |
|
| 16 | 16 |
#' @param ...,.name_repair ignored |
| 17 | 17 |
#' @return a `tibble` object |
| 18 | 18 |
#' |
| ... | ... |
@@ -24,18 +24,18 @@ |
| 24 | 24 |
#' |
| 25 | 25 |
#' expand(ce) |
| 26 | 26 |
|
| 27 |
-expand.CoverageExperiment <- function(x, ..., .name_repair = NULL) {
|
|
| 28 |
- tracks <- colData(x)$track |
|
| 29 |
- features <- rowData(x)$features |
|
| 30 |
- w <- width(rowRanges(x)[[1]])[[1]] |
|
| 31 |
- bin <- w / ncol(assay(x, "coverage")[1, 1][[1]]) |
|
| 27 |
+expand.CoverageExperiment <- function(data, ..., .name_repair = NULL) {
|
|
| 28 |
+ tracks <- colData(data)$track |
|
| 29 |
+ features <- rowData(data)$features |
|
| 30 |
+ w <- width(rowRanges(data)[[1]])[[1]] |
|
| 31 |
+ bin <- w / ncol(assay(data, "coverage")[1, 1][[1]]) |
|
| 32 | 32 |
df <- lapply(features, function(f) {
|
| 33 |
- rr <- rowRanges(x)[[f]] |
|
| 33 |
+ rr <- rowRanges(data)[[f]] |
|
| 34 | 34 |
rrdf <- as.data.frame(rr) |
| 35 | 35 |
coord <- lapply(seq_len(nrow(rrdf)), function(K) seq(rrdf[K, 'start'], rrdf[K, 'end'], by = bin)) |> unlist() |
| 36 | 36 |
coord.scaled <- lapply(seq_len(nrow(rrdf)), function(K) seq(-w/2, w/2-1, by = bin)) |> unlist() |
| 37 | 37 |
lapply(tracks, function(t) {
|
| 38 |
- m <- assay(x, "coverage")[f, t][[1]] |> |
|
| 38 |
+ m <- assay(data, "coverage")[f, t][[1]] |> |
|
| 39 | 39 |
as.data.frame() |> |
| 40 | 40 |
dplyr::mutate( |
| 41 | 41 |
track = t, features = f, |
| ... | ... |
@@ -55,7 +55,7 @@ expand.CoverageExperiment <- function(x, ..., .name_repair = NULL) {
|
| 55 | 55 |
}) |> dplyr::bind_rows() |
| 56 | 56 |
}) |> |
| 57 | 57 |
dplyr::bind_rows() |> |
| 58 |
- dplyr::left_join(colData(x) |> as.data.frame(), by = 'track') |> |
|
| 58 |
+ dplyr::left_join(colData(data) |> as.data.frame(), by = 'track') |> |
|
| 59 | 59 |
dplyr::group_by(track, features, ranges) |
| 60 | 60 |
return(df) |
| 61 | 61 |
} |
| ... | ... |
@@ -13,10 +13,10 @@ |
| 13 | 13 |
#' @rdname expand |
| 14 | 14 |
#' |
| 15 | 15 |
#' @param x a `CoverageExperiment` object |
| 16 |
-#' @param ... ignored |
|
| 16 |
+#' @param ...,.name_repair ignored |
|
| 17 | 17 |
#' @return a `tibble` object |
| 18 | 18 |
#' |
| 19 |
-#' @importFrom S4Vectors expand |
|
| 19 |
+#' @importFrom tidyr expand |
|
| 20 | 20 |
#' @export |
| 21 | 21 |
#' @examples |
| 22 | 22 |
#' data(ce) |
| ... | ... |
@@ -24,7 +24,7 @@ |
| 24 | 24 |
#' |
| 25 | 25 |
#' expand(ce) |
| 26 | 26 |
|
| 27 |
-setMethod("expand", signature(x = "CoverageExperiment"), function(x, ...) {
|
|
| 27 |
+expand.CoverageExperiment <- function(x, ..., .name_repair = NULL) {
|
|
| 28 | 28 |
tracks <- colData(x)$track |
| 29 | 29 |
features <- rowData(x)$features |
| 30 | 30 |
w <- width(rowRanges(x)[[1]])[[1]] |
| ... | ... |
@@ -33,6 +33,7 @@ setMethod("expand", signature(x = "CoverageExperiment"), function(x, ...) {
|
| 33 | 33 |
rr <- rowRanges(x)[[f]] |
| 34 | 34 |
rrdf <- as.data.frame(rr) |
| 35 | 35 |
coord <- lapply(seq_len(nrow(rrdf)), function(K) seq(rrdf[K, 'start'], rrdf[K, 'end'], by = bin)) |> unlist() |
| 36 |
+ coord.scaled <- lapply(seq_len(nrow(rrdf)), function(K) seq(-w/2, w/2-1, by = bin)) |> unlist() |
|
| 36 | 37 |
lapply(tracks, function(t) {
|
| 37 | 38 |
m <- assay(x, "coverage")[f, t][[1]] |> |
| 38 | 39 |
as.data.frame() |> |
| ... | ... |
@@ -45,10 +46,11 @@ setMethod("expand", signature(x = "CoverageExperiment"), function(x, ...) {
|
| 45 | 46 |
dplyr::relocate(tidyr::all_of(c("track", "features", "chr", "ranges", "strand")))
|
| 46 | 47 |
d <- tidyr::pivot_longer( |
| 47 | 48 |
m, |
| 48 |
- !tidyr::any_of(c("track", "features", "chr", "ranges", "strand")),
|
|
| 49 |
+ !tidyr::any_of(c("track", "features", "chr", "ranges", "strand", "coord.scaled")),
|
|
| 49 | 50 |
names_to = "coord", values_to = "coverage" |
| 50 | 51 |
) |
| 51 | 52 |
d$coord <- coord |
| 53 |
+ d$coord.scaled <- coord.scaled |
|
| 52 | 54 |
d |
| 53 | 55 |
}) |> dplyr::bind_rows() |
| 54 | 56 |
}) |> |
| ... | ... |
@@ -56,4 +58,4 @@ setMethod("expand", signature(x = "CoverageExperiment"), function(x, ...) {
|
| 56 | 58 |
dplyr::left_join(colData(x) |> as.data.frame(), by = 'track') |> |
| 57 | 59 |
dplyr::group_by(track, features, ranges) |
| 58 | 60 |
return(df) |
| 59 |
-}) |
|
| 61 |
+} |
| ... | ... |
@@ -1,4 +1,12 @@ |
| 1 |
-#' expand |
|
| 1 |
+#' Expand a CoverageExperiment object |
|
| 2 |
+#' |
|
| 3 |
+#' @description |
|
| 4 |
+#' A `CoverageExperiment` object can be coerced into a `tibble` using the |
|
| 5 |
+#' `tidySummarizedExperiment` package, but this will not turn |
|
| 6 |
+#' each coverage matrix into a "long" format. The `expand` function |
|
| 7 |
+#' provided here allows one to coerce a `CoverageExperiment` |
|
| 8 |
+#' object into a long data frame, and adds the `ranges` |
|
| 9 |
+#' and `seqnames` to the resulting `tibble`. |
|
| 2 | 10 |
#' |
| 3 | 11 |
#' @name expand |
| 4 | 12 |
#' @aliases expand,CoverageExperiment-method |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,51 @@ |
| 1 |
+#' expand |
|
| 2 |
+#' |
|
| 3 |
+#' @name expand |
|
| 4 |
+#' @aliases expand,CoverageExperiment-method |
|
| 5 |
+#' @rdname expand |
|
| 6 |
+#' |
|
| 7 |
+#' @param x a `CoverageExperiment` object |
|
| 8 |
+#' @param ... ignored |
|
| 9 |
+#' @return a `tibble` object |
|
| 10 |
+#' |
|
| 11 |
+#' @importFrom S4Vectors expand |
|
| 12 |
+#' @export |
|
| 13 |
+#' @examples |
|
| 14 |
+#' data(ce) |
|
| 15 |
+#' ce |
|
| 16 |
+#' |
|
| 17 |
+#' expand(ce) |
|
| 18 |
+ |
|
| 19 |
+setMethod("expand", signature(x = "CoverageExperiment"), function(x, ...) {
|
|
| 20 |
+ tracks <- colData(x)$track |
|
| 21 |
+ features <- rowData(x)$features |
|
| 22 |
+ w <- width(rowRanges(x)[[1]])[[1]] |
|
| 23 |
+ bin <- w / ncol(assay(x, "coverage")[1, 1][[1]]) |
|
| 24 |
+ df <- lapply(features, function(f) {
|
|
| 25 |
+ rr <- rowRanges(x)[[f]] |
|
| 26 |
+ rrdf <- as.data.frame(rr) |
|
| 27 |
+ coord <- lapply(seq_len(nrow(rrdf)), function(K) seq(rrdf[K, 'start'], rrdf[K, 'end'], by = bin)) |> unlist() |
|
| 28 |
+ lapply(tracks, function(t) {
|
|
| 29 |
+ m <- assay(x, "coverage")[f, t][[1]] |> |
|
| 30 |
+ as.data.frame() |> |
|
| 31 |
+ dplyr::mutate( |
|
| 32 |
+ track = t, features = f, |
|
| 33 |
+ chr = as.vector(seqnames(rr)), |
|
| 34 |
+ ranges = as.character(rr), |
|
| 35 |
+ strand = as.vector(strand(rr)) |
|
| 36 |
+ ) |> |
|
| 37 |
+ dplyr::relocate(tidyr::all_of(c("track", "features", "chr", "ranges", "strand")))
|
|
| 38 |
+ d <- tidyr::pivot_longer( |
|
| 39 |
+ m, |
|
| 40 |
+ !tidyr::any_of(c("track", "features", "chr", "ranges", "strand")),
|
|
| 41 |
+ names_to = "coord", values_to = "coverage" |
|
| 42 |
+ ) |
|
| 43 |
+ d$coord <- coord |
|
| 44 |
+ d |
|
| 45 |
+ }) |> dplyr::bind_rows() |
|
| 46 |
+ }) |> |
|
| 47 |
+ dplyr::bind_rows() |> |
|
| 48 |
+ dplyr::left_join(colData(x) |> as.data.frame(), by = 'track') |> |
|
| 49 |
+ dplyr::group_by(track, features, ranges) |
|
| 50 |
+ return(df) |
|
| 51 |
+}) |