<p>Since you haven't shared any data or code so I will show it using the example given below,</p>
<p>First you need to assign a specific cluster to the cells which are having another type like C_new.</p>
<pre><code>cells_df &lt;- data.frame(row.names=paste0(&quot;Cell&quot;, 1:10, sep = &quot;&quot;),as.vector(paste0(&quot;C&quot;,1:10,sep=&quot;&quot;)))

colnames(cells_df) &lt;- &quot;clusters&quot;


cluster_new &lt;- c(&quot;Cell6&quot;,&quot;Cell2&quot;)


cells_df<span class="math-container">$clusters &lt;- as.character(cells_df$</span>clusters)

for(i in 1:length(cluster_new)) {
j &lt;- cluster_new[i] 
cells_df[j,] &lt;- &quot;C_new&quot;
}

head(cells_df)
         clusters
 Cell1       C1
 Cell2    C_new
 Cell3       C3
 Cell4       C4
 Cell5       C5
 Cell6    C_new
</code></pre>
<p>And if you have already assigned new clusters to the cells then you can skip the above step. Now save the new cluster belongings into the seurat object using the <code>Idents</code> function</p>
<pre><code>Idents(seurat_obj) &lt;- cells_df$clusters
</code></pre>
<p>Then you can perform the differential gene expression as follows, for example if you want to find the differentially expressed genes between cluster <code>C7</code> vs <code>C_new</code> you can do it as follows,</p>
<pre><code>FindMarkers(seurat_obj,ident.1 = &quot;C7&quot;, ident.2 = &quot;C_new&quot; )
</code></pre>

CC BY-SA 4.0
2022-01-18T17:14:47.520Z
2022-01-18T17:14:47.520Z
Ammar Sabir Cheema
<p>Assuming <code>parentObj</code> is the original seurat object, and <code>tmp_subset</code> is your subset, you can</p>
<pre class="lang-r prettyprint-override"><code>new_metadata &lt;- parentObj@meta.data 
tmp_subset_annotation &lt;- tmp_subset@meta.data 

# new_metadata
           original_annotation
ATTCGGA-1   TCD4
AAATAAA-1   TCD4-naive
...

# tmp_subset_annotation
           new_cellType
ATTCGGA-1   TCD4-naive
AAATAAA-1   TCD4-naive
...

new_metadata$refined_annotation &lt;- sapply(rownames(new_metadata), 
     function(ita) ifelse(ita %in% rownames(tmp_subset_annotation),
            tmp_subset_annotation[match(ita,rownames(tmp_subset_annotation)),&quot;new_cellType&quot;],
            new_metadata[match(ita,rownames(new_metadata)),&quot;original_annotation&quot;]))
</code></pre>
<p>Basically you iterate each rownames of the original object, and assign the new annotation, if the UMI matches a UMI in the subset, otherwise you keep the original annotation.</p>

CC BY-SA 4.0
2022-01-20T00:25:46.827Z
Hi, I am getting an error saying

Error in ans[npos] <- rep(no, length.out = len)[npos] : 
  replacement has length zero
In addition: Warning message:
In rep(no, length.out = len) :
ShaniS
CC BY-SA 4.0
2022-01-20T08:31:30.307Z
You should add the first few lines of both metadata tables (e.g. `head(object@metadata)`). Without seeing the data it is difficult to help.
fra
CC BY-SA 4.0
2022-01-20T17:18:50.267Z
I added a new question with my actual code - https://bioinformatics.stackexchange.com/questions/18365/integrating-subcluster-idents-with-original-object
ShaniS
CC BY-SA 4.0
2022-01-20T20:08:04.673Z
You should edit your original question rather than create a new thread. Especially because you are asking exactly the same thing.
fra
CC BY-SA 4.0
2022-01-19T12:36:08.987Z
2023-06-13T15:09:33.190Z
2023-06-13T15:09:33.190Z
fra
<p>I have a scRNA-seq Seurat object I've analyzed, and I noticed that for some of the clusters, there's more than one cell type.</p>
<p>I've created a subset which and run FindClusters again to label the cell more efficiently, and now I want to &quot;paste&quot; the Idents I've assigned in the subcluster to the original object.</p>
<p>How do I do it?</p>

CC BY-SA 4.0
2023-09-10T02:47:12.943Z
Hi @ShaniS this question does look like it has been answered. Please consider upvoting and/or accepting an answer
M__
<scrnaseq><seurat><single-cell><clustering>
Assigning subcluster idents to original object
# Assigning subcluster idents to original object

(Asked by: username_0 on 2022-01-11)

I have a scRNA-seq Seurat object I've analyzed, and I noticed that for some of the clusters, there's more than one cell type.

I've created a subset which and run FindClusters again to label the cell more efficiently, and now I want to "paste" the Idents I've assigned in the subcluster to the original object.

How do I do it?

**Tags:** scrnaseq, seurat, single-cell, clustering
> **Comments:**
> **username_1**: Hi @ShaniS this question does look like it has been answered. Please consider upvoting and/or accepting an answer  

---

## 2 answers

### Answer by username_2 on 2022-01-18. Score: 1

Since you haven't shared any data or code so I will show it using the example given below,
First you need to assign a specific cluster to the cells which are having another type like C_new.
cells_df <- data.frame(row.names=paste0("Cell", 1:10, sep = ""),as.vector(paste0("C",1:10,sep="")))

colnames(cells_df) <- "clusters"


cluster_new <- c("Cell6","Cell2")


cells_df$clusters <- as.character(cells_df$clusters)

for(i in 1:length(cluster_new)) {
j <- cluster_new[i] 
cells_df[j,] <- "C_new"
}

head(cells_df)
         clusters
 Cell1       C1
 Cell2    C_new
 Cell3       C3
 Cell4       C4
 Cell5       C5
 Cell6    C_new

And if you have already assigned new clusters to the cells then you can skip the above step. Now save the new cluster belongings into the seurat object using the Idents function
Idents(seurat_obj) <- cells_df$clusters

Then you can perform the differential gene expression as follows, for example if you want to find the differentially expressed genes between cluster C7 vs C_new you can do it as follows,
FindMarkers(seurat_obj,ident.1 = "C7", ident.2 = "C_new" )


---

### Answer by username_3 on 2022-01-19. Score: 1

Assuming parentObj is the original seurat object, and tmp_subset is your subset, you can
new_metadata <- parentObj@meta.data 
tmp_subset_annotation <- tmp_subset@meta.data 

# new_metadata
           original_annotation
ATTCGGA-1   TCD4
AAATAAA-1   TCD4-naive
...

# tmp_subset_annotation
           new_cellType
ATTCGGA-1   TCD4-naive
AAATAAA-1   TCD4-naive
...

new_metadata$refined_annotation <- sapply(rownames(new_metadata), 
     function(ita) ifelse(ita %in% rownames(tmp_subset_annotation),
            tmp_subset_annotation[match(ita,rownames(tmp_subset_annotation)),"new_cellType"],
            new_metadata[match(ita,rownames(new_metadata)),"original_annotation"]))

Basically you iterate each rownames of the original object, and assign the new annotation, if the UMI matches a UMI in the subset, otherwise you keep the original annotation.

> **Comments:**
> **username_0**: Hi, I am getting an error saying  Error in ans[npos] <- rep(no, length.out = len)[npos] :    replacement has length zero In addition: Warning message: In rep(no, length.out = len) :  
> **username_3**: You should add the first few lines of both metadata tables (e.g. `head(object@metadata)`). Without seeing the data it is difficult to help.  
> **username_0**: I added a new question with my actual code - https://bioinformatics.stackexchange.com/questions/18365/integrating-subcluster-idents-with-original-object  
> **username_3**: You should edit your original question rather than create a new thread. Especially because you are asking exactly the same thing.  

---

