How to embed known cell information into Seurat object? AddMetaData?
2
1
Entering edit mode
4.2 years ago
Pratik ★ 1.1k

How to embed known cell information into Seurat object? Using AddMetaData?

Is this possible?

I read this documentation: https://www.rdocumentation.org/packages/Seurat/versions/3.1.4/topics/AddMetaData

However I need some clarification on how to go about doing this, please?

I have cell labels that are all aligned with the expression matrix such as this:

"12wks_fetal pancreas cell_acinar"  "19wks_fetal pancreas cell_beta"    "12wks_fetal pancreas cell_ductal"  "12wks_fetal pancreas cell_beta"    "12wks_fetal pancreas cell_acinar"  "22wks_fetal pancreas cell_acinar"  "12wks_fetal pancreas cell_acinar"  "12wks_fetal pancreas cell_alpha"   "12wks_fetal pancreas cell_ductal"  "12wks_fetal pancreas cell_acinar"  "14wks_fetal pancreas cell_endocrine.progenitor..."

I used these cell labels as cell names (column names) for the expression matrix. However that's not useful for seeing which cell is from what time point 12wks, 19wks, 14 wks, etc. on the UMAP for example.

Would there be a way to label the cells using the AddMetaData function so that when I visualize the data using UMAP, they are color coded based on the time of collection (ie. 12wks, 19wks, 22wks, etc.) or would there be a way I could also filter by both time of collection and/or cell type?

Is this possible in Seurat? How would I approach this?

I would really appreciate anyone's help.

Very Respectfully, Pratik

next-gen R Seurat3 scRNA-seq • 39k views
ADD COMMENT
5
Entering edit mode
4.2 years ago

You didn't think to try AddMetaData?

Yes, if you have one column with barcode names an another with any kind of information, you can add a new column of metadata. You can also make a new column of metadata by concatenating two columns you already have. You can use group.by in DimPlot to color the cells by any column in the metadata, and you can subset by metadata columns after setting that column with Ident.

ADD COMMENT
1
Entering edit mode

You can also just treat seurat_obj@meta.data as a standard data frame and modify it using generic functions.

ADD REPLY
2
Entering edit mode

So I tried the AddMetaData way by doing:

AddMetaData(object = scfp, metadata = celltypes_col, col.name = 'time.cell')

But that did not work!

What you suggested, igor, helped, treating the seurat_obj@meta.data as a data frame. I was able to add the meta data using this:

scfp@meta.data <-cbind(scfp@meta.data,celltypes_col)

and then changed the column name using this:

colnames(scfp@meta.data)[which(names(scfp@meta.data) == "V1")] <- "time.cell"

Thank you!

and then I followed the guidance of swbarnes2! Thank you Thank you!

ADD REPLY
1
Entering edit mode

I think you need col.name if you are adding a vector (so no obvious column name). If you are adding a data frame, there are columns with names already.

I would generally advise against cbind because you can't guarantee the same order.

ADD REPLY
1
Entering edit mode

Yss, cbind is very dangerous. Merge is far safer in general. But also in general, it's safer to access parts of an object by using the functions designed to interact with them, instead of writing straight to them yourself.

ADD REPLY
1
Entering edit mode

I understand, I would, but it didn't work for me : (

EDIT: I think I'll report it as a bug?

ADD REPLY
2
Entering edit mode

Hi,

I ordered the new meta data (colData$IsoPct) first with the order of cells from Seurat object before cbind. Then followed your steps.

Match the order:

colOrd <- rownames(FetchData(SeuObj,"ident"))
RcolData <- colData %>% slice(match(colOrd, Cells))

Add to metadata:

SeuObj@meta.data <-cbind(SeuObj@meta.data,RcolData$IsoPct)
colnames(SeuObj@meta.data)[which(names(SeuObj@meta.data) == "RcolData$IsoPct")] <- "IsoPct"

For replacing identity:

Idents(object = SeuObj, cells = 1:len) <- RcolData$condition
ADD REPLY
0
Entering edit mode

Thanks EagleEye

I actually realized I had been adding MetaData into Seurat incorrectly, all along. I EDITED one of the comments below for what I should have been doing: How to embed known cell information into Seurat object? AddMetaData?

Probably obvious to some, but here was my solution: How to read a dataframe into Seurat for AddMetaData properly?

ADD REPLY
0
Entering edit mode

I double checked the order prior to cbinding on of the Seurat object by doing > head(x = scfp@meta.data)

I would simply do AddMetaData, but it did not work for me, unfortunately. There was no error message. It just didn't show up under the Seurat object.

The method above, however, did work!

EDIT:

Check this out too. I figured out how to do through Seurat functions: How to read a dataframe into Seurat for AddMetaData properly?

ADD REPLY
0
Entering edit mode

Wow, this is incredibly helpful. I will keep this bookmarked, and attempt this method soon! Thank you Thank you swbarnes2!

ADD REPLY
0
Entering edit mode

Hi swbarnes2,

group.by does not work if there are numeric values in the column/meta.data. it is trying to assign colors for individual values rather than range of values. It would be nice if we have color.by feature or an option to define color range for the numeric values.

ADD REPLY
0
Entering edit mode

Can I add multiple columns of metadata at once? I have one column with cell barcodes and 4 other columns of cell meta-data. Can I pass the 4 columns to the meta-data section all in one command?

ADD REPLY
0
Entering edit mode

Can I add multiple columns of metadata at once? I have one column with cell barcodes and 4 other columns of cell meta-data. Can I pass the 4 columns to the meta-data section all in one command?

You may have figured it out already from just "guessing and checking", but yes, you can. You just have to have the cell barcodes as your row names, and then the other four columns of metadata as... well, just columns... as they are... (of course, corresponding to their respective cell bar codes in the row names)

See here, for a small tutorial: How to read a dataframe into Seurat for AddMetaData properly?

You can either add the meta data after creating your Seurat object. Or just do it all-in-one-go when you are creating your Seurat object. The tutorial above shows commands and screenshots for both ways.

ADD REPLY
3
Entering edit mode
4.2 years ago

Hope I got your question correctly, you can do everything to cell information via Ident function. Follow the manual to change the Idents of your cells. By this, you can replace the active.ident with what you want.

ADD COMMENT
1
Entering edit mode

Gosh, thank you Hamid! I am going to try this soon!

ADD REPLY

Login before adding your answer.

Traffic: 1877 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