Exemple #1
0
def read_tree_string(s, return_end_point=False, starting_point=0):
    tree = arbre.Tree()
    tree.id = 1
    tree.root = tree
    node = tree
    k = starting_point
    while k < len(s):
        c = s[k]
        if c == "(":
            node.left = arbre.Tree()
            node.left.parent = node
            node.left.root = node.root
            node.left.id = node.id * 2
            node = node.left
        elif c == ",":
            node = node.parent

            if node.right == None:
                node.right = arbre.Tree()
                node.right.parent = node
                node.right.root = node.root
                node.right.id = node.id * 2 + 1
                node = node.right
            else:
                if node.right2 == None:
                    node.right2 = arbre.Tree()
                    node.right2.parent = node
                    node.right2.root = node.root
                    node.right2.id = node.id * 2 + 1.5  #noeud qui apparait quand l'arbre n'est pas raciné
                    node = node.right2
                else:
                    print("Wasn't expecting multifurcating tree")
        elif c == ")":
            node = node.parent
        elif c == ";":
            if return_end_point:
                #print(s[k],k,len(s))
                #print("sk", s[k:])
                return tree, k + 2
            else:
                return tree
        else:
            name = ""
            while not c in ["(", ")", ",", ":", ";"]:
                name += c
                k += 1
                c = s[k]
            if c == ":":
                while not c in ["(", ")", ",", ";"]:
                    k += 1
                    c = s[k]
            if len(name) > 0:
                node.name = name
            k -= 1
        k += 1
def generate_tree(freq_by_branch, orig_prob, plus_S=0):
    #origination
    orig_branch=(rd.choices([u for u in orig_prob], weights=[orig_prob[u] for u in orig_prob]))[0]
    r=dict()
    tree=arbre.Tree()
    r[tree]=[orig_branch]
    to_see=[tree]
    while len(to_see)>0:
        u=to_see.pop()
        #e is the last host of u
        e=r[u][-1]
        weights_to_use=[]
        for E in freq_by_branch[e]:
            E2=E
            if type(E)==tuple:
                E2,h=E
            if E2=="SL" or E2=="S" or E2=="E":
                weights_to_use.append(freq_by_branch[e][E]+plus_S)
            else:
                weights_to_use.append(freq_by_branch[e][E])
        #E=rd.choices([E for E in freq_by_branch[e]], weights=[freq_by_branch[e][E] for E in freq_by_branch[e]])[0]
        E=rd.choices([E for E in freq_by_branch[e]], weights=weights_to_use)[0]

        if type(E)==tuple:
            E,h=E
        if E=="S":
            u.undated_birth()
            r[u.right]=[e.right]
            r[u.left]=[e.left]
            to_see.append(u.left)
            to_see.append(u.right)
        if E=="D":
            u.undated_birth()
            r[u.right]=[e]
            r[u.left]=[e]
            to_see.append(u.left)
            to_see.append(u.right)
        if E=="SL":
            r[u].append(h)
            to_see.append(u)
        if E=="T":
            u.undated_birth()
            r[u.left]=[e]
            r[u.right]=[h]
            to_see.append(u.left)
            to_see.append(u.right)
        if E=="TL":
            r[u].append(h)
            to_see.append(u)
        #if E=="E"#on s'arrête, rien à faire
    for u in tree.leaves():
        u.match=r[u][-1]
    return tree
Exemple #3
0
def read_tree_string_sagephy(s, more_output=False):
    nodes_info = dict()
    tree = arbre.Tree()
    tree.id = 1
    tree.root = tree
    node = tree
    k = 0
    while k < len(s):
        c = s[k]
        if c == "(":
            node.left = arbre.Tree()
            node.left.parent = node
            node.left.root = node.root
            node.left.id = node.id * 2
            node = node.left
        elif c == ",":
            node = node.parent

            if node.right == None:
                node.right = arbre.Tree()
                node.right.parent = node
                node.right.root = node.root
                node.right.id = node.id * 2 + 1
                node = node.right
            else:
                if node.right2 == None:
                    node.right2 = arbre.Tree()
                    node.right2.parent = node
                    node.right2.root = node.root
                    node.right2.id = node.id * 2 + 1.5  #noeud qui apparait quand l'arbre n'est pas raciné
                    node = node.right2
                else:
                    print("Wasn't expecting multifurcating tree")
        elif c == ")":
            node = node.parent
        elif c == ";":
            if more_output:
                return tree, nodes_info
            else:
                return tree
        else:
            name = ""
            while not c in ["(", ")", ",", ":", ";", "[", "]"]:
                name += c
                k += 1
                c = s[k]
            if c == ":":
                while not c in ["(", ")", ",", ";", "[", "]"]:
                    k += 1
                    c = s[k]
            if c == "[":
                words, k = read_from_to(s, k + 1, ["]"])
                l_word = words.split(sep=" ")
                l_word2 = []
                for u in l_word:
                    l_word2.append(u.split(sep="="))
                if not node in nodes_info:
                    #voir ce qu'on fait avec les infos dans l'autre cas
                    nodes_info[node] = l_word2

            if len(name) > 0:
                node.name = name
            k -= 1
        k += 1
def generate_tree_SPR2(host,freq_by_branch, orig_prob, plus_S=0):
    tree=host.copy()
    freq_by_branch_with_name=dict()
    for e in freq_by_branch:
        freq_by_branch_with_name[e.name]=dict()
        for E in freq_by_branch[e]:
            freq_by_branch_with_name[e.name][E]=freq_by_branch[e][E]
    freq_by_branch=freq_by_branch_with_name
    r=dict()
    name_to_tree_host=dict()
    for u in host.liste():
        name_to_tree_host[u.name]=u
        print(u.name)
    name_to_tree=dict()
    for u in tree.liste():
        print(u.name)
        name_to_tree[u.name]=u
    l_name=[e_name for e_name in freq_by_branch]
    rd.shuffle(l_name)
    for e_name in l_name:
        if e_name in [u.name for u in tree.liste()]:
            e=name_to_tree[e_name]
            weights_to_use=[]
            for E in freq_by_branch[e_name]:
                E2=E
                if type(E)==tuple:
                    E2,h=E
                if E2=="SL" or E2=="S" or E2=="E":
                    weights_to_use.append(freq_by_branch[e_name][E]+plus_S)
                else:
                    weights_to_use.append(freq_by_branch[e_name][E])
            E=rd.choices([E for E in freq_by_branch[e_name]], weights=weights_to_use)[0]
            if type(E)==tuple:
                E,h=E
            if E=="T" or E=="TL":
                print("once")
                print(e.name, e.isLeaf())
                if e.isLeaf():
                    e.left=arbre.Tree()
                    e.left.name=e.name
                    e.left.parent=e
                    e.right=h
                else:
                    tmpl=e.left
                    print(type(e.left))
                    tmpr=e.right
                    e.left=arbre.Tree()
                    e.left.left=tmpl
                    e.left.right=tmpl
                    tmpl.parent=e.left
                    tmpr.parent=e.left
                    e.right=h
                print(h.parent.name)
                print(h.name)
                print(h.parent.left.name, h.parent.right.name)
                if h.parent.left==h:
                    sibling=h.parent.right
                else:
                    sibling=h.parent.left
                if sibling.isLeaf():
                    sibling.parent.name=sibling
                    sibling.parent.left=None
                    sibling.parent.right=None
                else:
                    sibling.parent.left=sibling.left
                    sibling.parent.right=sibling.right
                    sibling.parent.left.parent=sibling.parent
                    sibling.parent.right.parent=sibling.parent
                h.parent=e

    for u in tree.leaves():
        u.match=name_to_tree_host[u.name]
    return tree
def generate_tree_SPR(freq_by_branch, orig_prob):
    #origination
    orig_branch=(rd.choices([u for u in orig_prob], weights=[orig_prob[u] for u in orig_prob]))[0]
    r=dict()
    tree=arbre.Tree()
    tree.time=0.1
    r[tree]=[orig_branch]
    to_see=[tree]
    upper_seen=dict()
    upper_seen[orig_branch]=True
    while len(to_see)>0:
        u=to_see.pop(rd.randrange(len(to_see)))
        #u=to_see.pop()
        #e is the last host of u
        e=r[u][-1]
        E=rd.choices([E for E in freq_by_branch[e]], weights=[freq_by_branch[e][E] for E in freq_by_branch[e]])[0]
        if type(E)==tuple:
            E,h=E
        if E=="S":
            if e.right in upper_seen:
                if not e.left in upper_seen:
                    E=="SL"
                    h=e.left
            if e.left in upper_seen:
                if not e.right in upper_seen:
                    E=="SL"
                    h=e.right
            if not(e.left in upper_seen or e.right in upper_seen):
                u.birth(0.5)
                u.left.time=0.5
                u.right.time=0.5
                r[u.right]=[e.right]
                r[u.left]=[e.left]
                to_see.append(u.left)
                to_see.append(u.right)
                upper_seen[e.right]=True
                upper_seen[e.left]=True
        if E=="T":
            if not h in upper_seen:
                u.birth(0.5)
                u.left.time=0.5
                u.right.time=0.5
                r[u.left]=[e]
                r[u.right]=[h]
                to_see.append(u.left)
                to_see.append(u.right)
                upper_seen[h]=True
        if E=="SL":
            if not h in upper_seen:
                r[u].append(h)
                to_see.append(u)
        if E=="TL":
            if not h in upper_seen:
                r[u].append(h)
                to_see.append(u)
                upper_seen[h]=True
        if E=="E":
            u.time=1
    for u in tree.leaves():
        u.match=r[u][-1]
    tree.prune_tree()
    return tree