Contents

1 Introduction

The analysis modules available through the Shiny app are also available as R functions for standard R console processing of single cell RNA-Seq data using a SCtkExperiment object. At any stage, you can load the Shiny App to interactively visualize and analyze a data set, but this vignette will show a standard workflow run entirely through the R console.

2 MAITS Example

The MAST package contains a convenient scRNA-Seq example data set of 96 Mucosal Associated Invariant T cells (MAITs), half of which were stimulated with cytokines to induce a response. For more details, consult the MAST package and vignette.

We will first convert the MAST example dataset to a SCtkExperiment object.

suppressPackageStartupMessages({
  library(MAST)
  library(singleCellTK)
  library(xtable)
})

data(maits, package="MAST")
maits_sce <- createSCE(assayFile = t(maits$expressionmat),
                       annotFile = maits$cdat,
                       featureFile = maits$fdat,
                       assayName = "logtpm",
                       inputDataFrames = TRUE,
                       createLogCounts = FALSE)
rm(maits)

2.1 summarizeTable

You can get summary metrics with the summarizeTable function:

knitr::kable(summarizeTable(maits_sce, useAssay = "logtpm"))
Metric Value
Number of Samples 96
Number of Genes 16302
Average number of reads per cell 17867
Average number of genes per cell 6833
Samples with <1700 detected genes 5
Genes with no expression across all samples 0

Typically, these summary statistics would be run on a “counts” matrix, but here we have log(tpm) values so the average number of reads per cell is calculated from the normalized values instead of raw counts.

2.2 Filtering by Annotation

Explore the available annotations in the data:

colnames(colData(maits_sce))
##  [1] "wellKey"          "condition"        "nGeneOn"          "libSize"         
##  [5] "PercentToHuman"   "MedianCVCoverage" "PCRDuplicate"     "exonRate"        
##  [9] "pastFastqc"       "ncells"           "ngeneson"         "cngeneson"       
## [13] "TRAV1"            "TRBV6"            "TRBV4"            "TRBV20"          
## [17] "alpha"            "beta"             "ac"               "bc"              
## [21] "ourfilter"
table(colData(maits_sce)$ourfilter)
## 
## FALSE  TRUE 
##    22    74

The data has a filtered dataset with 74 ‘pass filter’ samples, let’s subset the data to include the pass filter samples

maits_subset <- maits_sce[, colData(maits_sce)$ourfilter]
table(colData(maits_subset)$ourfilter)
## 
## TRUE 
##   74
knitr::kable(summarizeTable(maits_subset, useAssay = "logtpm"))
Metric Value
Number of Samples 74
Number of Genes 16302
Average number of reads per cell 16292
Average number of genes per cell 7539
Samples with <1700 detected genes 0
Genes with no expression across all samples 157

2.3 Visualization

Initially, there are no reduced dimensionality datasets stored in the object

reducedDims(maits_subset)
## List of length 0
## names(0):

PCA and t-SNE can be added to the object with the getPCA() and getTSNE() functions:

maits_subset <- getPCA(maits_subset, useAssay = "logtpm",
                       reducedDimName = "PCA_logtpm")
maits_subset <- getTSNE(maits_subset, useAssay = "logtpm",
                        reducedDimName = "TSNE_logtpm")
reducedDims(maits_subset)
## List of length 2
## names(2): PCA_logtpm TSNE_logtpm

2.3.1 PCA

PCA data can be visualized with the plotPCA() function:

plotPCA(maits_subset, reducedDimName = "PCA_logtpm", colorBy = "condition")

2.3.2 t-SNE

t-SNE data can be visualized with the plotTSNE() function:

plotTSNE(maits_subset, reducedDimName = "TSNE_logtpm", colorBy = "condition")

2.4 Converting Gene Names

The singleCellTK has the ability to convert gene ids to various formats using the org.*.eg.db Bioconductor annotation packages. These packages are not installed by default, so these must be manually installed before this function will work.

suppressPackageStartupMessages({
  library(org.Hs.eg.db)
})
maits_entrez <- maits_subset
maits_subset <- convertGeneIDs(maits_subset, inSymbol = "ENTREZID",
                               outSymbol = "SYMBOL", database = "org.Hs.eg.db")
#to remove confusion for MAST about the gene name:
rowData(maits_subset)$primerid <- NULL

2.5 Differential Expression with MAST

MAST is a popular package for performing differential expression analysis on scRNA-Seq data that models the effect of dropouts using a bimodal distribution and by including the cellular detection rate into the differential expression model. Functions in the toolkit allow you to perform this analysis on a SCtkExperiment object.

2.5.1 Adaptive Thresholding

First, an adaptive threshold is calculated by binning genes with similar expression levels.

thresholds <- thresholdGenes(maits_subset, useAssay = "logtpm")
par(mfrow = c(5, 4))
plot(thresholds)
par(mfrow = c(1, 1))

2.5.2 Run MAST

MAST analysis can be run with a single function

mast_results <- MAST(maits_subset, condition = "condition", useThresh = TRUE,
                     useAssay = "logtpm")

The resulting significantly differentially expressed genes can be visualized using a violin plot, linear model, or heatmap:

MASTviolin(maits_subset, useAssay = "logtpm", fcHurdleSig = mast_results,
           threshP = TRUE, condition = "condition")

MASTregression(maits_subset, useAssay = "logtpm", fcHurdleSig = mast_results,
               threshP = TRUE, condition = "condition")

plotDiffEx(maits_subset, useAssay = "logtpm", condition = "condition",
           geneList = mast_results$Gene[1:100], annotationColors = "auto",
           displayRowLabels = FALSE, displayColumnLabels = FALSE)

Among the top differentially expressed genes was interferon gamma, a cytokine that is known to be produced in response to stimulation.

2.5.3 Pathway Activity with GSVA

The singleCellTK supports pathway activity analysis using the GSVA package. Currently, the toolkit supports performing this analysis on human datasets with entrez IDs. Data can be visualized as a violin plot or a heatmap.

gsvaRes <- gsvaSCE(maits_entrez, useAssay = "logtpm",
                   "MSigDB c2 (Human, Entrez ID only)",
                   c("KEGG_PROTEASOME",
                     "REACTOME_VIF_MEDIATED_DEGRADATION_OF_APOBEC3G",
                     "REACTOME_P53_INDEPENDENT_DNA_DAMAGE_RESPONSE",
                     "BIOCARTA_PROTEASOME_PATHWAY",
                     "REACTOME_METABOLISM_OF_AMINO_ACIDS",
                     "REACTOME_REGULATION_OF_ORNITHINE_DECARBOXYLASE",
                     "REACTOME_CYTOSOLIC_TRNA_AMINOACYLATION",
                     "REACTOME_STABILIZATION_OF_P53",
                     "REACTOME_SCF_BETA_TRCP_MEDIATED_DEGRADATION_OF_EMI1"),
                    parallel.sz=1)
## Warning in .filterFeatures(expr, method): 157 genes with constant expression
## values throuhgout the samples.
## Warning in .filterFeatures(expr, method): Since argument method!="ssgsea", genes
## with constant expression values are discarded.
## Estimating GSVA scores for 9 gene sets.
## Estimating ECDFs with Gaussian kernels
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |======================================================================| 100%
set.seed(1234)
gsvaPlot(maits_subset, gsvaRes, "Violin", "condition")

gsvaPlot(maits_subset, gsvaRes, "Heatmap", "condition")

Among the top pathways that showed increased activity in the stimulated cells was KEGG_PROTEASOME, indicating proteasome related genes showed increased activity in the stimulated T cells. This pathway includes interferon gamma.

3 Batch Effects Example

It is possible to use ComBat within the Single Cell Toolkit. This support is experimental, since ComBat was not designed for scRNA-Seq. Here, we will load the bladderbatch example data into a SingleCellExperiment object.

library(bladderbatch)
data(bladderdata)
dat <- bladderEset

pheno <- pData(dat)
edata <- exprs(dat)
bladder_sctke <- createSCE(assayFile = edata,
                           annotFile = pheno,
                           assayName = "microarray",
                           inputDataFrames = TRUE,
                           createLogCounts = FALSE)

The plotBatchVariance() function can be used to plot the percent variation explained by condition and batch across the dataset.

plotBatchVariance(bladder_sctke, useAssay="microarray",
                  batch="batch", condition = "cancer")

The ComBatSCE() function can then be used to correct for batch effects

assay(bladder_sctke, "combat") <- ComBatSCE(inSCE = bladder_sctke,
                                            batch = "batch",
                                            useAssay = "microarray",
                                            covariates = "cancer")

After batch correction, a larger percentage of the explained variation can be explained by the condition

plotBatchVariance(bladder_sctke, useAssay="combat",
                  batch="batch", condition = "cancer")

Session info

## R version 4.0.0 (2020-04-24)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.4 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.11-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.11-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] bladderbatch_1.25.0         GSEABase_1.50.0            
##  [3] graph_1.66.0                annotate_1.66.0            
##  [5] XML_3.99-0.3                org.Hs.eg.db_3.10.0        
##  [7] AnnotationDbi_1.50.0        xtable_1.8-4               
##  [9] MAST_1.14.0                 singleCellTK_1.8.0         
## [11] SingleCellExperiment_1.10.0 SummarizedExperiment_1.18.0
## [13] DelayedArray_0.14.0         matrixStats_0.56.0         
## [15] Biobase_2.48.0              GenomicRanges_1.40.0       
## [17] GenomeInfoDb_1.24.0         IRanges_2.22.0             
## [19] S4Vectors_0.26.0            BiocGenerics_0.34.0        
## [21] BiocStyle_2.16.0           
## 
## loaded via a namespace (and not attached):
##  [1] nlme_3.1-147           bitops_1.0-6           GSVA_1.36.0           
##  [4] bit64_0.9-7            RColorBrewer_1.1-2     progress_1.2.2        
##  [7] tools_4.0.0            R6_2.4.1               mgcv_1.8-31           
## [10] DBI_1.1.0              colorspace_1.4-1       GetoptLong_0.1.8      
## [13] tidyselect_1.0.0       prettyunits_1.1.1      bit_1.1-15.2          
## [16] compiler_4.0.0         labeling_0.3           bookdown_0.18         
## [19] scales_1.1.0           genefilter_1.70.0      stringr_1.4.0         
## [22] digest_0.6.25          rmarkdown_2.1          XVector_0.28.0        
## [25] pkgconfig_2.0.3        htmltools_0.4.0        limma_3.44.0          
## [28] fastmap_1.0.1          highr_0.8              rlang_0.4.5           
## [31] GlobalOptions_0.1.1    RSQLite_2.2.0          shiny_1.4.0.2         
## [34] shape_1.4.4            farver_2.0.3           BiocParallel_1.22.0   
## [37] dplyr_0.8.5            RCurl_1.98-1.2         magrittr_1.5          
## [40] GenomeInfoDbData_1.2.3 Matrix_1.2-18          Rcpp_1.0.4.6          
## [43] munsell_0.5.0          abind_1.4-5            lifecycle_0.2.0       
## [46] edgeR_3.30.0           stringi_1.4.6          yaml_2.2.1            
## [49] zlibbioc_1.34.0        Rtsne_0.15             plyr_1.8.6            
## [52] grid_4.0.0             blob_1.2.1             promises_1.1.0        
## [55] crayon_1.3.4           lattice_0.20-41        splines_4.0.0         
## [58] circlize_0.4.8         hms_0.5.3              locfit_1.5-9.4        
## [61] magick_2.3             knitr_1.28             ComplexHeatmap_2.4.0  
## [64] pillar_1.4.3           rjson_0.2.20           reshape2_1.4.4        
## [67] fastmatch_1.1-0        glue_1.4.0             evaluate_0.14         
## [70] data.table_1.12.8      BiocManager_1.30.10    png_0.1-7             
## [73] vctrs_0.2.4            httpuv_1.5.2           gtable_0.3.0          
## [76] purrr_0.3.4            clue_0.3-57            assertthat_0.2.1      
## [79] ggplot2_3.3.0          xfun_0.13              mime_0.9              
## [82] GSVAdata_1.23.0        later_1.0.0            survival_3.1-12       
## [85] tibble_3.0.1           shinythemes_1.1.2      memoise_1.1.0         
## [88] cluster_2.1.0          sva_3.36.0             ellipsis_0.3.0