Question: Visualize Chromosome With Python ?
12
gravatar for Tg
8.3 years ago by
Tg310
Thailand
Tg310 wrote:

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.

ADD COMMENTlink written 8.3 years ago by Tg310
24
gravatar for Ryan Dale
8.3 years ago by
Ryan Dale4.8k
Bethesda, MD
Ryan Dale4.8k wrote:

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

 

ADD COMMENTlink modified 4.3 years ago • written 8.3 years ago by Ryan Dale4.8k

Thanks, exactly a great start for me.

ADD REPLYlink written 8.3 years ago by Tg310

This is very nice, thank you for posting it.

ADD REPLYlink written 8.3 years ago by Gww2.7k

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

ADD REPLYlink written 8.3 years ago by Weronika300

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. :)

ADD REPLYlink written 6.5 years ago by Dataminer2.6k
3
gravatar for Andreas
8.3 years ago by
Andreas2.4k
Singapore
Andreas2.4k wrote:

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

ADD COMMENTlink written 8.3 years ago by Andreas2.4k
0
gravatar for Gentle Yang
8.1 years ago by
Gentle Yang190
Shenzhen,China
Gentle Yang190 wrote:

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 .

ADD COMMENTlink written 8.1 years ago by Gentle Yang190
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: 773 users visited in the last hour