Back to index

python-biopython  1.60
Functions | Variables
Bio.Phylo.NexusIO Namespace Reference

Functions

def parse
def write

Variables

string __docformat__ = "restructuredtext en"
string NEX_TEMPLATE
string TREE_TEMPLATE = "Tree tree%(index)d=[&U]%(tree)s;"

Function Documentation

def Bio.Phylo.NexusIO.parse (   handle)
Parse the trees in a Nexus file.

Uses the old Nexus.Trees parser to extract the trees, converts them back to
plain Newick trees, and feeds those strings through the new Newick parser.
This way we don't have to modify the Nexus module yet. (Perhaps we'll
eventually change Nexus to use the new NewickIO parser directly.)

Definition at line 31 of file NexusIO.py.

00031 
00032 def parse(handle):
00033     """Parse the trees in a Nexus file.
00034 
00035     Uses the old Nexus.Trees parser to extract the trees, converts them back to
00036     plain Newick trees, and feeds those strings through the new Newick parser.
00037     This way we don't have to modify the Nexus module yet. (Perhaps we'll
00038     eventually change Nexus to use the new NewickIO parser directly.)
00039     """
00040     nex = Nexus.Nexus(handle)
00041     # NB: Once Nexus.Trees is modified to use Tree.Newick objects, do this:
00042     # return iter(nex.trees)
00043     # Until then, convert the Nexus.Trees.Tree object hierarchy:
00044     def node2clade(nxtree, node):
00045         subclades = [node2clade(nxtree, nxtree.node(n)) for n in node.succ]
00046         return Newick.Clade(
00047                 branch_length=node.data.branchlength,
00048                 name=node.data.taxon,
00049                 clades=subclades,
00050                 confidence=node.data.support,
00051                 comment=node.data.comment)
00052 
00053     for nxtree in nex.trees:
00054         newroot = node2clade(nxtree, nxtree.node(nxtree.root))
00055         yield Newick.Tree(root=newroot, rooted=nxtree.rooted, name=nxtree.name,
00056                           weight=nxtree.weight)

def Bio.Phylo.NexusIO.write (   obj,
  handle,
  kwargs 
)
Write a new Nexus file containing the given trees.

Uses a simple Nexus template and the NewickIO writer to serialize just the
trees and minimal supporting info needed for a valid Nexus file.

Definition at line 57 of file NexusIO.py.

00057 
00058 def write(obj, handle, **kwargs):
00059     """Write a new Nexus file containing the given trees.
00060 
00061     Uses a simple Nexus template and the NewickIO writer to serialize just the
00062     trees and minimal supporting info needed for a valid Nexus file.
00063     """
00064     trees = list(obj)
00065     writer = NewickIO.Writer(trees)
00066     nexus_trees = [TREE_TEMPLATE % {'index': idx+1, 'tree': nwk}
00067                    for idx, nwk in enumerate(
00068                         writer.to_strings(plain=False, plain_newick=True,
00069                                           **kwargs))]
00070     tax_labels = map(str, chain(*(t.get_terminals() for t in trees)))
00071     text = NEX_TEMPLATE % {
00072             'count':    len(tax_labels),
00073             'labels':   ' '.join(tax_labels),
00074             'trees':    '\n'.join(nexus_trees),
00075             }
00076     handle.write(text)
00077     return len(nexus_trees)

Variable Documentation

string Bio.Phylo.NexusIO.__docformat__ = "restructuredtext en"

Definition at line 7 of file NexusIO.py.

Initial value:
00001 """\
00002 #NEXUS
00003 Begin Taxa;
00004  Dimensions NTax=%(count)d;
00005  TaxLabels %(labels)s;
00006 End;
00007 Begin Trees;
00008  %(trees)s
00009 End;
00010 """

Definition at line 16 of file NexusIO.py.

string Bio.Phylo.NexusIO.TREE_TEMPLATE = "Tree tree%(index)d=[&U]%(tree)s;"

Definition at line 28 of file NexusIO.py.