Question: Draw Chromosome Features Using Biopython
gravatar for Mchimich
7.0 years ago by
Mchimich260 wrote:

Hello every body. Several people who did bioinformatic need a tool to draw some gene features on chromosomes, I found I nice way to do that using Biopython (Even if I never use it before). This is a nice link with exemples (see section 17.2.2 Annotated Chromosomes). I tried to do an exemple using the following code :

from reportlab.lib.units import cm
from Bio import SeqIO
from Bio.Graphics import BasicChromosome

entries = [("Chr I", "CHR_I/NC_003070.gbk"),
           ("Chr II", "CHR_II/NC_003071.gbk"),
           ("Chr III", "CHR_III/NC_003074.gbk"),
           ("Chr IV", "CHR_IV/NC_003075.gbk"),
           ("Chr V", "CHR_V/NC_003076.gbk")]

max_len = 30432563 #Could compute this
telomere_length = 1000000 #For illustration

chr_diagram = BasicChromosome.Organism()
chr_diagram.page_size = (29.7*cm, 21*cm) #A4 landscape

for index, (name, filename) in enumerate(entries):
    record =,"genbank")
    length = len(record)
    features = [f for f in record.features if f.type=="tRNA"]
    #Record an Artemis style integer color in the feature's qualifiers,
    #1 = Black, 2 = Red, 3 = Green, 4 = blue, 5 =cyan, 6 = purple 
    for f in features: f.qualifiers["color"] = [index+2]

    cur_chromosome = BasicChromosome.Chromosome(name)
    #Set the scale to the MAXIMUM length plus the two telomeres in bp,
    #want the same scale used on all five chromosomes so they can be
    #compared to each other
    cur_chromosome.scale_num = max_len + 2 * telomere_length

    #Add an opening telomere
    start = BasicChromosome.TelomereSegment()
    start.scale = telomere_length

    #Add a body - again using bp as the scale length here.
    body = BasicChromosome.AnnotatedChromosomeSegment(length, features)
    body.scale = length

    #Add a closing telomere
    end = BasicChromosome.TelomereSegment(inverted=True)
    end.scale = telomere_length

    #This chromosome is done

chr_diagram.draw("tRNA_chrom.pdf", "Arabidopsis thaliana")

The problem is that I have an error message

body = BasicChromosome.AnnotatedChromosomeSegment(length, features)
AttributeError: 'module' object has no attribute 'AnnotatedChromosomeSegment'

Any help will be appreciated Thanks

python biopython chromosome • 3.3k views
ADD COMMENTlink modified 7.0 years ago by Matt Shirley9.3k • written 7.0 years ago by Mchimich260

My first guess would be that your version of BioPython is older than when this feature has been added. What does this print?

import Bio
print Bio.__version__
ADD REPLYlink written 7.0 years ago by Istvan Albert ♦♦ 84k

Thanks for your reply ! this is the result of Bio version 1.58

ADD REPLYlink written 7.0 years ago by Mchimich260

It sounds like python cant find the AnnotatedChromosomeSegment module inside of BasicChromosome.

Assuming you managed to get biopython and reportlab installed correctly, I would bet that this is an issue with simply writing the import statement incorrectly. Sometimes things act wonky depending on how the internals of the package is set up (NOT gonna go into all that if we don't have to!).

But before anything else, can you confirm that you are using the same version of biopython that the tutorial is referencing? What is the output when you type the following into an interactive python session:

from Bio.Graphics import BasicChromosome

After that we might be able to help better.

ADD REPLYlink written 7.0 years ago by wadunn8390
gravatar for Istvan Albert
7.0 years ago by
Istvan Albert ♦♦ 84k
University Park, USA
Istvan Albert ♦♦ 84k wrote:

The documentation refers to the latest version 1.61 Your version of BioPython does not contain the necessary functionality. You will need to upgrade.

ADD COMMENTlink written 7.0 years ago by Istvan Albert ♦♦ 84k

It is possible to tell me a safe way to upgrade biopython without having some conflict between the tow versions ! Thanks in advance

ADD REPLYlink written 7.0 years ago by Mchimich260
$ easy_install pip
$ pip install -U biopython
ADD REPLYlink written 7.0 years ago by wadunn8390

This all depends on the operating system that you have. You just need to follow the standard installation instructions. The new version of Biopython should directly replace the old one.

ADD REPLYlink written 7.0 years ago by Istvan Albert ♦♦ 84k

Thanks Albert ! it works now ! So this how I upgrade Biopython in linux system :

$: easy_install -f biopython

if easy_install is not installed you have to do it by yourself :

$: sudo apt-get install python-setuptools python-dev build-essential

Thanks again great !

ADD REPLYlink modified 7.0 years ago by Giovanni M Dall'Olio27k • written 7.0 years ago by Mchimich260
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: 1027 users visited in the last hour