rbioinformaticsbioconductorseurat

Adding Donor-Level Metadata to an Integrated Seurat Object


I am trying to add patient-level metadata to an existing Seurat object. This includes biochemical information for each participant, such as blood glucose, HsCRP, BMI etc. How do I add this data to my Seurat object so that it is correctly aligned with each participant ID?

I tried running this, and it didn't work:

library(Seurat)
#extract BMI into a numeric variable
BMI <- Donor_parameters$BMI
#add donor names to BMI
names(BMI) <- colnames(seurat_obj$Donor)

#Add metadata to existing object
seurat_obj <- AddMetaData(
  object = seurat_obj,
  metadata = BMI,
  col.name = 'BMI')

Solution

  • Metadata in a Seurat object has as many rows as the data has cells. To see what it looks like, you can use head(seurat_obj[[]]). Be aware that if you have filtered the data, the number of cells may not be the same as in the original raw data, so if you have additional annotation data for each cell, it may not match the data in the Seurat object.

    The data to be added must have the same row.names as the Seurat metadata, usually the cell barcodes. If the barcodes are a variable in the additional data, you can do:

    row.names(Donor_parameters) <- Donor_parameters$Barcode
    seurat_obj <- AddMetaData(seurat_obj, metadata = Donor_parameters)
    

    If the additional data has an identifying variable (here called Pat_id) that also exists in the Seurat metadata, you can do:

    library(dplyr)
    add_data <- left_join(seurat_obj[["Pat_id"]], Donor_parameters)
    row.names(add_data) <- row.names(seurat_obj[[]])
    seurat_obj <- AddMetaData(seurat_obj, metadata = add_data)
    

    A toy example:

    library(dplyr)
    library(Seurat)
    # Data is from https://cf.10xgenomics.com/samples/cell/pbmc3k/pbmc3k_filtered_gene_bc_matrices.tar.gz 
    pbmc.data <- Read10X(data.dir = "temp/filtered_gene_bc_matrices/hg19/")
    pbmc <- CreateSeuratObject(counts = pbmc.data,
                               project = "pbmc3k", 
                               min.cells = 3, 
                               min.features = 200)
    # Create a grouping variable
    pbmc <- AddMetaData(pbmc,
                        metadata = sample(c("A", "B", "C"),
                                          size = ncol(pbmc),
                                          replace = TRUE),
                        col.name = "Group")
    # Metadata for the groups
    meta <- data.frame(Group = c("B", "A", "C"),
                       Desc = c("Second", "First", "Third"))
    add_data <- left_join(pbmc[["Group"]], meta)
    row.names(add_data) <- row.names(pbmc[[]])
    pbmc <- AddMetaData(pbmc, metadata = add_data)