R Columns to Seurat Object
1
0
Entering edit mode
13 days 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 • 424 views
ADD COMMENT
0
Entering edit mode
13 days ago
Amitm ★ 2.1k

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

Login before adding your answer.

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