knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  cache = FALSE
)


suppressPackageStartupMessages({
  library(SingleCellExperiment)
  library(scater)
  library(dplyr)
})

Overview

clonealign assigns cells measured using single-cell RNA-seq to their clones of origin using copy number data. This is especially useful when clones are inferred from shallow single-cell DNA-seq, in which case the copy number state of each clone is known, but the precise SNV structure is unknown.

To assign cells to clones, clonealign makes the assumption that

$$ \text{gene expression} \propto \text{number of gene copies} $$

This is demonstrated in the figure below.

Mathematically we have an $N \times G$ matrix $Y$ of raw gene expression counts (from RNA-seq) for $N$ cells and $G$ genes, where $y_{ng}$ is the counts to gene $g$ in cell $c$. We also have a $G \times C$ matrix $\Lambda$ of copy number variation for $C$ clones, where $\lambda_{gc}$ is the copy number of gene $g$ in clone $c$. We introduce a clone-assigning categorical variable $\pi_n$ for each cell, where

$$z_n = c \text{ if cell $n$ on clone $c$} $$

then clonealign models the conditional expected counts in a gene and cell as

$$ E[y_{ng} | z_n=c] = \frac{\lambda_{g,c} f(\mu_g) e^{\psi_n \cdot w_g}}{ \sum_{g'}\lambda_{g',c} f(\mu_{g'}) e^{\psi_n \cdot w_{g'}}} s_n $$

where $s_n$ is a cell-specific size factor, $\mu_g$ is the per-chromosome expression (normalized so that $\mu_1 = 1$ for model identifiability), $f$ is a function that maps copy number to a multiplicative factor of expression, and $\psi$ and $w$ are cell and gene specific random effects. As of clonealign 2.0, the noise distribution is assumed to be multinomial. Inference is performed using reparametrization-gradient variational inference to calculate a posterior distribution of the clone assignments $z_n$ and of all other model parameters.

Installation

clonealign is built upon Google's Tensorflow using the Tensorflow R package provided by Rstudio. To install tensorflow, run

install.packages("tensorflow")
tensorflow::install_tensorflow(extra_packages ="tensorflow-probability", version="2.1.0")

You can confirm the installation succeeded by running

sess = tf$Session()
hello <- tf$constant('Hello, TensorFlow!')
sess$run(hello)

For more details see the Rstudio page on tensorflow installation.

clonealign can then be installed using the devtools package via

devtools::install_github("kieranrcampbell/clonealign")

Basic usage

Data preparation

By default, clonealign requires two inputs:

Bundled with the package is an example SingleCellExperiment for 100 genes and 200 cells:

library(clonealign)
data(example_sce)
example_sce

This has raw integer counts in the assays slot as required for input to clonealign:

assay(example_sce, "counts")[1:5, 1:5]

The CNV data is stored in the rowData of the SingleCellExperiment for 3 clones (A, B, and C) and crucially the same genes as the expression data:

cnv_data <- rowData(example_sce)[, c("A", "B", "C")]
stopifnot(nrow(cnv_data) == nrow(example_sce)) # Make sure genes match up
head(cnv_data)

We next run the preprocess_for_clonealign that performs the following filtering steps:

Note that it is critical before running clonealign that genes on the sex chromosomes are removed, as these violate the gene dosage assumption due to e.g. X inactivation. To get a list of genes and their respective chromosomes, we suggest using the annotables package.

ca_data <- preprocess_for_clonealign(example_sce, cnv_data)

Model fitting

The model is fitted with a basic call to run_clonealign, which calls the clonealign function across a range of parameter initializations and picks the model fit that achieves the best ELBO.

set.seed(123L)
cal <- run_clonealign(ca_data$gene_expression_data, ca_data$copy_number_data)

Note we have set a random seed, which in turn will set seeds for each of the runs to ensure reproducibility.

print(cal)

We can plot the ELBO to ensure convergence:

qplot(seq_along(cal$convergence_info$elbo), cal$convergence_info$elbo, geom = c("point", "line")) +
  labs(x = "Iteration", y = "ELBO")

The maximum likelihood estimates of the clone assignments can be access through the clone slot:

clones <- cal$clone
table(clones)

and we can make a heatmap of the clonal assignment probabilities:

pheatmap::pheatmap(cal$ml_params$clone_probs)

This can easily be added to the SingleCellExperiment for visualization with scater, after subsetting to the retained cells from the preprocess_for_clonealign call

example_sce <- example_sce[, ca_data$retained_cells]
example_sce$clone <- clones
example_sce <- normalize(example_sce)
plotPCA(example_sce, ncomponents = 3, colour_by = "clone")

The clone assignments in clones can then be used for the desired downstream analysis, such as differential expression or SNV analysis.

Plotting results

The plot_clonealign function can be used to check the sanity of the fitted clones by ensuring that gene expression does correlate to the inferred copy number. For this we require the input SingleCellExperiment, copy number matrix, and clone assignments. Note that the SingleCellExperiment requires columns in rowData corresponding to the chromosome, start and end position of each feature (gene). These can conveniently be gathered using the getBMFeatureAnnos function in scater, e.g.

sce <- getBMFeatureAnnos(sce, filters = "ensembl_gene_id",
                         attributes = c("ensembl_gene_id", "start_position", "end_position"),
                         feature_symbol = "hgnc_symbol",
                         feature_id = "ensembl_gene_id",
                         dataset = "hsapiens_gene_ensembl")

For now we'll set these to made-up values:

gene_position <- as_data_frame(cnv_data) %>% 
  dplyr::mutate(gene = seq_len(nrow(cnv_data))) %>% 
  dplyr::arrange(A, B, C) %>% 
  dplyr::mutate(position = seq_len(nrow(cnv_data))) %>% 
  dplyr::arrange(gene) %>% 
  .$position

rowData(example_sce)$chromosome <- "1"
rowData(example_sce)$start_pos <- gene_position
rowData(example_sce)$end_pos <- gene_position

We can then plot the expression estimates using the plot_clonealign function:

plot_clonealign(example_sce, cal$clone, cnv_data,
                chromosome = "1",
                chr_str = "chromosome",
                start_str = "start_pos",
                end_str = "end_pos")

where the *_str identifies the columns of rowData(example_sce) to look for the chromosome names and feature start and end positions.

Evaluating the clone assignment

clonealign will assign single-cell RNA-seq to clones no matter how good the fit and no matter whether the clones actually exist in the expression data. A simple yet effective metric is to evaluate the correlation of the expression with copy number after assignment. If the clone assignment has been unsuccessful, then this should be a distribution scattered around a mean close to zero. This is computed in the correlations slot of the clonealign_fit.

While we have too few genes in this example to critically evaluate, ideally most of this distribution should sit above 0 in real data.

Advanced options

Controlling Variational Inference

Inference is performed using reparametrization gradient variational inference which uses the evidence lower bound (ELBO) to monitor convergence. This is controlled using the rel_tol parameter. When the difference

$$ \Delta ELBO = \frac{ELBO_{\text{new}} - ELBO_{\text{old}}}{|ELBO_{\text{old}}|} $$ falls below rel_tol, the optimization algorithm is considered converged. The maximum number of iterations to acheive this is set using the max_iter parameter.

Inference is performed using Adam (@kingma2014adam) which is controlled by three parameters:

Accessing posterior / maximum-likelihood parameter estimates

The object returned by a call to clonealign contains a clone slot for the maximum likelihood (ML) clone assignment for each cell. The ML estimates of the other parameters can be found in the cal$ml_params slot:

names(cal$ml_params)

The slot clone_probs gives the probability that each cell is assigned to each clone:

head(cal$ml_params$clone_probs)

while mu and phi give the maximum likelihood estimates of the $\mu$ and $\phi$ parameters from the model.

Technical

sessionInfo()

References



kieranrcampbell/clonealign documentation built on Dec. 18, 2020, 3:49 a.m.