Back to index

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

List of all members.

Public Member Functions

def __init__
def from_string
def parse

Public Attributes

 handle
 values_are_confidence
 rooted

Private Member Functions

def _parse_tree
def _parse_subtree
def _parse_tag

Detailed Description

Parse a Newick tree given a file handle.

Based on the parser in `Bio.Nexus.Trees`.

Definition at line 52 of file NewickIO.py.


Constructor & Destructor Documentation

def Bio.Phylo.NewickIO.Parser.__init__ (   self,
  handle 
)

Definition at line 58 of file NewickIO.py.

00058 
00059     def __init__(self, handle):
00060         self.handle = handle


Member Function Documentation

def Bio.Phylo.NewickIO.Parser._parse_subtree (   self,
  text 
) [private]
Parse ``(a,b,c...)[[[xx]:]yy]`` into subcomponents, recursively.

Definition at line 85 of file NewickIO.py.

00085 
00086     def _parse_subtree(self, text):
00087         """Parse ``(a,b,c...)[[[xx]:]yy]`` into subcomponents, recursively."""
00088         text = text.strip().rstrip(';')
00089         if text.count('(')!=text.count(')'):
00090             raise NewickError("Parentheses do not match in (sub)tree: " + text)
00091         # Text is now "(...)..." (balanced parens) or "..." (leaf node)
00092         if text.count('(') == 0:
00093             # Leaf/terminal node -- recursion stops here
00094             return self._parse_tag(text)
00095         # Handle one layer of the nested subtree
00096         # XXX what if there's a paren in a comment or other string?
00097         close_posn = text.rfind(')')
00098         subtrees = []
00099         # Locate subtrees by counting nesting levels of parens
00100         plevel = 0
00101         prev = 1
00102         for posn in range(1, close_posn):
00103             if text[posn] == '(':
00104                 plevel += 1
00105             elif text[posn] == ')':
00106                 plevel -= 1
00107             elif text[posn] == ',' and plevel == 0:
00108                 subtrees.append(text[prev:posn])
00109                 prev = posn + 1
00110         subtrees.append(text[prev:close_posn])
00111         # Construct a new clade from trailing text, then attach subclades
00112         clade = self._parse_tag(text[close_posn+1:])
00113         clade.clades = [self._parse_subtree(st) for st in subtrees]
00114         return clade

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.Phylo.NewickIO.Parser._parse_tag (   self,
  text 
) [private]
Extract the data for a node from text.

:returns: Clade instance containing any available data

Definition at line 115 of file NewickIO.py.

00115 
00116     def _parse_tag(self, text):
00117         """Extract the data for a node from text.
00118 
00119         :returns: Clade instance containing any available data
00120         """
00121         # Extract the comment
00122         comment_start = text.find(NODECOMMENT_START)
00123         if comment_start != -1:
00124             comment_end = text.find(NODECOMMENT_END)
00125             if comment_end == -1:
00126                 raise NewickError('Error in tree description: '
00127                                   'Found %s without matching %s'
00128                                   % (NODECOMMENT_START, NODECOMMENT_END))
00129             comment = text[comment_start+len(NODECOMMENT_START):comment_end]
00130             text = text[:comment_start] + text[comment_end+len(NODECOMMENT_END):]
00131         else:
00132             comment = None
00133         clade = Newick.Clade(comment=comment)
00134         # Extract name (taxon), and optionally support, branch length
00135         # Float values are support and branch length, the string is name/taxon
00136         values = []
00137         for part in (t.strip() for t in text.split(':')):
00138             if part:
00139                 try:
00140                     values.append(float(part))
00141                 except ValueError:
00142                     assert clade.name is None, "Two string taxonomies?"
00143                     clade.name = part
00144         if len(values) == 1:
00145             # Real branch length, or support as branch length
00146             if self.values_are_confidence:
00147                 clade.confidence = values[0]
00148             else:
00149                 clade.branch_length = values[0]
00150         elif len(values) == 2:
00151             # Two non-taxon values: support comes first. (Is that always so?)
00152             clade.confidence, clade.branch_length = values
00153         elif len(values) > 2:
00154             raise NewickError("Too many colons in tag: " + text)
00155         return clade
00156 
00157 
00158 # ---------------------------------------------------------
00159 # Output

Here is the caller graph for this function:

def Bio.Phylo.NewickIO.Parser._parse_tree (   self,
  text,
  rooted 
) [private]
Parses the text representation into an Tree object.

Definition at line 80 of file NewickIO.py.

00080 
00081     def _parse_tree(self, text, rooted):
00082         """Parses the text representation into an Tree object."""
00083         # XXX Pass **kwargs along from Parser.parse?
00084         return Newick.Tree(root=self._parse_subtree(text), rooted=self.rooted)

Here is the call graph for this function:

def Bio.Phylo.NewickIO.Parser.from_string (   cls,
  treetext 
)

Definition at line 62 of file NewickIO.py.

00062 
00063     def from_string(cls, treetext):
00064         handle = StringIO(treetext)
00065         return cls(handle)

def Bio.Phylo.NewickIO.Parser.parse (   self,
  values_are_confidence = False,
  rooted = False 
)
Parse the text stream this object was initialized with.

Definition at line 66 of file NewickIO.py.

00066 
00067     def parse(self, values_are_confidence=False, rooted=False):
00068         """Parse the text stream this object was initialized with."""
00069         self.values_are_confidence = values_are_confidence
00070         self.rooted = rooted    # XXX this attribue is useless
00071         buf = ''
00072         for line in self.handle:
00073             buf += line.rstrip()
00074             if buf.endswith(';'):
00075                 yield self._parse_tree(buf, rooted)
00076                 buf = ''
00077         if buf:
00078             # Last tree is missing a terminal ';' character -- that's OK
00079             yield self._parse_tree(buf, rooted)


Member Data Documentation

Definition at line 59 of file NewickIO.py.

Definition at line 69 of file NewickIO.py.

Definition at line 68 of file NewickIO.py.


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