This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Loops for Seurat functions

Hello community,

I have multiple samples (keeping it two for this example), lets call them S1 and S2 (seurat objects), which I want to run a traditional Seurat analysis on them. While it's manageable to run everything in the Seurat pipeline twice (e.g add percent mitochondria, filtering etc), I was wondering if there's a more automated/efficient way.

I tried writing a loop, but that didnt work at all....This is what I did :

samples = c('S1', 'S2')

for (sample in samples){
    {sample}[["percent.mt"]] <- PercentageFeatureSet({sample}, pattern = "^MT-")
}

Any advice you would have would be much appreciated.

seurat r

Why not write a function

do.seurat <- function(obj) {
  obj[['percent.mt']] <- PercentageFeatureSet(...)
  ...  # rest of "traditional Seurat analysis"
  obj
}

S1 <- do.seurat(S1)
S2 <- do.seurat(S2)

Fantastic, this definitely works, thank you!

Is there a way of putting this in a loop? Following on from your example above, I'm trying to do something like :

do.seurat <- function(obj) {
  obj[['percent.mt']] <- PercentageFeatureSet(...)
}

samples = c(S1, S2)

for (i in samples) {
i <- do.seurat(i)
}

but it doesnt seem to like that.

samples = list(S1, S2)
for ( i in 1:length(samples) ) {
  samples[[i]] <- do.seurat(i)
}

or

samples <- lapply(list(S1, S2), do.seurat)

0 answers

No answers yet.

Log in to answer this question.