Biostar Beta. Not for public use.
How to create empty Biopython MultipleSeqAlignment object?
0
Entering edit mode
4.8 years ago
nchuang • 200
@nchuang18948

Hi,

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.4k views
2
Entering edit mode
4.8 years ago
gearoid • 200
@gearoid25041

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

import Bio.Align
empty_align = Bio.Align.MultipleSeqAlignment([])

0
Entering edit mode

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
AAAACGT Alpha
AAA-CGT Beta
AAAAGGT Gamma
>>> align.annotations
{'tool': 'demo'}


oh yea and it works thank you!

2
Entering edit mode

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: https://github.com/biopython/biopython/commit/009d913ac2d8ed010db20aa43eab489e0031d9dd

1
Entering edit mode

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.

0
Entering edit mode

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

0
Entering edit mode

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