Calculate Cell Annotation Proportions per Neighbourhood
Source:R/nhood_annotation_props.R
nhood_annotation_props.RdComputes the proportion of cells assigned to each neighbourhood that carry each category of one or more annotation columns.
Usage
nhood_annotation_props(
object,
nhood_col = NULL,
annotation_cols = "celltype",
result_name = "nhood_annotation_props"
)Arguments
- object
A
MiloorSingleCellExperimentobject. Seurat objects must first be converted viaas.SingleCellExperiment.- nhood_col
Character string. For plain
SingleCellExperimentobjects (i.e. notMilo), the name of thecolDatacolumn containing neighbourhood assignments. Not required forMiloobjects.- annotation_cols
Character vector of
colDatacolumn names containing per-cell annotation categories to summarise per neighbourhood (e.g. cell type, lineage, cluster label). All specified columns must be present incolData; missing columns raise an error. Default:"celltype".- result_name
Character string. Key under which the results
data.frameis stored inmetadata(object). If an entry with this name already exists a warning is emitted and the entry is overwritten. Default:"nhood_annotation_props".
Value
The input object, with a data.frame stored in
metadata(object)[[result_name]]. The data.frame has one row
per neighbourhood and the following columns:
nhoodNeighbourhood identifier.
nhood_sizeTotal number of cells in the neighbourhood.
<annotation_col>_<category>_propProportion of cells carrying each annotation category. One column per category per entry in
annotation_cols— e.g. ifannotation_cols = "celltype"and the column contains"CD4"and"CD8", the output columns will becelltype_CD4_propandcelltype_CD8_prop. Neighbourhoods where all cells haveNAfor a given annotation column haveNAin all corresponding proportion columns.
Details
Object type handling
Milo objects: Neighbourhood membership is read directly from the sparse
nhoodsmatrix vianhoods. Row names of this matrix must matchcolnames(object); a mismatch raises an error to prevent silent misassignment.Plain SingleCellExperiment objects: A binary neighbourhood membership matrix is constructed internally from the column specified by
nhood_col. Cells withNAassignments are excluded with a warning.
Proportion calculation
For each neighbourhood and each annotation column, per-cell labels are
tabulated across all valid (non-NA) cells. Proportions are computed
relative to the number of valid cells in the neighbourhood for that
annotation column (i.e. each row sums to 1 over the proportion columns for
a given annotation_col, excluding any NA cells). Cells with
NA in a given annotation column are excluded from that column's
proportions and their count is reported via a warning.
Examples
if (FALSE) { # \dontrun{
# --- Milo object, single annotation column --------------------------------
milo <- nhood_annotation_props(
object = milo_object,
annotation_cols = "celltype"
)
results <- S4Vectors::metadata(milo)[["nhood_annotation_props"]]
# Output columns: nhood, nhood_size, celltype_CD4_prop, celltype_CD8_prop, ...
# --- Multiple annotation columns ------------------------------------------
milo <- nhood_annotation_props(
object = milo_object,
annotation_cols = c("celltype", "lineage"),
result_name = "nhood_annotation_props"
)
# --- Plain SingleCellExperiment object ------------------------------------
sce <- nhood_annotation_props(
object = sce_object,
nhood_col = "community_id",
annotation_cols = "celltype"
)
} # }