Benchmaking for the scMAP dataset - yan et al 2013

Load the dataset to use

dataset <- readRDS(url("https://scrnaseq-public-datasets.s3.amazonaws.com/scater-objects/yan.rds"))

scGPS

#Load everyting for scGPS Benchmarking
library(scGPS)
## Loading required package: SummarizedExperiment
## Loading required package: GenomicRanges
## Loading required package: stats4
## Loading required package: BiocGenerics
## Loading required package: parallel
## 
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:parallel':
## 
##     clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
##     clusterExport, clusterMap, parApply, parCapply, parLapply,
##     parLapplyLB, parRapply, parSapply, parSapplyLB
## The following objects are masked from 'package:stats':
## 
##     IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
## 
##     anyDuplicated, append, as.data.frame, basename, cbind,
##     colnames, dirname, do.call, duplicated, eval, evalq, Filter,
##     Find, get, grep, grepl, intersect, is.unsorted, lapply, Map,
##     mapply, match, mget, order, paste, pmax, pmax.int, pmin,
##     pmin.int, Position, rank, rbind, Reduce, rownames, sapply,
##     setdiff, sort, table, tapply, union, unique, unsplit, which,
##     which.max, which.min
## Loading required package: S4Vectors
## 
## Attaching package: 'S4Vectors'
## The following object is masked from 'package:base':
## 
##     expand.grid
## Loading required package: IRanges
## 
## Attaching package: 'IRanges'
## The following object is masked from 'package:grDevices':
## 
##     windows
## Loading required package: GenomeInfoDb
## Loading required package: Biobase
## Welcome to Bioconductor
## 
##     Vignettes contain introductory material; view with
##     'browseVignettes()'. To cite Bioconductor, see
##     'citation("Biobase")', and for packages 'citation("pkgname")'.
## Loading required package: DelayedArray
## Loading required package: matrixStats
## 
## Attaching package: 'matrixStats'
## The following objects are masked from 'package:Biobase':
## 
##     anyMissing, rowMedians
## Loading required package: BiocParallel
## 
## Attaching package: 'DelayedArray'
## The following objects are masked from 'package:matrixStats':
## 
##     colMaxs, colMins, colRanges, rowMaxs, rowMins, rowRanges
## The following objects are masked from 'package:base':
## 
##     aperm, apply, rowsum
## Loading required package: dynamicTreeCut
## Loading required package: SingleCellExperiment
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
library(scater)
## Loading required package: ggplot2
## 
## Attaching package: 'scater'
## The following object is masked from 'package:S4Vectors':
## 
##     rename
## The following object is masked from 'package:stats':
## 
##     filter
library(scran)

#Copy the dataset for scGPS analysis
yan_dat <- dataset

#Find the genes with all zero entries and remove
keep_features <- rowSums(assays(yan_dat)[["normcounts"]] > 0) > 0
yan_dat <- yan_dat[keep_features, ]
table(keep_features)
## keep_features
## FALSE  TRUE 
##   619 19595
#Use scran normalisation
computeSumFactors(yan_dat, assay.type = "normcounts")
## class: SingleCellExperiment 
## dim: 19595 90 
## metadata(0):
## assays(2): normcounts logcounts
## rownames(19595): C9orf152 RPS11 ... CTSC AQP7
## rowData names(1): feature_symbol
## colnames(90): Oocyte..1.RPKM. Oocyte..2.RPKM. ...
##   Late.blastocyst..3..Cell.7.RPKM.
##   Late.blastocyst..3..Cell.8.RPKM.
## colData names(2): cell_type1 cell_type2
## reducedDimNames(0):
## spikeNames(1): ERCC
normalize(yan_dat, exprs_values = "normcounts")
## Warning in .local(object, ...): using library sizes as size factors
## Warning in .get_all_sf_sets(object): spike-in set 'ERCC' should have its
## own size factors
## class: SingleCellExperiment 
## dim: 19595 90 
## metadata(1): log.exprs.offset
## assays(2): normcounts logcounts
## rownames(19595): C9orf152 RPS11 ... CTSC AQP7
## rowData names(1): feature_symbol
## colnames(90): Oocyte..1.RPKM. Oocyte..2.RPKM. ...
##   Late.blastocyst..3..Cell.7.RPKM.
##   Late.blastocyst..3..Cell.8.RPKM.
## colData names(2): cell_type1 cell_type2
## reducedDimNames(0):
## spikeNames(1): ERCC
#Remove spikes
is.spike <-grepl("^ERCC", rownames(yan_dat))
yan_dat <- yan_dat[!is.spike, ]

#Start the time here
start_time <- Sys.time()

#Extract the needed variables
yan_dat_exprs <- assays(yan_dat)[["logcounts"]]
yan_dat_cellnames <- colnames(yan_dat)
yan_dat_cellnames <- data.frame("cellBarcodes" = yan_dat_cellnames)
yan_dat_GeneMetaData <- rownames(yan_dat)
yan_dat_GeneMetaData <- data.frame("GeneSymbol" = yan_dat_GeneMetaData)

#Store Data in scGPS format
mixedpop <- new_summarized_scGPS_object(ExpressionMatrix = yan_dat_exprs, GeneMetadata = yan_dat_GeneMetaData, CellMetadata = yan_dat_cellnames)

#Cluster and plot data using SCORE
CORE_cluster_bagging <- CORE_bagging(mixedpop, remove_outlier = c(0), PCA=FALSE)
## Performing 1 round of filtering
## Identifying top variable genes
## Calculating distance matrix
## Performing hierarchical clustering
## Finding clustering information
## No more outliers detected in filtering round 1
## Identifying top variable genes
## Calculating distance matrix
## Performing hierarchical clustering
## Finding clustering information
## 90 cells left after filtering
## Running 20 bagging runs, with 0.8 subsampling...
## Done clustering, moving to stability calculation...
## Done finding optimal clustering
plot_CORE(CORE_cluster_bagging$tree, list_clusters = CORE_cluster_bagging$Cluster)

plot_optimal_CORE(original_tree= CORE_cluster_bagging$tree, optimal_cluster = unlist(CORE_cluster_bagging$Cluster[CORE_cluster_bagging$optimal_index]), shift = -100)
## Ordering and assigning labels...
## 2
## 1545NA
## 3
## 154575
## Plotting the colored dendrogram now....
## Plotting the bar underneath now....

#Stop the time here
end_time <- Sys.time()
time_difference_SCORE <- end_time - start_time

#Make a dataframe with the results we want to examine
cell_types1 <- colData(yan_dat)$cell_type1
cell_types2 <- colData(yan_dat)$cell_type2
label_list <- unlist(yan_dat_cellnames$cellBarcodes)
cluster_list <- unlist(CORE_cluster_bagging$Cluster[CORE_cluster_bagging$optimal_index])
compare_frame <- data.frame("Gene_label" = label_list, "type1" = cell_types1, "type2" = cell_types2, "cluster" = cluster_list)

#Find the Adjusted Rand Index
AdjustedRandIndex_SCORE <- mclust::adjustedRandIndex(compare_frame$type2, compare_frame$cluster)
HighResRand <- mclust::adjustedRandIndex(compare_frame$type2, unlist(CORE_cluster_bagging$Cluster[1]))

#Store the estimated k from the bagging runs
estimated_k_SCORE <- CORE_cluster_bagging$optimalMax

#Remove unwanted data and Retrieve output
rm(list = setdiff(ls(), c("AdjustedRandIndex_SCORE", "time_difference_SCORE", "estimated_k_SCORE", "HighResRand", "dataset")))
for ( obj in ls() ) { cat('---',obj,'---\n'); print(get(obj)) }
## --- AdjustedRandIndex_SCORE ---
## [1] 0.5880355
## --- dataset ---
## class: SingleCellExperiment 
## dim: 20214 90 
## metadata(0):
## assays(2): normcounts logcounts
## rownames(20214): C9orf152 RPS11 ... CTSC AQP7
## rowData names(1): feature_symbol
## colnames(90): Oocyte..1.RPKM. Oocyte..2.RPKM. ...
##   Late.blastocyst..3..Cell.7.RPKM.
##   Late.blastocyst..3..Cell.8.RPKM.
## colData names(2): cell_type1 cell_type2
## reducedDimNames(0):
## spikeNames(1): ERCC
## --- estimated_k_SCORE ---
## [1] 3
## --- HighResRand ---
## [1] 0.5880355
## --- time_difference_SCORE ---
## Time difference of 6.452729 secs

SC3

#Load everything required for SC3
library(SC3)
library(scater)

#Copy the dataset for SC3 analysis
sce <- dataset

#Find the genes with all zero entries and remove
keep_features <- rowSums(assays(sce)[["normcounts"]] > 0) > 0
sce <- sce[keep_features, ]

#Remove the spikes
is.spike <-grepl("^ERCC", rownames(sce))
table(is.spike)
## is.spike
## FALSE  TRUE 
## 19587     8
sce <- sce[!is.spike, ]

#place the rpkm in the counts column for SC3
counts(sce) <- normcounts(sce)

#Start the time here
start_time <- Sys.time()

#Run sc3 with an estimation for k
sce <- sc3_prepare(sce, n_cores = 1, gene_filter = TRUE, kmeans_nstart = 50)
## Setting SC3 parameters...
sce <- sc3_estimate_k(sce)
## Estimating k...
SC3_k_estimate <- as.integer(unlist(metadata(sce)$sc3$k_estimation))
sce <- sc3_calc_dists(sce)
## Calculating distances between the cells...
sce <- sc3_calc_transfs(sce)
## Performing transformations and calculating eigenvectors...
sce <- sc3_kmeans(sce, ks = SC3_k_estimate)
## Performing k-means clustering...
sce <- sc3_calc_consens(sce)
## Calculating consensus matrix...
#Stop the time here
end_time <- Sys.time()
time_difference_SC3 <- end_time - start_time

#Make a dataframe with the results we want to examine
cell_types1 <- colData(sce)$cell_type1
cell_types2 <- colData(sce)$cell_type2
label_list <- rownames(colData(sce))
cluster_list <- as.numeric(colData(sce)[, paste0("sc3_", SC3_k_estimate, "_clusters")])
compare_frame <- data.frame("Gene_label" = label_list, "type1" = cell_types1, "type2" = cell_types2, "cluster" = cluster_list)

#Find the Adjusted Rand Index
AdjustedRandIndex_SC3 <- mclust::adjustedRandIndex(compare_frame$type2, compare_frame$cluster)

#Remove unwanted data
rm(list = setdiff(ls(), c("AdjustedRandIndex_SC3", "time_difference_SC3", "SC3_k_estimate", "dataset")))
for ( obj in ls() ) { cat('---',obj,'---\n'); print(get(obj)) }
## --- AdjustedRandIndex_SC3 ---
## [1] 0.6499654
## --- dataset ---
## class: SingleCellExperiment 
## dim: 20214 90 
## metadata(0):
## assays(2): normcounts logcounts
## rownames(20214): C9orf152 RPS11 ... CTSC AQP7
## rowData names(1): feature_symbol
## colnames(90): Oocyte..1.RPKM. Oocyte..2.RPKM. ...
##   Late.blastocyst..3..Cell.7.RPKM.
##   Late.blastocyst..3..Cell.8.RPKM.
## colData names(2): cell_type1 cell_type2
## reducedDimNames(0):
## spikeNames(1): ERCC
## --- SC3_k_estimate ---
## [1] 6
## --- time_difference_SC3 ---
## Time difference of 14.79941 secs