Skip to contents

Summarise cell metadata for each metacell. Metadata fields can be either numeric and then the summary function func is applied for the values of each field, or categorical metadata fields which are expanded to multiple metadata columns with the fraction of cells (in each metacell) for every category. Variables that are either character, factor or are explicitly set at categorical are treated as categorical.

cell_metadata_to_metacell converts cell metadata to metacell metadata from data frames.
cell_metadata_to_metacell_from_h5ad extracts metadata fields and cell_to_metacell from cells h5ad file and then runs cell_metadata_to_metacell.
cell_metadata_to_metacell_from_metacell1 extracts metadata fields and cell_to_metacell from metacell1 scdb and then runs cell_metadata_to_metacell.

Usage

cell_metadata_to_metacell(
  cell_metadata,
  cell_to_metacell,
  func = mean,
  categorical = c()
)

cell_metadata_to_metacell_from_metacell1(
  scdb,
  matrix,
  mc,
  metadata_fields,
  func = mean,
  categorical = c()
)

cell_metadata_to_metacell_from_h5ad(
  anndata_file,
  metadata_fields,
  func = mean,
  categorical = c(),
  rm_outliers = TRUE
)

Arguments

cell_metadata

data frame with a column named "cell_id" with the cell id and other metadata columns, or a name of a delimited file which contains such data frame.

cell_to_metacell

data frame with a column named "cell_id" with cell id and another column named "metacell" with the metacell the cell is part of, or a name of a delimited file which contains such data frame.

func

summary function for the cell metadata for non categorical metadata columns (e.g. mean, median, sum)

categorical

a vector with names of categorical variables. The returned data frame would have a column for each category where the values are the fraction of cells with the category in each metacell.

scdb, matrix, mc

scdb, matrix and mc objects from metacell1. See import_dataset_metacell1 for more information.

metadata_fields

names of fields in the anndata object$obs which contains metadata for each cell.

anndata_file

path to h5ad file which contains the output of metacell2 pipeline (metacells python package).

rm_outliers

do not calculate statistics for cells that are marked as outliers (outiler=TRUE in object$obs) (only relevant when running cell_metadata_to_metacell_from_h5ad)

Value

A data frame with a column named "metacell" and the metadata columns from cell_metadata summarized for each metacell using func for non-categorical variables, and a column for each category of the categorical metadata variables incell_metadata, where the values are the fraction of cells with the category in each metacell.

Functions

  • cell_metadata_to_metacell_from_metacell1():

  • cell_metadata_to_metacell_from_h5ad():

Examples

set.seed(60427)
n_cells <- 5e6
cell_metadata <- tibble::tibble(
    cell_id = 1:n_cells,
    md1 = sample(1:5, size = n_cells, replace = TRUE),
    md2 = rnorm(n = n_cells),
    md_categorical1 = sample(paste0("batch", 1:5), size = n_cells, replace = TRUE),
    md_categorical2 = sample(1:5, size = n_cells, replace = TRUE)
)

cell_to_metacell <- tibble::tibble(
    cell_id = 1:n_cells,
    metacell = sample(0:1535, size = n_cells, replace = TRUE)
)
metadata <- cell_metadata_to_metacell(
    cell_metadata[, 1:3],
    cell_to_metacell
)
#>  Numerical variables: md1, md2
head(metadata)
#> # A tibble: 6 x 3
#>   metacell      md1          md2
#> 1        0 2.979448 -0.021942785
#> 2        1 2.958991  0.015177899
#> 3        2 2.990102  0.005634195
#> 4        3 3.012686 -0.026841161
#> 5        4 3.005740  0.011632598
#> 6        5 2.960923  0.008044519

metadata1 <- cell_metadata_to_metacell(
    cell_metadata[, 1:3], cell_to_metacell,
    func = function(x) x * 2
)
#>  Numerical variables: md1, md2
#> Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in dplyr 1.1.0.
#>  Please use `reframe()` instead.
#>  When switching from `summarise()` to `reframe()`, remember that `reframe()` always returns an ungrouped
#>   data frame and adjust accordingly.
#>  The deprecated feature was likely used in the dplyr package.
#>   Please report the issue at <https://github.com/tidyverse/dplyr/issues>.
head(metadata1)
#> # A tibble: 6 x 3
#>   metacell md1        md2
#> 1        0   6 -0.8932316
#> 2        0   2 -2.1180205
#> 3        0  10  2.2575215
#> 4        0   4 -0.7134182
#> 5        0   2  1.6701686
#> 6        0   6  1.4134616


metadata3 <- cell_metadata_to_metacell(
    cell_metadata,
    cell_to_metacell,
    categorical = c("md_categorical1", "md_categorical2")
)
#>  Categorical variables: md_categorical1, md_categorical2
#>  Numerical variables: md1, md2

head(metadata3)
#> # A tibble: 6 x 13
#>   metacell md_categorical1: batch1 md_categorical1: batch2
#> 1        0               0.1953988               0.2039877
#> 2        1               0.1876972               0.2015773
#> 3        2               0.2206258               0.1985951
#> 4        3               0.1921979               0.2029813
#> 5        4               0.1878189               0.2053571
#> 6        5               0.2058462               0.1932308
#>   md_categorical1: batch3 md_categorical1: batch4 md_categorical1: batch5
#> 1               0.2046012               0.2058282               0.1901840
#> 2               0.2100946               0.2037855               0.1968454
#> 3               0.1928480               0.1973180               0.1906130
#> 4               0.2131304               0.1925151               0.1991754
#> 5               0.2050383               0.2005740               0.2012117
#> 6               0.2033846               0.1996923               0.1978462
#>   md_categorical2: 1 md_categorical2: 2 md_categorical2: 3 md_categorical2: 4
#> 1          0.1935583          0.2141104          0.1966258          0.1889571
#> 2          0.2056782          0.2012618          0.1914826          0.2028391
#> 3          0.2043423          0.1979566          0.1985951          0.2097701
#> 4          0.2017127          0.2026641          0.1928322          0.2102759
#> 5          0.2117347          0.2037628          0.1903699          0.1951531
#> 6          0.2030769          0.1920000          0.2046154          0.2033846
#>   md_categorical2: 5      md1          md2
#> 1          0.2067485 2.979448 -0.021942785
#> 2          0.1987382 2.958991  0.015177899
#> 3          0.1893359 2.990102  0.005634195
#> 4          0.1925151 3.012686 -0.026841161
#> 5          0.1989796 3.005740  0.011632598
#> 6          0.1969231 2.960923  0.008044519
if (FALSE) { # \dontrun{
cell_metadata_to_metacell_from_h5ad("cells.h5ad", c("pile", "age", "batch"), categorical = "batch")
} # }