Generate Krona charts from Taxa Barplots

I have written a python script to generate Krona charts from taxa barplots, and I would like to share it.

Krona charts are interactive pie charts, and I think they are useful because it visualizes samples one by one very good.

But the problem is we need to change our OTU/ASV tables to a tsv file which Krona likes.

Krona tsv format is like this: krona-format.tsv (64 Bytes)

An example Krona tsv: O5.tsv (15.4 KB)

An example Krona chart (upload as zip since it is not allowed to upload html): Krona.zip (50.8 KB)

To get started we need to install Krona. Here is the github page : https://github.com/marbl/Krona/wiki

After the install I do not recomment to run update scripts in KronaTools because we won’t use them and it will start a huge download.

We need to be able to call Krona from bash so make sure that ktImportText command works.

After that, we just need a taxa-bar-plots.qzv file to work with this Python script: generate-krona.py (2.1 KB)

Simply, the script extracts level-7.csv from taxa-bar-plots.qzv, creates tsv files for krona, generates krona file as html, removes anthing unnecessary in the process.

You can read the comments to make changes in the script. Script is written to work with python3.

Have fun.

(First time posting a tutorial, I need all the criticism and suggestions to improve it. )

4 Likes

Awesome work @the_dummy! I am looking forward to giving this a spin!

Looks like you are just a stone’s-throw away from writing your own QIIME 2 plugin… interested in giving that a try? We can point you in the right direction!

#extract level-7.csv from taxa barplots

Why extract from the barplot.qzv? Why not accept a feature table and taxonomy file (or a feature table that has been collapsed to a specific level) as input? That would be the first step toward making this a little more plugin-like and efficient.

3 Likes

Well, now I will look into how the plugins work.

Why extract from the barplot.qzv? Why not accept a feature table and taxonomy file (or a feature table that has been collapsed to a specific level) as input? That would be the first step toward making this a little more plugin-like and efficient.

This is a very good idea. With this, you won’t need to visualize everything if you want a limit to a level.

Now, with argparse, I have added some tricks to the script.

As @Nicholas_Bokulich suggested I tried to use collapsed table, and it extracts metadata.tsv from collapsed taxa visualization (a qzv file).

I didn’t want to deal with a feature-table.biom because I don’t know the nature of the file.

This script has input, output, exclude, regex options now!

krona-arg.py (2.3 KB)

Let’s hear out how it can be improved much more…

2 Likes

Another good reason to write this as a QIIME 2 plugin — you can take a FeatureTable[Frequency] artifact as input, QIIME 2 will validate the input type and transform it into a pandas.DataFrame for manipulating and saving as a TSV for krona. No need to figure out the biom-format API (which while useful for many other things is not needed here since you are really just formatting the table for krona)

1 Like

Pneumothorax hits again!

Eh… I’m back again, but I need to look into this later. I was at hospital for like 10 days and everything is a mess.

Of course if anyone has problems with the script, I can help. Improvements have to wait :frowning:

1 Like

Ahhh! I hope you feel better soon and sort that out. Im glad you’re out of the hospital.

Hey, I’m glad you’re recovering!
This is a really nice work and I’am in complete agrement you should work on a plugin. People would use it.
Cheers

Hello there, I tried running your script with the taxa-bar-plots.qzv flie but I keep gettting a syntax error, it looks like this:

./generate-krona.py: line 6: syntax error near unexpected token (' ./generate-krona.py: line 6: def unzip(qzv_file):’

Well, first thing I thought was your python version. I have written it in python3 so you could check that.

Also, try this krona-arg.py. Check --help with this one.

And shout out to everyone, I’m back and will work on changing this into a plugin!

2 Likes

welcome back @the_dummy! Glad to hear you are better!

1 Like

(base) [[email protected] ~]$ python --version
Python 3.7.4
Is this version of python a problem?, the krona-arg.py script also gives a similar error:

(base) [[email protected] Healthy-bloem] ./krona-arg.py ./krona-arg.py: line 10: syntax error near unexpected token `(' ./krona-arg.py: line 10: `parser = argparse.ArgumentParser()' (base) [[email protected] Healthy-bloem] parser = argparse.ArgumentParser()’
bash: syntax error near unexpected token `(’

Then after trying --help i get this:

(base) [[email protected] Healthy-bloem]$ python krona-arg.py --help
File “krona-arg.py”, line 25
xif args.exclude:
^
SyntaxError: invalid syntax

Quick question does the script require the taxa-bar-plots.qzv and the level-7.csv files to be in a specific folder. Another thing I work with fungal data using the UNITE database, does this have to be considered?

Hello again, I’m having an issue running the script

Traceback (most recent call last):
File “krona-arg.py”, line 72, in
lines.pop(0)
IndexError: pop from empty list

Well, you are hitting an error in almost every single line of the script. I believe there is a common problem with all of these errors.

How did you solve the previous problem? This might lead me somewhere.