library(Canek)
# Functions
## Function to plot the pca coordinates
<- function(pcaData = NULL, label = NULL, legPosition = "topleft"){
plotPCA <- as.integer(label)
col plot(x = pcaData[,"PC1"], y = pcaData[,"PC2"],
col = as.integer(label), cex = 0.75, pch = 19,
xlab = "PC1", ylab = "PC2")
legend(legPosition, pch = 19,
legend = levels(label),
col = unique(as.integer(label)))
}
On this toy example we use the two simulated batches included in the SimBatches
data from Canek’s package. SimBatches
is a list containing:
batches
: Simulated scRNA-seq datasets with genes (rows) and cells (columns). Simulations were performed using Splatter.cell_type
: a factor containing the celltype labels of the batches<- list(B1 = SimBatches$batches[[1]], B2 = SimBatches$batches[[2]])
lsData <- factor(c(rep("Batch-1", ncol(lsData[[1]])),
batch rep("Batch-2", ncol(lsData[[2]]))))
<- SimBatches$cell_types
celltype table(batch)
#> batch
#> Batch-1 Batch-2
#> 631 948
table(celltype)
#> celltype
#> Cell Type 1 Cell Type 2 Cell Type 3 Cell Type 4
#> 1451 53 38 37
We perform the Principal Component Analysis (PCA) of the joined datasets and scatter plot the first two PCs. The batch-effect causes cells to group by batch.
<- Reduce(cbind, lsData)
data <- prcomp(t(data), center = TRUE, scale. = TRUE)$x pcaData
plotPCA(pcaData = pcaData, label = batch, legPosition = "bottomleft")
plotPCA(pcaData = pcaData, label = celltype, legPosition = "bottomleft")
We correct the toy batches using the function RunCanek. This function accepts:
On this example we use the list of matrices created before.
<- RunCanek(lsData) data
We perform PCA of the corrected datasets and plot the first two PCs. After correction, the cells group by their corresponding cell type.
<- prcomp(t(data), center = TRUE, scale. = TRUE)$x pcaData
plotPCA(pcaData = pcaData, label = batch, legPosition = "topleft")
plotPCA(pcaData = pcaData, label = celltype, legPosition = "topleft")
sessionInfo()
#> R version 4.1.2 (2021-11-01)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19044)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=C LC_CTYPE=English_World.1252
#> [3] LC_MONETARY=English_World.1252 LC_NUMERIC=C
#> [5] LC_TIME=English_World.1252
#> system code page: 932
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] Canek_0.2.0
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.8 highr_0.9 DEoptimR_1.0-10
#> [4] bslib_0.3.1 compiler_4.1.2 bluster_1.4.0
#> [7] jquerylib_0.1.4 class_7.3-19 prabclus_2.3-2
#> [10] BiocNeighbors_1.12.0 numbers_0.8-2 tools_4.1.2
#> [13] mclust_5.4.9 digest_0.6.29 jsonlite_1.8.0
#> [16] evaluate_0.15 lattice_0.20-45 pkgconfig_2.0.3
#> [19] rlang_1.0.1 Matrix_1.4-0 igraph_1.2.11
#> [22] cli_3.1.1 rstudioapi_0.13 yaml_2.3.5
#> [25] parallel_4.1.2 xfun_0.29 fastmap_1.1.0
#> [28] stringr_1.4.0 knitr_1.37 cluster_2.1.2
#> [31] sass_0.4.0 S4Vectors_0.32.3 fpc_2.2-9
#> [34] diptest_0.76-0 nnet_7.3-16 stats4_4.1.2
#> [37] grid_4.1.2 robustbase_0.93-9 R6_2.5.1
#> [40] flexmix_2.3-17 BiocParallel_1.28.3 rmarkdown_2.12
#> [43] irlba_2.3.5 kernlab_0.9-29 magrittr_2.0.2
#> [46] matrixStats_0.61.0 modeltools_0.2-23 MASS_7.3-54
#> [49] htmltools_0.5.2 BiocGenerics_0.40.0 stringi_1.7.6
#> [52] FNN_1.1.3