|
1 | 1 | #' Summarise transcript expression to exon-level expression |
2 | 2 | #' @title summarise by exon |
3 | 3 | #' @param se a \code{SummarizedExperiment} object from \code{\link{bambu}} |
4 | | -#' @return A data.table with columns: exon_id, seqnames, start, end, strand, |
5 | | -#' GENEID, and one count column per sample |
| 4 | +#' @return A \code{RangedSummarizedExperiment} with exon-level counts |
6 | 5 | #' @details Counts are summed across all transcripts that share the same exon |
7 | 6 | #' (defined by identical seqnames, start, end, and strand). The returned |
8 | 7 | #' counts therefore represent the total evidence attributed to each unique |
9 | 8 | #' exonic locus across all overlapping transcripts. |
10 | 9 | #' @import data.table |
11 | 10 | #' @importFrom Matrix sparseMatrix |
12 | | -#' @importFrom SummarizedExperiment assays rowRanges rowData |
13 | | -#' @importFrom GenomicRanges seqnames start end strand |
| 11 | +#' @importFrom SummarizedExperiment assays rowRanges rowData colData SummarizedExperiment |
| 12 | +#' @importFrom GenomicRanges GRanges seqnames start end strand |
| 13 | +#' @importFrom IRanges IRanges |
14 | 14 | #' @export |
15 | 15 | summariseByExon <- function(se) { |
16 | 16 | # Unlist GRangesList: one row per exon-transcript combination |
@@ -63,14 +63,19 @@ summariseByExon <- function(se) { |
63 | 63 | txCounts <- assays(se)$counts |
64 | 64 | exonCounts <- exonTxMat %*% txCounts |
65 | 65 |
|
66 | | - # Combine metadata with aggregated counts |
67 | | - result <- cbind( |
68 | | - exonMeta, |
69 | | - as.data.table(as.matrix(exonCounts)) |
| 66 | + # Build GRanges for unique exons |
| 67 | + exonGRanges <- GRanges( |
| 68 | + seqnames = exonMeta$seqnames, |
| 69 | + ranges = IRanges(start = exonMeta$start, end = exonMeta$end), |
| 70 | + strand = exonMeta$strand |
70 | 71 | ) |
| 72 | + names(exonGRanges) <- exonMeta$exon_id |
| 73 | + mcols(exonGRanges)$GENEID <- exonMeta$GENEID |
71 | 74 |
|
72 | | - # Sort by genomic position |
73 | | - result <- result[order(seqnames, start, end)] |
74 | | - |
75 | | - return(result) |
| 75 | + # Return as SummarizedExperiment |
| 76 | + return(SummarizedExperiment( |
| 77 | + assays = list(counts = exonCounts), |
| 78 | + rowRanges = exonGRanges, |
| 79 | + colData = colData(se) |
| 80 | + )) |
76 | 81 | } |
0 commit comments