This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Iterate through Seurat object to add patient information

I want to add patient information to individual Seurat objects before merging them. I have a the patient characteristics as dataframe named covid-19

enter image description here

Adding info to individual obj as below works,

cov01 <- AddMetaData(cov01, metadata = c(covid_19[1,]), col.name =
colnames(covid_19))

However, when I I try to use forloop to automate, I am getting an error.

Error in UseMethod(generic = "AddMetaData", object = object) : 
no applicable method for 'AddMetaData' applied to an object of class "list"
S_obj = c(cov01, cov02, cov03, cov04, cov07, cov08, cov09, cov10, cov11, cov12, cov17, cov18) # name of my Seurat objects

for (i in 1:12) {
  S_obj[i] <- AddMetaData(S_obj[i], metadata = c(covid_19[i,]), col.name = colnames(covid_19))
}

How to iterate through Seurat objects?

r seurat

What is the output to class(S_obj[1])? Also, try using as.vector(covid_19[i]) instead of c(covid_19[i,]).

> class(S_obj[1])
[1] "list"

> class(S_obj[[1]])
[1] "Seurat"
attr(,"package")
[1] "SeuratObject"

When I run the loop with two square brackets S_obj[[i]], it runs without taking the object name and without throwing error. When I check the metadata the patient info is not added.

> S_obj = c(cov01, cov02, cov03, cov04, cov07, cov08, cov09, cov10, cov11, cov12, cov17, cov18)
> 
> for (i in 1:12) {
+ 
+ S_obj[[i]] <- AddMetaData(S_obj[[i]], metadata = c(covid_19[i,]), col.name = colnames(covid_19))
+ }
>   

The problem is the forloop doesn't take the object name from the list during iteration

What do you mean by "object name"?

First off, ensure it works for one object before you run a loop. Change your code to this:

S_obj = c(cov01, cov02, cov03, cov04, cov07, cov08, cov09, cov10, cov11, cov12, cov17, cov18)


## Use `unlist` instead of `c`; the former makes the row a character vector, see the other code segment:
S_obj[[1]] <- AddMetaData(S_obj[[1]], metadata = unlist(covid_19[1,]), col.name = colnames(covid_19))

## Check S_obj[[1]] and make sure things are OK before running the code on ALL objects

for (i in 2:12) {
  S_obj[[i]] <- AddMetaData(S_obj[[i]], metadata = unlist(covid_19[i,]), col.name = colnames(covid_19))
}

How I picked unlist:

library(dplyr)
df <- tibble(Sample_id=c("cov01","cov02","cov03","cov04","cov07"),Age=c(73,53,75,59,84), Disease_status=c(rep("covid-19", times=4),"Healthy"), Disease_severity=c("Severe","Moderate","Moderate","Severe",NA), Days_since_symptom_onset=c(15,9,2,16,NA))

df
# A tibble: 5 × 5
  Sample_id   Age Disease_status Disease_severity Days_since_symptom_onset
  <chr>     <dbl> <chr>          <chr>                               <dbl>
1 cov01        73 covid-19       Severe                                 15
2 cov02        53 covid-19       Moderate                                9
3 cov03        75 covid-19       Moderate                                2
4 cov04        59 covid-19       Severe                                 16
5 cov07        84 Healthy        NA                                     NA

df[1,]
# A tibble: 1 × 5
  Sample_id   Age Disease_status Disease_severity Days_since_symptom_onset
  <chr>     <dbl> <chr>          <chr>                               <dbl>
1 cov01        73 covid-19       Severe                                 15

class(df[1,])
[1] "tbl_df"     "tbl"        "data.frame"

class(c(df[1,]))
[1] "list"

class(as.vector(df[1,]))
[1] "list"

class(unlist(df[1,]))
[1] "character"

unlist(df[1,])
               Sample_id                      Age           Disease_status         Disease_severity Days_since_symptom_onset
                 "cov01"                     "73"               "covid-19"                 "Severe"                     "15"

By object name I mean the name I gave to Seurat objects for my individual samples (I have 12 'individuals' in the dataset).

The code works fine for individual samples, e.g.

cov01 <- AddMetaData(cov01, metadata = c(covid_19[1,]), col.name = colnames(covid_19))

However, the forloop doesn't iterate through the 12 Seurat objects.

If I run individually like this it works;

cov01 <- AddMetaData(cov01, metadata = c(covid_19[1,]), col.name = colnames(covid_19))
cov02 <- AddMetaData(cov02, metadata = c(covid_19[2,]), col.name = colnames(covid_19))
cov03 <- AddMetaData(cov03, metadata = c(covid_19[3,]), col.name = colnames(covid_19))
cov04 <- AddMetaData(cov04, metadata = c(covid_19[4,]), col.name = colnames(covid_19))

But the forloop doesn't.

I combined your comments into a single comment. Please note that "forloop" is not a thing, it's a for loop - 2 words.

Anyway, this is quite odd. Can I ask you try one last thing - use list() instead of c() to create the S_obj list.

0 answers

No answers yet.

Log in to answer this question.