Loops for Seurat functions
0
0
Entering edit mode
17 months ago
V ▴ 380

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 • 1.6k views
ADD COMMENT
2
Entering edit mode

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)
ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode
samples = list(S1, S2)
for ( i in 1:length(samples) ) {
  samples[[i]] <- do.seurat(i)
}

or

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

Login before adding your answer.

Traffic: 1928 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6