library(Seurat) library(scran) pbmc.0 <- readRDS("pmbc_1683691139445.rds") obj <- pbmc.0 mat <- obj@assays$RNA@counts |> as.matrix() sce <- SingleCellExperiment(assays=list(counts=mat)) clusters <- quickCluster(sce) sce <- computeSumFactors(sce, clusters=clusters) summary(sizeFactors(sce)) sce <- logNormCounts(sce) dec <- modelGeneVar(sce) plot(dec$mean, dec$total, xlab="Mean log-expression", ylab="Variance") curve(metadata(dec)$trend(x), col="blue", add=TRUE) top.hvgs <- getTopHVGs(dec, n=2000) sce <- fixedPCA(sce, subset.row=top.hvgs) reducedDimNames(sce) sced <- denoisePCA(sce, dec, subset.row=top.hvgs) ncol(reducedDim(sced, "PCA")) reducedDim(sce, "PCAsub") <-reducedDim(sced, "PCA") # output <- getClusteredPCs(reducedDim(sce)) # npcs <- metadata(output)$chosen # reducedDim(sce, "PCAsub") <- reducedDim(sce, "PCA")[,1:npcs,drop=FALSE] # npcs g <- buildSNNGraph(sce, use.dimred="PCAsub") cluster <- igraph::cluster_walktrap(g)$membership colLabels(sce) <- factor(cluster) table(colLabels(sce)) library(scater) sce <- runTSNE(sce, dimred="PCAsub") plotTSNE(sce, colour_by="label", text_by="label") marker_gene_list <- list( "Naive CD4+ T" = c("IL7R", "CCR7"), "CD14+ Mono" = c("CD14", "LYZ"), "Memory CD4+" = c("IL7R", "S100A4"), "B" = c("MS4A1"), "CD8+ T" = c("CD8A"), "FCGR3A+ Mono" = c("FCGR3A","MS4A7"), "NK" = c("GNLY","NKG7"), "DC" = c("FCER1A","CST3"), "Platelet" = c("PPBP") ) marker_gene_mat <- marker_list_to_mat(marker_gene_list) s <- sizeFactors(sce) fit <- cellassign(sce[rownames(marker_gene_mat),], marker_gene_info = marker_gene_mat, s =s, learning_rate = 1e-2, shrinkage = TRUE, verbose = FALSE) pheatmap::pheatmap(cellprobs(fit)) sce@colData$cellassign <-celltypes(fit) pt1 <- plotTSNE(sce, colour_by="label", text_by="label") pt2 <- plotTSNE(sce, colour_by="cellassign", text_by="cellassign") pt1+pt2
https://bioconductor.org/packages/release/bioc/vignettes/scran/inst/doc/scran.html