Question: Matplotlib comprehensive chromosome drawing
gravatar for Sakti
4.6 years ago by
United States
Sakti390 wrote:

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.



ADD COMMENTlink modified 3.9 years ago by fransua390 • written 4.6 years ago by Sakti390
gravatar for Ryan Dale
4.6 years ago by
Ryan Dale4.9k
Bethesda, MD
Ryan Dale4.9k wrote:

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 COMMENTlink modified 4.6 years ago • written 4.6 years ago by Ryan Dale4.9k

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 REPLYlink written 4.6 years ago by Sakti390

@ 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 REPLYlink written 6 months ago by ChIP550
gravatar for fransua
3.9 years ago by
fransua390 wrote:

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 COMMENTlink written 3.9 years ago by fransua390

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 REPLYlink written 3.9 years ago by Ryan Dale4.9k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1079 users visited in the last hour