Exporting barplot to .qzv from python API

Hello,

I'm a new qiime user trying to make and view a taxonomy barplot within the python API in a jupyter notebook. I've imported FeatureTable[Frequency] and featureData[Taxonomy] objects and created a barplot with them, all without errors. I am trying to export the barplot object to a .qzv file, but when I try to save, I get an error message AttributeError: 'Results' object has no attribute 'save'.

I was following along with this similar issue.

import qiime2
import pandas as pd
from qiime2 import Artifact
from qiime2 import Visualization
from qiime2.plugins import feature_table as ft_plugin
from qiime2.plugins.taxa.visualizers import barplot

# importing the frequency table
table_artifact = qiime2.Artifact.import_data('FeatureTable[Frequency]', table_df)

# importing taxonomy artifact
tax_artifact = qiime2.Artifact.import_data('FeatureData[Taxonomy]', tax_test)

# make barplot
barplot_output = barplot(table = table_artifact, taxonomy = tax_artifact) 
barplot_output.save('barplot.qzv')

I'm not able to view the barplot from the command barplot(table = table_artifact, taxonomy = tax_artifact), but see a uuid displayed and assume I'm doing something right. I suspect the failure to visualize is not a qiime issue, so I wanted to try exporting to a qzv.

I am running version 2024.5.0.dev0+1.g6306962 of qiime2, installed via conda (24.3.0) and activated with a jupyter notebook (notebook v. 6.5.5). Running Python 3.8.19 on Mac OS Sonoma 14.3.

Please let me know and I can share something reproducible or clarify anything.
Thanks!
Scott

Forgot to mention I also tried barplot_output.export_data('barplot.qzv') and got a similar error: AttributeError: 'Results' object has no attribute 'export_data'.

It's a bit unintuitive but try editing the above line to:

barplot_output, = barplot(table = table_artifact, taxonomy = tax_artifact)

With a , after barplot_output. The reason for this is that QIIME 2 actions return a Results object which is a form of NamedTuple. This object is set up to accommodate multiple outputs, so you need to pull the outputs you specifically want off of it. Even in the case of an action that only produces one actual output we return a Results object for the sake of keeping the interface consistent.

By putting the , there with only one variable name you are telling Python you are expecting an iterable object that only has one element and you want to destructure that iterable object and put the one element in the variable.

If that's a bit confusing, it's the same kind of idea as the following:

x, = [1] # x is now 1 not [1]

or

x, y = [1, 2] # x is now 1 y is now 2

4 Likes

Perfect! The export works now. Thanks @Oddant1

1 Like