Matplotlib comprehensive chromosome drawing
1
1
Entering edit mode
6.5 years ago
Sakti ▴ 480

Hello Biostars,

Ryan Dale posted 4 years ago a wonderful answer to the question of how to start your own chromosome plots using Python's Matplotlib Visualize Chromosome With Python ?.

I am migrating from Perl to Python and have been bashing my head for 3 days trying to add extra lines and gene annotations to Ryan's code, so that the extra gene "tracks" appear below each corresponding chromosome. I'd also like to zoom in to specific regions, and feed that as ranges in the code (i.e. from positions 1-100,000 in each chromosome). So far I have been unsuccessful :(

Anyone with matplotlib experience in here who could give me a hand?

While there are many chromosome plotters out there I would like to learn how to use Matplotlib and Python. I have seen many programmers do rather amazing publication quality figures with it.

Best,

Sakti

matplotlib python chromosome gene plot • 7.0k views
11
Entering edit mode
6.5 years ago
Ryan Dale 5.0k

Upon revisiting this, I've now made it more general. You can now give it any BED file to plot along with the ideograms; here I'm just showing UCSC's knownGenes table from hg38.

To just show a particular region, you could use BEDTools outside of the script or pybedtools inside of it, to subset the input files to your region of interest. You might find pybedtools.BedTool.from_dataframe() and pybedtools.BedTool.to_dataframe() helpful for this. Also note you'll probably have to tweak the chromosome list and figure size appropriately.

I've commented the script to hopefully help you along in learning Python and matplotlib.

2
Entering edit mode

Fantastic work. I noticed a little mistake in the chromosome_collection function. line 61, facecolors should be group['colors'] and not df['colors']. thanks a lot anyway @Ryan_Dale

1
Entering edit mode

Ah, you're right. It wasn't raising an exception because it looks like matplotlib.Collection allows the colors to cycle, which means they don't have to be the same length as xrange, yrange. So the stain colors were wrong for all but the first chromosome. I updated the gist (code and plot) to fix this. Thank you, nice catch.

0
Entering edit mode

Beautiful reply, and great code to continue with my Python and matlib trainings. I really appreciate your reply, and hope it's useful for more Biostarters!!!

0
Entering edit mode

@ Ryan Dale: How can I add multiple peak sets? I guess the one here is for one peak set. Could you please highlight that part or adjust the script that it works for more than one peakset.

0
Entering edit mode

I tried to run it and it did not work the same as on the picture I would recommend a karyoploteR library in R to make these vizualizations.