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
ADD COMMENTlink
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([])
ADD COMMENTlink
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!

ADD REPLYlink
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

ADD REPLYlink
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.

ADD REPLYlink
0
Entering edit mode

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

ADD REPLYlink
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.

ADD REPLYlink

Login before adding your answer.

Similar Posts
Loading Similar Posts
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.3