2 回答
TA贡献1796条经验 获得超4个赞
如果您的 adata.obs 在运行后有一个我期望的“louvain”列tl.louvain
,您可以进行子集化,就好像 adata[adata.obs["louvain"] == "2"]
您想要获取一个集群并 adata[adata.obs['louvain'].isin(['2', '4'])]
获取集群 2 和 4 一样。
TA贡献1883条经验 获得超3个赞
请随意使用我为我的工作编写的这个函数。
import AnnData
import numpy as np
def cluster_sampled(adata: AnnData, clusters: list, n_samples: int) -> AnnData:
"""Sample n_samples randomly from each louvain cluster from the provided clusters
Parameters
----------
adata
AnnData object
clusters
List of clusters to sample from
n_samples
Number of samples to take from each cluster
Returns
-------
AnnData
Annotated data matrix with sampled cells from the clusters
"""
l = []
adata_cluster_sampled = adata[adata.obs["louvain"].isin(clusters), :].copy()
for k, v in adata_cluster_sampled.obs.groupby("louvain").indices.items():
l.append(np.random.choice(v, n_samples, replace=False))
return adata_cluster_sampled[np.concatenate(l)]
添加回答
举报