Bio.Nexus.Nexus module

Nexus class. Parse the contents of a NEXUS file.

Based upon ‘NEXUS: An extensible file format for systematic information’ Maddison, Swofford, Maddison. 1997. Syst. Biol. 46(4):590-621

exception Bio.Nexus.Nexus.NexusError

Bases: Exception

Provision for the management of Nexus exceptions.

class Bio.Nexus.Nexus.CharBuffer(string)

Bases: object

Helps reading NEXUS-words and characters from a buffer (semi-PRIVATE).

This class is not intended for public use (any more).

__init__(self, string)

Initialize the class.


Return the first character from the buffer.


Return the first character from the buffer, do not include spaces.


Iterate over NEXUS characters in the file.


Check for next non whitespace character in NEXUS file.


Skip whitespace characters in NEXUS file.

next_until(self, target)

Iterate over the NEXUS file until a target character is reached.

peek_word(self, word)

Return a word stored in the buffer.


Return the next NEXUS word from a string.

This deals with single and double quotes, whitespace and punctuation.


Return the rest of the string without parsing.

class Bio.Nexus.Nexus.StepMatrix(symbols, gap)

Bases: object

Calculate a stepmatrix for weighted parsimony.


__init__(self, symbols, gap)

Initialize the class.

set(self, x, y, value)

Set a given value in the matrix’s position.

add(self, x, y, value)

Add the given value to existing, in matrix’s position.


Calculate the associations, makes matrix of associations.


Calculate the transformation matrix.

Normalizes the columns of the matrix of associations.


Calculate the Phylogenetic weight matrix.

Constructed from the logarithmic transformation of the transformation matrix.

smprint(self, name='your_name_here')

Print a stepmatrix.

Bio.Nexus.Nexus.safename(name, mrbayes=False)

Return a taxon identifier according to NEXUS standard.

Wrap quotes around names with punctuation or whitespace, and double single quotes.

mrbayes=True: write names without quotes, whitespace or punctuation for the mrbayes software package.


Remove quotes and/or double quotes around identifiers.

Bio.Nexus.Nexus.get_start_end(sequence, skiplist='-', '?')

Return position of first and last character which is not in skiplist.

Skiplist defaults to [‘-‘,’?’].


Combine matrices in [(name,nexus-instance),…] and return new nexus instance.

combined_matrix=combine([(name1,nexus_instance1),(name2,nexus_instance2),…] Character sets, character partitions and taxon sets are prefixed, readjusted and present in the combined matrix.

class Bio.Nexus.Nexus.Commandline(line, title)

Bases: object

Represent a commandline as command and options.

__init__(self, line, title)

Initialize the class.

class Bio.Nexus.Nexus.Block(title=None)

Bases: object

Represent a NEXUS block with block name and list of commandlines.

__init__(self, title=None)

Initialize the class.

class Bio.Nexus.Nexus.Nexus(input=None)

Bases: object

Create the Nexus class, main class for the management of Nexus files.

__init__(self, input=None)

Initialize the class.


Included for backwards compatibility (DEPRECATED).

set_original_taxon_order(self, value)

Included for backwards compatibility (DEPRECATED).

property original_taxon_order

Included for backwards compatibility (DEPRECATED).

read(self, input)

Read and parse NEXUS input (a filename, file-handle, or string).

write_nexus_data_partitions(self, matrix=None, filename=None, blocksize=None, interleave=False, exclude=(), delete=(), charpartition=None, comment='', mrbayes=False)

Write a nexus file for each partition in charpartition.

Only non-excluded characters and non-deleted taxa are included, just the data block is written.

write_nexus_data(self, filename=None, matrix=None, exclude=(), delete=(), blocksize=None, interleave=False, interleave_by_partition=False, comment=None, omit_NEXUS=False, append_sets=True, mrbayes=False, codons_block=True)

Write a nexus file with data and sets block to a file or handle.

Character sets and partitions are appended by default, and are adjusted according to excluded characters (i.e. character sets still point to the same sites (not necessarily same positions), without including the deleted characters.

  • filename - Either a filename as a string (which will be opened, written to and closed), or a handle object (which will be written to but NOT closed).

  • interleave_by_partition - Optional name of partition (string)

  • omit_NEXUS - Boolean. If true, the ‘#NEXUS’ line normally at the start of the file is omitted.

Returns the filename/handle used to write the data.

append_sets(self, exclude=(), delete=(), mrbayes=False, include_codons=True, codons_only=False)

Return a sets block.

export_fasta(self, filename=None, width=70)

Write matrix into a fasta file.

export_phylip(self, filename=None)

Write matrix into a PHYLIP file.

Note that this writes a relaxed PHYLIP format file, where the names are not truncated, nor checked for invalid characters.

constant(self, matrix=None, delete=(), exclude=())

Return a list with all constant characters.

cstatus(self, site, delete=(), narrow=True)

Summarize character.

narrow=True: paup-mode (a c ? –> ac; ? ? ? –> ?) narrow=false: (a c ? –> a c g t -; ? ? ? –> a c g t -)

weighted_stepmatrix(self, name='your_name_here', exclude=(), delete=())

Calculate a stepmatrix for weighted parsimony.

See Wheeler (1990), Cladistics 6:269-275 and Felsenstein (1981), Biol. J. Linn. Soc. 16:183-196

crop_matrix(self, matrix=None, delete=(), exclude=())

Return a matrix without deleted taxa and excluded characters.

bootstrap(self, matrix=None, delete=(), exclude=())

Return a bootstrapped matrix.

add_sequence(self, name, sequence)

Add a sequence (string) to the matrix.

insert_gap(self, pos, n=1, leftgreedy=False)

Add a gap into the matrix and adjust charsets and partitions.

pos=0: first position pos=nchar: last position

invert(self, charlist)

Return all character indices that are not in charlist.

gaponly(self, include_missing=False)

Return gap-only sites.

terminal_gap_to_missing(self, missing=None, skip_n=True)

Replace all terminal gaps with missing character.

Mixtures like ???——??——- are properly resolved.