Skip to content

Instantly share code, notes, and snippets.

@diazdc
Last active May 24, 2024 16:34
Show Gist options
  • Save diazdc/1735102c243cd16acb1b1f3fd09a26e1 to your computer and use it in GitHub Desktop.
Save diazdc/1735102c243cd16acb1b1f3fd09a26e1 to your computer and use it in GitHub Desktop.
Multicore solution for Seurat FindAllMarkers()
n_clust <- 1:(max(as.numeric(Idents(seurat_obj))))
mcFindMarkers <- function(i){
ident1 <- i
ident2 <- n_clust[n_clust != i]
table <- FindMarkers(seurat_obj,
ident.1 = ident1, ident.2 = ident2, only.pos = TRUE)
table$Gene.name.uniq <- rownames(table)
table$cluster <- rep(i, nrow(table))
return(table)
}
marker_results <- list()[n_clust]
ptm <- proc.time()
marker_results <- parallel::mclapply(n_clust, mcFindMarkers, mc.cores = 16)
time_diff <- proc.time() - ptm
time_diff
# nice way to flatten list into a single DF
markers <- dplyr::bind_rows(markers)
@dBenedek
Copy link

Hi, just a minor comment: in my case Idents(seurat_obj) is a factor, with levels from 0 to 19, so instead of using 1:(max(as.numeric(Idents(seurat_obj)))), I used Idents(seurat_obj) %>% unique() %>% as.character() %>% as.numeric(), otherwise the factor to numeric conversion does not give what I need.

@tnystul
Copy link

tnystul commented May 24, 2024

Very helpful--thank you! BTW, I think that markers <- dplyr::bind_rows(markers) should be markers <- dplyr::bind_rows(marker_results)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment