Last active
February 20, 2020 15:58
-
-
Save dosorio/7e1297b9fe88cb3909e2788805c4dae3 to your computer and use it in GitHub Desktop.
SC-02-20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install Packages | |
install.packages(c('devtools','ggplot2')) | |
BiocManager::install(c('fgsea', 'MAST', 'ensembldb', 'GenomeInfoDb')) | |
devtools::install_github('cailab-tamu/scTypeGSEA') | |
# Loading libraries | |
library(Seurat) | |
library(MAST) | |
library(ggplot2) | |
library(scTypeGSEA) | |
# Moving to downloads | |
setwd("../Downloads/") | |
# Getting the input files from 10X Genomics | |
inputFile <- "http://cf.10xgenomics.com/samples/cell-exp/3.0.0/pbmc_1k_protein_v3/pbmc_1k_protein_v3_filtered_feature_bc_matrix.tar.gz" | |
download.file(inputFile, "PBMC.tar.gz") | |
untar("PBMC.tar.gz") | |
#Input Folder | |
inputFolder <- "filtered_feature_bc_matrix/" | |
# Loading Seurat | |
library(Seurat) | |
# Reading the dataset | |
PBMC <- Read10X(inputFolder) | |
PBMC <- PBMC[[1]] | |
# QC | |
mtReads <- PBMC[grepl("MT-", rownames(PBMC)),] | |
mtRate <- (apply(mtReads,2,sum)/apply(PBMC,2,sum)) | |
plot(mtRate, pch=16) | |
abline(h = 0.1, col = "red", lty = 2) | |
# Filtering | |
PBMC <- PBMC[apply(PBMC,1,sum) != 0,] | |
PBMC <- PBMC[,mtRate < 0.1] | |
# Creating a Seurat Object | |
PBMC <- CreateSeuratObject(PBMC) | |
# Normalizing the dataset | |
PBMC <- NormalizeData(PBMC) | |
PBMC <- ScaleData(PBMC) | |
# Accessing to the raw data | |
PBMC@assays$RNA@counts | |
# Accessing to the normalized data | |
PBMC@assays$RNA@data | |
# Dimentionality Reduction | |
PBMC <- FindVariableFeatures(PBMC) | |
PBMC <- RunPCA(PBMC) | |
PCAPlot(PBMC) | |
PBMC <- RunUMAP(PBMC, dims = 1:50) | |
UMAPPlot(PBMC) | |
PBMC <- RunTSNE(PBMC, perplexity = 100) | |
TSNEPlot(PBMC) | |
# Finding the clusters | |
PBMC <- FindNeighbors(PBMC, do.plot = TRUE) | |
PBMC <- FindClusters(PBMC,resolution = 0.1) | |
UMAPPlot(PBMC) | |
TSNEPlot(PBMC) | |
PCAPlot(PBMC) | |
## Characterizing the clusters | |
# Using Wilcoxon | |
markerGenesW <- FindAllMarkers(PBMC, test.use = 'wilcox') | |
#Plotting | |
top10 <- lapply(unique(Idents(PBMC)), function(X){ | |
Xm <- markerGenesW[markerGenesW$cluster %in% X,] | |
Xm <- Xm[order(Xm$avg_logFC, decreasing = TRUE),] | |
Xm[1:10,7] | |
}) | |
DoHeatmap(PBMC, features = unlist(top10), angle = 0, hjust = 2) | |
DotPlot(PBMC, features = unlist(top10)) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
# Using MAST | |
markerGenesM <- FindAllMarkers(PBMC, test.use = 'MAST') | |
top10 <- lapply(unique(Idents(PBMC)), function(X){ | |
Xm <- markerGenesM[markerGenesM$cluster %in% X,] | |
Xm <- Xm[order(Xm$avg_logFC, decreasing = TRUE),] | |
Xm[1:10,7] | |
}) | |
DoHeatmap(PBMC, features = unlist(top10), angle = 0, hjust = 2) | |
DotPlot(PBMC, features = unlist(top10)) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) | |
# scTypeGSEA | |
library(scTypeGSEA) | |
A <- scTypeGSEA::assignCellType(PBMC, test.use = 'MAST') | |
B <- scTypeGSEA::labelCelltype(PBMC, A$cluster_celltype) | |
PBMC <- B$obj | |
UMAPPlot(PBMC) | |
TSNEPlot(PBMC) | |
DoHeatmap(PBMC, features = unlist(top10), angle = 0, hjust = -10) | |
DotPlot(PBMC, features = unlist(top10)) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment