Krona Compare

July 19th, 2024
bio, nao, tech
Cross-posted from my NAO Notebook.

When trying to understand how metagenomic samples differ I often want to drill down through the taxonomic hierarchy, comparing relative abundances. I've tried several tools for this, existing and custom, and haven't been all that happy with any. For most purposes, the tool I like most is Krona, which shows an interactive chart. For example, here's Krona showing the results of running the NAO's v1 metagenomic sequencing pipeline on the unenriched Hyperion Treatment Plant samples from Rothman et al. 2021:


(interactive version)

What I often wish I had, however, are linked Krona charts, where I could see multiple samples at once, and drilling down in one sample showed the corresponding portion of the other samples. After failing to find something like this, I hacked something together by monkey-patching the output of Krona. Here's it comparing the samples from several wastewater treatment plants in the same study:

When I click on "viruses" in any of the plots, all four zoom in on the viral fraction:

That's a lot of Tobamovirus!

I only just made this, so it's likely buggy, but if that doesn't put you off you're welcome to give it a try. The interactive version of the charts above is here (warning: 60MB) and the generation code is open source, on github.


If you're interested in the technical details of how I made this:

  • It's a wrapper around the ktImportTaxonomy command from KronaTools.

  • To get multiple charts on the same page, they're each in their own iframe, via srcdoc.

  • There is unfortunately no CSS way of saying "please lay out these rectangles to take up as much of the viewport as possible while maintaining an aspect ratio close to 1:1", so I use some awkward JS. It checks each possible number of columns and takes the one that maximizes the minimum dimension (width or height) of the charts. Luckily there are only a few options to consider.

  • So that the colors match between the charts, each chart on the page has the data from all the charts. I reach into each iframe to set the dataset dropdown's selectedIndex and call onDatasetChange. It's not ideal needing to duplicate the Krona output for each pane in the HTML source and then in the rendered DOMs, but I don't see another way to keep the colors matching.

  • To intercept navigation, the wrapper rewrites the KronaTools HTML output to hook navigateForwd, navigateBack, and, especially, selectNode. It inserts some code that reaches into all of the other iframes on the page and navigates them equivalently.

  • Duplicating selectNode is a little tricky because normally it takes a Node as an argument, but that's not equivalent between charts. So I walk up to the root of the tree and then depth-first search until I find a node with a name matching the intended target.

  • It's all quite heavy, with the Rothman et al. (2021) screenshot above coming from a 60MB HTML file, but it's fast enough on my computer to be useful to me.

Comment via: facebook, lesswrong, mastodon

Recent posts on blogs I like:

Starting With Chords

A lot of people play fiddle. Basically nobody starts by learning chords before learning melodies. But that's actually how I learned. I started with chords. One of the nice things about learning to play violin this way is that you can go busking even…

via Anna Wise's Blog Posts November 15, 2024

Stuffies

I have some stuffies and I just have a bunny. Bunny is a rabbit. Woof is a seal. My favorite stuffie is bun bun. I play with my stuffies. Sometimes I jump up with them and I roll them. I can just throw them in the air when I want to play bthululubp wi…

via Nora Wise's Blog Posts November 15, 2024

You Can Buy A Malaria Net

2024 election takes

via Thing of Things November 6, 2024

more     (via openring)