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
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.