Using pygr-draw with simple annotations

First, import the necessary components, load the genome/FASTA database in, and pick a sequence:

>>> from pygr import seqdb
>>> genome = seqdb.BlastDB('example.fa')
>>> sequence_name = 'chrI'

Now, import pygr_draw and the Annotation class:

>>> import pygr_draw
>>> from pygr_draw import Annotation

Instantiate the sequence picture class; in this case, use a BitmapSequencePicture to produce a PNG. You can also use PDFSequencePicture to create PDFs, with otherwise identical code.

>>> picture_class = pygr_draw.BitmapSequencePicture
>>> colors = picture_class.colors

Create a bunch of overlapping annotations in various colors:

>>> annotations1 = {}
>>> annotations1['exon1'] = Annotation('exon1', sequence_name, 0, 500,
...                                    color=colors.blue)
>>> annotations1['exon2'] = Annotation('exon2', sequence_name, 200, 500,
...                                    color=colors.green)
>>> annotations1['exon3'] = Annotation('exon3', sequence_name, 250, 300,
...                                    color=colors.black)
>>> for i in range(250, 500, 10):
...    name = 'exon%d' % (i + 4)
...    start = i
...    end = 2000
...    annotations1[name] = Annotation(name, sequence_name, start, end,
...                                    color=colors.red)

Convert the annotations into an NLMSA annotations map:

>>> annotations_map = pygr_draw.create_annotation_map(annotations1, genome)

Draw 'em:

>>> p = pygr_draw.draw_annotation_maps(genome[sequence_name],
...                                    (annotations_map,),
...                                    picture_class=picture_class)

And then save the image:

>>> image = p.finalize()
>>> open('simple-example.png', 'w').write(image)
simple-example.png