Matplotlib comprehensive chromosome drawing
2
0
Entering edit mode
5.8 years ago
Sakti ▴ 440

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 • 6.1k views
ADD COMMENT
10
Entering edit mode
5.8 years ago
Ryan Dale 4.9k

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.

ADD COMMENT
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!!!

ADD REPLY
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.

ADD REPLY
2
Entering edit mode
5.1 years ago
fransua ▴ 390

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

ADD COMMENT
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.

ADD REPLY

Login before adding your answer.

Traffic: 2415 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6