This is a test version of Biostars. For the public version, visit https://www.biostars.org.
What is the best way to process a metadata file that SEURAT can read?

Hello, I have been working with the Seurat pipeline. I want to know what the best way to process a metadata file is. I have been using AddMetaData() from Seurat, but I don't know how to transform my metadata file such that all the cell barcodes are represented, as required by the function. I have attached partial images of alldata and metadata objects.

alldata_m<-AddMetaData(alldata, metadata)

head(alldata)

metadata

Any help would be appreciated, Thanks

metadata scrnaseq seurat

1 answer

The metadata is saved in the meta.data slot at it is just a table, meaning that you can treat it like a normal dataframe.

# create the SampleID column by splitting the rownames 
seurat@meta.data$SampleID <- gsub("(^.+)_.*$","\\1", rownames(seurat@meta.data)
# or seurat@meta.data$SampleID <- seurat@meta.data$orig.ident

# add the other columns by matching the values from the other table
exp_meta <- the other dataframe

seurat@meta.data$patient <- sapply(seurat@meta.data$SampleID, function(ita) exp_meta[match(ita,exp_meta$SampleID),"Patient"])
seurat@meta.data$Type <- sapply(seurat@meta.data$SampleID, function(ita) exp_meta[match(ita,exp_meta$SampleID),"Type"])

Alternatively you can use merge.data.frame or any other function that join (left/right) the 2 table together.

If you want to use the AddMetaData wrapper

metadata <- seurat@meta.data
# add all the columns to `metadata`
...
seurat <- AddMetaData(seurat,metadata = metadata)

Log in to answer this question.