# Differentially expressed genes between two Leiden clusters

**URL:** <https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193>\
**Category:** scvi-tools\
**Tags:** diff-exp, totalvi\
**Created:** [October 7, 2021, 9:31pm UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193 "2021-10-07T21:31:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![taopeng1100](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/taopeng1100/32/84_2.png) [@taopeng1100](https://discourse.scverse.org/u/taopeng1100)\
**Post date:** [October 7, 2021, 9:31pm UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193/1 "2021-10-07T21:31:37Z")

</div>

Hi I wonder how I can perform a test of differentially expressed genes between two Leiden clusters in scvi totalVI. I can do this in Scanpy using rank\_genes\_groups. Thanks!

---

<div class="post-metadata">

**Author:** ![valehvpa](https://avatars.discourse-cdn.com/v4/letter/v/8c91f0/32.png) [@valehvpa](https://discourse.scverse.org/u/valehvpa)\
**Post date:** [October 7, 2021, 11:19pm UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193/2 "2021-10-07T23:19:55Z")

</div>

Hi taopeng1100. Thanks for reaching out. We have a tutorial that explains how to perform DE with the TOTALVI model. You can find it here: [CITE-seq analysis with totalVI — scvi-tools](https://docs.scvi-tools.org/en/stable/tutorials/notebooks/totalVI.html#Differential-expression). It is similar to scanpy’s DE API.  
Let us know if that doesn’t help.

---

<div class="post-metadata">

**Author:** ![taopeng1100](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/taopeng1100/32/84_2.png) [@taopeng1100](https://discourse.scverse.org/u/taopeng1100)\
**Post date:** [October 8, 2021, 4:47am UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193/3 "2021-10-08T04:47:15Z")

</div>

The link leads to DEG for one versus all other clusters. How can I do one cluster versus another one for DEG? Thx!

---

<div class="post-metadata">

**Author:** ![taopeng1100](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/taopeng1100/32/84_2.png) [@taopeng1100](https://discourse.scverse.org/u/taopeng1100)\
**Post date:** [October 8, 2021, 5:19pm UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193/4 "2021-10-08T17:19:41Z")

</div>

Something is not exactly related to DEG here. When I save my analysis results in h5ad and read it into Jupyter notebook, the vae file is NOT available? vae generation is very time consuming, how can I save vae results so I can load it again next time for analysis?  
Thx!  
Tao

---

<div class="post-metadata">

**Author:** ![valehvpa](https://avatars.discourse-cdn.com/v4/letter/v/8c91f0/32.png) [@valehvpa](https://discourse.scverse.org/u/valehvpa)\
**Post date:** [October 8, 2021, 7:05pm UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193/5 "2021-10-08T19:05:58Z")

</div>

You can use the `groupby` parameter the `differential_expression` method, by passing the set of the two clusters you want to compare to each other.

```auto
groupby
    The key of the observations grouping to consider.
group1
    Subset of groups, e.g. [`'g1'`, `'g2'`, `'g3'`], to which comparison
    shall be restricted, or all groups in `groupby` (default).
group2
    If `None`, compare each group in `group1` to the union of the rest of the groups
    in `groupby`. If a group identifier, compare with respect to this group.

```

---

<div class="post-metadata">

**Author:** ![valehvpa](https://avatars.discourse-cdn.com/v4/letter/v/8c91f0/32.png) [@valehvpa](https://discourse.scverse.org/u/valehvpa)\
**Post date:** [October 8, 2021, 7:08pm UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193/6 "2021-10-08T19:08:38Z")

</div>

We have utility functions to [save](https://docs.scvi-tools.org/en/stable/api/reference/scvi.model.base.BaseModelClass.save.html#scvi.model.base.BaseModelClass.save) and [load](https://docs.scvi-tools.org/en/stable/api/reference/scvi.model.base.BaseModelClass.load.html#scvi.model.base.BaseModelClass.load) the model, if that is what you are wanting to do.

---

<div class="post-metadata">

**Author:** ![taopeng1100](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/taopeng1100/32/84_2.png) [@taopeng1100](https://discourse.scverse.org/u/taopeng1100)\
**Post date:** [October 9, 2021, 3:56am UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193/7 "2021-10-09T03:56:09Z")

</div>

Thank you so much for you prompt reply!  
Can I ask you this question?  
For this step: sc.pp.highly\_variable\_genes, the tutorial is like this:  
n\_top\_genes=4000,  
flavor=“seurat\_v3”,  
I used to do like this:  
sc.pp.highly\_variable\_genes(adata, min\_mean=0.0125, max\_mean=3, min\_disp=0.5)

What are the best practice to select highly\_variable\_genes?

I appreciate your insights!

Tao

---

<div class="post-metadata">

**Author:** ![valehvpa](https://avatars.discourse-cdn.com/v4/letter/v/8c91f0/32.png) [@valehvpa](https://discourse.scverse.org/u/valehvpa)\
**Post date:** [October 10, 2021, 5:35pm UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193/8 "2021-10-10T17:35:25Z")

</div>

There are some helpful resources on scanpy’s discourse (such as [this](https://scanpy.discourse.group/t/highly-variable-genes-best-practice/29) post), although they do not address your question specifically. @adamgayoso is likely to have good insights on this front so I delegate to him.

---

<div class="post-metadata">

**Author:** ![munfred](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/munfred/32/9_2.png) [@munfred](https://discourse.scverse.org/u/munfred)\
**Post date:** [October 15, 2021, 6:11pm UTC](https://discourse.scverse.org/t/differentially-expressed-genes-between-two-leiden-clusters/193/9 "2021-10-15T18:11:11Z")

</div>

A good discussion on gene selection is this article on M3Drop: [https://academic.oup.com/bioinformatics/article/35/16/2865/5258099](https://academic.oup.com/bioinformatics/article/35/16/2865/5258099)

Original repository of M3Drop: [GitHub - tallulandrews/M3Drop](https://github.com/tallulandrews/M3Drop)

If interested in using their method it is implemented in scvi with the `poisson_gene_selection` function: [scvi-tools/\_preprocessing.py at master · YosefLab/scvi-tools · GitHub](https://github.com/YosefLab/scvi-tools/blob/master/scvi/data/_preprocessing.py)
