Example #1
0
def _label(tree, ancestors):
    """
    Relabel internal nodes of a tree and map them to the corresponding name of 
    ancestral sequences.
    
    :param tree: str, a NEWICK format string or file for a tree (must start with
        "(" and end with ';').
    :param ancestors: dict, a dict object stores sequences.
    :return: tuple, a relabeled tree object and a dict object for sequences.
    """
    
    if isinstance(tree, str):
        if os.path.isfile(tree):
            pass
        elif tree.startswith('(') and tree.endswith(';'):
            tree = StringIO(tree)
        else:
            error('Invalid tree encounter, tree relabel aborted.')
            sys.exit(1)
        tree = Phylo.read(tree, 'newick')
    
    number, maps = tree.count_terminals(), {}
    for clade in tree.find_clades():
        if not clade.is_terminal():
            clade.confidence = None
            number += 1
            old, new = clade.name, 'NODE{}'.format(number)
            maps[old] = new
            clade.name = new
    
    ancestors = {maps.get(k, k): v for k, v in ancestors.items()}
    return tree, ancestors