<commit_before>#!/usr/bin/env python

"""
Rooting class
"""

from .utils import NodeAssist, ToytreeError


class Rooter:
    def __init__(self, tree, nastuple, resolve_root_dist, edge_features):
        """
        See docstring in ToyTree.root()
        """
        # store args
        self.tree = tree
        self.resolve_root_dist = resolve_root_dist
        self.edge_features = edge_features
        self.features = {"name", "dist", "support"}
        self.nas = None

        # dict for relabeling nodes {node: [parent, child, feat]}
        self.tdict = {}

        # the new node that will be inserted
        self.nnode = None

        # make a copy and ensure supports are either all int or float
        self.maxsup = max([int(i.support) for i in self.tree.treenode.traverse()])
        self.maxsup = (1.0 if self.maxsup <= 1.0 else 100)
        self.get_features()

        # parse node selecting arguments (nastuple) with NodeAssist
        self.get_match(*nastuple)

        # get the mrca node (or tip node) of the monopyletic matched query.
        self.node1 = self.nas.get_mrca()

        # the node on the other side of the edge to be split.
        self.node2 = self.node1.up

        # if rooting where root already exists then return current tree
        x0 = (self.node1.is_root())
        x1 = (self.node2.is_root() and self.tree.is_rooted())
        if x0 or x1:
            return self

        # create new root node on an existing edge to split it.
        self.insert_new_node()

        # update edge lengths given new node insertion
        self.config_root_dist()

        # redirect support values and possibly others
        self.redirect_edge_features()
        self.restructure_tree()

        # update coodrds on tree
        self.update_tree_from_tdict()
        self.update()
<commit_msg>fix error when rooting on existing root<commit_after>#!/usr/bin/env python

"""
Rooting class
"""

from .utils import NodeAssist, ToytreeError


class Rooter:
    def __init__(self, tree, nastuple, resolve_root_dist, edge_features):
        """
        See docstring in ToyTree.root()
        """
        # store args
        self.tree = tree
        self.resolve_root_dist = resolve_root_dist
        self.edge_features = edge_features
        self.features = {"name", "dist", "support"}
        self.nas = None

        # dict for relabeling nodes {node: [parent, child, feat]}
        self.tdict = {}

        # the new node that will be inserted
        self.nnode = None

        # make a copy and ensure supports are either all int or float
        self.maxsup = max([int(i.support) for i in self.tree.treenode.traverse()])
        self.maxsup = (1.0 if self.maxsup <= 1.0 else 100)
        self.get_features()

        # parse node selecting arguments (nastuple) with NodeAssist
        self.get_match(*nastuple)

        # get the mrca node (or tip node) of the monopyletic matched query.
        self.node1 = self.nas.get_mrca()

        # the node on the other side of the edge to be split.
        self.node2 = self.node1.up

        # if rooting where root already exists then return current tree
        x0 = (self.node1.is_root())
        x1 = (self.node2.is_root() and self.tree.is_rooted())
        if not (x0 or x1):           

            # create new root node on an existing edge to split it.
            self.insert_new_node()

            # update edge lengths given new node insertion
            self.config_root_dist()

            # redirect support values and possibly others
            self.redirect_edge_features()
            self.restructure_tree()

            # update coodrds on tree
            self.update_tree_from_tdict()
            self.update()

{'fraction_non_alphanumeric': 0.08770513855259618, 'fraction_numerical': 0.008609093354856066, 'mean_word_length': 2.1217464315701093, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2083159', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1072, 'n_words': 347}