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

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.

1 Like

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:

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:

        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!

3 Likes

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