R Columns to Seurat Object
2
0
Entering edit mode
22 months ago

I currently have a table containing Cell IDs and the second column containing Celltype info; I need to (1) edit the table to where "E10_5_AGCGAGCC..." becomes "AGCGAGCC...". Essentially I need to remove the string "E10_5_" from all rows for the first column, then (2) transfer these two columns to a Seurat object; where the CellIDs in this Seurat object to match the CellIDs from the first column of the table and match the CellTypes accordingly.

How would I construct this code?

seurat scrna-seq • 4.1k views
ADD COMMENT
1
Entering edit mode
22 months ago
Amitm ★ 2.3k

Hi, What you want to do is to tinker with the meta data slot of the seurat object. The meta data slot can be accessed like -

head(my.seuObj@meta.data,3)
                   orig.ident nCount_RNA nFeature_RNA percent.mt
AAACCTGAGATGAGAG-1    SomeLib       2876         1917   1.634214
AAACCTGAGGAGCGTT-1    SomeLib       1697          879  10.194461
AAACCTGAGGCTCTTA-1    SomeLib       3011         1921   8.136832

You can also add columns (like any custom data) to the meta data slot like -

my.seuObj@meta.data$new.pheno.data <- some_new_values

Given "some_new_values" is of the same length as the no. of cells in the seurat object. It could be character/ numeric class. Lets suppose your dataframe (with the new data) is called df and has two columns: cellID and CellType. You can then ensure that your df has the same order of cell IDs by using match function -

df <- df[order(match(df$cellID,rownames(my.seuObj@meta.data))),]

The above assumes that the cellIDs are in the same fashion in both cases. Use gsub function (or any similar) to format the cell IDs in your df object. You could create a separate object from the seurat meta data slot and try your steps on there, if you are unsure (have complex customisations needed). Once the cells are in same order in your meta data slot and the new df, you can simply use the $ to add the new values as a column into the meta data slot as -

my.seuObj@meta.data$new.CellType <- df$CellType
ADD COMMENT
0
Entering edit mode

Hm; it says the object is not subsettable when trying to derive df from the match function

ADD REPLY
0
Entering edit mode

df is supposed to be the object (data frame) that contains the cellID and the cellType info. So the above workaround is expecting df to be a data frame with two columns at least. What info you get if you do class(df). If it says its a data frame then it should be subsettable.

ADD REPLY
0
Entering edit mode

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"??

ADD REPLY
0
Entering edit mode

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 -

dim(my.seuObj@meta.data)[1] == dim(df)[1]

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.

df.present <- df[which(df$cellID %in% rownames(my.seuObj@meta.data)),]

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.

ADD REPLY
0
Entering edit mode

Still the same error a couple hours later with the same issue in the data frame..

ADD REPLY
0
Entering edit mode

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 from df always double check that the order of the cell barcodes is identical.

ADD REPLY
0
Entering edit mode
21 months ago
John • 0

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.

  1. Ideally, make sure that the table is a data frame
  2. Make sure that the data frame has rownames
  3. 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.)

ADD COMMENT

Login before adding your answer.

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