Add Metadata to Seurat Object
Asked Answered
C

2

6

I am working with a R package called "Seurat" for single cell RNA-Seq analysis. I am trying to add metadata information about individual cell samples to the Seurat Object. But the downstream plotting commands are not working. I am wondering if anyone knows how I could check the modified Seurat object to confirm that the metadata was added in the correct slot and column.

To add the metadata i used the following commands.

First I extracted the cell names from the Seurat object

> Cells <- WhichCells(seurat_object)

Then I created a list of the morphologically determined cell types using numbers 1-3 this NOTE: the list is much longer but abbreviated as the first 3 here

> MorphCellTypes = c(1,2,3)

Then I merged cells and MorphCellTypes together as a data.frame

> MorphCellTypesDF = data.frame(Cells, MorphCellTypes)

Then I tried to add this MorphCellTypesDF metadata to the seurat object using the following command which ran successfully

> AddMetaData(Sleuth_object, MorphCellTypesDF, col.name = MorphCellTypes

Then I tried to visualize it with TSNEPlot using the following command

> TSNEPlot(pbmc, do.label = TRUE, pt.size = 3, group.by = MorphCellTypes

This returned the following Error:

“Error in DimPlot(object, reduction.use = "tsne", cells.use = cells.use,  :
  object 'MorphCellTypes' not found”

So I think that I am either adding the meta data to the wrong location in my seurat object or i am somehow messing up the col.name. Either way it appears that I am using the AddMetaData or TSNEPlot() functions incorrectly. If by any chance you spot the error or could point me to an example of how to use AddMetaData it would be very much appreciated.

Calvincalvina answered 16/2, 2017 at 16:37 Comment(0)
C
9

After much trial and error I found a way to successfully add columns of experimental meta data. The trick was to maintain the correct formatting from the Seurat Object while adding your data. I did this by copying the [email protected] table and then modifying it by adding a column to it. Then by importing the modified table back into Seurat. The following code adds a column of random numbers called Gene_ID's to the Seurat object in the [email protected] slot. Then it tests the addition of this data with a visualization.

CellsMeta = [email protected]
head(CellsMeta)

randomnumbers <- runif(2700, 0.0, 1.1)

CellsMeta["Gene_IDs"] <- randomnumbers
head(CellsMeta)

CellsMetaTrim <- subset(CellsMeta, select = c("Gene_IDs"))
head(CellsMetaTrim)

pbmc <- AddMetaData(pbmc, CellsMetaTrim)

head([email protected])

VlnPlot(pbmc, c("nGene", "nUMI", "Gene_IDs"), nCol = 3)
Calvincalvina answered 17/2, 2017 at 18:13 Comment(1)
I have used [email protected] instead of [email protected]. Thanks.Auvil
S
4

What you pass to the metadata argument in AddMetaData() must be a dataframe with rownames that match the rownames in [email protected]. I would guess that your MorphCellTypes object does not meet these two requirements, which is what causes the error.

From the docs for AddMetaData():

metadata: Data frame where the row names are cell names (note : these must correspond exactly to the items in [email protected]), and the columns are additional metadata items.

Sanatory answered 9/11, 2018 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.