1

I have a single-cell multi-omic Seurat object that contains RNA, cell-surface-protein(ADT) assays and metadata. I subsetted the object based on ADT levels of interest. I want to add metadata to annotate these subsets specifically within the original object.

What I tried does not work:

data[subset] 

gives the error:

Error in as.character.default(x) : 
  no method for coercing this S4 class to a vector

Or data@assays$ADT[subset]

Error in GetAssayData(object = x)[i, j, ..., drop = FALSE] : 
  invalid or not-yet-implemented 'Matrix' subsetting

Or data@meta.data[subset] OR data@meta.data[[subset]]

Error in `[.default`(data@meta.data, subset) : 
  invalid subscript type 'S4'

Any help would be appreciated, thank you.

UPDATE: It works using the approach:

# add annotation to each subset
  subset1@meta.data$newLabel<-"subset1"
  subset2@meta.data$newLabel<-"subset2"
  subset3@meta.data$newLabel<-"subset3"
  subset4@meta.data$newLabel<-"subset4"  

# function that merges the metadata of the subsets
    multiannot<-function(x)
    {
      CellsName<-NULL
      for(i in 1: length(x))
      {
        CellsNameInt <- subset(x[[i]]@meta.data, select = c("newLabel"))
        if(is.null(CellsName)==F)
          CellsName <-rbind(CellsName,CellsNameInt) else
            CellsName <- CellsNameInt
      }
      return(CellsName)
    }

# call the function using a vector that contains the subsets
  metadatanew<-multiannot(c(subset1,subset2,subset3,subset4))

# add the metadata to the original object
  data<-AddMetaData(data, metadatanew)

  • 2
    This is a very specific question requiring some knowledge about a specialist library. You might get a better response at the [Bioinformatics forum](https://bioinformatics.stackexchange.com/) or even the [Seurat discussion page](https://github.com/satijalab/seurat/discussions). – neilfws Jun 07 '23 at 22:39

1 Answers1

1

From what I uderstood you question is already answered here: Add Metadata to Seurat Object

I am assuming you have successfully subset your object already based on what you wrote in your question. One way to add metadata back to the original object is the following:

    CellsMetaTrim <- subset(data@meta.data, select = c("name_of_column"))
    #data is your seurat object with only ADT

    #"name_of_column" is the name of the annotation column you want to transfer from ADT data set to the object will all your cells.

    head(CellsMetaTrim)

    All_Data_Atlas <- AddMetaData(All_Data_Atlas, CellsMetaTrim)
    #All_Data_Atlas is the seurat object with all the cells.

Let me know if it works

Alternatively if you have a metadata column in your object with all the cells that distinguishes between ADT and non you can also create a new metadata column and modify it if it matches a certain value in said column:

     All_Data_Atlas$newLabel <- as.character(All_Data_Atlas$orig.ident)
     #orig.ident is the metadata column where you have labelled ADT from the other cells
     #$newLabel is the column for the new metadata
     All_Data_Atlas$newLabel  <- replace(All_Data_Atlas$newLabel, All_Data_Atlas$newLabel %in% "ADT", "New_Information")

In case you want to add metadata from multiple datasets you can use rbind on CellsMetaTrim: First make sure all the datasets have matching column names for the datacolumn you want to add, you can do this by creating a new column if you have to:

data$newLabel <- data1$oldLabel1 
data$newLabel <- data2$oldLabel2 
#Then obtain multiple CellsMetaTrim and bind them together:
CellsMetaTrim1 <- subset(data@meta.data, select = c("newLabel")) 
CellsMetaTrim2 <- subset(data@meta.data, select = c("newLabel")) 
CellsMetaTrim<-rbind(CellsMetaTrim2,CellsMetaTrim1) 
#Now you can add it to your dataset
All_Data_Atlas <- AddMetaData(All_Data_Atlas, CellsMetaTrim)
  • Thank you Luca, the first option works for a single annotation. However, the problem now is that I have 10 subsets that I need to annotate, and I would like to have them all within the same column, as they should be mutually exclusive. If I use AddMetaData more than once, it overwrites the values completely as opposed to adding where there are NAs only. Any ideas how to overcome this? – lostnewbiecoder Jun 08 '23 at 17:13
  • I tried coalesce(CellsMetaTrim,CellsMetaTrim1) but they would not work due to different sizes of course. – lostnewbiecoder Jun 08 '23 at 17:25
  • I remember having a similar issue. You can solve it with rbind() function: You just need to have the columns from the 10 different objects labelled with the same name. You can do this by creating a new column if you have to: data$newLabel <- data1$oldLabel1 data$newLabel <- data2$oldLabel2 CellsMetaTrim1 <- subset(data@meta.data, select = c("newLabel")) CellsMetaTrim2 <- subset(data@meta.data, select = c("newLabel")) CellsMetaTrim<-rbind(CellsMetaTrim2,CellsMetaTrim1) Now you can add all the metadata together: All_Data_Atlas <- AddMetaData(All_Data_Atlas, CellsMetaTrim) – Luca Mannino Jun 09 '23 at 07:47
  • I added my comment to the reply for clarity. Let me know if it works :) – Luca Mannino Jun 09 '23 at 07:54
  • Thank you Luca, that worked! I also wrote a little function to simplify the code, if helpful for anyone looking for the same question: – lostnewbiecoder Jun 09 '23 at 13:37
  • `subset1@meta.data$newLabel<-"subset1" # add metadata to all subsets subset4@meta.data$newLabel<-"subset4" multiannot<-function(x) { CellsName<-NULL for(i in 1: length(x)) { CellsNameInt <- subset(x[[i]]@meta.data, select = c("newLabel")) if(is.null(CellsName)==F) CellsName <-rbind(CellsName,CellsNameInt) else CellsName <- CellsNameInt } return(CellsName) } metadatanew<-multiannot(c(subset1,subset2,subset3,subset4)) data<-AddMetaData(data, metadatanew) ` – lostnewbiecoder Jun 09 '23 at 13:47