I am able to generate the barplot with our taxonomy, that much works (see below). I am unable to extract the CSV from the barplot which has all the data combined using python or the qiime2 API.
from qiime2.plugins.taxa.visualizers import barplot
taxa_bar_plot = barplot(denoised.table, taxonomy.classification, metadata_table)
taxa_bar_plot.visualization
The only way I can generate the CSV I want (as shown in my screen capture below) is by dragging my visualization file (.qzv) into the online qiime2 viewer and manually using the download CSV option at https://view.qiime2.org. See here my screen capture.
See here the video capture of the file we want to get but via code not in the viewer as shown here:
Of course, we are just testing this in the online viewer tool but really we want to generate this same CSV but using the Qiime2 API in python in our Jupyter notebook. Does anyone know how we can do this in code?
That button in the online qiime2 viewer is getting me the EXACT CSV file we need to proceed beyond where we are stuck. We need to do this in python of course. We will use this table / data frame to do our next step which will be diversity analysis.
I would appreciate any help on this as it has been a few days trying to figure this out.
THANK YOU IN ADVANCE.
Col
thermokarst
(Matthew Ryan Dillon)
assigned thermokarst
#2
Luckily, all QIIME 2 outputs are zip files, and have a convenient API for getting at the contents of the zip file.
import pathlib
import shutil
import tempfile
# this function exports all CSV files from a QIIME 2 Visualization
# to the filepath specified in `dest`
def extract_csvs(viz, dest):
# create a temp dir to work in, that way we don't have
# to manually clean up, later
with tempfile.TemporaryDirectory() as temp:
# export the `data` directory from the visualization
viz.export_data(temp)
temp_pathlib = pathlib.Path(temp)
# iterate through all of the files that we just extracted above
for file in temp_pathlib.iterdir():
# if the file is a csv file, copy it to the final dest
if file.suffix == '.csv':
shutil.copy(file, dest)
The interesting part is the viz.export_data() line - you can export anywhere - in this example I chose to export to a temp directory, that way I can pull out the files that I am interested in, later on in the function.
To string this together with what you have posted above:
You could also modify the function to load those CSVs as Pandas dataframes, for example, and return those, instead of copying the CSV files. If you would like, I could show you an example of that, too - just let me know!
4 Likes
thermokarst
(Matthew Ryan Dillon)
unassigned thermokarst
#4
A third option (because there are many ways to go!) would be to generate that table yourself. So, I think qiime taxa collapse will get you a feature table at a specific taxonomic level (i.e. genus) without having to go through visualization.