A package facilitating unsupervised classification via k-Nearest-Neighbors and graph-based Louvain community detection.
if(!"devtools" %in% rownames(installed.packages())) {
install.packages("devtools")
}
devtools::install_github("EngeLab/knnClassification")
The classification works by:
Two datasets are included in the package which include counts and metadata for a single cell RNA sequencing experiment performed in 3 cell lines (A375, HCT116, and HOS). Using this dataset we can run the algorithm using the following commands:
library(kNNclassification)
#setup input data
s <- stringr::str_detect(colnames(testCounts), "^s")
e <- stringr::str_detect(rownames(testCounts), "^ERCC\\-[0-9]*$")
c <- testCounts[!e, s]
cpm <- t(t(c) / colSums(c) * 10^6)
#pre-run PCA
pca <- gmodels::fast.prcomp(t(cpm), scale. = TRUE)$x
#run KNN graph classification
kc <- kNNclassify(cpm, 1:nrow(c), 20, 15, pca = pca)
##
## Using eigendecomposition of correlation matrix.
## Computing: 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
#plot
pData <- merge(kc, matrix_to_tibble(pca[, 1:2], "sample"))
plot(
pData$PC1, pData$PC2, col = rainbow(4)[pData$louvain], pch = 16,
xlab = "PC1", ylab = "PC2"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.