Question: How to create empty Biopython MultipleSeqAlignment object?
gravatar for nchuang
4.1 years ago by
United States
nchuang210 wrote:


How do I initialize an empty MultipleSeqAlignment object?

from Bio import AlignIO, SeqIO
from Bio.Seq import Seq

empty_align = MultipleSeqAlignment([])

Gives me "not defined" error

biopython • 1.7k views
ADD COMMENTlink modified 4.1 years ago by gearoid200 • written 4.1 years ago by nchuang210
gravatar for gearoid
4.1 years ago by
gearoid200 wrote:

MultipleSeqAlignment is in Bio.Align, so you just need to import that:

import Bio.Align
empty_align = Bio.Align.MultipleSeqAlignment([])
ADD COMMENTlink written 4.1 years ago by gearoid200

this whole library import thing is a little bit confusing.

How come I can't do

from Bio import Align

also according to their documentation they do it completely differently including only calling MultipleSeqAlignment without the Bio.Align in front?

>>> from Bio.Alphabet import generic_dna
>>> from Bio.Seq import Seq
>>> from Bio.SeqRecord import SeqRecord
>>> a = SeqRecord(Seq("AAAACGT", generic_dna), id="Alpha")
>>> b = SeqRecord(Seq("AAA-CGT", generic_dna), id="Beta")
>>> c = SeqRecord(Seq("AAAAGGT", generic_dna), id="Gamma")
>>> align = MultipleSeqAlignment([a, b, c], annotations={"tool": "demo"})
>>> print(align)
DNAAlphabet() alignment with 3 rows and 7 columns
>>> align.annotations
{'tool': 'demo'}

oh yea and it works thank you!

ADD REPLYlink modified 4.1 years ago • written 4.1 years ago by nchuang210

Well spotted - the Bio.Align example didn't make it explicit we expected you to do:

from Bio.Align import MultipleSeqAlignment

That's been fixed ready for the next release:

ADD REPLYlink written 4.1 years ago by Peter5.8k

So the short version is that to use a class or function you either need to define it in the same file or import it from somewhere else. In this case class MultipleSeqAlign is defined in module Bio.Align. So you can get it two ways

from Bio.Align import MultipleSeqAlign

which then allows you to use MultipleSeqAlign (and only MultipleSeqAlign) from that module with no prefix, or

import Bio.Align

which gives you access to everything defined in Bio.Align, but you need to use the full name, like Bio.Align.MultipleSeqAlign.

So that's why my version was different from the others. I think in that example you found, though, they are missing the correct import statement. They either forgot or it's implied that you've already imported MultipleSeqAlign.

ADD REPLYlink written 4.1 years ago by gearoid200

thank you! really appreciate you guys teaching me little things like this!

ADD REPLYlink written 4.1 years ago by nchuang210

I also realized that the MultipleSeqAlignment object is not compatible with AlignIO's read object. I can't add them to each other.

ADD REPLYlink written 4.1 years ago by nchuang210
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: 1234 users visited in the last hour