Esempio n. 1
0
def init_leafs(G, H, k, H_number_of_nodes, sigma, nodes_table):
    for leaf in G.leaf_nodes():
        if not tree_operations.isolated(leaf):
            H.add_node(H_number_of_nodes,
                       s=leaf.label,
                       t=sigma[leaf.label],
                       l=list())
            big_node = H_number_of_nodes
            H_number_of_nodes += 1
            i = 0
            for i in range(0, k):
                if i == 0:
                    cost = 0
                else:
                    cost = math.inf
                new_item = {
                    's': leaf.label,
                    't': sigma[leaf.label],
                    'cost': cost,
                    'event': "leaf",
                    'list_place': i
                }
                H.nodes[big_node]['l'].insert(i, new_item)
            nodes_table[sigma[leaf.label]][leaf.label] = big_node
    return H, H_number_of_nodes, nodes_table
Esempio n. 2
0
def init_leafs_efficient(G, H, k, H_number_of_nodes, sigma, nodes_table):
    for leaf in G.leaf_nodes():
        if not tree_operations.isolated(leaf):
            target = ''
            if leaf.label in sigma:
                target = sigma[leaf.label]
            else:
                for species in sigma:
                    if leaf.label.find(species) != -1 or species.find(
                            leaf.label) != -1:
                        target = sigma[species]
            H.add_node(H_number_of_nodes, s=leaf.label, t=target, l=list())
            big_node = H_number_of_nodes
            H_number_of_nodes += 1
            for i in range(0, k):
                if i == 0:
                    cost_no_losses = 0
                    cost_with_losses = 0
                else:
                    cost_no_losses = math.inf
                    cost_with_losses = math.inf
                new_item = {
                    's': leaf.label,
                    't': target,
                    'cost_without_losses': cost_no_losses,
                    'cost_with_losses': cost_with_losses,
                    'event': "leaf",
                    'list_place': i
                }
                H.nodes[big_node]['l'].insert(i, new_item)
            nodes_table[target][leaf.label] = big_node
    return H, H_number_of_nodes, nodes_table
Esempio n. 3
0
def main(path, create_sigma, res):
    if create_sigma:
        with open(path + '/FASTA.txt', 'r') as fp:
            res = ''
            for line in fp:
                if line[0] == '>':
                    start = line.find('[') + 1
                    end = line.find(']')
                    while start < end:
                        if line[start] == ' ':
                            res += ' '
                        else:
                            res += line[start]
                        start += 1
                    res += '\n'
        file = open(path + "/taxa_names.txt", 'w')
        file.write(str(res))
        file.close()

    else:
        try:
            S = tr.Tree.get_from_path(path + "/S.txt", schema="newick")
        except:
            res['error'] += "\nSpecies tree '/data/S.txt' was not found.\n" \
                        "In order to create it, please go to https://www.ncbi.nlm.nih.gov/Taxonomy/CommonTree/wwwcmt.cgi\n" \
                        "and follow the instructions:\n" \
                        " -> Choose File -> select the file 'NCBI_tax_ID.txt' from the folder data/your_data \n" \
                        "-> Add from file: -> select the subtree you want (e.i. Proteobacteria) and press Choose ->\n" \
                        "Expand All -> Save As phylip tree.\n " \
                        "Finally you need to rename the file 'S.txt' and move it to the data/your_data/ directory."
            return
        res = ''
        for leaf in S.leaf_nodes():
            if not tree_operations.isolated(leaf):
                res += leaf.taxon.label + ','
        file = open(path + '' + "/taxa_names.txt", 'w')
        file.write(str(res))
        file.close()
Esempio n. 4
0
def main (path,create_form_fasta,res_map):
    if create_form_fasta:
        res = '{ '
        try:
            S_text = open(path + '/S.txt', 'r').read()
        except:
            res_map['error'] += "\nSpecies tree '/data/S.txt' was not found.\n" \
                            "In order to create it, please go to https://www.ncbi.nlm.nih.gov/Taxonomy/CommonTree/wwwcmt.cgi\n" \
                            "and follow the instructions:\n" \
                            " -> Choose File -> select the file 'NCBI_tax_ID.txt' from the folder data/your_data \n" \
                            "-> Add from file: -> select the subtree you want (e.i. Proteobacteria) and press Choose ->\n" \
                            "Expand All -> Save As phylip tree.\n " \
                            "Finally you need to rename the file 'S.txt' and move it to the data/your_data/ directory."
            return
        input = open(path+"/old_new_names.txt", 'r')
        old_new_names = []
        for line in input:
            old_new_names.append(eval(line))
        old_new_names = old_new_names[0]
        S_text = S_text.replace(' ','').replace('\n','')
        with open(path+'/FASTA.txt','r') as fp:
            for line in fp:
                if line[0] == '>':
                    start = 1
                    end = line.find(' ')
                    gene = ''
                    while start < end :
                        gene += line[start]
                        start += 1
                    gene = gene.replace("_"," ")
                    start = line.find('[')+1
                    end = line.find(']')
                    specie = ''
                    while start < end :
                        specie += line[start]
                        start += 1
                    if S_text.find(old_new_names[specie].replace(' ','')) != -1:
                        res += "'"+gene+"' : '"+old_new_names[specie].replace(' ','')+"',"

        res += ' }'
        file = open(path + "/sigma.txt", 'w')
        file.write(str(res))
        file.close()
    else:
        try:
            G = tr.Tree.get_from_path(path + "/G.txt", schema="newick")
        except:
            res_map['error'] += "\nGene tree '/data/G.txt' was not found.\n" \
                            "In order to create it, go to https://www.ebi.ac.uk/Tools/msa/clustalo/\n" \
                            "and follow the instructions:\n" \
                            "Choose File -> select 'FASTA.txt' from data/your_data/ directory\n" \
                            "-> select OUTPUT FORMAT: -> wait until job is done..\n" \
                            ""
            return
        res = '{ '
        for leaf in G.leaf_nodes():
            if not tree_operations.isolated(leaf):
                res += " '"+leaf.taxon.label+"' "+':'+" '"+leaf.taxon.label[0:1]+"'"+','
        res += ' }'
        file = open(path + '' + "/sigma.txt", 'w')
        file.write(str(res))
        file.close()
Esempio n. 5
0
def draw_S_and_G(S, G, old_sigma, colors, sigma, path, sol, ext, to_color):
    plt.clf()
    S_to_draw = nx.DiGraph()
    G_to_draw = nx.DiGraph()
    index = 1
    nodes_color_S = []
    nodes_color_G = []

    ##FOR S
    for u in S.postorder_node_iter():
        S_to_draw.add_node(index, label=u.label)
        if not tree_operations.is_a_leaf(u):
            child = u.child_nodes()
            i = 0
            while i < len(child):
                if len(child) >= i + 1:
                    S_to_draw.add_edge(
                        index,
                        list(S.postorder_node_iter()).index(child[i]) + 1)
                i = i + 1
        index += 1
    labels_S = nx.get_node_attributes(S_to_draw, 'label')
    for k, l in labels_S.items():
        for x in S.postorder_node_iter():
            if x.taxon != None:
                if x.label == l:
                    l = l + "\n (" + str(x.taxon) + ")"
                    labels_S.update({k: l})

    for u in S.postorder_node_iter():
        if u.label != None and u.label in colors and to_color:
            if colors[u.label] == 'red':
                nodes_color_S.append('red')
            elif colors[u.label] == 'black':
                nodes_color_S.append('grey')
            else:
                nodes_color_S.append('pink')
        else:
            nodes_color_S.append('white')
    ## FOR G
    index = 1
    for u in G.postorder_node_iter():
        G_to_draw.add_node(index, label=u.label)
        if not tree_operations.is_a_leaf(u):
            child = u.child_nodes()
            i = 0
            while i < len(child):
                if len(child) >= i + 1:
                    G_to_draw.add_edge(
                        index,
                        list(G.postorder_node_iter()).index(child[i]) + 1)
                i = i + 1
        index += 1
    labels_G = nx.get_node_attributes(G_to_draw, 'label')
    for k, l in labels_G.items():
        for x in G.postorder_node_iter():
            if x.taxon != None:
                if x.label == l and (x.taxon.label in old_sigma):
                    l = l + "\n (" + str(x.taxon) + ")"
                    l = l + '\n' + str(old_sigma[x.taxon.label])
                    labels_G.update({k: l})

    for u in G.postorder_node_iter():
        degel = False
        if sol != None:
            for int, temp_sol in sol.items():
                for p in range(0, len(temp_sol['list_of_couples'])):
                    if (u.label == temp_sol['Marked']
                            or u.label == temp_sol['list_of_couples'][p][0]
                            or u.label == temp_sol['list_of_couples'][p][1]
                        ) and not degel:
                        nodes_color_G.append('blue')
                        degel = True
        if not degel and tree_operations.is_a_leaf(
                u) and u.label in sigma and not tree_operations.isolated(
                    u) and sigma[u.label] in colors and to_color:
            if colors[sigma[u.label]] == 'red':
                nodes_color_G.append('red')
            elif colors[sigma[u.label]] == 'black':
                nodes_color_G.append('grey')
            else:
                nodes_color_G.append('pink')
        elif not degel:
            nodes_color_G.append('white')

    postree_S = graphviz_layout(S_to_draw, prog='dot')
    postree_G = graphviz_layout(G_to_draw, prog='dot')
    for k, v in postree_G.items():
        # Shift the x values of every node by 10 to the right
        lst = list(v)
        lst[0] = lst[0] + 100
        postree_G.update({k: tuple(lst)})

    fig, axes = plt.subplots(1, 2, figsize=(90, 50))
    ax = axes.flatten()

    ax[0].set_title('Species tree',
                    fontsize=50,
                    rotation='vertical',
                    x=-0.1,
                    y=0.5)
    ax[1].set_title('Gene tree',
                    fontsize=50,
                    rotation='vertical',
                    x=-0.1,
                    y=0.5)

    nx.draw(S_to_draw,
            postree_S,
            arrows=True,
            node_color=nodes_color_S,
            ax=ax[0])
    nx.draw(G_to_draw,
            postree_G,
            arrows=True,
            node_color=nodes_color_G,
            ax=ax[1])

    t1 = nx.draw_networkx_labels(S_to_draw,
                                 postree_S,
                                 labels_S,
                                 font_size=7,
                                 ax=ax[0])
    t2 = nx.draw_networkx_labels(G_to_draw,
                                 postree_G,
                                 labels_G,
                                 font_size=7,
                                 ax=ax[1])
    for _, t in t1.items():
        t.set_rotation('vertical')
    for _, t in t2.items():
        t.set_rotation('vertical')

    nx.draw_networkx_edges(S_to_draw, postree_S, ax=ax[0])
    nx.draw_networkx_edges(G_to_draw, postree_G, ax=ax[1])

    to_create = path + '/figures/'
    os.makedirs(os.path.dirname(to_create), exist_ok=True)

    plt.savefig(path + '/figures/S+G' + ext + '.png')
Esempio n. 6
0
def draw_tree(tree, name, old_sigma, colors, sigma, path, color_tree, x_axis,
              y_axis, label_flag):
    tree_to_draw = nx.DiGraph()
    index = 1
    nodes_color = []
    for u in tree.postorder_node_iter():
        tree_to_draw.add_node(index, label=u.label)
        if not tree_operations.is_a_leaf(u):
            child = u.child_nodes()
            i = 0
            while i < len(child):
                if len(child) >= i + 1:
                    tree_to_draw.add_edge(
                        index,
                        list(tree.postorder_node_iter()).index(child[i]) + 1)
                i = i + 1
        index += 1
    labels = nx.get_node_attributes(tree_to_draw, 'label')
    for k, l in labels.items():
        for x in tree.postorder_node_iter():
            if x.taxon != None:
                if x.label == l:
                    l = l + "\n (" + str(x.taxon) + ")"
                    if name == 'G':
                        l = l + '\n' + str(old_sigma[x.taxon.label])
                        labels.update({k: l})
                    else:
                        labels.update({k: l})

    if name == 'S':
        for u in tree.postorder_node_iter():
            if color_tree:
                if u.label != None and u.label in colors:
                    if colors[u.label] == 'red':
                        nodes_color.append('red')
                    elif colors[u.label] == 'black':
                        nodes_color.append('grey')
                    else:
                        nodes_color.append('pink')
                else:
                    nodes_color.append('white')
            else:
                nodes_color.append('white')
    else:
        for u in tree.postorder_node_iter():
            if tree_operations.is_a_leaf(u) and not tree_operations.isolated(
                    u) and sigma[u.label] in colors:
                if colors[sigma[u.label]] == 'red':
                    nodes_color.append('red')
                else:
                    nodes_color.append('grey')
            else:
                nodes_color.append('white')

    postree = graphviz_layout(tree_to_draw, prog='dot')

    plt.figure(12, figsize=(x_axis, y_axis))  # size of fig

    nx.draw(tree_to_draw, postree, arrows=True, node_color=nodes_color)
    if label_flag:
        text = nx.draw_networkx_labels(tree_to_draw,
                                       postree,
                                       labels,
                                       font_size=7)
        for _, t in text.items():
            t.set_rotation('vertical')
    nx.draw_networkx_edges(tree_to_draw, postree)
    plt.savefig(path + name + '.png')
    print('Drawing' + name)