How to access level#.csv files from the variable generated by the barplot function?

I want to save each level#.csv in a pandas dataframe.

taxa_barplot = barplot(table = table, taxonomy = taxonomy, metadata = metadata)

Is it possible to access level#.csv files from the variable taxa_barplot? Without accessing the file taxa_barplot.qzv.

Hey there @Rodrigo_Peralta!

Here is a quick snippet for loading up all the taxonomic levels into a dictionary of pandas dataframes:

import pathlib
import tempfile

import qiime2
import pandas as pd


# Replace this with the filepath to your barplot viz.
# If you already have a viz in memory don't worry about
# saving and loading, just start with your barplot result.
taxa_barplot_fp = 'taxa-bar-plots.qzv'
taxa_barplot = qiime2.Visualization.load(taxa_barplot_fp)

csvs = dict()
with tempfile.TemporaryDirectory() as tmpdir:
    # This method is on every QIIME 2 Visualization
    taxa_barplot.export_data(tmpdir)
    data_dir_fp = pathlib.Path(tmpdir)
    csv_fps = sorted(data_dir_fp.glob('level-*csv'))
    for csv_fp in csv_fps:
        name = str(csv_fp.name)
        csvs[name] = pd.read_csv(csv_fp, index_col='index')

The keys are 'level-1.csv', 'level-2.csv', ..., 'level-n.csv', and the values look like:

Hope that helps!

:qiime2:

4 Likes

It’s just what I needed
Really Thanks !!

1 Like