knitr::opts_chunk$set( fig.width = 5, fig.height = 5, fig.align = "center", collapse = TRUE, comment = "#>" )
library(Canek) # Functions ## Function to plot the pca coordinates plotPCA <- function(pcaData = NULL, label = NULL, legPosition = "topleft"){ col <- as.integer(label) 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 batcheslsData <- list(B1 = SimBatches$batches[[1]], B2 = SimBatches$batches[[2]]) batch <- factor(c(rep("Batch-1", ncol(lsData[[1]])), rep("Batch-2", ncol(lsData[[2]])))) celltype <- SimBatches$cell_types table(batch) table(celltype)
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.
data <- Reduce(cbind, lsData) pcaData <- prcomp(t(data), center = TRUE, scale. = TRUE)$x
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.
data <- RunCanek(lsData)
We perform PCA of the corrected datasets and plot the first two PCs. After correction, the cells group by their corresponding cell type.
pcaData <- prcomp(t(data), center = TRUE, scale. = TRUE)$x
plotPCA(pcaData = pcaData, label = batch, legPosition = "topleft") plotPCA(pcaData = pcaData, label = celltype, legPosition = "topleft")
sessionInfo()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.