sumCountsAcrossFeatures {scater} | R Documentation

## Sum counts across feature sets

### Description

Sum together expression values (by default, counts) for each feature set in each cell.

### Usage

    sumCountsAcrossFeatures(x, ...)

    ## S4 method for signature 'ANY'
    sumCountsAcrossFeatures(
      x,
      ids,
      subset_row = NULL,
      subset_col = NULL,
      average = FALSE,
      BPPARAM = SerialParam()
    )

    ## S4 method for signature 'SummarizedExperiment'
    sumCountsAcrossFeatures(x, ..., exprs_values = "counts")

    aggregateAcrossFeatures(x, ids, ..., use_exprs_values = "counts")

### Arguments

`x` | For `sumCountsAcrossFeatures`, a numeric matrix of counts containing features in rows and cells in columns. Alternatively, a SummarizedExperiment object containing such a count matrix. For `aggregateAcrossFeatures`, a SummarizedExperiment containing a count matrix.
`...` | For the `sumCountsAcrossFeatures` generic, further arguments to be passed to specific methods. For the SummarizedExperiment method, further arguments to be passed to the ANY method. For `aggregateAcrossFeatures`, further arguments to be passed to `sumCountsAcrossFeatures`.
`ids` | A factor of length `nrow(x)`, specifying the set to which each feature in `x` belongs. Alternatively, a list of integer or character vectors, where each vector specifies the indices or names of features in a set.
`subset_row` | An integer, logical or character vector specifying the features to use. Defaults to all features.
`subset_col` | An integer, logical or character vector specifying the cells to use. Defaults to all cells with non-`NA` entries of `ids`.
`average` | Logical scalar indicating whether the average should be computed instead of the sum.
`BPPARAM` | A BiocParallelParam object specifying whether summation should be parallelized.
`exprs_values` | A string or integer scalar specifying the assay of `x` containing the matrix of counts (or any other expression quantity that can be meaningfully summed).
`use_exprs_values` | A character or integer vector specifying the assay(s) of `x` containing count matrices.

### Details

This function provides a convenient method for aggregating counts across multiple rows for each cell. Several possible applications are listed below:

- Using a list of genes in `ids`, we can obtain a summary expression value for all genes in one or more gene sets. This allows the activity of various pathways to be compared across cells.

- Genes with multiple mapping locations in the reference will often manifest as multiple rows with distinct Ensembl/Entrez IDs. These counts can be aggregated into a single feature by setting the shared identifier (usually the gene symbol) as `ids`.

- It is theoretically possible to aggregate transcript-level counts to gene-level counts with this function. However, it is often better to do so with dedicated functions (e.g., from the tximport or tximeta packages) that account for differences in length across isoforms.

The behaviour of this function is equivalent to that of `rowsum`. However, this function can operate on any matrix representation in `object`, and can do so in a parallelized manner for large matrices without resorting to block processing.

If `ids` is a factor, any `NA` values are implicitly ignored and will not be considered or reported. This may be useful, e.g., to remove undesirable feature sets by setting their entries in `ids` to `NA`.

Setting `average=TRUE` will compute the average in each set rather than the sum. This is particularly useful if `x` contains expression values that have already been normalized in some manner, as computing the average avoids another round of normalization to account for differences in the size of each set.

### Value

For `sumCountsAcrossFeatures`, a count matrix is returned with one row per level of `ids`. In each cell, counts for all features in the same set are summed together. Rows are ordered according to `levels(ids)`.

For `aggregateAcrossFeatures`, a SummarizedExperiment of the same class as `x` is returned, containing summed matrices generated by `sumCountsAcrossFeatures` on all assays in `use_exprs_values`. Row metadata is retained for the first instance of a feature from each set in `ids`.

### Author(s)

Aaron Lun

### Examples

    example_sce <- mockSCE()
    ids <- sample(LETTERS, nrow(example_sce), replace=TRUE)
    out <- sumCountsAcrossFeatures(example_sce, ids)
    str(out)

[Package scater version 1.16.2 Index]