Permalink
Browse files

AlignIO now interleaves (wraps) large Nexus alignments to avoid long …

…lines.

This should allow it to be used with MrBayes, while continuing to
output non-interleaved Nexus for smaller alignments where there
have been no issues reported. See mailing list dicussions:
http://lists.open-bio.org/pipermail/biopython-dev/2010-December/008487.html
http://lists.open-bio.org/pipermail/biopython/2013-March/008457.html
  • Loading branch information...
peterjc committed Mar 27, 2013
1 parent d3b286a commit 38b366a950fd0deaa317bcaa9c11752a67650889
Showing with 7 additions and 2 deletions.
  1. +7 −2 Bio/AlignIO/NexusIO.py
View
@@ -99,7 +99,8 @@ def write_alignment(self, alignment):
#and then gets Nexus to prepare the output.
if len(alignment) == 0:
raise ValueError("Must have at least one sequence")
- if alignment.get_alignment_length() == 0:
+ columns = alignment.get_alignment_length()
+ if columns == 0:
raise ValueError("Non-empty sequences are required")
minimal_record = "#NEXUS\nbegin data; dimensions ntax=0 nchar=0; " \
+ "format datatype=%s; end;" \
@@ -108,7 +109,11 @@ def write_alignment(self, alignment):
n.alphabet = alignment._alphabet
for record in alignment:
n.add_sequence(record.id, str(record.seq))
- n.write_nexus_data(self.handle)
+ #For smaller alignments, don't bother to interleave.
+ #For larger alginments, interleave to avoid very long lines
+ #in the output - something MrBayes can't handle.
+ #TODO - Default to always interleaving?
+ n.write_nexus_data(self.handle, interleave=(columns > 1000))
def _classify_alphabet_for_nexus(self, alphabet):
"""Returns 'protein', 'dna', 'rna' based on the alphabet (PRIVATE).

0 comments on commit 38b366a

Please sign in to comment.