This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Visualize Chromosome With Python ?

http://biostar.stackexchange.com/questions/378/drawing-chromosome-ideogams-with-data <- related

Is there library could visualize chromosome like in above thread in python ? Mainly, draw chromosome, mapping gene, color/dot plot on interest region.

If not, is it possible to do that in matplotlib ?

thanks.

chromosome visualization python

3 answers

Here's one way to do it in Python using matplotlib and data downloaded from the cytoBandIdeo table from UCSC's Table Browser saved as ideogram.txt:

"""
Rough script to plot chromosome ideograms using data from UCSC
"""
from matplotlib import pyplot as plt
from matplotlib.collections import BrokenBarHCollection

color_lookup = {
                  'gneg': (1., 1., 1.),
                'gpos25': (.6, .6, .6),
                'gpos50': (.4, .4, .4),
                'gpos75': (.2, .2, .2),
               'gpos100': (0., 0., 0.),
                  'acen': (.8, .4, .4),
                  'gvar': (.8, .8, .8),
                 'stalk': (.9, .9, .9),
               }

height = 0.9
spacing = 0.9

def ideograms(fn):
    last_chrom = None
    fin = open(fn)
    fin.readline()
    xranges, colors = [], []
    ymin = 0

    for line in fin:
        chrom, start, stop, label, stain = line.strip().split('\t')
        start = int(start)
        stop = int(stop)
        width = stop - start
        if chrom == last_chrom or (last_chrom is None):
            xranges.append((start, width))
            colors.append(color_lookup[stain])
            last_chrom = chrom
            continue

        ymin += height + spacing
        yrange = (ymin, height)
        yield xranges, yrange, colors, last_chrom
        xranges, colors = [], []
        xranges.append((start, width))
        colors.append(color_lookup[stain])
        last_chrom = chrom

    # last one
    ymin += height + spacing
    yrange = (ymin, height)
    yield xranges, yrange, colors, last_chrom

fig = plt.figure()
ax = fig.add_subplot(111)
d = {}
yticks = []
yticklabels = []

# ideogram.txt downloaded from UCSC's table browser
for xranges, yrange, colors, label in ideograms('ideogram.txt'):
    coll = BrokenBarHCollection(xranges, yrange, facecolors=colors)
    ax.add_collection(coll)
    center = yrange[0] + yrange[1]/2.
    yticks.append(center)
    yticklabels.append(label)
    d[label] = xranges

ax.axis('tight')
ax.set_yticks(yticks)
ax.set_yticklabels(yticklabels)
ax.set_xticks([])
plt.show()

I don't know details about the staining (so my colors are probably off), but this should hopefully get you started. Since it's off-the-shelf matplotlib and the x-axis is in bp, you can easily add lines/annotations for your own data.

alt text

Edit, 4 yrs later... see an improved version over at A: Matplotlib comprehensive chromosome drawing

Thanks, exactly a great start for me.

This is very nice, thank you for posting it.

Wow, very nice, thank you! I needed something similar.

Hi! This is a great way to visualise binding sites on ideogram but how can this be further made elegant in terms of looks. I mean instead of horzontal lines can't we have it as vertical and with a bit more resolution? I know, I am asking too much but was just wondering. :)

There is Easyfig which is basically a GUI for genome annotation and comparison. You might be able to reuse some of it's code.

And there's AnnotationSketch (part of genometools), which has Python bindings according to the website.

Andreas

If you want to code your python scripts to fix this,I know two ways : 1 use the 3rd lib to plot, like matplotlib, pysvg , ... ; 2 generate svg (xml) script directly (need to know SVG grammar).

I like the latter .

Log in to answer this question.