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__(string)

Initialize the class.

peek()

Return the first character from the buffer.

peek_nonwhitespace()

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

__next__()

Iterate over NEXUS characters in the file.

next_nonwhitespace()

Check for next non whitespace character in NEXUS file.

skip_whitespace()

Skip whitespace characters in NEXUS file.

next_until(target)

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

peek_word(word)

Return a word stored in the buffer.

next_word()

Return the next NEXUS word from a string.

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

rest()

Return the rest of the string without parsing.

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

Bases: object

Calculate a stepmatrix for weighted parsimony.

See : COMBINATORIAL WEIGHTS IN PHYLOGENETIC ANALYSIS - A STATISTICAL PARSIMONY PROCEDURE Wheeler (1990), Cladistics 6:269-275.

__init__(symbols, gap)

Initialize the class.

set(x, y, value)

Set a given value in the matrix’s position.

add(x, y, value)

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

sum()

Calculate the associations, makes matrix of associations.

transformation()

Calculate the transformation matrix.

Normalizes the columns of the matrix of associations.

weighting()

Calculate the Phylogenetic weight matrix.

Constructed from the logarithmic transformation of the transformation matrix.

smprint(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.

Bio.Nexus.Nexus.quotestrip(word)

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 [‘-‘,’?’].

Bio.Nexus.Nexus.combine(matrices)

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__(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__(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__(input=None)

Initialize the class.

get_original_taxon_order()

Included for backwards compatibility (DEPRECATED).

set_original_taxon_order(value)

Included for backwards compatibility (DEPRECATED).

property original_taxon_order

Included for backwards compatibility (DEPRECATED).

read(input)

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

write_nexus_data_partitions(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(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(exclude=(), delete=(), mrbayes=False, include_codons=True, codons_only=False)

Return a sets block.

export_fasta(filename=None, width=70)

Write matrix into a fasta file.

export_phylip(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(matrix=None, delete=(), exclude=())

Return a list with all constant characters.

cstatus(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(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(matrix=None, delete=(), exclude=())

Return a matrix without deleted taxa and excluded characters.

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

Return a bootstrapped matrix.

add_sequence(name, sequence)

Add a sequence (string) to the matrix.

insert_gap(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(charlist)

Return all character indices that are not in charlist.

gaponly(include_missing=False)

Return gap-only sites.

terminal_gap_to_missing(missing=None, skip_n=True)

Replace all terminal gaps with missing character.

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