def parsimony_up2(subtree, nodelist, parent, siblings):
    """parsimony part: up direction -> from root to leafs"""
    # Arguments:
    #   subtree
    #                   0   1       2           3           4
    #   nodelist    - [id, depth, originaltag, finaltag, calc[taglist]]
    #   parent      - nodelist element
    #   siblings     - [nodelist element]
    element = find_element_in_nodelist(subtree.name, nodelist)

    parent_tag = parent[4][
        0]  # parent[4] could look like [['0', '1'], ['1']] or [['1']]
    both_tags = []
    both_tags.append(parent_tag)
    for sibling in siblings:
        both_tags.append(sibling[4][0])

    # get intersection or union
    tag_list = get_intersect_or_union(both_tags)
    # add new tag
    element[4].append(tag_list)

    # go on with children
    if not subtree.is_terminal():
        children = []
        for clade in subtree.clades:
            child = find_element_in_nodelist(clade.name, nodelist)
            children.append(child)
        for i in range(0, len(subtree.clades)):
            clade = subtree.clades[i]
            child = find_element_in_nodelist(clade.name, nodelist)
            sublist = deepcopy(children)
            del sublist[i]
            parsimony_up2(clade, nodelist, element, sublist)
    return
Exemple #2
0
def parsimony_down(subtree, nodelist):
    """parsimony part: down direction -> from leafs to root"""
    # Arguments:
    #   subtree
    #                   0   1       2           3           4
    #   nodelist    - [id, depth, originaltag, finaltag, calc[taglist]]
    child_tags = []
    for clade in subtree.clades:
        child = find_element_in_nodelist(clade.name, nodelist)
        # if child is not tagged, first tag it:
        if child[4] == []:
            parsimony_down(clade, nodelist)
        child_tags.append(child[4][0])
    element = find_element_in_nodelist(subtree.name, nodelist)
    # get intersection or union
    tag_list = get_intersect_or_union(child_tags)
    # add new tag
    element[4].append(tag_list)
    return
Exemple #3
0
def parsimony_final(subtree, nodelist):
    """parsimony final part: combine multiple tags of node to one final tag"""
    # Arguments:
    #   subtree
    #                   0   1       2           3           4
    #   nodelist    - [id, depth, originaltag, finaltag, calc[taglist]]
    element = find_element_in_nodelist(subtree.name, nodelist)
    if subtree.is_terminal() and len(element[4][0]) == 1:
        element[3] = element[4][0][0]
    else:
        # get intersection or union
        tag_list = get_intersect_or_union(element[4])
        # add final tag
        tag_string = ""
        for tag in tag_list:
            tag_string += str(tag) + "&"
        tag_string = tag_string[:len(tag_string)-1]
        element[3] = tag_string
    # go on with children
    if not subtree.is_terminal(): 
        for clade in subtree.clades:
            parsimony_final(clade, nodelist)
    return