Vignette 1: Basic workflow
Yilin Yang
2024-09-12
vignette_1_basic_workflow.RmdVignette Info
This vignette depicts a basic workflow of INClock that generates dispersion parameters for each cell type / age group. Examples of downstream analyses can be found in Vignette 2.
Prepare the test data
We will use single-cell RNA sequencing data from Tabula Muris Senis as our test data. Specifically, it concerns with the bone marrow cells of mouse at different ages. As the original dataset is too large for demonstration, we will only take a subset of its most relevant cell types and age groups, which includes one younger (3 months) and one older (30 months) age groups, as well as several stem cell types, such as hematopoietic precursor cell (HSC). Ideally, if you want to reproduce results similar to the ones in our paper, you should probably ignore the subsetting part of data preparation below. The dataset is loaded with TabulaMurisSenisData, which is already integrated into our package for test purposes.
# Load test data from Tabula Muris Senis
tms_marrow <- TabulaMurisSenisData::TabulaMurisSenisDroplet(tissues = "Marrow")
tms_marrow_counts <- tms_marrow$Marrow@assays@data$counts
rownames(tms_marrow_counts) <- rownames(tms_marrow$Marrow)
colnames(tms_marrow_counts) <- colnames(tms_marrow$Marrow)
tms_marrow_counts <- as(tms_marrow_counts, "dgCMatrix")
# Subset age groups and cell types (ignore if to reproduce results from paper)
subset_ages <- which(tms_marrow$Marrow$age %in% c("3m", "30m"))
subset_cell_types <- which(tms_marrow$Marrow$cell_ontology_class %in% c(
"hematopoietic precursor cell", "megakaryocyte-erythroid progenitor cell",
"precursor B cell"
))
tms_marrow_counts <- tms_marrow_counts[, intersect(subset_ages, subset_cell_types)]Compute the manifold
We can simply call compute_manifold() to compute the
manifold matrix for our test data. The function only requires one input
files:
- Gene expression count matrix (Gene by Cell)
(
counts)
The matrix has to be in either matrix or
dgCMatrix format, as required by dependent packages such as
SAVER and Seurat.
Before running, we need to specify the method to use,
either 'SAVER' or 'neighbor'. If
'SAVER' is selected, then you have the following
customizable options:
- Number of CPU cores to use. Default is 1. (
ncores)
If 'neighbor' is selected, then you have the following
customizable options:
- Number of features for finding variable features. Default is 3000.
(
nfeatures) - Number of dimensions for finding nearest neighbors. Default is 20.
(
dims) - Number of neighbors to output. Default is 20.
(
neighbors)
The number of features and dimensions typically don’t affect the quality of the manifold much. The number of neighbors used, however, can somewhat affect. To facilitate an optimal selection of number of neighbors, it is recommended to go through Vignette 3 (WIP). From our previous tests, datasets of less than 100,000 cells typically have an optimal number of neighbors around 20 to 30.
# Compute the manifold with SAVER
tms_marrow_manifold_saver <- compute_manifold(tms_marrow_counts, method = "SAVER")
# Compute the manifold with neighbor-based method
tms_marrow_manifold_neighbor <- compute_manifold(tms_marrow_counts, method = "neighbor")The output will be the manifold matrix of the same dimensions as the original count matrix, but of course it is not sparse any more. Be cautious that this step will use relatively large amount of memory.
Estimate dispersion parameters
Now that we have the manifold computed, we can call
estimate_dispersion to estimate the dispersion parameters.
The function takes the following inputs:
- Gene expression count matrix (Gene by Cell)
(
counts) - Manifold matrix (
manifold) - A vector of cell types each cell in the dataset belongs to
(
cell.types) - A vector of age groups each cell in the dataset belongs to
(
ages)
It is also necessary to choose a variance model for the
estimation, which can be 'cCV' (constant Coefficient of
Variation), 'cFF' (constant Fano Factor), or
'cVar' (constant Variance). We recommend using constant CV,
which is also the default model to be used.
Besides, there are a few more options to customize the process:
- Number of CPU cores to use. Default is 1. (
ncores) - Cell size normalization factors. Default uses mean library size
normalization. (
size.factor) - A vector of cell types labels that will be used in the estimation
process. (
cell.types.to.use) - A vector of age group labels that will be used in the estimation
process. (
ages.to.use) - Minimum number of cells in a cell type / age group. Groups with
fewer cells than this number will be skipped. Default is 10.
(
cell.types.cutoff)
# Create cell type and age labels (for demonstration)
tms_marrow_cell_types <- tms_marrow$Marrow$cell_ontology_class[intersect(subset_ages, subset_cell_types)]
tms_marrow_ages <- tms_marrow$Marrow$age[intersect(subset_ages, subset_cell_types)]
# Create cell type and age labels (to reproduce results from paper)
# tms_marrow_cell_types <- tms_marrow$Marrow$cell_ontology_class
# tms_marrow_ages <- tms_marrow$Marrow$age
# Estimate the dispersion parameters using constant CV
tms_marrow_dispersions <- estimate_dispersion(tms_marrow_counts, tms_marrow_manifold_neighbor,
tms_marrow_cell_types, tms_marrow_ages,
model = "cCV", ncores = 2
)Finally, we are able to generate a list object containing multiple dataframes, each of which corresponds to a cell type. In each dataframe, the first column will be genes, and the later ones will be the dispersion parameter values for each age group.