# alpha diversity analysis in QIIME2 vs Jupyter Notebook (python)

**URL:** <https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909>\
**Category:** User Support\
**Created:** [June 28, 2023, 4:28am UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909 "2023-06-28T04:28:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![malenaamer](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@malenaamer](https://forum.qiime2.org/u/malenaamer)\
**Post date:** [June 28, 2023, 4:28am UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/1 "2023-06-28T04:28:41Z")

</div>

Hello,

I'm trying to make a table of the Kruskal-Wallis results for 35 different variables. It would take a long time to copy and paste each value from the .qzv/view files. So I created a script in python using the tsv files produced in QIIME2 of Shannon, FaithPD, Evenness and ObservedOTUs metrics.

However, I noticed they don't produce the exact same results. Could you help me explain why? Are there any nuances I should take into account?

I'm using these commands in python:

```auto
import pandas as pd
from scipy.stats import kruskal
        # Perform Kruskal-Wallis test
        H, p = kruskal(category1_values, category2_values)

```

![Captura de Pantalla 2023-06-27 a la(s) 21.21.14](https://forum-qiime2-org.s3.dualstack.us-west-2.amazonaws.com/optimized/3X/5/d/5db7196fd0c9c916079ca7378009b8f444d1675f_2_690x69.png)

And I'm using this command in QIIME2:

```auto
!qiime diversity alpha-group-significance \
  --i-alpha-diversity faith_pd_vector.qza \
  --m-metadata-file sample-metadata-v2.tsv \
  --o-visualization faith-pd-group-significance_230625.qzv

```

 ![Captura de Pantalla 2023-06-27 a la(s) 21.20.20](https://forum-qiime2-org.s3.dualstack.us-west-2.amazonaws.com/original/3X/5/c/5cad0587441f0b34e266a90f6df7f1a9765410e5.png)

I appreciate your help!  
Malena

---

<div class="post-metadata">

**Author:** ![gregcaporaso](https://forum.qiime2.org/user_avatar/forum.qiime2.org/gregcaporaso/32/17769_2.png) [@gregcaporaso](https://forum.qiime2.org/u/gregcaporaso)\
**Post date:** [June 28, 2023, 1:19pm UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/2 "2023-06-28T13:19:03Z")

</div>



---

<div class="post-metadata">

**Author:** ![gregcaporaso](https://forum.qiime2.org/user_avatar/forum.qiime2.org/gregcaporaso/32/17769_2.png) [@gregcaporaso](https://forum.qiime2.org/u/gregcaporaso)\
**Post date:** [June 28, 2023, 2:00pm UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/3 "2023-06-28T14:00:22Z")

</div>

Hi @malenaamer, You should be getting the same result. I suspect there may be a difference in how the lists of values being compared are created. Could you provide more context on how you're computing your `category1_values` and `category2_values`? Ideally you could share code showing this starting from the same artifact that you're providing to `alpha-group-significance`.

If you haven't used QIIME 2 artifacts from within Python before, you can load your Faith PD vector and sample metadata into pandas objects (a pd.Series and pd.DataFrame, respectively) as follows:

```python
In [1]: import qiime2

In [3]: import pandas as pd

In [4]: faith_pd = qiime2.Artifact.load('./faith_pd_vector.qza').view(pd.Series)

In [6]: sample_metadata = qiime2.Metadata.load('./sample-metadata.tsv').to_dataframe()

```

---

<div class="post-metadata">

**Author:** ![malenaamer](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@malenaamer](https://forum.qiime2.org/u/malenaamer)\
**Post date:** [June 29, 2023, 11:20pm UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/4 "2023-06-29T23:20:34Z")

</div>

Thank you so much for the quick response, Greg!

I'm glad I asked the question because I definitely had a mistake in the list preparation as you suggested, I was actually skipping a row:

```auto
import pandas as pd
from scipy.stats import kruskal

# Read the Excel
#MISTAKE! -->data = pd.read_excel('Manually_made_file.xls', sheet_name='AlphaGraphs', header=0, skiprows=[1])
data = pd.read_excel('Manually_made_file.xls', sheet_name='AlphaGraphs', header=0)
columns_to_analyze = data.columns[7:73].tolist()
print(columns_to_analyze)
alpha_diversity_outcomes = ['Shannon', 'Observed_OTUs', 'FaithPD', 'Evenness']

# Kruskal-Wallis
results = []
for variable in columns_to_analyze:
    for outcome in alpha_diversity_outcomes:
        category1_values = data.loc[data[variable] == 0, outcome]
        category2_values = data.loc[data[variable] == 1, outcome]
        #print(category1_values)
        #print(category2_values)
        
        H, p = kruskal(category1_values, category2_values)

        results.append({
            'Testing Variable': variable,
            'Outcome': outcome,
            'H': H,
            'p-value': p, #only two categories, no q
        })

```

As you can notice in my code, I used a "Manually\_made\_file.xls". I made it by downloading all the .TSV files made from the alpha\_diveristy.qzv files and copying the data into different columns in excel. Then I also merged the metadata file.

I had not called the QIIME2 artifacts from python yet, but I am going to update my script now so I don't have to manually copy and paste the downloaded .TSV file from the view.qiime2 page. I'm having trouble with the automatic merge because the .qza file has as the index the SampleIDs and then the data as a column. So I need to find a way to merge them properly.

---

<div class="post-metadata">

**Author:** ![timanix](https://forum.qiime2.org/user_avatar/forum.qiime2.org/timanix/32/17879_2.png) [@timanix](https://forum.qiime2.org/u/timanix)\
**Post date:** [June 30, 2023, 6:17am UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/5 "2023-06-30T06:17:35Z")

</div>

Glad that you found an error - I silently followed your post since I am also applying Kruskal-Wallis test outside of qiime2 since it is faster for me and I don't remember when I checked for the last time if results are the same with qiime2.

I just add all alpha diversity metrics to metadata file:

```auto
def add_alpha(qza):  
    a = !unzip $qza
    out = a[1].split('/')[0].replace(' inflating: ', '')
    inf = f'{out}/data/alpha-diversity.tsv'
    df = pd.read_csv(inf, sep='\t', index_col=0)
    !rm -rf $out
    return df 

META = pd.read_csv('metadata.tsv', sep='\t', index_col=0)
ALPHAS = {
    'shannon': 'Shannon entropy', 
    'observed_features': 'Observed features',
    'faith_pd': 'Faith\'s PD',
    'evenness': 'Pielou\'s evenness'}

COREM = 'Results/Core-metrics'
for alpha in ALPHAS:
    data = add_alpha(f'{COREM}/{alpha}_vector.qza')
    META[alpha] = data.iloc[:, 0]

META.to_csv('alpha.tsv', sep='\t')

```

This file then can be used for different tests and plots.  
I use dictionary for alpha metrics instead of list just because I added metric names as values that I usually plot on figures instead of metrics itself.

---

<div class="post-metadata">

**Author:** ![gregcaporaso](https://forum.qiime2.org/user_avatar/forum.qiime2.org/gregcaporaso/32/17769_2.png) [@gregcaporaso](https://forum.qiime2.org/u/gregcaporaso)\
**Post date:** [June 30, 2023, 11:00pm UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/6 "2023-06-30T23:00:13Z")

</div>



---

<div class="post-metadata">

**Author:** ![malenaamer](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@malenaamer](https://forum.qiime2.org/u/malenaamer)\
**Post date:** [July 8, 2023, 5:10am UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/7 "2023-07-08T05:10:45Z")

</div>

Thank you so much for sharing this, I'll give it a try!  
Have you run a Lefse analysis in python?

---

<div class="post-metadata">

**Author:** ![timanix](https://forum.qiime2.org/user_avatar/forum.qiime2.org/timanix/32/17879_2.png) [@timanix](https://forum.qiime2.org/u/timanix)\
**Post date:** [July 9, 2023, 11:47am UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/8 "2023-07-09T11:47:33Z")

</div>

Hi!  
Yes, I run lefse analysis couple of times on the data front Qiime2 in Jupyter lab. Now I prefer Ancom-BC that is already installed in the latest versions of Qiime2 since I like this analysis more

---

<div class="post-metadata">

**Author:** ![malenaamer](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@malenaamer](https://forum.qiime2.org/u/malenaamer)\
**Post date:** [July 10, 2023, 5:54pm UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/9 "2023-07-10T17:54:18Z")

</div>

> [@timanix](#):
>
> Ancom-BC

I didn't know about this analysis, why do you like it better? Can you link a tutorial?  
I'm trying to make prettier graphs with my Lefse results, but it's a slow process for a beginner 🙂

---

<div class="post-metadata">

**Author:** ![timanix](https://forum.qiime2.org/user_avatar/forum.qiime2.org/timanix/32/17879_2.png) [@timanix](https://forum.qiime2.org/u/timanix)\
**Post date:** [July 10, 2023, 9:17pm UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/11 "2023-07-10T21:17:06Z")

</div>

I like this comparison of DA tests:

> **[Microbiome differential abundance methods produce different results across 38...](https://pmc.ncbi.nlm.nih.gov/articles/PMC8763921/)**
>
> Identifying differentially abundant microbes is a common goal of microbiome studies. Multiple methods are used interchangeably for this purpose in the literature. Yet, there are few large-scale studies systematically exploring the appropriateness of...

And, of course, this one:

> **[Microbiome Datasets Are Compositional: And This Is Not Optional](https://pmc.ncbi.nlm.nih.gov/articles/PMC5695134/)**
>
> Datasets collected by high-throughput sequencing (HTS) of 16S rRNA gene amplimers, metagenomes or metatranscriptomes are commonplace and being used to study human disease states, ecological differences between sites, and the built environment. There...

Besides, Lefse should be installed into different environment and one will need to export feature table in Qiime2, add metadata information like class and subclass and then switch to the other environment, convert it to the right format for lefse and then run lefse.

I think Ancom-BC is more appropriate for compositional microbiome data, have an advantage of formulas for multiple factors and is better for comparisons with more than two levels.

Best,

---

<div class="post-metadata">

**Author:** ![malenaamer](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@malenaamer](https://forum.qiime2.org/u/malenaamer)\
**Post date:** [July 14, 2023, 4:00pm UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/17 "2023-07-14T16:00:12Z")

</div>

> [@timanix](#):
>
> [Frontiers | Microbiome Datasets Are Compositional: And This Is Not Optional](https://www.frontiersin.org/articles/10.3389/fmicb.2017.02224/full)

Great, thank you so much for sharing this! I'll add it to my "To Learn" list 🙂

---

<div class="post-metadata">

**Author:** ![system](https://forum-qiime2-org.s3.dualstack.us-west-2.amazonaws.com/original/3X/2/1/21af5fe23cb6f4579467c66a9ed94e55274ca7bd.svg) [@system](https://forum.qiime2.org/u/system)\
**Post date:** [August 27, 2023, 7:33am UTC](https://forum.qiime2.org/t/alpha-diversity-analysis-in-qiime2-vs-jupyter-notebook-python/26909/19 "2023-08-27T07:33:29Z")

</div>

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
