Although Seurat objects are indeed made up of slots such as meta.data
it is generally not recommended to poke around with slots unless there is no alternative. This is because the structure of Seurat objects might evolve with future versions of Seurat, making code that relies on accessing slots directly more likely to break.
Instead, wherever possible, using "accesor functions" such as GetAssayData()
or -- in this case -- AddMetaData()
: https://rdrr.io/cran/SeuratObject/man/AddMetaData.html
In order to get your meta data table into the Seurat object, you will need to reshape into something that AddMetaData
can work with.
- Ideally, make sure that the table is a data frame
- Make sure that the data frame has rownames
- Make sure that the rownames match the cell (or spot) barcodes
You haven't said what format your "table" is in, so I'll leave 1) up to you. That should be the easy bit. For CSV/TSV files I like readr::read_csv()
etc but there are alternatives.
It is probably easier to address 3) before 2). You can use gsub
as suggested above but I find stringr::str_replace()
a bit more intuitive and it pairs well with dplyr::mutate()
.
Once your barcode column matches the barcodes in your Seurat object (after deleting "E10_5_") you can use tibble::column_to_rownames()
to convert this column in your table to rownames.
This is also an opportunity to drop columns from your table (using, for example, dplyr::select()
), or to add computed columns (dplyr::mutate()
). Wrangling data frames is a bit easier than wrangling Seurat objects, so whip your meta data into shape before adding it
Now you are ready to use Seurat::AddMetaData()
. Since you are adding a data frame, you don't need the col.name
option. (That is only required for atomic metadata, such as vectors or unnamed lists.)
Hm; it says the object is not subsettable when trying to derive df from the match function
df
is supposed to be the object (data frame) that contains the cellID and the cellType info. So the above workaround is expectingdf
to be a data frame with two columns at least. What info you get if you doclass(df)
. If it says its a data frame then it should be subsettable.So I got it to work, but now it's returning the error after entering: my.seuObj@meta.data$new.CellType <- df$CellType
"Error in
$<-.data.frame
(*tmp*
, new.CellType, value = c("Pericytes", : replacement has 5761 rows, data has 5332"??That probably means that the number of cells in your seurat object is not equal to the no. of rows in the dataframe with the new cell type labels (
df
). Check -In case the seurat obj. has lesser number of cells, then you could subset out the
df
before using it to append values into the meta data slot.Hopefully, the possible solutions till now have made it clear how you can tinker with the meta data slot (of the seurat object). General issues regarding R dataframe/ object manipulation are better asked on StackOverflow.
Still the same error a couple hours later with the same issue in the data frame..
One final suggestion would be to try the specialised function in Seurat to add columns to meta data: here There are a couple of BioStars post around this. This one has 'accepted answer' posts. In my experience, I was able to make work both the
AddMetaData
, as well as generic R functions to modify the metaData slot. In any case, before appending columns fromdf
always double check that the order of the cell barcodes is identical.