Wow, this is incredibly helpful. I will keep this bookmarked, and attempt this method soon! Thank you Thank you swbarnes2!
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
2 answers
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.
You can also just treat seurat_obj@meta.data as a standard data frame and modify it using generic functions.
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!
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.
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.
I understand, I would, but it didn't work for me : (
EDIT: I think I'll report it as a bug?
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
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?
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?
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?
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.
Log in to answer this question.