1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
|
#! /usr/bin/env python
##############################################################################
## DendroPy Phylogenetic Computing Library.
##
## Copyright 2010-2015 Jeet Sukumaran and Mark T. Holder.
## All rights reserved.
##
## See "LICENSE.rst" for terms and conditions of usage.
##
## If you use this work or any portion thereof in published work,
## please cite it as:
##
## Sukumaran, J. and M. T. Holder. 2010. DendroPy: a Python library
## for phylogenetic computing. Bioinformatics 26: 1569-1571.
#
##############################################################################
"""
Parsing of NEWICK-format tree from a stream.
"""
import re
import warnings
from dendropy.utility import error
from dendropy.utility import deprecate
from dendropy.utility.textprocessing import StringIO
from dendropy.dataio import tokenizer
from dendropy.dataio import nexusprocessing
from dendropy.dataio import ioservice
##############################################################################
## NewickReader
class NewickReader(ioservice.DataReader):
"""
Parser for NEWICK-formatted data.
"""
_default_rooting_directive = None
_default_tree_weight = 1.0
class NewickReaderError(error.DataParseError):
def __init__(self, message,
line_num=None,
col_num=None,
stream=None):
error.DataParseError.__init__(self,
message=message,
line_num=line_num,
col_num=col_num,
stream=stream)
class NewickReaderInvalidTokenError(NewickReaderError):
def __init__(self,
message,
line_num=None,
col_num=None,
stream=None):
NewickReader.NewickReaderError.__init__(self,
message=message,
line_num=line_num,
col_num=col_num,
stream=stream)
class NewickReaderMalformedStatementError(NewickReaderError):
def __init__(self,
message,
line_num=None,
col_num=None,
stream=None):
NewickReader.NewickReaderError.__init__(self,
message=message,
line_num=line_num,
col_num=col_num,
stream=stream)
class NewickReaderIncompleteTreeStatementError(NewickReaderMalformedStatementError):
def __init__(self,
message,
line_num=None,
col_num=None,
stream=None):
NewickReader.NewickReaderMalformedStatementError.__init__(self,
message=message,
line_num=line_num,
col_num=col_num,
stream=stream)
class NewickReaderInvalidValueError(NewickReaderError):
def __init__(self,
message,
line_num=None,
col_num=None,
stream=None):
NewickReader.NewickReaderError.__init__(self,
message=message,
line_num=line_num,
col_num=col_num,
stream=stream)
class NewickReaderDuplicateTaxonError(NewickReaderError):
def __init__(self,
message,
line_num=None,
col_num=None,
stream=None):
detailed = ("Multiple occurrences of the same taxa on trees are not"
" supported: trees with duplicate node labels can only be"
" processed if the labels are not parsed as operational taxonomic"
" unit concepts but instead as simply node labels by specifying"
" 'suppress_internal_node_taxa=True, suppress_leaf_node_taxa=True'."
" Duplicate taxon labels: {}").format(message)
NewickReader.NewickReaderError.__init__(self,
message=detailed,
line_num=line_num,
col_num=col_num,
stream=stream)
def __init__(self, **kwargs):
"""Keyword Arguments
-----------------
rooting : string, {['default-unrooted'], 'default-rooted', 'force-unrooted', 'force-rooted'}
Specifies how trees in the data source should be intepreted with
respect to their rooting:
'default-unrooted' [default]:
All trees are interpreted as unrooted unless a '[&R]'
comment token explicitly specifies them as rooted.
'default-rooted'
All trees are interpreted as rooted unless a '[&U]'
comment token explicitly specifies them as unrooted.
'force-unrooted'
All trees are unconditionally interpreted as unrooted.
'force-rooted'
All trees are unconditionally interpreted as rooted.
edge_length_type : type, default: ``float``
Specifies the type of the edge lengths (``int`` or ``float``). Tokens
interpreted as branch lengths will be cast to this type.
Defaults to ``float``.
suppress_edge_lengths : boolean, default: |False|
If |True|, edge length values will not be processed. If |False|,
edge length values will be processed.
extract_comment_metadata : boolean, default: |True|
If |True| (default), any comments that begin with '&' or '&&' will
be parsed and stored as part of the annotation set of the
corresponding object (accessible through the ``annotations``
attribute of the object). This requires that the comment
contents conform to a particular format (NHX or BEAST: 'field =
value'). If |False|, then the comments will not be parsed,
but will be instead stored directly as elements of the ``comments``
list attribute of the associated object.
store_tree_weights : boolean, default: |False|
If |True|, process the tree weight (e.g. "[&W 1/2]") comment
associated with each tree, if any. Defaults to |False|.
finish_node_fn : function object, default: |None|
If specified, this function will be applied to each node after
it has been constructed.
case_sensitive_taxon_labels : boolean, default: |False|
If |True|, then taxon labels are case sensitive (e.g., "P.regius"
and "P.REGIUS" wil be treated as different operation taxonomic
unit concepts). Otherwise, taxon label intepretation will be made
without regard for case.
preserve_underscores : boolean, default: |False|
If |True|, unquoted underscores in labels will *not* converted to
spaces. Defaults to |False|: all underscores not protected by
quotes will be converted to spaces.
suppress_internal_node_taxa : boolean, default: |True|
If |False|, internal node labels will be instantantiated into
|Taxon| objects. If |True|, internal node labels
will *not* be instantantiated as strings.
suppress_leaf_node_taxa : boolean, default: |False|
If |False|, leaf (external) node labels will be instantantiated
into |Taxon| objects. If |True|, leaff (external) node
labels will *not* be instantantiated as strings.
is_parse_jplace_tokens : boolean: |False|
If |True|, then accept edge numbering according to the jplace
format, as described in Matsen et. al. PLoS One, 2012
http://dx.doi.org/10.1371/journal.pone.0031009. An instance variable
edge_index is added to the returned tree, and an edge_number is
added to each edge. If False [default], encountering edge labels
raises a NewickReaderMalformedStatementError.
is_assign_internal_labels_to_edges : boolean, default: |None|
If |True|, internal node labels will be assigned as edge labels.
terminating_semicolon_required : boolean, default: |True|
If |True| [default], then a tree statement that does not end in a
semi-colon is an error. If |False|, then no error will be raised.
ignore_unrecognized_keyword_arguments : boolean, default: |False|
If |True|, then unsupported or unrecognized keyword arguments will
not result in an error. Default is |False|: unsupported keyword
arguments will result in an error.
"""
# base
ioservice.DataReader.__init__(self)
self._rooting = None
## (TEMPORARY and UGLY!!!!) Special handling for legacy signature
if "as_unrooted" in kwargs or "as_rooted" in kwargs or "default_as_rooted" in kwargs or "default_as_unrooted" in kwargs:
import collections
legacy_kw = ("as_unrooted", "as_rooted", "default_as_rooted", "default_as_unrooted")
legacy_kw_str = ", ".join("'{}'".format(k) for k in legacy_kw)
if "rooting" in kwargs:
raise ValueError("Cannot specify 'rooting' keyword argument in conjunction with any of the (legacy) keyword arguments ({}). Use 'rooting' alone.".format(legacy_kw_str))
specs = collections.Counter(k for k in kwargs.keys() if k in legacy_kw)
if sum(specs.values()) > 1:
raise ValueError("Cannot specify more than one of {{ {} }} at the same time".format(legacy_kw_str))
kw = list(specs.keys())[0]
if kw == "as_unrooted":
if kwargs[kw]:
corrected = "force-unrooted"
else:
corrected = "force-rooted"
elif kw == "as_rooted":
if kwargs[kw]:
corrected = "force-rooted"
else:
corrected = "force-unrooted"
elif kw == "default_as_unrooted":
if kwargs[kw]:
corrected = "default-unrooted"
else:
corrected = "default-rooted"
elif kw == "default_as_rooted":
if kwargs[kw]:
corrected = "default-rooted"
else:
corrected = "default-unrooted"
msg = StringIO()
# error.critical_deprecation_alert("\n{}\nUse of keyword argument '{}={}' is deprecated; use 'rooting=\"{}\"' instead".format(msg.getvalue(), kw, kwargs[kw], corrected),
# stacklevel=4)
deprecate.dendropy_deprecation_warning(
preamble="Deprecated since DendroPy 4:",
old_construct="{}={}".format(kw, kwargs[kw]),
new_construct="rooting='{}'".format(corrected),
stacklevel=7)
kwargs.pop(kw)
kwargs["rooting"] = corrected
if "allow_duplicate_taxon_labels" in kwargs:
raise ValueError(
"'allow_duplicate_taxon_labels' is no longer"
" supported: trees with duplicate node labels can only be"
" processed if the labels are not parsed as operational taxonomic"
" unit concepts but instead as simply node labels by specifying"
" 'suppress_internal_node_taxa=True, suppress_leaf_node_taxa=True'."
)
# self.rooting = kwargs.pop("rooting", "default-unrooted")
self.rooting = kwargs.pop("rooting", self.__class__._default_rooting_directive)
self.edge_length_type = kwargs.pop("edge_length_type", float)
self.suppress_edge_lengths = kwargs.pop("suppress_edge_lengths", False)
self.extract_comment_metadata = kwargs.pop('extract_comment_metadata', True)
self.store_tree_weights = kwargs.pop("store_tree_weights", False)
self.default_tree_weight = kwargs.pop("default_tree_weight", self.__class__._default_tree_weight)
self.finish_node_fn = kwargs.pop("finish_node_fn", None)
self.case_sensitive_taxon_labels = kwargs.pop('case_sensitive_taxon_labels', False)
self.preserve_unquoted_underscores = kwargs.pop('preserve_underscores', False)
self.suppress_internal_node_taxa = kwargs.pop("suppress_internal_node_taxa", True)
self.suppress_leaf_node_taxa = kwargs.pop("suppress_external_node_taxa", False) # legacy (will be deprecated)
self.suppress_leaf_node_taxa = kwargs.pop("suppress_leaf_node_taxa", self.suppress_leaf_node_taxa)
self.is_parse_jplace_tokens = kwargs.pop("is_parse_jplace_tokens", False)
self.is_assign_internal_labels_to_edges = kwargs.pop("is_assign_internal_labels_to_edges", None)
if self.is_assign_internal_labels_to_edges and not self.suppress_internal_node_taxa:
raise ValueError("Conflicting options: cannot simultaneously assign internal labels to edges and to internal taxa")
self.terminating_semicolon_required = kwargs.pop("terminating_semicolon_required", True)
self.check_for_unused_keyword_arguments(kwargs)
# per-tree book-keeping
self._tree_statement_complete = None
self._parenthesis_nesting_level = None
self._seen_taxa = None
def tree_iter(self,
stream,
taxon_symbol_mapper,
tree_factory):
"""
Iterator that yields trees in NEWICK-formatted source.
Parameters
----------
stream : file or file-like object
A file or file-like object opened for reading.
taxon_namespace : |TaxonNamespace|
Operational taxonomic unit namespace to use for taxon management.
tree_factory : function object
A function that returns a new |Tree| object when called
without arguments.
Returns
-------
iter : :py`collections.Iterator` [|Tree|]
An iterator yielding |Tree| objects constructed based on
data in ``stream``.
"""
nexus_tokenizer = nexusprocessing.NexusTokenizer(stream,
preserve_unquoted_underscores=self.preserve_unquoted_underscores)
while True:
tree = self._parse_tree_statement(
nexus_tokenizer=nexus_tokenizer,
tree_factory=tree_factory,
taxon_symbol_map_fn=taxon_symbol_mapper.require_taxon_for_symbol)
yield tree
if tree is None:
raise StopIteration
def _read(self,
stream,
taxon_namespace_factory=None,
tree_list_factory=None,
char_matrix_factory=None,
state_alphabet_factory=None,
global_annotations_target=None):
taxon_namespace = taxon_namespace_factory(label=None)
tree_list = tree_list_factory(label=None, taxon_namespace=taxon_namespace)
taxon_symbol_mapper = nexusprocessing.NexusTaxonSymbolMapper(
taxon_namespace=taxon_namespace,
enable_lookup_by_taxon_number=False,
case_sensitive=self.case_sensitive_taxon_labels)
tree_factory = tree_list.new_tree
for tree in self.tree_iter(stream=stream,
taxon_symbol_mapper=taxon_symbol_mapper,
tree_factory=tree_factory):
pass
product = self.Product(
taxon_namespaces=None,
tree_lists=[tree_list],
char_matrices=None)
return product
def _get_rooting(self):
"""
Get rooting interpretation configuration.
"""
return self._rooting
def _set_rooting(self, val):
"""
Set rooting interpretation configuration.
"""
if val not in ("force-unrooted", "force-rooted", "default-unrooted", "default-rooted", None,):
raise ValueError("Unrecognized rooting directive: '{}'".format(val))
self._rooting = val
rooting = property(_get_rooting, _set_rooting)
def _parse_tree_statement(self,
nexus_tokenizer,
tree_factory,
taxon_symbol_map_fn):
"""
Parses a single tree statement from a token stream and constructs a
corresponding Tree object. Expects that the first non-comment and
non-semi-colon token to be found, including the current token, to be
the parenthesis that opens the tree statement. When complete, the
current token will be the token immediately following the semi-colon,
if any.
"""
current_token = nexus_tokenizer.current_token
tree_comments = nexus_tokenizer.pull_captured_comments()
while (current_token == ";" or current_token is None) and not nexus_tokenizer.is_eof():
current_token = nexus_tokenizer.require_next_token()
tree_comments = nexus_tokenizer.pull_captured_comments()
if nexus_tokenizer.is_eof():
return None
if current_token != "(":
# allow for possibility of single node tree, e.g.: T0:10;
self._parenthesis_nesting_level = 0
# raise NewickReader.NewickReaderInvalidTokenError(
# message="Expecting '{}' but found '{}'".format("(", current_token),
# line_num=nexus_tokenizer.token_line_num,
# col_num=nexus_tokenizer.token_column_num,
# stream=nexus_tokenizer.src)
else:
self._parenthesis_nesting_level = 1
tree = tree_factory()
self._process_tree_comments(tree, tree_comments, nexus_tokenizer)
self._tree_statement_complete = False
self._seen_taxa = set()
self._parse_tree_node_description(
nexus_tokenizer=nexus_tokenizer,
tree=tree,
current_node=tree.seed_node,
taxon_symbol_map_fn=taxon_symbol_map_fn,
is_internal_node=None)
current_token = nexus_tokenizer.current_token
if not self._tree_statement_complete:
raise NewickReader.NewickReaderIncompleteTreeStatementError(
message="Incomplete or improperly-terminated tree statement (last character read was '{}' instead of a semi-colon ';')".format(nexus_tokenizer.current_token),
line_num=nexus_tokenizer.token_line_num,
col_num=nexus_tokenizer.token_column_num,
stream=nexus_tokenizer.src)
self._seen_taxa = None
self._parenthesis_nesting_level = None
self._tree_statement_complete = None
while current_token == ";" and not nexus_tokenizer.is_eof():
nexus_tokenizer.clear_captured_comments()
current_token = nexus_tokenizer.next_token()
return tree
def _process_tree_comments(self, tree, tree_comments, nexus_tokenizer):
# NOTE: this also unconditionally sets the tree rootedness and
# weighting if no comment indicating these are found; for this to work
# in the current implementation, this method must be called once and
# exactly once per tree.
if not tree_comments:
tree.is_rooted = self._parse_tree_rooting_state("")
if self.store_tree_weights:
tree.weight = self.default_tree_weight
return
rooting_token_found = False
weighting_token_found = False
for comment in tree_comments:
stripped_comment = comment.strip()
if stripped_comment in ["&u", "&U", "&r", "&R"]:
tree.is_rooted = self._parse_tree_rooting_state(stripped_comment)
rooting_token_found = True
elif (self.store_tree_weights
and (stripped_comment.startswith("&W ") or stripped_comment.startswith("&w "))
):
try:
weight_expression = stripped_comment[2:]
if not weight_expression:
raise ValueError
we_parts = weight_expression.split("/")
if len(we_parts) > 2:
raise ValueError
# raise NewickReader.NewickReaderInvalidValueError(
# message="Invalid tree weight expression: '{}'".format(weight_expression),
# line_num=nexus_tokenizer.token_line_num,
# col_num=nexus_tokenizer.token_column_num,
# stream=nexus_tokenizer.src)
elif len(we_parts) == 2:
x = float(we_parts[0])
y = float(we_parts[1])
tree.weight = x/y
else:
tree.weight = float(we_parts[0])
weighting_token_found = True
except ValueError:
exc = NewickReader.NewickReaderInvalidValueError(
message="Invalid tree weight expression: '{}'".format(stripped_comment),
line_num=nexus_tokenizer.token_line_num,
col_num=nexus_tokenizer.token_column_num,
stream=nexus_tokenizer.src)
exc.__context__ = None # Python 3.0, 3.1, 3.2
exc.__cause__ = None # Python 3.3, 3.4
raise exc
elif self.extract_comment_metadata and comment.startswith("&"):
annotations = nexusprocessing.parse_comment_metadata_to_annotations(
comment=comment)
if annotations:
tree.annotations.update(annotations)
else:
tree.comments.append(comment)
else:
tree.comments.append(comment)
if not rooting_token_found:
tree.is_rooted = self._parse_tree_rooting_state("")
if self.store_tree_weights and not weighting_token_found:
tree.weight = self.default_tree_weight
def _parse_tree_rooting_state(self, rooting_comment=None):
"""
Returns rooting state for tree with given rooting comment token, taking
into account ``rooting`` configuration.
"""
if self._rooting == "force-unrooted":
return False
elif self._rooting == "force-rooted":
return True
elif rooting_comment == "&R" or rooting_comment == "&r":
return True
elif rooting_comment == "&U" or rooting_comment == "&u":
return False
elif self._rooting == "default-rooted":
return True
elif self._rooting == "default-unrooted":
return False
elif self._rooting is None:
return None
else:
raise TypeError("Unrecognized rooting directive: '{}'".format(self._rooting))
def _parse_tree_node_description(
self,
nexus_tokenizer,
tree,
current_node,
taxon_symbol_map_fn,
is_internal_node=None):
"""
Assuming that the iterator is currently sitting on a parenthesis that
opens a node with children or the label of a leaf node, this will
populate the node ``node`` appropriately (label, edge length, comments,
metadata etc.) and recursively parse and add the node's
children. When complete, the token will be the token immediately
following the end of the node or tree statement if this is the root
node, i.e. the token *following* the closing parenthesis of the node or
the semi-colon terminating a tree statement.
"""
current_node_comments = nexus_tokenizer.pull_captured_comments()
if nexus_tokenizer.current_token == "(":
# self._parenthesis_nesting_level += 1 # handled by calling code
nexus_tokenizer.require_next_token()
node_created = False
while True:
if nexus_tokenizer.current_token == ",":
if not node_created: #184
# no node has been created yet: ',' designates a
# preceding blank node
new_node = tree.node_factory()
nexusprocessing.process_comments_for_item(item=new_node,
item_comments=nexus_tokenizer.pull_captured_comments(),
extract_comment_metadata=self.extract_comment_metadata)
self._finish_node(new_node)
current_node.add_child(new_node)
## node_created = True # do not flag node as created to allow for an extra node to be created in the event of (..,)
nexus_tokenizer.require_next_token()
while nexus_tokenizer.current_token == ",": #192
# another blank node
new_node = tree.node_factory()
nexusprocessing.process_comments_for_item(item=new_node,
item_comments=nexus_tokenizer.pull_captured_comments(),
extract_comment_metadata=self.extract_comment_metadata)
self._finish_node(new_node)
current_node.add_child(new_node)
# node_created = True; # do not flag node as created: extra node needed in the event of (..,)
nexus_tokenizer.require_next_token()
if not node_created and nexus_tokenizer.current_token == ")": #200
# end of node
new_node = tree.node_factory();
nexusprocessing.process_comments_for_item(item=new_node,
item_comments=nexus_tokenizer.pull_captured_comments(),
extract_comment_metadata=self.extract_comment_metadata)
self._finish_node(new_node)
current_node.add_child(new_node)
node_created = True;
elif nexus_tokenizer.current_token == ")": #206
# end of child nodes
self._parenthesis_nesting_level -= 1
nexus_tokenizer.require_next_token()
break
else: #210
# assume child nodes: a leaf node (if a label) or
# internal (if a parenthesis)
if nexus_tokenizer.current_token == "(":
self._parenthesis_nesting_level += 1
is_new_internal_node = True
else:
is_new_internal_node = False
new_node = tree.node_factory();
nexusprocessing.process_comments_for_item(item=new_node,
item_comments=nexus_tokenizer.pull_captured_comments(),
extract_comment_metadata=self.extract_comment_metadata)
self._parse_tree_node_description(
nexus_tokenizer=nexus_tokenizer,
tree=tree,
current_node=new_node,
taxon_symbol_map_fn=taxon_symbol_map_fn,
is_internal_node=is_new_internal_node,
)
current_node.add_child(new_node);
node_created = True;
label_parsed = False
self._tree_statement_complete = False
if is_internal_node is None:
# Initial call using ``seed_node`` does not set ``is_internal_node`` to
# |True| or |False|, explicitly, but rather |None|. If this is the
# case, the rest of the tree has be constructed, and we simply look
# at whether there are children or not to determine if it is an
# internal node. This approach allows for a single-tip tree.
if current_node._child_nodes:
is_internal_node = True
if current_node_comments is None:
current_node_comments = []
while True:
cc = nexus_tokenizer.pull_captured_comments()
if cc is not None:
current_node_comments.extend(cc)
if nexus_tokenizer.current_token == ":": #246
nexus_tokenizer.require_next_token()
if not self.suppress_edge_lengths:
try:
edge_length = self.edge_length_type(nexus_tokenizer.current_token)
except ValueError:
raise NewickReader.NewickReaderMalformedStatementError(
message="Invalid edge length: '{}'".format(nexus_tokenizer.current_token),
line_num=nexus_tokenizer.token_line_num,
col_num=nexus_tokenizer.token_column_num,
stream=nexus_tokenizer.src)
current_node.edge.length = edge_length
try:
nexus_tokenizer.require_next_token()
except tokenizer.Tokenizer.UnexpectedEndOfStreamError as e:
if self.terminating_semicolon_required:
message = e.message + ". (Perhaps the terminating semicolon for the tree statement is missing? If so, add a semicolon to the tree statement or specify 'terminating_semicolon_required=False' to allow for missing semicolons)"
raise tokenizer.Tokenizer.UnexpectedEndOfStreamError(
message=message,
line_num=e.line_num,
col_num=e.col_num,
stream=e.stream)
else:
self._tree_statement_complete = True
break
elif nexus_tokenizer.current_token == ")": #253
# closing of parent token
# self._parenthesis_nesting_level -= 1 # handled by calling code
nexusprocessing.process_comments_for_item(item=current_node,
item_comments=current_node_comments,
extract_comment_metadata=self.extract_comment_metadata)
self._finish_node(current_node)
return current_node
elif nexus_tokenizer.current_token == ";": #256
# end of tree statement
self._tree_statement_complete = True
nexus_tokenizer.next_token()
break
elif nexus_tokenizer.current_token == ",": #260
# end of this node
nexusprocessing.process_comments_for_item(item=current_node,
item_comments=current_node_comments,
extract_comment_metadata=self.extract_comment_metadata)
self._finish_node(current_node)
return current_node
elif nexus_tokenizer.current_token == "(": #263
# start of another node or tree without finishing this
# node
self._parenthesis_nesting_level += 1
raise NewickReader.NewickReaderMalformedStatementError(
message="Malformed tree statement",
line_num=nexus_tokenizer.token_line_num,
col_num=nexus_tokenizer.token_column_num,
stream=nexus_tokenizer.src)
elif self.is_parse_jplace_tokens and nexus_tokenizer.current_token == '{':
# Edge number from .jplace format
nexus_tokenizer.require_next_token()
edge_number = int(nexus_tokenizer.current_token)
edge = current_node.edge
edge.edge_number = edge_number
try:
tree.edge_index.insert(edge_number, edge)
except AttributeError:
tree.edge_index = []
tree.edge_index.insert(edge_number, edge)
nexus_tokenizer.require_next_token() # for closing '}'
nexus_tokenizer.require_next_token()
else: #267
# label
if label_parsed: #269
msg = "Expecting ':'"
if self.is_parse_jplace_tokens:
msg += ", '{'"
msg += ", ')', ',' or ';' after reading label but found '{}'".format(nexus_tokenizer.current_token)
raise NewickReader.NewickReaderMalformedStatementError(
message=msg,
line_num=nexus_tokenizer.token_line_num,
col_num=nexus_tokenizer.token_column_num,
stream=nexus_tokenizer.src)
else:
# Label
label = nexus_tokenizer.current_token
if ( (is_internal_node and self.suppress_internal_node_taxa)
or ((not is_internal_node) and self.suppress_leaf_node_taxa) ):
if self.is_assign_internal_labels_to_edges:
current_node.edge.label = label
else:
current_node.label = label
else:
node_taxon = taxon_symbol_map_fn(label)
if node_taxon in self._seen_taxa:
raise NewickReader.NewickReaderDuplicateTaxonError(
message=node_taxon.label,
line_num=nexus_tokenizer.token_line_num,
col_num=nexus_tokenizer.token_column_num,
stream=nexus_tokenizer.src)
self._seen_taxa.add(node_taxon)
current_node.taxon = node_taxon
label_parsed = True;
# nexus_tokenizer.require_next_token()
try:
nexus_tokenizer.require_next_token()
except tokenizer.Tokenizer.UnexpectedEndOfStreamError:
if self.terminating_semicolon_required:
raise
else:
break
## if we are here, we have reached the end of the tree
if self._parenthesis_nesting_level != 0:
raise NewickReader.NewickReaderMalformedStatementError(
message="Unbalanced parentheses at tree statement termination: balance index = {}".format(self._parenthesis_nesting_level),
line_num=nexus_tokenizer.token_line_num,
col_num=nexus_tokenizer.token_column_num,
stream=nexus_tokenizer.src)
nexusprocessing.process_comments_for_item(item=current_node,
item_comments=current_node_comments,
extract_comment_metadata=self.extract_comment_metadata)
self._finish_node(current_node)
return current_node
def _finish_node(self, node):
if self.finish_node_fn is not None:
self.finish_node_fn(node)
|