Question: Matplotlib comprehensive chromosome drawing
0
gravatar for Sakti
3.8 years ago by
Sakti360
United States
Sakti360 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.

Best,

Sakti

ADD COMMENTlink modified 3.1 years ago by fransua390 • written 3.8 years ago by Sakti360
10
gravatar for Ryan Dale
3.8 years ago by
Ryan Dale4.8k
Bethesda, MD
Ryan Dale4.8k 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 3.8 years ago • written 3.8 years ago by Ryan Dale4.8k

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 3.8 years ago by Sakti360
2
gravatar for fransua
3.1 years ago by
fransua390
Spain
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.1 years ago by fransua390
1

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.1 years ago by Ryan Dale4.8k
Please log in to add an answer.

Help
Access

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