def get_example_tree(): # Performs a tree reconciliation analysis gene_tree_nw = '((Dme_001,Dme_002),(((Cfa_001,Mms_001),((Hsa_001,Ptr_001),Mmu_001)),(Ptr_002,(Hsa_002,Mmu_002))));' t = PhyloTree(gene_tree_nw) ts = TreeStyle() # disable default PhyloTree Layout ts.layout_fn = lambda x: True t.link_to_alignment(alg) node2content = t.get_cached_content() for node in t.traverse(): node.img_style["size"] = 0 if not node.is_leaf(): leaves = node2content[node] # get columns with different aa subseqs, relevant_columns = mutation_columns([lf.sequence for lf in leaves]) for seq in subseqs: f = SeqMotifFace(seq, seq_format="seq", width=10, height=8) f.margin_top = 2 f.margin_right = 6 node.add_face(f, column=0, position="branch-bottom") for j, col in enumerate(relevant_columns): col_f = RectFace(10, 10, fgcolor=None, bgcolor=None, label={"text":str(col), "fonttype":"Courier", "color":"black", "fontsize":6}) node.add_face(col_f, column=j, position="branch-top") col_f.margin_bottom = 2 else: f = SeqMotifFace(node.sequence, seq_format="seq", width=6) node.add_face(f, column=0, position="aligned") alg_length = len(lf.sequence) ts.draw_aligned_faces_as_table = False for colnum in xrange(alg_length): col_f = RectFace(10, 10, fgcolor=None, bgcolor=None, label={"text":str(colnum), "fonttype":"Courier", "color":"black", "fontsize":6}) ts.aligned_header.add_face(col_f, column=colnum) return t, ts
""" # Load a tree and link it to an alignment. As usual, 'alignment' can # be the path to a file or data in text format. t = PhyloTree("(((seqA,seqB),seqC),seqD);", alignment=fasta_txt, alg_format="fasta") #We can now access the sequence of every leaf node print "These are the nodes and its sequences:" for leaf in t.iter_leaves(): print leaf.name, leaf.sequence #seqD MAEAPDETIQQFMALTNVSHNIAVQYLSEFGDLNEAL--------------REEAH #seqC MAEIPDATIQ---ALTNVSHNIAVQYLSEFGDLNEALNSYYASQTDDQPDRREEAH #seqA MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAH #seqB MAEIPDATIQQFMALTNVSHNIAVQY--EFGDLNEALNSYYAYQTDDQKDRREEAH # # The associated alignment can be changed at any time t.link_to_alignment(alignment=iphylip_txt, alg_format="iphylip") # Let's check that sequences have changed print "These are the nodes and its re-linked sequences:" for leaf in t.iter_leaves(): print leaf.name, leaf.sequence #seqD MAEAPDETIQQFMALTNVSHNIAVQYLSEFGDLNEAL--------------REEAHQ----------FMALTNVSH #seqC MAEIPDATIQ---ALTNVSHNIAVQYLSEFGDLNEALNSYYASQTDDQPDRREEAHQFMALTNVSH---------- #seqA MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAHQFMALTNVSHQFMALTNVSH #seqB MAEIPDATIQQFMALTNVSHNIAVQY--EFGDLNEALNSYYAYQTDDQKDRREEAHQFMALTNVSH---------- # # The sequence attribute is considered as node feature, so you can # even include sequences in your extended newick format! print t.write(features=["sequence"], format=9) # # # (((seqA[&&NHX:sequence=MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAHQF
def uploaded_secuence(request): path = BASE_DIR + '/secuences/secuence.fasta' handler = SequenceHandler() handler.validate_sequences(path) upload_id = calendar.timegm(time.gmtime()) if not handler.has_errors: for fasta in handler.dic_data: coordenadas = convertDirectionToCoordinates(fasta['loc']) fasta_to_insert = Secuence() fasta_to_insert.address = fasta['loc'] fasta_to_insert.date = fasta['date'] fasta_to_insert.latitud = coordenadas[0] fasta_to_insert.longitud = coordenadas[1] fasta_to_insert.bio_id = fasta['gb'] fasta_to_insert.content = fasta['seq'] fasta_to_insert.length = len(fasta['seq']) fasta_to_insert.upload_id = upload_id fasta_to_insert.source = fasta['source'] fasta_to_insert.save() handler.clean_data() if not handler.is_aligned: if platform.system() == 'Linux': handler.lnx_alignment(path) handler.lnx_build_tree() else: handler.win_alignment(path) handler.win_build_tree() else: # la secuencia esta alineada handler.make_file_aln_for_iqtree() if platform.system() == 'Linux': handler.lnx_build_tree() else: handler.win_build_tree() messages.success(request, f"El archivo se ha subido correctamente") tree_file = BASE_DIR + "/secuences/secuence.fasta_aln.fasta.treefile" aln_path = BASE_DIR + "/secuences/secuence.fasta_aln.fasta" ts = TreeStyle() ts.force_topology = True ts.show_branch_support = True ts.show_leaf_name = False ts.branch_vertical_margin = 10 t = PhyloTree(tree_file) t.link_to_alignment(aln_path) img_name = "TpFinalBioApp/static/TpFinalBioApp/img/output/myTree" + "_" + str( upload_id) + ".svg" t.render(img_name, tree_style=ts) print(t) if platform.system() != 'Linux': os.remove(BASE_DIR + "/secuences/secuence.fasta_aln.fasta.model.gz") return redirect('Map', upload_id) else: messages.error(request, f"El archivo no es correcto. " + handler.error_message) return redirect('Home')
# Performs a tree reconciliation analysis gene_tree_nw = '((Dme_001,Dme_002),(((Cfa_001,Mms_001),((Hsa_001,Ptr_001),Mmu_001)),(Ptr_002,(Hsa_002,Mmu_002))));' species_tree_nw = "((((Hsa, Ptr), Mmu), (Mms, Cfa)), Dme);" genetree = PhyloTree(gene_tree_nw) sptree = PhyloTree(species_tree_nw) recon_tree, events = genetree.reconcile(sptree) recon_tree.link_to_alignment(alg) return recon_tree, TreeStyle() if __name__ == "__main__": # Visualize the reconciled tree t, ts = get_example_tree() t.show(tree_style=ts) #recon_tree.render("phylotree.png", w=750) fasta_txt = """ >seqA MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAH >seqB MAEIPDATIQQFMALTNVSHNIAVQY--EFGDLNEALNSYYAYQTDDQKDRREEAH >seqC MAEIPDATIQ---ALTNVSHNIAVQYLSEFGDLNEALNSYYASQTDDQPDRREEAH >seqD MAEAPDETIQQFMALTNVSHNIAVQYLSEFGDLNEAL--------------REEAH """ # Load a tree and link it to an alignment. t = PhyloTree("(((seqA,seqB),seqC),seqD);") t.link_to_alignment(alignment=fasta_txt, alg_format="fasta") t.show()
# be the path to a file or data in text format. t = PhyloTree("(((seqA,seqB),seqC),seqD);", alignment=fasta_txt, alg_format="fasta") #We can now access the sequence of every leaf node print "These are the nodes and its sequences:" for leaf in t.iter_leaves(): print leaf.name, leaf.sequence #seqD MAEAPDETIQQFMALTNVSHNIAVQYLSEFGDLNEAL--------------REEAH #seqC MAEIPDATIQ---ALTNVSHNIAVQYLSEFGDLNEALNSYYASQTDDQPDRREEAH #seqA MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAH #seqB MAEIPDATIQQFMALTNVSHNIAVQY--EFGDLNEALNSYYAYQTDDQKDRREEAH # # The associated alignment can be changed at any time t.link_to_alignment(alignment=iphylip_txt, alg_format="iphylip") # Let's check that sequences have changed print "These are the nodes and its re-linked sequences:" for leaf in t.iter_leaves(): print leaf.name, leaf.sequence #seqD MAEAPDETIQQFMALTNVSHNIAVQYLSEFGDLNEAL--------------REEAHQ----------FMALTNVSH #seqC MAEIPDATIQ---ALTNVSHNIAVQYLSEFGDLNEALNSYYASQTDDQPDRREEAHQFMALTNVSH---------- #seqA MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAHQFMALTNVSHQFMALTNVSH #seqB MAEIPDATIQQFMALTNVSHNIAVQY--EFGDLNEALNSYYAYQTDDQKDRREEAHQFMALTNVSH---------- # # The sequence attribute is considered as node feature, so you can # even include sequences in your extended newick format! print t.write(features=["sequence"], format=9) # # # (((seqA[&&NHX:sequence=MAEIPDETIQQFMALT---HNIAVQYLSEFGDLNEALNSYYASQTDDIKDRREEAHQF
if node.collapsed == 1: node.img_style["draw_descendants"]= False if node.is_leaf(): if hasattr (node, "sequence"): seqface = MySequenceFace(node.sequence, "nt", fsize=10, col_w=11, interactive=True) faces.add_face_to_node(seqface, node, 1, aligned=True) if __name__ == "__main__": tree = PhyloTree('(Orangutan,Human,Chimp);') tree.link_to_alignment(""" >Chimp HARWLNEKLRCELRTLKKLGLDGYKAVSQYVKGRA >Orangutan DARWINEKLRCVSRTLKKLGLDGYKGVSQYVKGRP >Human DARWHNVKLRCELRTLKKLGLVGFKAVSQFVIRRA """) nt_sequences = {"Human" : "GACGCACGGTGGCACAACGTAAAATTAAGATGTGAATTGAGAACTCTGAAAAAATTGGGACTGGTCGGCTTCAAGGCAGTAAGTCAATTCGTAATACGTCGTGCG", "Chimp" : "CACGCCCGATGGCTCAACGAAAAGTTAAGATGCGAATTGAGAACTCTGAAAAAATTGGGACTGGACGGCTACAAGGCAGTAAGTCAGTACGTTAAAGGTCGTGCG", "Orangutan": "GATGCACGCTGGATCAACGAAAAGTTAAGATGCGTATCGAGAACTCTGAAAAAATTGGGACTGGACGGCTACAAGGGAGTAAGTCAATACGTTAAAGGTCGTCCG" } for l in nt_sequences: (tree & l).nt_sequence = nt_sequences[l] tree.dist = 0 ts = TreeStyle() ts.title.add_face(TextFace("Example for nucleotides...", fsize=15), column=0) ts.layout_fn = test_layout_evol tree.show(tree_style=ts)
def make_tree_ali_detect_combi(g_tree, ali_nf, Out, hist_up="", x_values=[], hp=[], dict_benchmark={}, reorder=False, det_tool=False, title=""): reptree = g_tree.reptree cz_nodes = g_tree.cz_nodes ### Tree ## Tree style phylotree_style = TreeStyle() phylotree_style.show_leaf_name = False phylotree_style.show_branch_length = False phylotree_style.draw_guiding_lines = True phylotree_style.min_leaf_separation = 1 ## For noisy tree cz_nodes_s = {} if cz_nodes: cols = [ "#008000", "#800080", "#007D80", "#9CA1A2", "#A52A2A", "#ED8585", "#FF8EAD", "#8EB1FF", "#FFE4A1", "#ADA1FF" ] col_i = 0 for Cz in cz_nodes.keys(): cz_nodes_s[Cz] = NodeStyle() cz_nodes_s[Cz]["fgcolor"] = cols[col_i] cz_nodes_s[Cz]["size"] = 5 cz_nodes_s[Cz]["hz_line_width"] = 2 cz_nodes_s[Cz]["vt_line_width"] = 2 cz_nodes_s[Cz]["vt_line_color"] = cols[col_i] cz_nodes_s[Cz]["hz_line_color"] = cols[col_i] col_i += 1 sim_root_ND = g_tree.outgroup_ND def my_layout(node): ## Sequence name F = TextFace(node.name, tight_text=True) add_face_to_node(F, node, column=0, position="aligned") ## Sequence motif if node.is_leaf(): motifs_n = [] box_color = "black" opacity = 1 if node.T == "True" or node.C == "True": motifs_n.append([ 0, len(node.sequence), "[]", 10, 12, box_color, box_color, None ]) motifs_n.append( [0, len(node.sequence), "seq", 10, 10, None, None, None]) seq_face = SeqMotifFace(seq=node.sequence, seqtype='aa', seq_format='seq', fgcolor=box_color, motifs=motifs_n) seq_face.overlaping_motif_opacity = opacity add_face_to_node(seq_face, node, column=1, position='aligned') ## Nodes style if det_tool and node.T == "True": node.set_style(nstyle_T_sim) add_t(node) elif det_tool and node.C == "True": node.set_style(nstyle_C_sim) elif det_tool: node.set_style(nstyle) #if not det_tool no background elif node.T == "True" and not int( node.ND) in g_tree.conv_events.nodesWithTransitions_est: node.set_style(nstyle_T_sim) add_t(node) elif node.T == "True" and int( node.ND) in g_tree.conv_events.nodesWithTransitions_est: node.set_style(nstyle_T_sim_est) add_t(node) elif int(node.ND) in g_tree.conv_events.nodesWithTransitions_est: node.set_style(nstyle_T_est) elif node.C == "True" and not int( node.ND) in g_tree.conv_events.nodesWithConvergentModel_est: node.set_style(nstyle_C_sim) elif node.C == "True" and int( node.ND) in g_tree.conv_events.nodesWithConvergentModel_est: node.set_style(nstyle_C_sim_est) elif int(node.ND) in g_tree.conv_events.nodesWithConvergentModel_est: node.set_style(nstyle_C_est) elif cz_nodes_s and node.Cz != "False": node.set_style(cz_nodes_s[int(node.Cz)]) if int(node.ND) == int(cz_nodes[int(node.Cz)][0]): add_t(node) else: node.set_style(nstyle) if int(node.ND) == sim_root_ND and not det_tool: add_sim_root(node) phylotree_style.layout_fn = my_layout # Get tree dimensions tree_nf = g_tree.annotated_tree_fn_est logger.debug("tree_nf: %s", tree_nf) t = PhyloTree(tree_nf) t.link_to_alignment(ali_nf) t.render(Out) tree_face = TreeFace(t, phylotree_style) tree_face.update_items() tree_h = tree_face._height() tree_w = tree_face._width() ### X axes: if not x_values: # Complete representation x_values_up = [ x + 1 for x in range(0, len(dict_benchmark.values()[0])) ] inter = 5 else: # Filtered representation x_values_up = x_values inter = 1 ### Histogram up if hist_up in ["PCOC", "PC", "OC", "Topological", "Identical"]: header_hist_value_up = 'Posterior probability (' + hist_up.upper( ) + ' model)' hist_value_up = dict_benchmark[hist_up] # Define emphased lines hlines = [0.8, 0.9, 0.99] hlines_col = ['#EFDB00', 'orange', 'red'] # Type of representation kind = 'stick' # bar/curve/stick y_values_up = hist_value_up hist = SequencePlotFace_mod(y_values_up, hlines=hlines, hlines_col=hlines_col, kind=kind, header=header_hist_value_up, height=40, col_width=10, ylim=[0, 1]) hist.hp = hp hist.x_values = x_values_up hist.x_inter_values = inter hist.set_sticks_color() if reorder: # draw colored boxes hist.put_colored_boxes = (True, tree_h + 40) phylotree_style.aligned_header.add_face(hist, 1) ### Rect all model: sequencescorebox = SequenceScoreFace(dict_benchmark, col_width=10) sequencescorebox.hp = hp sequencescorebox.x_values = x_values_up sequencescorebox.x_inter_values = inter if not hist_up in ["PCOC", "PC", "OC", "Topological", "Identical"]: sequencescorebox.x_axis = True phylotree_style.aligned_header.add_face(sequencescorebox, 1) if title: phylotree_style.title.add_face(TextFace(title), column=0) tree_nf = reptree + "/annotated_tree.nhx" logger.debug("tree_nf: %s", tree_nf) res = t.render(Out, tree_style=phylotree_style) del t return (res)
def getPhyloTree(self): # Performs a tree reconciliation analysis gene_tree_nw = self.newick genetree = PhyloTree(gene_tree_nw) genetree.link_to_alignment(self.alg) return genetree, TreeStyle()
if node.is_leaf(): if hasattr(node, "sequence"): seqface = MySequenceFace(node.sequence, "nt", fsize=10, col_w=11, interactive=True) faces.add_face_to_node(seqface, node, 1, aligned=True) if __name__ == "__main__": tree = PhyloTree('(Orangutan,Human,Chimp);') tree.link_to_alignment(""" >Chimp HARWLNEKLRCELRTLKKLGLDGYKAVSQYVKGRA >Orangutan DARWINEKLRCVSRTLKKLGLDGYKGVSQYVKGRP >Human DARWHNVKLRCELRTLKKLGLVGFKAVSQFVIRRA """) nt_sequences = { "Human": "GACGCACGGTGGCACAACGTAAAATTAAGATGTGAATTGAGAACTCTGAAAAAATTGGGACTGGTCGGCTTCAAGGCAGTAAGTCAATTCGTAATACGTCGTGCG", "Chimp": "CACGCCCGATGGCTCAACGAAAAGTTAAGATGCGAATTGAGAACTCTGAAAAAATTGGGACTGGACGGCTACAAGGCAGTAAGTCAGTACGTTAAAGGTCGTGCG", "Orangutan": "GATGCACGCTGGATCAACGAAAAGTTAAGATGCGTATCGAGAACTCTGAAAAAATTGGGACTGGACGGCTACAAGGGAGTAAGTCAATACGTTAAAGGTCGTCCG" } for l in nt_sequences: (tree & l).nt_sequence = nt_sequences[l] tree.dist = 0 ts = TreeStyle()