Hello,
I'm using seurat to perform a single cell analysis and am interested in exporting the normalized and/or scaled expression data for all cells within each of my clusters.
I found this code here on Github which works to export all of the cells as a csv (results in a 1.4GB file) but does not include any metadata:
library(data.table)
data_to_write_out <- as.data.frame(as.matrix([email protected]))
fwrite(x = data_to_write_out, row.names = TRUE, file = "outfile.csv")
I'm quite new to R (anything I've learned about coding I've learned in the past 3 days), so any help with exporting the normalized data by cluster.id (or other metadata parameter) would be greatly appreciated.
Thanks
Robert
Hi Robert,
The metadata is stored in [email protected]. Assuming you want one file per cluster (or other metadata group), you could do something along the lines of:
meta.data.groups <- unique(x = [email protected]$PARAMETER)
for(group in meta.data.groups) {
group.cells <- WhichCells(object = object, subset.name = "PARAMETER" , accept.value = group)
data_to_write_out <- as.data.frame(x = as.matrix(x = [email protected][, group.cells]))
fwrite(x = data_to_write_out, row.names = TRUE, file = paste0(group, "_outfile.csv"))
}
Where PARAMETER is the name of the metadata column containing the cluster ids or another grouping and object is your Seurat object.
Hi Andrew,
Thanks so much for the code. I had some errors in my meta.data file that I had to fix, and I couldn't figure out the fwrite function, but I got this code working perfectly for writing out the raw data and the scaled data:
meta.data.cluster <- unique(x = [email protected]$annotation)
for(group in meta.data.cluster) {
group.cells <- WhichCells(object = object, subset.name = "annotation" , accept.value = group)
data_to_write_out <- as.data.frame(x = as.matrix(x = [email protected][, group.cells]))
write.csv(x = data_to_write_out, row.names = TRUE, file = paste0(save_dir, "/", group, "_Cluster_outfile.csv"))
}
Best,
Robert
Most helpful comment
Hi Robert,
The metadata is stored in
[email protected]. Assuming you want one file per cluster (or other metadata group), you could do something along the lines of:Where
PARAMETERis the name of the metadata column containing the cluster ids or another grouping andobjectis your Seurat object.