Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Member Functions
Bio.Phylo.NewickIO.Writer Class Reference

List of all members.

Public Member Functions

def __init__
def write
def to_strings

Public Attributes

 trees

Private Member Functions

def _info_factory

Detailed Description

Based on the writer in Bio.Nexus.Trees (str, to_string).

Definition at line 160 of file NewickIO.py.


Constructor & Destructor Documentation

def Bio.Phylo.NewickIO.Writer.__init__ (   self,
  trees 
)

Definition at line 163 of file NewickIO.py.

00163 
00164     def __init__(self, trees):
00165         self.trees = trees


Member Function Documentation

def Bio.Phylo.NewickIO.Writer._info_factory (   self,
  plain,
  confidence_as_branch_length,
  branch_length_only,
  max_confidence,
  format_confidence,
  format_branch_length 
) [private]
Return a function that creates a nicely formatted node tag.

Definition at line 215 of file NewickIO.py.

00215 
00216             format_branch_length):
00217         """Return a function that creates a nicely formatted node tag."""
00218         if plain:
00219             # Plain tree only. That's easy.
00220             def make_info_string(clade, terminal=False):
00221                 return ''
00222 
00223         elif confidence_as_branch_length:
00224             # Support as branchlengths (eg. PAUP), ignore actual branchlengths
00225             def make_info_string(clade, terminal=False):
00226                 if terminal:
00227                     # terminal branches have 100% support
00228                     return ':' + format_confidence % max_confidence
00229                 else:      
00230                     return ':' + format_confidence % clade.confidence
00231 
00232         elif branch_length_only:
00233             # write only branchlengths, ignore support
00234             def make_info_string(clade, terminal=False):
00235                 return ':' + format_branch_length % clade.branch_length
00236 
00237         else:
00238             # write support and branchlengths (e.g. .con tree of mrbayes)
00239             def make_info_string(clade, terminal=False):
00240                 if (terminal or
00241                         not hasattr(clade, 'confidence') or
00242                         clade.confidence is None):
00243                     return (':' + format_branch_length
00244                             ) % (clade.branch_length or 0.0)
00245                 else:
00246                     return (format_confidence + ':' + format_branch_length
00247                             ) % (clade.confidence, clade.branch_length or 0.0)
00248 
00249         return make_info_string
00250 

Here is the caller graph for this function:

def Bio.Phylo.NewickIO.Writer.to_strings (   self,
  confidence_as_branch_length = False,
  branch_length_only = False,
  plain = False,
  plain_newick = True,
  ladderize = None,
  max_confidence = 1.0,
  format_confidence = '%1.2f',
  format_branch_length = '%1.5f' 
)
Return an iterable of PAUP-compatible tree lines.

Definition at line 177 of file NewickIO.py.

00177 
00178             format_confidence='%1.2f', format_branch_length='%1.5f'):
00179         """Return an iterable of PAUP-compatible tree lines."""
00180         # If there's a conflict in the arguments, we override plain=True
00181         if confidence_as_branch_length or branch_length_only:
00182             plain = False
00183         make_info_string = self._info_factory(plain,
00184                 confidence_as_branch_length, branch_length_only, max_confidence,
00185                 format_confidence, format_branch_length)
00186         def newickize(clade):
00187             """Convert a node tree to a Newick tree string, recursively."""
00188             if clade.is_terminal():    #terminal
00189                 return ((clade.name or '')
00190                         + make_info_string(clade, terminal=True))
00191             else:
00192                 subtrees = (newickize(sub) for sub in clade)
00193                 return '(%s)%s' % (','.join(subtrees),
00194                         (clade.name or '') + make_info_string(clade))
00195 
00196         # Convert each tree to a string
00197         for tree in self.trees:
00198             if ladderize in ('left', 'LEFT', 'right', 'RIGHT'):
00199                 # Nexus compatibility shim, kind of
00200                 tree.ladderize(reverse=(ladderize in ('right', 'RIGHT')))
00201             rawtree = newickize(tree.root) + ';'
00202             if plain_newick:
00203                 yield rawtree
00204                 continue
00205             # Nexus-style (?) notation before the raw Newick tree
00206             treeline = ['tree', (tree.name or 'a_tree'), '=']
00207             if tree.weight != 1:
00208                 treeline.append('[&W%s]' % round(float(tree.weight), 3))
00209             if tree.rooted:
00210                 treeline.append('[&R]')
00211             treeline.append(rawtree)
00212             yield ' '.join(treeline)

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.Phylo.NewickIO.Writer.write (   self,
  handle,
  kwargs 
)
Write this instance's trees to a file handle.

Definition at line 166 of file NewickIO.py.

00166 
00167     def write(self, handle, **kwargs):
00168         """Write this instance's trees to a file handle."""
00169         count = 0
00170         for treestr in self.to_strings(**kwargs):
00171             handle.write(treestr + '\n')
00172             count += 1
00173         return count

Here is the call graph for this function:


Member Data Documentation

Definition at line 164 of file NewickIO.py.


The documentation for this class was generated from the following file: