suppressPackageStartupMessages({
  library(MultiAssayExperiment)
  library(SingleCellExperiment)
  library(scater)
  library(scran)
  library(dplyr)
  library(ggplot2)
})

Load MultiAssayExperiment object

(maex <- readRDS("../../data/data_raw/GSE52529-GPL16791.rds"))

## Extract the gene-level length-scaled TPMs
cts <- assays(experiments(maex)[["gene"]])[["count_lstpm"]]

## Extract the phenotype data.
phn <- colData(maex)
phn$phenoid <- sapply(strsplit(as.character(gsub("Myoblast_Cell ", "", 
                                                 phn$source_name_ch1)), "_"), .subset, 1)
table(phn$phenoid)

Create a SingleCellExperiment object

stopifnot(all(colnames(cts) == rownames(phn)))

sce <- SingleCellExperiment(
  assays = list(counts = cts), 
  colData = phn
)
sce <- normalise(sce, exprs_values = "counts", return_log = TRUE, 
                 return_norm_as_exprs = TRUE) ## generates logcounts(sce)

Exclude features that are not expressed

keep_features <- rowSums(counts(sce) > 0) > 0
table(keep_features)
sce <- sce[keep_features, ]
dim(sce)

Identify the remaining ERCC spike-ins.

is.spike <- grepl("^ERCC", rownames(sce))
table(is.spike)
summary(colSums(counts(sce[is.spike, ])))

Calculate QC metrics

sce <- calculateQCMetrics(sce)

Quality control using PCA on column data

We create a PCA plot based the quality metrics for each cell, e.g., the total number of reads, the total number of features and the proportion of spike-in reads.

sce <- scater::runPCA(sce, pca_data_input = "coldata")
scater::plotPCA(sce, colour_by = "phenoid")

Filter cells

We remove cells with log-library sizes (or total features) that are more than 3 median absolute deviations (MADs) below the median log-library size (or total features).

colData(sce)$libsize.drop <- isOutlier(sce$total_counts, nmads = 3, type = "lower", log = TRUE)
ggplot(as.data.frame(colData(sce)), aes(x = total_counts)) + 
  geom_histogram(bins = 20, fill = "grey80") + xlab("Total count") + 
  ylab("Number of cells") + 
  geom_vline(xintercept = min(sce$total_counts[!sce$libsize.drop]), 
             color = "red", linetype = "dashed") + 
  theme_bw()

colData(sce)$feature.drop <- isOutlier(sce$total_features, nmads = 3, type = "lower", log = TRUE)
ggplot(as.data.frame(colData(sce)), aes(x = total_features)) + 
  geom_histogram(bins = 20, fill = "grey80") + xlab("Number of detected features") + 
  ylab("Number of cells") + 
  geom_vline(xintercept = min(sce$total_features[!sce$feature.drop]), 
             color = "red", linetype = "dashed") + 
  theme_bw()

table(libsize = sce$libsize.drop, libsize = sce$feature.drop)

We also filter out observations that were classified as debris or doublets.

colData(sce)$is.debris <- colData(sce)$characteristics_ch1.2 == "debris: TRUE"
colData(sce)$is.multiplecell <-  colData(sce)$characteristics_ch1.4 != "cells in well: 1"
table(debris = sce$is.debris, multiple = sce$is.multiplecell)

sce <- sce[, !(sce$libsize.drop | sce$feature.drop | sce$is.debris | sce$is.multiplecell)]
dim(sce)

Quality control using highest expressed genes

plotQC(sce, type = "highest-expression", n = 50)

Data normalization

sce <- computeSumFactors(sce, sizes = pmin(ncol(sce), seq(20, 120, 20)), min.mean = 0.1)
summary(sizeFactors(sce))
sce <- normalise(sce, exprs_values = "counts", return_log = TRUE, 
                 return_norm_as_exprs = TRUE)
sce <- normalise(sce, exprs_values = "counts", return_log = FALSE, 
                 return_norm_as_exprs = FALSE)

Plot the proportion of explained variances

expl_vars <- c("phenoid", "log10_total_counts", "log10_total_features", "pct_dropout",
               "pct_counts_top_200_features", "log10_counts_feature_controls",
               "pct_counts_feature_controls")
plotQC(sce, type = "explanatory-variables", variables = expl_vars)

Plot t-SNE representations

set.seed(1234)
sce <- runTSNE(sce, exprs_values = "logcounts", perplexity = 10)
plotTSNE(sce, colour_by = "phenoid")
plotTSNE(sce, colour_by = "total_features", size_by = "total_counts")

Save the normalized and cell filtered dataset

sce <- sce[!grepl("^ERCC", rownames(sce)), ]
dim(sce)
table(sce$phenoid)
saveRDS(sce, file = "../../data/sce_full/sce_full_Trapnell.rds")

Session info

date()
sessionInfo()


csoneson/DuoClustering2018 documentation built on May 18, 2024, 7:13 a.m.