KeTuTu's picture
Upload 46 files
2999286 verified
#!/usr/bin/env python
# coding: utf-8
# # Differential expression analysis in single cell
#
# Sometimes we need to compare differentially expressed genes or differentially expressed features between two cell types on single cell data, but existing methods focus more on cell-specific gene analysis. Researchers need to transfer bulk RNA-seq analysis to single-cell analysis, which involves interaction between different programming languages or programming tools, adding significantly to the workload of the researcher.
#
# Here, we use omicverse's bulk RNA-seq pyDEG method to complete differential expression analysis at the single cell level. We will present two different perspectives, one from the perspective of all cells and one from the perspective of the metacellular.
#
# Colab_Reproducibility:https://colab.research.google.com/drive/12faBRh0xT7v6KSy8NCSRqbegF_AEoDXr?usp=sharing
# In[1]:
import omicverse as ov
import scanpy as sc
import scvelo as scv
ov.utils.ov_plot_set()
# ## Data preprocessed
#
# We need to normalized and scale the data at first.
# In[2]:
adata = scv.datasets.pancreas()
adata
# In[3]:
adata.X.max()
# We found that the max value of anndata object larger than 10 and type is int. We need to normalize and log1p it
# In[4]:
#quantity control
adata=ov.pp.qc(adata,
tresh={'mito_perc': 0.05, 'nUMIs': 500, 'detected_genes': 250})
#normalize and high variable genes (HVGs) calculated
adata=ov.pp.preprocess(adata,mode='shiftlog|pearson',n_HVGs=2000,)
#save the whole genes and filter the non-HVGs
adata.raw = adata
adata = adata[:, adata.var.highly_variable_features]
#scale the adata.X
ov.pp.scale(adata)
#Dimensionality Reduction
ov.pp.pca(adata,layer='scaled',n_pcs=50)
# In[5]:
adata.X.max()
# ## Different expression in total level
#
# We then select the target cells to be analysed, including `Alpha` and `Beta`, derive the expression matrix using `to_df()` and build the differential expression analysis module using `pyDEG`
# In[6]:
test_adata=adata[adata.obs['clusters'].isin(['Alpha','Beta'])]
test_adata
# In[7]:
dds=ov.bulk.pyDEG(test_adata.to_df(layer='lognorm').T)
# In[8]:
dds.drop_duplicates_index()
print('... drop_duplicates_index success')
# We also need to set up an experimental group and a control group, i.e. the two types of cells to be compared and analysed
# In[9]:
treatment_groups=test_adata.obs[test_adata.obs['clusters']=='Alpha'].index.tolist()
control_groups=test_adata.obs[test_adata.obs['clusters']=='Beta'].index.tolist()
result=dds.deg_analysis(treatment_groups,control_groups,method='ttest')
# In[10]:
result.sort_values('qvalue').head()
# In[11]:
# -1 means automatically calculates
dds.foldchange_set(fc_threshold=-1,
pval_threshold=0.05,
logp_max=10)
# In[12]:
dds.plot_volcano(title='DEG Analysis',figsize=(4,4),
plot_genes_num=8,plot_genes_fontsize=12,)
# In[13]:
dds.plot_boxplot(genes=['Irx1','Adra2a'],treatment_groups=treatment_groups,
control_groups=control_groups,figsize=(2,3),fontsize=12,
legend_bbox=(2,0.55))
# In[14]:
ov.utils.embedding(adata,
basis='X_umap',
frameon='small',
color=['clusters','Irx1','Adra2a'])
# ## Different expression in Metacells level
#
# Here, we calculated the metacells from the whole scRNA-seq datasets using SEACells, and the same analyze with total level.
# ### Constructing a metacellular object
#
# We can use `ov.single.MetaCell` to construct a metacellular object to train the SEACells model, the arguments can be found in below.
#
# - :param ad: (AnnData) annotated data matrix
# - :param build_kernel_on: (str) key corresponding to matrix in ad.obsm which is used to compute kernel for metacells
# Typically 'X_pca' for scRNA or 'X_svd' for scATAC
# - :param n_SEACells: (int) number of SEACells to compute
# - :param use_gpu: (bool) whether to use GPU for computation
# - :param verbose: (bool) whether to suppress verbose program logging
# - :param n_waypoint_eigs: (int) number of eigenvectors to use for waypoint initialization
# - :param n_neighbors: (int) number of nearest neighbors to use for graph construction
# - :param convergence_epsilon: (float) convergence threshold for Franke-Wolfe algorithm
# - :param l2_penalty: (float) L2 penalty for Franke-Wolfe algorithm
# - :param max_franke_wolfe_iters: (int) maximum number of iterations for Franke-Wolfe algorithm
# - :param use_sparse: (bool) whether to use sparse matrix operations. Currently only supported for CPU implementation.
# In[15]:
meta_obj=ov.single.MetaCell(adata,use_rep='scaled|original|X_pca',n_metacells=150,
use_gpu=True)
# In[16]:
meta_obj.initialize_archetypes()
# ## Train and save the model
# In[17]:
meta_obj.train(min_iter=10, max_iter=50)
# In[34]:
meta_obj.save('seacells/model.pkl')
# In[ ]:
meta_obj.load('seacells/model.pkl')
# ## Predicted the metacells
#
# we can use `predicted` to predicted the metacells of raw scRNA-seq data. There are two method can be selected, one is `soft`, the other is `hard`.
#
# In the `soft` method, Aggregates cells within each SEACell, summing over all raw data x assignment weight for all cells belonging to a SEACell. Data is un-normalized and pseudo-raw aggregated counts are stored in .layers['raw']. Attributes associated with variables (.var) are copied over, but relevant per SEACell attributes must be manually copied, since certain attributes may need to be summed, or averaged etc, depending on the attribute.
#
# In the `hard` method, Aggregates cells within each SEACell, summing over all raw data for all cells belonging to a SEACell. Data is unnormalized and raw aggregated counts are stored .layers['raw']. Attributes associated with variables (.var) are copied over, but relevant per SEACell attributes must be manually copied, since certain attributes may need to be summed, or averaged etc, depending on the attribute.
# In[19]:
ad=meta_obj.predicted(method='soft',celltype_label='clusters',
summarize_layer='lognorm')
# In[20]:
ad.X.min(),ad.X.max()
# In[21]:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(4,4))
ov.utils.embedding(
meta_obj.adata,
basis="X_umap",
color=['clusters'],
frameon='small',
title="Meta cells",
#legend_loc='on data',
legend_fontsize=14,
legend_fontoutline=2,
size=10,
ax=ax,
alpha=0.2,
#legend_loc='',
add_outline=False,
#add_outline=True,
outline_color='black',
outline_width=1,
show=False,
#palette=ov.utils.blue_color[:],
#legend_fontweight='normal'
)
ov.single._metacell.plot_metacells(ax,meta_obj.adata,color='#CB3E35',
)
# ### Differentially expressed analysis
#
# Similar to total cells for differential expression analysis, we used metacells for differential expression in the same way.
# In[23]:
test_adata=ad[ad.obs['celltype'].isin(['Alpha','Beta'])]
test_adata
# In[24]:
dds_meta=ov.bulk.pyDEG(test_adata.to_df().T)
# In[25]:
dds_meta.drop_duplicates_index()
print('... drop_duplicates_index success')
# We also need to set up an experimental group and a control group, i.e. the two types of cells to be compared and analysed
# In[27]:
treatment_groups=test_adata.obs[test_adata.obs['celltype']=='Alpha'].index.tolist()
control_groups=test_adata.obs[test_adata.obs['celltype']=='Beta'].index.tolist()
result=dds_meta.deg_analysis(treatment_groups,control_groups,method='ttest')
# In[28]:
result.sort_values('qvalue').head()
# In[29]:
# -1 means automatically calculates
dds_meta.foldchange_set(fc_threshold=-1,
pval_threshold=0.05,
logp_max=10)
# In[30]:
dds_meta.plot_volcano(title='DEG Analysis',figsize=(4,4),
plot_genes_num=8,plot_genes_fontsize=12,)
# In[31]:
dds_meta.plot_boxplot(genes=['Ctxn2','Mnx1'],treatment_groups=treatment_groups,
control_groups=control_groups,figsize=(2,3),fontsize=12,
legend_bbox=(2,0.55))
# In[32]:
ov.utils.embedding(adata,
basis='X_umap',
frameon='small',
color=['clusters','Ctxn2','Mnx1'])