How to append annotations from sub-clustering objects?

Hi All,

I have adata object from which I generated several sub-objects with some of the clusters in order to refine the cluster labeling (up to 2-3 levels).

Now I wish to add the cell annotations from the sub-clustering to the ‘main’ adata, how is this done in scanpy?

Thanks,
Roy

The cell labels are saved in .obs, which is a Pandas data frame. You can’t edit a column in a Pandas dataframe, so I’d suggest making a new column. Let’s say you subclustered cluster 4:

sub_obj = adata[adata.obs["leiden"] == "4"]
sc.tl.leiden(sub_obj)
sub_obj.obs["leiden"] = "4_" + sub_obj.obs["leiden"].astype(str)
# have to convert leiden from categorical to string
adata.obs["leiden"] = adata.obs["leiden"].astype(str)
adata.obs["leiden"].update(sub_obj.obs["leiden"])