Seurat: Exporting Normalized Data by Cluster

Created on 3 Feb 2019  路  2Comments  路  Source: satijalab/seurat

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

Analysis Question

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:

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.

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings