Example #1
0
 def compose_node(self, node):
     """
     Given a DendroPy Node, this returns the Node as a NEWICK
     statement according to the class-defined formatting rules.
     """
     child_nodes = node.child_nodes()
     if child_nodes:
         subnodes = [self.compose_node(child) for child in child_nodes]
         statement = '(' + ','.join(subnodes) + ')'
         if not (self.suppress_internal_taxon_labels and self.suppress_internal_node_labels):
             statement = statement + self.choose_display_tag(node)
         if node.edge and node.edge.length != None and not self.suppress_edge_lengths:
             statement =  "%s:%s" % (statement, self.edge_label_compose_func(node.edge))
     else:
         statement = self.choose_display_tag(node)
         if node.edge and node.edge.length != None and not self.suppress_edge_lengths:
             statement =  "%s:%s" % (statement, self.edge_label_compose_func(node.edge))
     if not self.suppress_annotations or self.annotations_as_nhx:
         node_annotation_comments = nexustokenizer.format_annotation_as_comments(node, nhx=self.annotations_as_nhx)
         edge_annotation_comments = nexustokenizer.format_annotation_as_comments(node.edge, nhx=self.annotations_as_nhx)
         statement = statement + node_annotation_comments + edge_annotation_comments
     #if self.nhx_key_to_func:
     #    nhx_to_print = []
     #    for k, v in self.nhx_key_to_func.items():
     #        r = v(node.edge)
     #        if r is not None:
     #            nhx_to_print.append("%s=%s" % (k, str(r)))
     #    if nhx_to_print:
     #        statement = statement + ('[&&NHX:%s]' % ':'.join(nhx_to_print))
     node_comment_str = self.compose_comment_string(node)
     statement += node_comment_str
     return statement
 def write_taxa_block(self, taxon_set, stream):
     block = []
     block.append("BEGIN TAXA;")
     if not self.suppress_annotations:
         block.append(nexustokenizer.format_annotation_as_comments(taxon_set, nhx=self.annotations_as_nhx))
     if self._link_blocks():
         title = self.compose_block_title(taxon_set)
         if title:
             block.append("    %s;" % title)
     block.append("    DIMENSIONS NTAX=%d;" % len(taxon_set))
     block.append("    TAXLABELS")
     for taxon in taxon_set:
         if self.suppress_annotations:
             annotation_str = ""
         else:
             annotation_str = " " + nexustokenizer.format_annotation_as_comments(taxon, nhx=self.annotations_as_nhx)
         block.append(
             "        %s%s"
             % (
                 textutils.escape_nexus_token(
                     taxon.label,
                     preserve_spaces=self.preserve_spaces,
                     quote_underscores=not self.unquoted_underscores,
                 ),
                 annotation_str,
             )
         )
     block.append("  ;")
     block.append("END;\n\n")
     stream.write("\n".join(block))
Example #3
0
 def write_taxa_block(self, taxon_set, stream):
     block = []
     block.append('BEGIN TAXA;')
     if not self.suppress_annotations:
         block.append(
             nexustokenizer.format_annotation_as_comments(
                 taxon_set, nhx=self.annotations_as_nhx))
     if self._link_blocks():
         title = self.compose_block_title(taxon_set)
         if title:
             block.append('    %s;' % title)
     block.append('    DIMENSIONS NTAX=%d;' % len(taxon_set))
     block.append('    TAXLABELS')
     for taxon in taxon_set:
         if self.suppress_annotations:
             annotation_str = ""
         else:
             annotation_str = " " + nexustokenizer.format_annotation_as_comments(
                 taxon, nhx=self.annotations_as_nhx)
         block.append('        %s%s' % (textutils.escape_nexus_token(
             taxon.label,
             preserve_spaces=self.preserve_spaces,
             quote_underscores=not self.unquoted_underscores),
                                        annotation_str))
     block.append('  ;')
     block.append('END;\n\n')
     stream.write('\n'.join(block))
Example #4
0
 def write_tree(self, tree, stream):
     """
     Composes and writes `tree` to `stream`.
     """
     if tree.rooting_state_is_undefined or self.suppress_rooting:
         rooting = ""
     elif tree.is_rooted:
         rooting = "[&R] "
     elif not tree.is_rooted:
         rooting = "[&U] "
     else:
         rooting = ""
     if self.store_tree_weights and tree.weight is not None:
         weight = "[&W %s] " % tree.weight
     else:
         weight = ""
     if not self.suppress_annotations or self.annotations_as_nhx:
         annotation_comments = nexustokenizer.format_annotation_as_comments(tree, nhx=self.annotations_as_nhx)
     else:
         annotation_comments = ""
     tree_comments = self.compose_comment_string(tree)
     stream.write("%s%s%s%s%s;\n" % (rooting,
             weight,
             annotation_comments,
             tree_comments,
             self.compose_node(tree.seed_node)))
Example #5
0
 def write_tree(self, tree, stream):
     """
     Composes and writes `tree` to `stream`.
     """
     if tree.rooting_state_is_undefined or self.suppress_rooting:
         rooting = ""
     elif tree.is_rooted:
         rooting = "[&R] "
     elif not tree.is_rooted:
         rooting = "[&U] "
     else:
         rooting = ""
     if self.store_tree_weights and tree.weight is not None:
         weight = "[&W %s] " % tree.weight
     else:
         weight = ""
     if not self.suppress_annotations or self.annotations_as_nhx:
         annotation_comments = nexustokenizer.format_annotation_as_comments(
             tree, nhx=self.annotations_as_nhx)
     else:
         annotation_comments = ""
     tree_comments = self.compose_comment_string(tree)
     stream.write("%s%s%s%s%s;\n" %
                  (rooting, weight, annotation_comments, tree_comments,
                   self.compose_node(tree.seed_node)))
    def write(self, stream):
        """
        Writes attached `DataSource` or `TaxonDomain` to the file-like object
        `stream`.
        """

        assert self.dataset is not None, "NexusWriter instance is not attached to a DataSet: no source of data"

        if (
            self.is_write_block_titles == False
            and self.attached_taxon_set is None
            and (len(self.dataset.taxon_sets) > 1)
        ):
            _LOG.warn(
                "Multiple taxon sets in data, but directed not to write block titles: data file may not be interpretable"
            )

        stream.write("#NEXUS\n\n")
        if self.file_comments is not None:
            if isinstance(self.file_comments, list):
                for line in self.file_comments:
                    if line.strip().replace("\n", "").replace("\r", ""):
                        stream.write("[ %s ]\n" % line)
                    else:
                        stream.write("\n")
                stream.write("\n")
            else:
                stream.write("[ %s ]\n\n" % self.file_comments)
        if not self.suppress_annotations:
            stream.write(nexustokenizer.format_annotation_as_comments(self.dataset, nhx=self.annotations_as_nhx))
            stream.write("\n")
        if self.preamble_blocks:
            for block in self.preamble_blocks:
                stream.write(block)
                stream.write("\n")
            stream.write("\n")
        if (
            (
                ((not self.exclude_chars) and self.dataset.char_matrices)
                or ((not self.exclude_trees) and self.dataset.tree_lists)
            )
            and (not self.simple)
            and (not self.suppress_taxa_block)
        ):
            for taxon_set in self.dataset.taxon_sets:
                if self.attached_taxon_set is None or taxon_set is self.attached_taxon_set:
                    self.write_taxa_block(taxon_set, stream=stream)
        if not self.exclude_chars:
            for char_matrix in self.dataset.char_matrices:
                if self.attached_taxon_set is None or char_matrix.taxon_set is self.attached_taxon_set:
                    self.write_char_block(char_matrix=char_matrix, stream=stream)
        if not self.exclude_trees:
            for tree_list in self.dataset.tree_lists:
                if self.attached_taxon_set is None or tree_list.taxon_set is self.attached_taxon_set:
                    self.write_trees_block(tree_list=tree_list, stream=stream)
        if self.supplemental_blocks:
            for block in self.supplemental_blocks:
                stream.write(block)
                stream.write("\n")
Example #7
0
    def write(self, stream):
        """
        Writes attached `DataSource` or `TaxonDomain` to the file-like object
        `stream`.
        """

        assert self.dataset is not None, \
            "NexusWriter instance is not attached to a DataSet: no source of data"

        if self.is_write_block_titles == False \
                and self.attached_taxon_set is None \
                and (len(self.dataset.taxon_sets) > 1):
            _LOG.warn(
                "Multiple taxon sets in data, but directed not to write block titles: data file may not be interpretable"
            )

        stream.write('#NEXUS\n\n')
        if self.file_comments is not None:
            if isinstance(self.file_comments, list):
                for line in self.file_comments:
                    if line.strip().replace("\n", "").replace("\r", ""):
                        stream.write("[ %s ]\n" % line)
                    else:
                        stream.write("\n")
                stream.write("\n")
            else:
                stream.write("[ %s ]\n\n" % self.file_comments)
        if not self.suppress_annotations:
            stream.write(
                nexustokenizer.format_annotation_as_comments(
                    self.dataset, nhx=self.annotations_as_nhx))
            stream.write("\n")
        if self.preamble_blocks:
            for block in self.preamble_blocks:
                stream.write(block)
                stream.write("\n")
            stream.write("\n")
        if (( (not self.exclude_chars) and self.dataset.char_matrices) \
                or ( (not self.exclude_trees) and self.dataset.tree_lists)) \
                and (not self.simple) \
                and (not self.suppress_taxa_block):
            for taxon_set in self.dataset.taxon_sets:
                if self.attached_taxon_set is None or taxon_set is self.attached_taxon_set:
                    self.write_taxa_block(taxon_set, stream=stream)
        if not self.exclude_chars:
            for char_matrix in self.dataset.char_matrices:
                if self.attached_taxon_set is None or char_matrix.taxon_set is self.attached_taxon_set:
                    self.write_char_block(char_matrix=char_matrix,
                                          stream=stream)
        if not self.exclude_trees:
            for tree_list in self.dataset.tree_lists:
                if self.attached_taxon_set is None or tree_list.taxon_set is self.attached_taxon_set:
                    self.write_trees_block(tree_list=tree_list, stream=stream)
        if self.supplemental_blocks:
            for block in self.supplemental_blocks:
                stream.write(block)
                stream.write("\n")
 def write_trees_block(self, tree_list, stream):
     block = []
     newick_writer = newick.NewickWriter(
         suppress_rooting=self.suppress_rooting,
         suppress_edge_lengths=self.suppress_edge_lengths,
         unquoted_underscores=self.unquoted_underscores,
         preserve_spaces=self.preserve_spaces,
         store_tree_weights=self.store_tree_weights,
         suppress_annotations=self.suppress_annotations,
         annotations_as_nhx=self.annotations_as_nhx,
         suppress_item_comments=self.suppress_item_comments,
         suppress_leaf_taxon_labels=self.suppress_leaf_taxon_labels,
         suppress_leaf_node_labels=self.suppress_leaf_node_labels,
         suppress_internal_taxon_labels=self.suppress_internal_taxon_labels,
         suppress_internal_node_labels=self.suppress_internal_node_labels,
         node_label_element_separator=self.node_label_element_separator,
         node_label_compose_func=self.node_label_compose_func,
         edge_label_compose_func=self.edge_label_compose_func,
     )
     block.append("BEGIN TREES;")
     if not self.suppress_annotations:
         block.append(nexustokenizer.format_annotation_as_comments(tree_list, nhx=self.annotations_as_nhx))
     if self._link_blocks():
         title = self.compose_block_title(tree_list)
         if title:
             block.append("    %s;" % title)
         if tree_list.taxon_set.labels:
             block.append(
                 "    LINK TAXA = %s;"
                 % textutils.escape_nexus_token(
                     tree_list.taxon_set.label,
                     preserve_spaces=self.preserve_spaces,
                     quote_underscores=not self.unquoted_underscores,
                 )
             )
     for treeidx, tree in enumerate(tree_list):
         if tree.label:
             tree_name = tree.label
         else:
             tree_name = str(treeidx)
         newick_str = newick_writer.compose_tree(tree)
         block.append(
             "    TREE %s = %s"
             % (
                 textutils.escape_nexus_token(
                     tree_name, preserve_spaces=self.preserve_spaces, quote_underscores=not self.unquoted_underscores
                 ),
                 newick_str,
             )
         )
     block.append("END;\n\n")
     stream.write("\n".join(block))
Example #9
0
 def compose_node(self, node):
     """
     Given a DendroPy Node, this returns the Node as a NEWICK
     statement according to the class-defined formatting rules.
     """
     child_nodes = node.child_nodes()
     if child_nodes:
         subnodes = [self.compose_node(child) for child in child_nodes]
         statement = '(' + ','.join(subnodes) + ')'
         if not (self.suppress_internal_taxon_labels
                 and self.suppress_internal_node_labels):
             statement = statement + self.choose_display_tag(node)
         if node.edge and node.edge.length != None and not self.suppress_edge_lengths:
             statement = "%s:%s" % (statement,
                                    self.edge_label_compose_func(node.edge))
     else:
         statement = self.choose_display_tag(node)
         if node.edge and node.edge.length != None and not self.suppress_edge_lengths:
             statement = "%s:%s" % (statement,
                                    self.edge_label_compose_func(node.edge))
     if not self.suppress_annotations or self.annotations_as_nhx:
         node_annotation_comments = nexustokenizer.format_annotation_as_comments(
             node, nhx=self.annotations_as_nhx)
         edge_annotation_comments = nexustokenizer.format_annotation_as_comments(
             node.edge, nhx=self.annotations_as_nhx)
         statement = statement + node_annotation_comments + edge_annotation_comments
     #if self.nhx_key_to_func:
     #    nhx_to_print = []
     #    for k, v in self.nhx_key_to_func.items():
     #        r = v(node.edge)
     #        if r is not None:
     #            nhx_to_print.append("%s=%s" % (k, str(r)))
     #    if nhx_to_print:
     #        statement = statement + ('[&&NHX:%s]' % ':'.join(nhx_to_print))
     node_comment_str = self.compose_comment_string(node)
     statement += node_comment_str
     return statement
Example #10
0
 def write_trees_block(self, tree_list, stream):
     block = []
     newick_writer = newick.NewickWriter(
         suppress_rooting=self.suppress_rooting,
         suppress_edge_lengths=self.suppress_edge_lengths,
         unquoted_underscores=self.unquoted_underscores,
         preserve_spaces=self.preserve_spaces,
         store_tree_weights=self.store_tree_weights,
         suppress_annotations=self.suppress_annotations,
         annotations_as_nhx=self.annotations_as_nhx,
         suppress_item_comments=self.suppress_item_comments,
         suppress_leaf_taxon_labels=self.suppress_leaf_taxon_labels,
         suppress_leaf_node_labels=self.suppress_leaf_node_labels,
         suppress_internal_taxon_labels=self.suppress_internal_taxon_labels,
         suppress_internal_node_labels=self.suppress_internal_node_labels,
         node_label_element_separator=self.node_label_element_separator,
         node_label_compose_func=self.node_label_compose_func,
         edge_label_compose_func=self.edge_label_compose_func,
     )
     block.append('BEGIN TREES;')
     if not self.suppress_annotations:
         block.append(
             nexustokenizer.format_annotation_as_comments(
                 tree_list, nhx=self.annotations_as_nhx))
     if self._link_blocks():
         title = self.compose_block_title(tree_list)
         if title:
             block.append('    %s;' % title)
         if tree_list.taxon_set.labels:
             block.append(
                 '    LINK TAXA = %s;' % textutils.escape_nexus_token(
                     tree_list.taxon_set.label,
                     preserve_spaces=self.preserve_spaces,
                     quote_underscores=not self.unquoted_underscores))
     for treeidx, tree in enumerate(tree_list):
         if tree.label:
             tree_name = tree.label
         else:
             tree_name = str(treeidx)
         newick_str = newick_writer.compose_tree(tree)
         block.append('    TREE %s = %s' % (textutils.escape_nexus_token(
             tree_name,
             preserve_spaces=self.preserve_spaces,
             quote_underscores=not self.unquoted_underscores), newick_str))
     block.append('END;\n\n')
     stream.write('\n'.join(block))