# problem in merge some diffierent alpha-diversity index to one file.

**URL:** <https://forum.qiime2.org/t/problem-in-merge-some-diffierent-alpha-diversity-index-to-one-file/15056>\
**Category:** User Support\
**Tags:** artifact-api\
**Created:** [May 22, 2020, 2:11pm UTC](https://forum.qiime2.org/t/problem-in-merge-some-diffierent-alpha-diversity-index-to-one-file/15056 "2020-05-22T14:11:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LFE](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/l/76d3ee/32.png) [@LFE](https://forum.qiime2.org/u/LFE)\
**Post date:** [May 22, 2020, 2:11pm UTC](https://forum.qiime2.org/t/problem-in-merge-some-diffierent-alpha-diversity-index-to-one-file/15056/1 "2020-05-22T14:11:25Z")

</div>

hello, guys, I am working on alpha-diversities and I need 5 diffietent diversities. I use command line to import OTU table to import a .qza file.  
\> biom convert -i otu\_table.txt -o table.from\_txt\_json.biom --table-type="OTU table" --to-json  
\> qiime tools import   
\> --input-path table.from\_txt\_json.biom   
\> --type 'FeatureTable[Frequency]'   
\> --input-format BIOMV100Format   
\> --output-path feature-table-1.qza

after that, i run a python script to generate diffierent alpha diversities:

```
from qiime2.plugins.diversity.methods import alpha
from qiime2 import Artifact
import os

feature_table = Artifact.load('feature-table-1.qza')
to_be_cal = ['goods_coverage', 'shannon', 'simpson', 'pielou_e', 'chao1']
for a in to_be_cal:
    al = alpha(table=feature_table, metric=a).alpha_diversity
    al.export_data("alpha")
    os.rename("./alpha/alpha-diversity.tsv", "./alpha/%s.tsv"%a)

```

Problem is that I want to merge different diversities to ONE file and not to operate with the generated files,. In other words, I want to show the alpha diversity directly on my terminal not in the file.

---

<div class="post-metadata">

**Author:** ![thermokarst](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@thermokarst](https://forum.qiime2.org/u/thermokarst)\
**Post date:** [May 22, 2020, 2:12pm UTC](https://forum.qiime2.org/t/problem-in-merge-some-diffierent-alpha-diversity-index-to-one-file/15056/2 "2020-05-22T14:12:22Z")

</div>



---

<div class="post-metadata">

**Author:** ![thermokarst](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@thermokarst](https://forum.qiime2.org/u/thermokarst)\
**Post date:** [May 22, 2020, 2:36pm UTC](https://forum.qiime2.org/t/problem-in-merge-some-diffierent-alpha-diversity-index-to-one-file/15056/3 "2020-05-22T14:36:22Z")

</div>

Hey there @LFE! This is a great question!

QIIME 2 has a built-in feature for invoking data transformations on QIIME 2 results - this can be super handy for cases like this. I modified your example above to demonstrate transforming the alpha div results to QIIME 2 Metadata, which let's you see everything in one table:

```python
import qiime2
from qiime2.plugins.diversity.methods import alpha

def merge_alpha_div(table, metrics):
    results = []
    for metric in metrics:
        div = alpha(table=table, metric=metric).alpha_diversity
        md = div.view(qiime2.Metadata)
        results.append(md)
    first, rest = results[0], results[1:]
    return first.merge(*rest)

table = qiime2.Artifact.load('table.qza')
metrics = ['goods_coverage', 'shannon', 'simpson', 'pielou_e', 'chao1']
merged = merge_alpha_div(table, metrics)

####

print(merged.to_dataframe())
merged.save('merged.tsv')

```

The printed results (from the second-to-last-line) look like:

```python
        goods_coverage shannon simpson pielou_e chao1
id                                                         
L1S105 1.0 3.867599 0.870469 0.647050 63.0
L1S140 1.0 3.838934 0.851851 0.637446 65.0
L1S208 1.0 4.499572 0.899548 0.702028 85.0
L1S257 1.0 4.701689 0.925626 0.741609 81.0
L1S281 1.0 4.595285 0.907516 0.744788 72.0
L1S57 1.0 4.188241 0.865531 0.683317 70.0
L1S76 1.0 3.589847 0.795913 0.605295 61.0
L1S8 1.0 3.177628 0.793858 0.582044 44.0
...

```

and, the table that is saved (`merged.tsv`) has the same data, just in TSV format.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![thermokarst](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/t/8e7dd6/32.png) [@thermokarst](https://forum.qiime2.org/u/thermokarst)\
**Post date:** [May 22, 2020, 2:36pm UTC](https://forum.qiime2.org/t/problem-in-merge-some-diffierent-alpha-diversity-index-to-one-file/15056/4 "2020-05-22T14:36:27Z")

</div>



---

<div class="post-metadata">

**Author:** ![LFE](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/l/76d3ee/32.png) [@LFE](https://forum.qiime2.org/u/LFE)\
**Post date:** [May 23, 2020, 4:39am UTC](https://forum.qiime2.org/t/problem-in-merge-some-diffierent-alpha-diversity-index-to-one-file/15056/5 "2020-05-23T04:39:07Z")

</div>

hello, @thermokarst . I use your code, it work 100% good. Thank you very much for your help.

---

<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:** [June 23, 2020, 10:44am UTC](https://forum.qiime2.org/t/problem-in-merge-some-diffierent-alpha-diversity-index-to-one-file/15056/6 "2020-06-23T10:44:01Z")

</div>

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