Skip to content

Instantly share code, notes, and snippets.

@daskelly
Last active April 5, 2018 18:40
Show Gist options
  • Save daskelly/09c1d2ae8dc3b1de1fe2ec2dbd0dd44d to your computer and use it in GitHub Desktop.
Save daskelly/09c1d2ae8dc3b1de1fe2ec2dbd0dd44d to your computer and use it in GitHub Desktop.
Use Seurat to find specific markers of each cell type
library(tidyverse)
library(Seurat)
# Find specific markers for each cell type
celltypes <- # list of cell types (unique ident labels of Seurat object)
obj <- # Seurat object
specific_markers <- NULL
# First we do all pairwise comparisons and retain any markers that
# are even somewhat higher in the cell type of interest
for (celltype1 in celltypes) {
other_types <- setdiff(celltypes, celltype1)
celltype_markers <- NULL
for (celltype2 in other_types) {
markers <- FindMarkers(obj, ident.1=celltype1, ident.2=celltype2,
only.pos=TRUE, min.diff.pct=0.05, test.use="roc") %>%
rownames_to_column("gene") %>%
mutate(ident.2=celltype2)
celltype_markers <- rbind(celltype_markers, markers)
}
celltype_markers$ident.1 <- celltype1
specific_markers <- rbind(specific_markers, celltype_markers)
}
# In specific_markers data.frame, ident.1 is which cell pop the marker is tagging
# and ident.2 is which other cell pop we are contrasting with
#
# We want markers that are considerably higher in ident.1 than in any other pop.
# As a basic filter we require any marker for ident.1 to be expressed in <50% of
# cells of each of the other cell types.
#
# Here we will filter to get the top 10 markers for each cell type
topN <- 10
npop <- length(celltypes)
final_markers <- mutate(specific_markers, pct_diff=pct.1 - pct.2) %>%
group_by(ident.1, gene) %>%
filter(n() == npop - 1, all(pct.2 < 0.6)) %>%
summarize(median_AUC=median(myAUC), pct.1=mean(pct.1), pct.2=mean(pct.2)) %>%
mutate(cluster=ident.1) %>%
filter(median_AUC > 0.65) %>%
group_by(cluster) %>%
arrange(desc(median_AUC)) %>%
do(head(., topN))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment