<p>I'm new to single cell sequencing analysis. I have made different clusters using seurat. Now I would like to extract the raw gene counts based on the cluster generated by seurat.
i have provided my R code used in the analysis.</p>
<pre><code>library(dplyr)
library(Seurat)
library(patchwork)
ftp=&quot;https://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201254/suppl/GSE201254_count_mat_Control-NoCrush.csv.gz&quot;
counts=data.table::fread(ftp)
C_nocrush=counts %&gt;%
select(contains(c(&quot;V1&quot;,&quot;CtCReRGCS&quot;)))
rownames(C_nocrush)=C_nocrush$V1
C_nocrush$V1=NULL
CNC=CreateSeuratObject(counts = C_nocrush,project = &quot;He RGC Data&quot;, min.cells = 3, min.features = 200)
CNC[[&quot;percent.mt&quot;]] = PercentageFeatureSet(CNC, pattern = &quot;^MT-&quot;)
VlnPlot(CNC, features = c(&quot;nFeature_RNA&quot;, &quot;nCount_RNA&quot;,&quot;percent.mt&quot;), ncol = 3)
plot1 &lt;- FeatureScatter(CNC, feature1 = &quot;nCount_RNA&quot;, feature2 = &quot;percent.mt&quot;)
plot2 &lt;- FeatureScatter(CNC, feature1 = &quot;nCount_RNA&quot;, feature2 = &quot;nFeature_RNA&quot;)
plot1+plot2
CNC = subset(CNC, subset = nFeature_RNA &gt; 200 &amp; nFeature_RNA &lt; 2500 &amp; percent.mt &lt; 5)
CNC = NormalizeData(CNC, normalization.method = &quot;LogNormalize&quot;, scale.factor = 10000)
CNC = FindVariableFeatures(CNC, selection.method = &quot;vst&quot;, nfeatures = 2000)
top10 &lt;- head(VariableFeatures(CNC), 10)
plot1 &lt;- VariableFeaturePlot(CNC)
plot2 &lt;- LabelPoints(plot = plot1, points = top10, repel = TRUE)
plot1+plot2
all.genes &lt;- rownames(CNC)
CNC &lt;- ScaleData(CNC, features = all.genes)
CNC &lt;- RunPCA(CNC, features = VariableFeatures(object = CNC))
VizDimLoadings(CNC, dims = 1:2, reduction = &quot;pca&quot;)
DimPlot(CNC, reduction = &quot;pca&quot;)
DimHeatmap(CNC, dims = 1:15, cells = 500, balanced = TRUE)
CNC &lt;- JackStraw(CNC, num.replicate = 100)
CNC &lt;- ScoreJackStraw(CNC, dims = 1:20)
JackStrawPlot(CNC, dims = 1:15)
ElbowPlot(CNC)
CNC &lt;- FindNeighbors(CNC, dims = 1:10)
CNC &lt;- FindClusters(CNC, resolution = 0.5)
head(Idents(CNC), 5)
CNC &lt;- RunUMAP(CNC, dims = 1:10)
DimPlot(CNC, reduction = &quot;umap&quot;)
saveRDS(CNC, file = &quot;Desktop/Single_cell_RNA/CNC.rds&quot;)
cluster2.markers &lt;- FindMarkers(CNC, ident.1 = 2, min.pct = 0.25)
head(cluster2.markers, n = 5)
new.cluster.ids=c(&quot;T5/F RGCs&quot;, &quot;T5/F RGCs&quot;,&quot;T-RGCs&quot;,&quot;Other RGcs and cartpt RGCs&quot;,&quot;T5/F RGCs&quot;,&quot;IP-RGCs&quot;,&quot;Other RGcs and cartpt RGCs&quot;,&quot;N-RGCs&quot;,&quot;Alpha-RGCs&quot; )
names(new.cluster.ids)=levels(CNC)
CNC=RenameIdents(CNC,new.cluster.ids)
 DimPlot(CNC,reduction = &quot;umap&quot;, label = T, pt.size = 0.5)+NoLegend()
</code></pre>
<p>Tried using this command to extract the raw counts based on the cluster but is not working for me.</p>
<pre><code>meta.data.cluster=unique(x=CNC@meta.data$active.ident)
meta.data.cluster=unique(Idents(CNC))
for(group in meta.data.cluster) {
    group.cells &lt;- WhichCells(object = CNC, idents = group)
    data_to_write_out &lt;- as.data.frame(GetAssayData(CNC,slot = 'counts')[,group.cells])
    write.csv(data_to_write_out, row.names = TRUE, file = &quot;1_cluster_outfile.csv&quot;)
}
</code></pre>

CC BY-SA 4.0
2022-10-06T11:14:49.633Z
Hi, thank you for supplying detailed code. Could you kindly explain the precise question? I assume the code is throwing an error or not working how it was anticipated. Describing this would be great.
M__
CC BY-SA 4.0
2022-10-06T12:47:26.350Z
I have updated my script. I want to extract the raw counts of the genes using the selected cluster.
Manojkumar K
CC BY-SA 4.0
2022-10-07T18:27:32.897Z
How do you know that the count extraction code you wrote is not working?
gringer
<scrnaseq><seurat><single-cell>
extracting raw gene counts based on the cluster generated by seurat
# extracting raw gene counts based on the cluster generated by seurat

(Asked by: username_0 on 2022-10-06)

I'm new to single cell sequencing analysis. I have made different clusters using seurat. Now I would like to extract the raw gene counts based on the cluster generated by seurat.
i have provided my R code used in the analysis.
library(dplyr)
library(Seurat)
library(patchwork)
ftp="https://ftp.ncbi.nlm.nih.gov/geo/series/GSE201nnn/GSE201254/suppl/GSE201254_count_mat_Control-NoCrush.csv.gz"
counts=data.table::fread(ftp)
C_nocrush=counts %>%
select(contains(c("V1","CtCReRGCS")))
rownames(C_nocrush)=C_nocrush$V1
C_nocrush$V1=NULL
CNC=CreateSeuratObject(counts = C_nocrush,project = "He RGC Data", min.cells = 3, min.features = 200)
CNC[["percent.mt"]] = PercentageFeatureSet(CNC, pattern = "^MT-")
VlnPlot(CNC, features = c("nFeature_RNA", "nCount_RNA","percent.mt"), ncol = 3)
plot1 <- FeatureScatter(CNC, feature1 = "nCount_RNA", feature2 = "percent.mt")
plot2 <- FeatureScatter(CNC, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
plot1+plot2
CNC = subset(CNC, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
CNC = NormalizeData(CNC, normalization.method = "LogNormalize", scale.factor = 10000)
CNC = FindVariableFeatures(CNC, selection.method = "vst", nfeatures = 2000)
top10 <- head(VariableFeatures(CNC), 10)
plot1 <- VariableFeaturePlot(CNC)
plot2 <- LabelPoints(plot = plot1, points = top10, repel = TRUE)
plot1+plot2
all.genes <- rownames(CNC)
CNC <- ScaleData(CNC, features = all.genes)
CNC <- RunPCA(CNC, features = VariableFeatures(object = CNC))
VizDimLoadings(CNC, dims = 1:2, reduction = "pca")
DimPlot(CNC, reduction = "pca")
DimHeatmap(CNC, dims = 1:15, cells = 500, balanced = TRUE)
CNC <- JackStraw(CNC, num.replicate = 100)
CNC <- ScoreJackStraw(CNC, dims = 1:20)
JackStrawPlot(CNC, dims = 1:15)
ElbowPlot(CNC)
CNC <- FindNeighbors(CNC, dims = 1:10)
CNC <- FindClusters(CNC, resolution = 0.5)
head(Idents(CNC), 5)
CNC <- RunUMAP(CNC, dims = 1:10)
DimPlot(CNC, reduction = "umap")
saveRDS(CNC, file = "Desktop/Single_cell_RNA/CNC.rds")
cluster2.markers <- FindMarkers(CNC, ident.1 = 2, min.pct = 0.25)
head(cluster2.markers, n = 5)
new.cluster.ids=c("T5/F RGCs", "T5/F RGCs","T-RGCs","Other RGcs and cartpt RGCs","T5/F RGCs","IP-RGCs","Other RGcs and cartpt RGCs","N-RGCs","Alpha-RGCs" )
names(new.cluster.ids)=levels(CNC)
CNC=RenameIdents(CNC,new.cluster.ids)
 DimPlot(CNC,reduction = "umap", label = T, pt.size = 0.5)+NoLegend()

Tried using this command to extract the raw counts based on the cluster but is not working for me.
meta.data.cluster=unique(x=CNC@meta.data$active.ident)
meta.data.cluster=unique(Idents(CNC))
for(group in meta.data.cluster) {
    group.cells <- WhichCells(object = CNC, idents = group)
    data_to_write_out <- as.data.frame(GetAssayData(CNC,slot = 'counts')[,group.cells])
    write.csv(data_to_write_out, row.names = TRUE, file = "1_cluster_outfile.csv")
}

**Tags:** scrnaseq, seurat, single-cell
> **Comments:**
> **username_1**: Hi, thank you for supplying detailed code. Could you kindly explain the precise question? I assume the code is throwing an error or not working how it was anticipated. Describing this would be great.  
> **username_0**: I have updated my script. I want to extract the raw counts of the genes using the selected cluster.  
> **username_2**: How do you know that the count extraction code you wrote is not working?  

---

## 0 answers

