def draw_tree(tree, root=None, labels=None, clear=True, title='', ax=None): """Draw phylogenetic tree with biopython""" import pylab as plt if ax == None: fig,ax=plt.subplots(figsize=(10,8)) if clear == True: try: clear_clades(tree) except: pass if root != None: tree.root_with_outgroup(root) if labels != None: for clade in tree.get_terminals(): key = clade.name if key in labels: clade.name = '%s; %s' %(key,labels[key]) #clade.name = labels[key] Phylo.draw(tree,axes=ax,axis=('off',), do_show=False, label_colors=None, branch_labels=None, show_confidence=False) ax.set_title(title,fontsize=16) ax.set_frame_on(False) return
def plot_pres_abs(roary_sorted,t, matrix_pdf, red=False): #this next part of code works very well, except does not do well with entire pan genome if red==True: reduced_mat=roary_sorted.T[roary_sorted.T.columns[0:30000]] else: reduced_mat=roary_sorted.T # Max distance to create better plots mdist = max([t.distance(t.root, x) for x in t.get_terminals()]) with sns.axes_style('whitegrid'): fig = plt.figure(figsize=(17, 10)) ax1=plt.subplot2grid((1,40), (0, 10), colspan=30) a=ax1.matshow(reduced_mat, cmap=plt.cm.Blues, vmin=0, vmax=1, aspect='auto', interpolation='none') ax1.set_yticks([]) ax1.set_xticks([]) ax1.axis('off') ax = fig.add_subplot(1,2,1) ax=plt.subplot2grid((1,40), (0, 0), colspan=10, axisbg='white') fig.subplots_adjust(wspace=0, hspace=0) ax1.set_title('Presence/absence matrix\n(%d gene clusters)'%roary_sorted.shape[0]) Phylo.draw(t, axes=ax, show_confidence=False, label_func=lambda x: None, xticks=([],), yticks=([],), ylabel=('',), xlabel=('',), xlim=(-mdist*0.1,mdist+mdist*0.1), axis=('off',), title=('Tree\n(%d genotypes)'%roary_sorted.shape[1],), do_show=False, ) plt.savefig(matrix_pdf, dpi=600) plt.clf()
def producePhylo(fig,gridspace1,tree_path): font= mpl.rcParams['font.size']=13.0 tree=Phylo.read(tree_path,"newick") ##rect_phyl = [-0.7, 0.3, 0.3, 0.8] phyl_ax=fig.add_subplot(gridspace1) ##phyl_ax = plt.axes(rect_phyl,frameon=True) # This is where i think that we are getting all that rectangle nonsense ''' phyl_ax.add_patch(Rectangle((5.6,16.7),10.2,16.8,edgecolor="brown", fill=False)) phyl_ax.add_patch(Rectangle((5.6,9.5),10.2,6.8,edgecolor="magenta", fill=False)) phyl_ax.add_patch(Rectangle((5.6,6.6),10.2,2.6,edgecolor="black", fill=False)) phyl_ax.add_patch(Rectangle((5.6,0.4),10.2,5.9,edgecolor="turquoise", fill=False)) ''' Phylo.draw(tree, axes=phyl_ax, do_show=False,show_confidence=False) #Phylo.draw_graphviz(tree, phyl_ax, ) #print os.path.join(joined_path,operon+"phylo.png"); #target1 = open.write(operon+"phylo.png",'w'); #Phylo.draw_ascii(tree,target1) phyl_ax.set_xlim(0,16) phyl_ax.set(xlabel='',ylabel='') plt.setp(phyl_ax.get_xticklabels(),visible=False) plt.setp(phyl_ax.get_yticklabels(),visible=False) plt.setp(phyl_ax.get_xticklines(),visible=False) plt.setp(phyl_ax.get_yticklines(),visible=False) return phyl_ax
def _get_bootstrap_consensus_tree_plot(self) -> plt: # Draw the consensus tree as a matplotlib object. Phylo.draw( self._get_bootstrap_consensus_tree(), do_show=False, show_confidence=True, branch_labels=lambda clade: "{0:.4f}\n".format(clade.branch_length) if clade.branch_length is not None else "") # Set labels for the plot. plt.xlabel("Branch Length") plt.ylabel("Documents") # Hide the two unused border. plt.gca().spines["top"].set_visible(False) plt.gca().spines["right"].set_visible(False) # Extend x-axis to the right to fit longer labels. x_left, x_right, y_low, y_high = plt.axis() plt.axis((x_left, x_right * 1.25, y_low, y_high)) # Set graph size, title and tight layout. plt.gcf().set_size_inches(w=9.5, h=(len(self._id_temp_label_map) * 0.3 + 1)) plt.title("Bootstrap Consensus Tree Result") plt.gcf().tight_layout() # Change line spacing for text in plt.gca().texts: text.set_linespacing(spacing=0.1) return plt
def nearest(file_path, file_format, node1, node2): trees = tree_utils.load(file_path, file_format) if trees is not None: for tree in trees: mrca = tree.common_ancestor({"name": node1}, {"name": node2}) mrca.color = "salmon" Phylo.draw(tree)
def dna(file_path, file_format, algorithm): # Read the sequences and align aln = AlignIO.read(file_path, file_format) # Print the alignment print(aln) # Calculate the distance matrix calculator = DistanceCalculator('identity') dm = calculator.get_distance(aln) # Print the distance Matrix print('\nDistance Matrix\n===================') print(calculator) # Construct the phylogenetic tree using choosen algorithm constructor = DistanceTreeConstructor() if algorithm.lower() == 'upgma': tree = constructor.upgma(dm) elif algorithm.lower() == 'nj': tree = constructor.nj(dm) else: click.echo('Invalid algorithm!') # Draw the phylogenetic tree Phylo.draw(tree) # Print the phylogenetic tree in the terminal print('\nPhylogenetic Tree\n===================') Phylo.draw_ascii(tree)
def Arvore(): #from matplotlib import pyplot print("Tratar da arvore...") align = AlignIO.read("myfasta.aln", "clustal") tree=Phylo.read("backs\clustalw.dnd", "newick") tree.rooted = True Phylo.draw(tree)
def draw_tree(tree, node_label = node_label_func, branch_label = lambda x:None, cmap = cm.jet, axes = None, cb = True): ''' plots a tree on an empty canvas including a scalebar of length 0.005 ''' import matplotlib.pyplot as plt from Bio import Phylo if axes is None: fig = plt.figure(figsize = (8,6)) axes = plt.subplot(111) Phylo.draw(tree, label_func = node_label, show_confidence = False,branch_labels = branch_label, axes=axes) axes.axis('off') xlimits = axes.get_xlim() ylimits = axes.get_ylim() x0 = xlimits[0]+(xlimits[1]-xlimits[0])*0.05 x1 = x0+0.005 y0 = ylimits[0]+(ylimits[1]-ylimits[0])*0.05 plt.plot([x0,x1], [y0,y0], lw=2, c='k') plt.text(x0+0.0025, y0+(ylimits[1]-y0)*0.01, '0.005', ha='center') # fake a colorbar if cb: sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=0, vmax=100)) sm._A = [] cbar = plt.colorbar(sm, ticks=[0,100], shrink=0.5, aspect=10,pad=-0.05) cbar.set_ticklabels(['worst','best']) plt.draw() return axes
def ML_tree(infile, outfile, file_type): # Tree creation with maximum-likelihood algorithm (phyML) # input : infile = .fasta alignment file that the user can import or paste, outfile = name of output file, file_type = clustal is the clustal too has been used, fasta if muscle tool has been used # output : .newick file and .png picture to display # phylogeny page should allow to choose maximum likelihood method # convert file to phylip records = SeqIO.parse("static/data/sauvegardes/" + dirName + infile, file_type) # clustal <-> fasta count = SeqIO.write(records, "static/data/sauvegardes/" + dirName + outfile + ".phylip", "phylip") print("Converted %i records" % count) if (user_OS == 'darwin'): cmd = PhymlCommandline(cmd='static/tools/MacOS/PhyML-3.1/PhyML-3.1_macOS-MountainLion', input='static/data/sauvegardes/' + dirName + outfile + '.phylip') if (user_OS == 'linux'): cmd = PhymlCommandline(cmd='static/tools/Linux/PhyML-3.1/PhyML-3.1_linux64', input='static/data/sauvegardes/' + dirName + outfile + '.phylip') if (user_OS == 'win32'): cmd = PhymlCommandline(cmd= current_path + '/static/tools/Windows/PhyML-3.1/PhyML-3.1_win32.exe', input='static/data/sauvegardes/' + dirName + outfile + '.phylip') out_log, err_log = cmd() tree = Phylo.read('static/data/sauvegardes/' + dirName + outfile + '.phylip_phyml_tree.txt', 'newick') Phylo.draw(tree, do_show=False) Phylo.write(tree, 'static/data/sauvegardes/' + dirName + 'tree.txt', "newick") foo = current_path + '/static/data/sauvegardes/' + dirName + 'tree.png' plt.savefig(foo)
def _get_bootstrap_consensus_tree_plot(self) -> plt: # Draw the consensus tree as a MatPlotLib object. Phylo.draw( self._get_bootstrap_consensus_tree(), do_show=False, branch_labels=lambda clade: "{0:.4f}\n".format(clade.branch_length) if clade.branch_length is not None else "" ) # Set labels for the plot. plt.xlabel("Branch Length") plt.ylabel("Documents") # Hide the two unused border. plt.gca().spines["top"].set_visible(False) plt.gca().spines["right"].set_visible(False) # Extend x-axis to the right to fit longer labels. x_left, x_right, y_low, y_high = plt.axis() plt.axis((x_left, x_right * 1.25, y_low, y_high)) # Set graph size, title and tight layout. plt.gcf().set_size_inches( w=9.5, h=(len(self._id_temp_label_map) * 0.3 + 1) ) plt.title("Bootstrap Consensus Tree Result") plt.gcf().tight_layout() # Change line spacing for text in plt.gca().texts: text.set_linespacing(spacing=0.1) return plt
def build_phylogeny_trees(): path = "out/homologous_gene_sequences/" output_path = "out/aligned_homologous_gene_sequences/" for homologous_gene_sequence in os.listdir(path): input = path + homologous_gene_sequence output = output_path + homologous_gene_sequence clustal_omega = ClustalOmegaCommandline(infile=input, outfile=output, verbose=True, auto=True) os.system(str(clustal_omega)) multi_seq_align = AlignIO.read(output, 'fasta') # Distance Matrix calculator = DistanceCalculator('identity') dist_mat = calculator.get_distance(multi_seq_align) tree_constructor = DistanceTreeConstructor() phylo_tree = tree_constructor.upgma(dist_mat) Phylo.draw(phylo_tree) print('\nPhylogenetic Tree\n', homologous_gene_sequence) Phylo.draw_ascii(phylo_tree) Phylo.write([phylo_tree], 'out/phylogenetic_trees/{}_tree.nex'.format(homologous_gene_sequence), 'nexus')
def main(): args = get_args() phyloTree = Phylo.read(args.newickFN, 'newick') if args.draw: Phylo.draw(phyloTree) else: Phylo.draw_ascii(phyloTree)
def main(argv): # Test table data and corresponding labels M_labels = [ 'Wuttagoonaspis', 'Romundina', 'Brindabellaspis', 'Eurycaraspis', 'Entelognathus' ] print(M_labels) #A through G M = np.loadtxt(open(argv[1], "rb"), delimiter=",") l = np.tril(M) temp = np.ones((5, 5)) u = np.triu(temp) l += u np.fill_diagonal(l, 0) M = l.tolist() for j in range(0, 5): for i in range(0, 5): M[i] = list(filter(lambda a: a != 1, M[i])) m = _Matrix(M_labels, M) print(type(m)) constructor = DistanceTreeConstructor() tree = constructor.upgma(m) Phylo.draw(tree)
def main(): parser = argparse.ArgumentParser(description="Model a tree relationship of organisms based on a distance matrix") parser.add_argument("-i", help="Input file", required=True) parser.add_argument("-f", help= "Type of function: n for neighbor joining and u for UPGMA", required=True) args = parser.parse_args() inFile = args.i func = args.f func.lower() distMat = readMatrix(inFile) if func == "n": #run neighbor joining distArr = neighborjoin(distMat) strVer = distArrToVisualStyle(distArr) elif func == "u": #run upgma distArr = [] else: print("Error: invalid commandline arguments") handle = StringIO(strVer) tree = Phylo.read(handle, 'newick') Phylo.draw(tree)
def main(): file_name = "data/coding.fa" # file_name = "data/cons_noncode.fa" alignment = MultipleSeqAlignment([], Gapped(IUPAC.unambiguous_dna, "-")) for seq_record in SeqIO.parse(file_name, "fasta"): alignment.extend([seq_record]) print("Number of characters in alignment:", len(alignment[0])) #################### # Neighbor joining # #################### calculator = DistanceCalculator('identity') dm = calculator.get_distance(alignment) constructor = DistanceTreeConstructor() start = time.time() tree = constructor.nj(dm) end = time.time() print("Neighbor joining ran in {} seconds.".format(end - start)) Phylo.draw(tree, label_func=get_label) ######### # UPGMA # ######### start = time.time() tree = constructor.upgma(dm) end = time.time() print("UPGMA ran in {} seconds.".format(end - start)) Phylo.draw(tree, label_func=get_label)
def visualizeDM(self): import seaborn as sns orderedPops = [clade.name for clade in self.majorityTree.get_terminals()] dm = pd.DataFrame(self.dmNei, columns=self.popnames, index=self.popnames) dm = dm[orderedPops].reindex(orderedPops) ## arrange rows and columns according to tree ax = sns.heatmap(dm) Phylo.draw(self.majorityTree)
def main(): # getting the tree tree_gen = Phylo.parse(PATH_EXAMPLE, 'newick') tree_object = next(tree_gen) # the tree basic information print(tree_info(tree_object)) # drawing the tree Phylo.draw(tree_object) # distance comparing tns = dendropy.TaxonNamespace() tre_one = Tree.get_from_path(PATH_EXAMPLE, 'newick', taxon_namespace=tns) tre_two = Tree.get_from_path(PATH_BIF, 'newick', taxon_namespace=tns) euclidean_distance = treecompare.euclidean_distance(tre_one, tre_two) robinson_distance = treecompare.robinson_foulds_distance(tre_one, tre_two) print("Robinson Foulds distance: ", robinson_distance) print("Euclidean distance: ", euclidean_distance) # common ancestors common_ancestor_tree = tree_object.common_ancestor({"name": "C"}, {"name": "D"}) common_ancestor_tree.color = "blue" print("COMMON ANCESTOR: ", common_ancestor_tree) Phylo.draw(common_ancestor_tree)
def plot_tree(tree, output_file='out.pdf'): matplotlib.rc('font', size=10) fig = plt.figure(figsize=(10, 20), dpi=100) axes = fig.add_subplot(1, 1.5, 1) Phylo.draw(tree, axes=axes, do_show=False) plt.savefig('plot_tree/' + output_file, dpi=100)
def producePhylo(fig, gridspace1, tree_path): font = mpl.rcParams['font.size'] = 13.0 tree = Phylo.read(tree_path, "newick") ##rect_phyl = [-0.7, 0.3, 0.3, 0.8] phyl_ax = fig.add_subplot(gridspace1) ##phyl_ax = plt.axes(rect_phyl,frameon=True) # This is where i think that we are getting all that rectangle nonsense ''' phyl_ax.add_patch(Rectangle((5.6,16.7),10.2,16.8,edgecolor="brown", fill=False)) phyl_ax.add_patch(Rectangle((5.6,9.5),10.2,6.8,edgecolor="magenta", fill=False)) phyl_ax.add_patch(Rectangle((5.6,6.6),10.2,2.6,edgecolor="black", fill=False)) phyl_ax.add_patch(Rectangle((5.6,0.4),10.2,5.9,edgecolor="turquoise", fill=False)) ''' Phylo.draw(tree, axes=phyl_ax, do_show=False, show_confidence=False) #Phylo.draw_graphviz(tree, phyl_ax, ) #print os.path.join(joined_path,operon+"phylo.png"); #target1 = open.write(operon+"phylo.png",'w'); #Phylo.draw_ascii(tree,target1) phyl_ax.set_xlim(0, 16) phyl_ax.set(xlabel='', ylabel='') plt.setp(phyl_ax.get_xticklabels(), visible=False) plt.setp(phyl_ax.get_yticklabels(), visible=False) plt.setp(phyl_ax.get_xticklines(), visible=False) plt.setp(phyl_ax.get_yticklines(), visible=False) return phyl_ax
def printGeneTree(self): """ Print gene trees with matplotlib and in the terminal for the four largest target ORFs of coronaviruses. Takes a .phy file containing multiple alligned sequences, generates a matrix based on sequence composition and compares each sequence (genome) to one another. sequences with grater scores (similarity) are ranked closer together on the phylogenetic trees. input: A .phy file that contains coronavirus gene sequences to draw phylogenetic tree output: A visual representation of a gene tree on terminal and matplotlib """ align = AlignIO.read( self.newPhylip, 'phylip') # Reads created .phy file containing the SeqRecord #print (align) # prints concatenated allignments calculator = DistanceCalculator('identity') dm = calculator.get_distance(align) # Calculate the distance matrix print( '\n======================================== DISTANCE MATRIX =======================================\n' ) print(dm, "\n\n") # Print the distance Matrix constructor = DistanceTreeConstructor( ) # Construct the phylogenetic tree using UPGMA algorithm tree = constructor.upgma(dm) print( '\n========================================= GENE TREE ===========================================\n' ) Phylo.draw( tree ) # Draw the phylogenetic tree (must install matplotlib to use this formatting) Phylo.draw_ascii(tree) # Print the phylogenetic tree in terminal
def DrawAdvan(self): if self.chosenFileName == '': self.showOpenFileWindow() if self.tree != 0: self.tree.root.color = '#808080' Phylo.draw(self.tree, branch_labels=lambda c: c.branch_length)
def plot(self, filename=None, show=True, labelColors=None, **kwargs): """ Use the Phylo package to plot the NJ tree, showing branch support. @param filename: A C{str} file name to which to write the tree image. @param show: If C{True} the image will be displayed. This is only useful when C{filename} is not C{None}. @param labelColors: A function or a dictionary specifying the color of the tip label. If a dictionary is used, its keys should be strings of sequence ids and its values strings of color names. If a function is used, it will be called with a string of a sequence id and should return its corresponding color. If labelColors is None the label will be shown in black. @param kwargs: Additional (optional) arguments to be passed to savefig. For available options, see: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.savefig """ fp = StringIO(self.newick()) Phylo.draw(Phylo.read(fp, 'newick', comments_are_confidence=True), do_show=show, label_colors=labelColors) if filename: plt.gcf().savefig(filename, **kwargs) if not show: plt.close()
def plot_tree(tree): from Bio import Phylo from tree_util import to_Biopython, color_BioTree_by_attribute btree = to_Biopython(tree) color_BioTree_by_attribute(btree,"cHI", transform = lambda x:x) Phylo.draw(btree, label_func = lambda x: 'X' if x.serum else '' if x.HI_info else '', show_confidence= False) #, branch_labels = lambda x:x.mutations)
def main(): args = parse_arguments() msa = ParsimonyTree.read_msa(args.a) i_tree = ParsimonyTree.read_tree(args.n) nb_f = ParsimonyTree.get_nni_neighbors if args.spr: nb_f = ParsimonyTree.get_spr_neighbors elif args.tbr: nb_f = ParsimonyTree.get_tbr_neighbors mcmc = MonteCarlo(msa, i_tree, nb_f, args.r, args.p) f_tree = mcmc.get_tree() with open(args.o, "w") as outfile: Phylo.write(f_tree, outfile, "newick") print("\n=========================\n") print("Original Tree") print("Score:", ParsimonyTree.get_parsimony_score(msa, i_tree)) Phylo.draw(i_tree) Phylo.draw_ascii(i_tree) print("\n=========================\n") print("Final Tree") print("Score:", ParsimonyTree.get_parsimony_score(msa, f_tree)) Phylo.draw(f_tree) Phylo.draw_ascii(f_tree) print("\n=========================\n") print("Histogram of Parsimony Scores") plt.title("Histogram of Parsimony Scores") plt.hist(mcmc.get_scores()) plt.show()
def draw_tree(datafile, tree_type, newick, node_ids, output_file): with open(datafile) as f: reader = csv.DictReader(f, delimiter='\t') for row in reader: labels[row["Node"]] = row tree = Phylo.read(StringIO(newick), "newick") id_tree = Phylo.read(StringIO(node_ids), "newick") for clade, c_id in zip(tree.find_clades(), id_tree.find_clades()): clade.info = labels[c_id.name][display( tree_type)] if c_id.name in labels else "" tree.ladderize() # Flip branches so deeper clades are displayed at top plt.ion() fig = plt.figure(frameon=False) Phylo.draw(tree, axes=fig.gca(), do_show=False, label_func=label, label_colors=lambda n: colors[tree_type]) plt.title(display(tree_type) + " (count)") prettify_tree(fig) plt.ioff() if output_file: fig.savefig(output_file, format='png', bbox_inches='tight', dpi=300) else: plt.show()
def drawConsensusTreeDendropy(self): if self.path1 != '' and self.path2 != '': #get files extensions self.fileExtension1 = (os.path.splitext(self.path1)[1])[1:] self.fileExtension2 = (os.path.splitext(self.path2)[1])[1:] #open tree files self.tree1 = dendropy.Tree.get_from_path(self.path1, self.fileExtension1) self.tree2 = dendropy.Tree.get_from_path(self.path2, self.fileExtension2) #prepare tree list self.trees = dendropy.TreeList() self.trees.append(self.tree1) self.trees.append(self.tree2) #generate consensus tree self.consensus_tree = self.trees.consensus(min_freq=0.2) #draw tree self.handle = StringIO(self.consensus_tree._as_newick_string()) # POPRAWIONY BLAD Z KONWERSJA DO BUFORA #self.handle = StringIO(self.consensus_tree.to_string(plain_newick=True)) self.tree = Phylo.read(self.handle, 'newick') self.tree.root.color = '#808080' Phylo.draw(self.tree)
def plot_phylo_tree(align: MultipleSeqAlignment, accession_numbers: dict): """ Plots a phylogenetic tree :param align: MultipleSeqAlignment with the alignment result to be plotted :param accession_numbers: dict of accession numbers and their translation to human-understandable names :return: figure-handle of the plotted phylogenetic tree """ # calculate distance - https://biopython.org/wiki/Phylo calculator = DistanceCalculator('identity') dm = calculator.get_distance(align) # construct a tree constructor = DistanceTreeConstructor() tree = constructor.upgma(dm) # remove the names for the non-terminals for better visual appeal for non_terminal in tree.get_nonterminals(): non_terminal.name = '' # change accession numbers into human more understandable names for terminal in tree.get_terminals(): terminal.name = accession_numbers[re.match("(^\S*)(?=\.)", terminal.name)[0]] print(Phylo.draw_ascii(tree)) # plot the tree fig, ax = plt.subplots(1, 1) # draw the resulting tree Phylo.draw(tree, show_confidence=False, axes=ax, do_show=False) ax.set_xlim(right=0.8) return fig
def buildTree(FASTAFile): myAlignment = AlignIO.read(FASTAFile, "fasta") # Create a tip mapping from the fasta file tipMapping = {} for record in myAlignment: tipMapping[record.id] = str(record.seq) # Compute a distance matrix and construct tree calculator = DistanceCalculator("identity") myMatrix = calculator.get_distance(myAlignment) constructor = DistanceTreeConstructor() upgmaTree = constructor.nj(myMatrix) upgmaTree.root_at_midpoint() Phylo.draw(upgmaTree) # Convert phyloxml tree to newick # biopython does not provide a function to do this so it was necessary # to write to a buffer in newick to convert then get rid of unneeded info for clade in upgmaTree.get_terminals(): clade.name = "\"" + clade.name + "\"" buf = cStringIO.StringIO() Phylo.write(upgmaTree, buf, 'newick', plain = True) tree = buf.getvalue() tree = re.sub(r'Inner\d*', '', tree) tree = tree.replace(";", "") tree = literal_eval(tree) #newick format # RLR tree required for maxParsimony function tree = NewicktoRLR(tree) return tree
def prettyprint_tree(tree, file=None): # Convert the "tree" object (list of clades) to a BioPython tree # to take advantage of their output methods def create_ntree(tree): ntree = BaseTree.Clade() for key in tree: el = tree[key] if len(el.values()) > 0: ntree.clades.append(create_ntree(el)) else: ntree.clades.append(BaseTree.Clade(name=list(key)[0])) return ntree # Sort the clades from largest to smallest new_tree = sorted(tree, key=lambda x: -len(x)) # Build a dictionary representation of the tree tree_dict = {} for clade in new_tree: tree_dict = create_tree_dict(tree_dict, clade) # Convert the dictionary representation to a BioPython Tree object ntree = BaseTree.Tree(create_ntree(tree_dict)) # Use the BioPython print method Phylo.draw_ascii(ntree, file=file) try: Phylo.draw(ntree) except: pass return
def draw_tree(): alignment = AlignIO.read('outfile_padded.aln', 'clustal') # reading the alignment file calculator = DistanceCalculator('identity') dm = calculator.get_distance(alignment) msas = bootstrap(alignment, 100) calculator = DistanceCalculator('blosum62') constructor = DistanceTreeConstructor(calculator) trees = bootstrap_trees(alignment, 100, constructor) consensus_tree = bootstrap_consensus(alignment, 1000, constructor, majority_consensus) consensus_tree.ladderize() consensus_tree.root.color="green" #mrca = tree.common_ancestor({"name": "PC_00004"}, {"name": "BG_I_00594"}) mrca = consensus_tree.common_ancestor({"name": "PC_00004|DNA"}) mrca.color = "salmon" Phylo.write(consensus_tree, 'TreeToCutOff.xml', 'phyloxml') #plt.rc('font', size=10) # controls default text sizes #HERE IS THE SETTING FOR THAT ALLOWS ME TO HIDE THE BRANCH TIP LABELS #plt.rc('axes', titlesize=14) # fontsize of the axes title #plt.rc('xtick', labelsize=10) # fontsize of the tick labels #plt.rc('ytick', labelsize=10) # fontsize of the tick labels #plt.rc('figure', titlesize=18) # fontsize of the figure title #plt.savefig("TreeToCutOff_check.svg", format='svg', dpi=1200, bbox_inches='tight') Phylo.draw(consensus_tree, show_confidence=True) pylab.gcf().set_dpi(300) pylab.savefig("phylo-dot.png") pylab.clf()
def drawConsensusTreeBioNexus(self): if self.path1 != '' and self.path2 != '': #get files extensions self.fileExtension1 = (os.path.splitext(self.path1)[1])[1:] self.fileExtension2 = (os.path.splitext(self.path2)[1])[1:] #open tree files self.trees = [] #first tree self.f = open(self.path1, 'r') self.tree1 = Trees.Tree(self.f.read()) self.trees.append(self.tree1) self.f.close() #second tree self.f = open(self.path2, 'r') self.tree2 = Trees.Tree(self.f.read()) self.trees.append(self.tree2) self.f.close() #generate consensus tree self.consensus_tree = Trees.consensus(self.trees) #draw tree self.handle = StringIO(self.consensus_tree.to_string(plain_newick=True)) self.tree = Phylo.read(self.handle, 'newick') self.tree.root.color = '#808080' Phylo.draw(self.tree)
def showSequenceTree(hits): """Returns a plot that contains a dendrogram of the sequence similarities among the sequences in given hit list. :arg hits: A dictionary that contains hits that are obtained from a blast record object. :type hits: dict """ clustalw = which('clustalw') if clustalw is None: print( "The executable for clustalw does not exists, install or add clustalw to path." ) return try: from Bio import Phylo except: raise ImportError("Phylo is not installed properly.") with open("hits.fasta", "w") as inp: for z in hits: inp.write(">" + str(z) + "\n") inp.write(hits[z]['hseq']) inp.write("\n") cmd = clustalw + " hits.fasta" os.system(cmd) tree = Phylo.read("hits.dnd", "newick") try: import pylab except: raise ImportError("Pylab or matplotlib is not installed.") Phylo.draw(tree) return
def build_trees(filename, tree_name): # Compute alignment with ClustalW algorithm clustalw_cline = ClustalwCommandline("clustalw", infile="{}.fa".format(filename)) clustalw_cline() alignment = AlignIO.read("{}.aln".format(filename), format="clustal") # Create distance matrix calculator = DistanceCalculator('blosum62') dist_matrix = calculator.get_distance(alignment) # Build phylogenetic trees using upgma and nj methods constructor = DistanceTreeConstructor() upgma_tree = constructor.upgma(dist_matrix) nj_tree = constructor.nj(dist_matrix) # Draw the trees label_func = lambda clade: "" if clade.name.startswith("Inner") else clade Phylo.draw(upgma_tree, label_func=label_func, do_show=False) plt.title("{} × upgma".format(tree_name)) plt.show() Phylo.draw(nj_tree, label_func=label_func, do_show=False) plt.title("{} × nj".format(tree_name)) plt.show()
def showMatplotlibTreeWindow(self): if self.chosenFileName == '': self.showOpenFileDialog() if self.tree != 0: self.tree.root.color = '#808080' Phylo.draw(self.tree, branch_labels = lambda c: c.branch_length)
def run(self): """Main method to process build tree step""" LOGGER.info("Reconstructing the final tree..") if os.path.isfile(self._tree_input_file) is False: LOGGER.error("The input file (concatenated sequence) " "for running FastTree doesn't exist!") raise FileExistsError(f"The input file {self._tree_input_file} " f"for FastTree doesn't exist") fsl = FastaSeqList(self._tree_input_file) if fsl.get_seq_list_len() < 4: LOGGER.error('Too few species for building tree') raise ValueError('Too few species for FastTree') ft = FastTree(self.config) ft.run(self._tree_input_file, tree_args=self._args_tree, output_file=os.path.join(self._align_output_dir, 'PyUBCG' + self.config.postfixes.align_tree_const)) self._reconstruct_gene_trees() self._make_all_gene_trees() if self.config.draw: tree = Phylo.read(os.path.join(self._align_output_dir, 'PyUBCG' + self.config.postfixes.align_tree_const), 'newick') Phylo.draw(tree, savefig={'fname': os.path.join(self._align_output_dir, self.config.align_prefix+'.png'), 'dpi': 300, 'bbox_inches': 'tight'})
def tree_gen(self, network_file): ''' Generate a tree via the Phylo module ''' print("Building guided tree...") tree = Phylo.read(self.network_file, "newick") Phylo.draw(tree)
def draw_combo_tree(datafile, newick, node_ids, output_file): with open(datafile) as f: reader = csv.DictReader(f, delimiter='\t') for row in reader: labels[row["Node"]] = row tree = Phylo.read(StringIO(newick), "newick") id_tree = Phylo.read(StringIO(node_ids), "newick") for clade, c_id in zip(tree.find_clades(), id_tree.find_clades()): clade.gains = labels[c_id.name][display( 'Gains')] if c_id.name in labels else "" clade.losses = labels[c_id.name][display( 'Losses')] if c_id.name in labels else "" tree.ladderize() # Flip branches so deeper clades are displayed at top plt.ion() matplotlib.rc( 'font', size=8, ) # backgroundcolor='silver') fig = plt.figure(frameon=False, figsize=((15, 11))) Phylo.draw(tree, axes=fig.gca(), do_show=False, label_func=combo_label, label_colors=lambda n: 'blue') plt.title("Gene Gains (+) and Losses (-)") prettify_tree(fig) plt.ioff() fig.savefig(output_file, format='png', bbox_inches='tight', dpi=300)
def draw_tree(self): """ Draws the phylogenetic tree from the tree file in 'newick' format. """ tree = Phylo.read(self.tree_file, 'newick') Phylo.draw(tree) plt.show()
def draw(x, y=''): import matplotlib.pyplot as plt plt.figure(figsize=(12,6)) axes = plt.axes(frameon=False) axes.get_xaxis().set_visible(False) axes.get_yaxis().set_visible(False) bp.draw(x, do_show=False, axes=axes) base, extension = '.'.join(filename.split('.')[:-1]), filename.split('.')[-1] plt.savefig(base + y + '.' + extension)
def phylogeny(filename): tree = Phylo.read(filename, "newick") print tree print Phylo.draw_ascii(tree) tree.rooted = True Phylo.draw(tree) #alignments("alinhamentos.phy") #phylogeny("filogenia.dnd")
def tree_main(msa_files): for msa in msa_files: alignment = AlignIO.read(msa, 'clustal') calculator = TreeConstruction.DistanceCalculator('identity') dist_matrix = calculator.get_distance(alignment) constructor = TreeConstruction.DistanceTreeConstructor(calculator, 'nj') tree = constructor.build_tree(alignment) tree.rooted = True Phylo.draw(tree)
def heatMapTree(heats,treeFile,title,xlabel,ylabel,showX=False,showY=False): scoremap = DataFrame(heats).T.fillna(0) xlabels = list(scoremap.columns) ylabels = list(scoremap.index) #if showX: pylab.xticks(xpos, xlabels,rotation=90) #if showY: pylab.yticks(ypos, ylabels) fig = plt.figure() plt.suptitle(title,fontsize=22) normalizeTree(treeFile) tree=Phylo.read(treeFile,"newick") tree = prune_tree(tree,ylabels) tree.ladderize() scoremap = reorderHeatMap(scoremap,tree) phyloLabels = zip(map(str,tree.get_terminals()),scoremap.index) gs=gridspec.GridSpec(1, 2,width_ratios=[1,1,-2,2], wspace=0) phyl_ax=plt.subplot(gs[0]) phyl_ax.set_ylabel(ylabel) Phylo.draw(tree,axes=phyl_ax,show_confidence=False,do_show=False) plt.rcParams['font.size']=8 xlabels = list(scoremap.columns) ylabels = list(scoremap.index) xpos = np.arange(len(xlabels))+1 xminorpos = np.arange(len(xlabels))+0.5 ypos = np.arange(len(ylabels)+1) #plt.grid() ht_ax=plt.subplot(gs[1]) ht_ax.set_xlabel(xlabel) ht_ax.set_xlim(0,len(xlabels)) ht_ax.set_ylim(0,len(ylabels)) plt.setp(phyl_ax.get_xticklabels(),visible=False) plt.setp(phyl_ax.get_yticklabels(),visible=False) plt.setp(ht_ax.get_xticklabels(),visible=True) plt.setp(ht_ax.get_yticklabels(),visible=False) plt.setp(phyl_ax.get_xticklines(),visible=True) plt.setp(phyl_ax.get_yticklines(),visible=True) plt.setp(ht_ax.get_xticklines(),visible=True) plt.setp(ht_ax.get_yticklines(),visible=True) ht_ax.xaxis.set_ticks(xminorpos) ht_ax.yaxis.set_ticks(ypos) ht_ax.set_xticklabels(xlabels,rotation=45,fontsize=10) ht_ax.set_yticklabels(ylabels,fontsize=10,alpha=1.0) ht_ax.xaxis.set_tick_params(pad=4) ht_ax.yaxis.set_tick_params(pad=4) pylab.xticks(xpos, xlabels) pylab.yticks(ypos, ylabels) for tick in ht_ax.xaxis.get_major_ticks(): tick.label1.set_horizontalalignment('right') heatmap = plt.pcolor(scoremap,norm=LogNorm()) plt.grid(True) plt.colorbar()
def phylo_tree(dnd, draw): """ Takes a dnd file and draws a phyloginetic tree. If draw is False draws ascii tree. """ from Bio import Phylo tree = Phylo.read("lab3.dnd", "newick") if draw: Phylo.draw(tree) else: Phylo.draw_ascii(tree)
def Arvore3(output_file): tree=Phylo.read("backs\clustalw.dnd", "newick") matplotlib.rc('font', size=6) # set the size of the figure fig = plt.figure(figsize=(10, 20), dpi=100) # alternatively #fig.set_size_inches(10, 20) axes = fig.add_subplot(1, 1, 1) Phylo.draw(tree, axes=axes) plt.savefig(output_file) return
def export(): #abre uma janela com a arvore filogenetica resultante e ser possivel guarda-la de forma a exportar para o site import pylab from Bio import Phylo try: lista = interesting_list for i in range(len(lista)): align_tree = Phylo.read(("Malign" + str(i+1) + ".phy_phyml_tree.txt"), "newick") Phylo.draw(align_tree) pylab.show() except: print("Vizualizing Tree error!")
def test_draw_with_label_colors_callable(self): """Run the tree layout algorithm with a label_colors argument passed in as a callable. Don't display tree.""" pyplot.ioff() # Turn off interactive display dollo = Phylo.read(EX_DOLLO, 'phyloxml') apaf = Phylo.read(EX_APAF, 'phyloxml') label_colors_dollo = lambda label: 'r' if label == 'f_50' else 'k' label_colors_apaf = lambda label: 'r' Phylo.draw(dollo, label_colors=label_colors_dollo, do_show=False) Phylo.draw(apaf, label_colors=label_colors_apaf, do_show=False)
def distanceTree(seqfile=None, seqs=None, ref=None): """Phylo tree for sequences""" aln = clustalAlignment(seqfile, seqs) from Bio import Phylo tree = Phylo.read('temp.dnd', 'newick') leafList = tree.get_terminals() if ref != None: tree.root_with_outgroup(ref) f=plt.figure() #Phylo.draw_graphviz(tree,font_size='9', prog='neato') Phylo.draw(tree) return
def produce_hm(full_len,outlier,max_num,min_num,file_handle,txtnames): tree=Phylo.read("/home/asmariyaz/Desktop/reorder.nwk","newick") a=1.0 data=np.array(full_len) cmap = mpl.cm.hot if outlier==0: threshold=1 else: threshold=outlier-0.01 fig = plt.figure(figsize=(25,25)) plt.suptitle(file_handle.replace('_del.csv',''),fontsize=22) cmap.set_over('green') cmap.set_under('grey') gs=gridspec.GridSpec(1, 2,height_ratios=[1,1,-2,2] ,width_ratios=[1,1,-2,2],hspace=0,wspace=0) phyl_ax=plt.subplot(gs[0]) Phylo.draw(tree, axes=phyl_ax,do_show=False) ht_ax=plt.subplot(gs[1]) ht_ax.set_xlim(0,34) ht_ax.set_ylim(0,34) ht_ax.grid(True, which='both') ##cb_ax,kw =mpl.colorbar.make_axes(ht_ax, shrink=0.65) plt.setp(phyl_ax.get_xticklabels(),visible=False) plt.setp(phyl_ax.get_yticklabels(),visible=False) plt.setp(ht_ax.get_xticklabels(),visible=True) plt.setp(ht_ax.get_yticklabels(),visible=False) plt.setp(phyl_ax.get_xticklines(),visible=False) plt.setp(phyl_ax.get_yticklines(),visible=False) plt.setp(ht_ax.get_xticklines(),visible=False) plt.setp(ht_ax.get_yticklines(),visible=False) xticks=range(34) ht_ax.xaxis.set_ticks(xticks) ht_ax.yaxis.set_ticks(xticks) ht_ax.set_xticklabels(txtnames,rotation=45,fontsize=12.5,alpha=a) ht_ax.xaxis.set_tick_params(pad=4) for tick in ht_ax.xaxis.get_major_ticks(): tick.label1.set_horizontalalignment('right') ##ticks_at = [-2,0,2] img = ht_ax.imshow(data, cmap=cmap, interpolation='none',vmax=threshold,aspect='auto',extent=[34,0,34,0],origin='lower') ##cb = mpl.colorbar.ColorbarBase(ax=cb_ax,cmap=cmap,ticks=ticks_at,extend='neither',**kw) ##cb.cmap.set_over('green') ##img= mpimg.imread('/home/asmariyaz/Desktop/mytree.png') ##phyl_ax.imshow(img,interpolation='bilinear',aspect='auto') heatmap_file=fig.savefig('/home/asmariyaz/Desktop/heatmap1/'+file_handle.replace('.csv','')+'.pdf',bbox_inches='tight',dpi=150) return heatmap_file
def upgma(dist_matrix): """Unweighted Pair Group Method with Arithmetic Mean""" node_for_code = {} # Make our own copy distance_matrix = dict(dist_matrix) for elem_one, elem_two in list(distance_matrix): distance_matrix[elem_two, elem_one] = distance_matrix[elem_one, elem_two] node_for_code[elem_one] = Node(None, None, 0, 0, 0, elem_one) node_for_code[elem_two] = Node(None, None, 0, 0, 0, elem_two) while distance_matrix: # Find 2 closest elements and merge them code_one, code_two = min(distance_matrix, key=distance_matrix.get) min_dist = distance_matrix[code_one, code_two] new_code = code_one + code_two nodes_to_add = {} n_one = node_for_code[code_one] n_two = node_for_code[code_two] node_for_code[new_code] = Node(n_one, n_two, min_dist / 2 - n_one.overall_dist, min_dist / 2 - n_two.overall_dist, min_dist / 2, new_code) # Recalculate distance to new element for k in distance_matrix: if code_one in k: other_node = k[1] if k[0] == code_one else k[0] if other_node != code_two: nodes_to_add[other_node, new_code] = \ (distance_matrix[other_node, code_one] * len(code_one) + distance_matrix[other_node, code_two] * len(code_two)) / (len(code_one) + len(code_two)) nodes_to_add[new_code, other_node] = \ (distance_matrix[other_node, code_one] * len(code_one) + distance_matrix[other_node, code_two] * len(code_two)) / (len(code_one) + len(code_two)) # Delete distances to objects which we merged. list() for copy for k in list(distance_matrix): if code_one in k or code_two in k: del distance_matrix[k] for k in nodes_to_add: distance_matrix[k] = nodes_to_add[k] print(node_for_code[max(node_for_code, key=len)].to_string()) # Optional code for displaying tree with Biopy and matplotlib tree = Phylo.read(StringIO(node_for_code[max(node_for_code, key=len)].to_string()), "newick") tree.ladderize() # Flip branches so deeper clades are displayed at top Phylo.draw(tree)
def prettyprint_tree(tree): def create_ntree(tree): ntree = BaseTree.Clade() for key in tree: el = tree[key] if type(el) == dict: ntree.clades.append(create_ntree(el)) else: ntree.clades.append(BaseTree.Clade(name=list(key)[0])) return ntree ntree = BaseTree.Tree(create_ntree(tree)) Phylo.draw_ascii(ntree) Phylo.draw(ntree) return
def draw_phylogeny(phylogeny_txt, out_file): tree_string, target_name = open(phylogeny_txt, "r").read().splitlines() g2c.table[target_name] = "red" tree = Phylo.read(StringIO(tree_string), "newick") tree.clade.branch_length = 0 for clade in tree.find_clades(): if clade.is_terminal(): clade.color = g2c(clade.name) tree.ladderize() pylab.rcParams["lines.linewidth"] = 3.0 Phylo.draw(tree, do_show=False) pylab.savefig(out_file)
def drawConsensusTreeBio(self): if self.path1 != '' and self.path2 != '': self.fileEx1 = (os.path.splitext(self.path1)[1])[1:] self.fileEx2 = (os.path.splitext(self.path2)[1])[1:] self.trees = [] self.f = open(self.path1, 'r') self.tree1 = Trees.Tree(self.f.read()) self.trees.append(self.tree1) self.f.close() self.f = open(self.path2, 'r') self.tree2 = Trees.Tree(self.f.read()) self.trees.append(self.tree2) self.f.close() self.consensus_tree = Trees.consensus(self.trees) # SHOWTIME self.handle = StringIO(self.consensus_tree.to_string(plain_newick=True)) self.tree = Phylo.read(self.handle, 'newick') self.tree.root.color = '#808080' #self.OpenInfoWindow() Phylo.draw(self.tree) else: print "Nie wybrano punktow" # WYSWIETLA INFORMACJE img = pylab.imread('img/wally.png', 'rb') pylab.imshow(img) pylab.plot(0, 0) # DAJE CZYSTY OBRAZ BEZ OSI ** PEWNIE MOZNA PROSCIEJ frame1 = pylab.gca() for xlabel_i in frame1.axes.get_xticklabels(): xlabel_i.set_visible(False) xlabel_i.set_fontsize(0.0) for xlabel_i in frame1.axes.get_yticklabels(): xlabel_i.set_fontsize(0.0) xlabel_i.set_visible(False) for tick in frame1.axes.get_xticklines(): tick.set_visible(False) for tick in frame1.axes.get_yticklines(): tick.set_visible(False) # SHOWTIME pylab.show()
def test_draw_with_label_colors_dict(self): """Run the tree layout algorithm with a label_colors argument passed in as a dictionary. Don't display tree.""" pyplot.ioff() # Turn off interactive display dollo = Phylo.read(EX_DOLLO, 'phyloxml') apaf = Phylo.read(EX_APAF, 'phyloxml') label_colors_dollo = { 'f_50': 'red', 'f_34': 'blue', } label_colors_apaf = { '22_MOUSE': 'red', '18_NEMVE': 'blue', } Phylo.draw(dollo, label_colors=label_colors_dollo, do_show=False) Phylo.draw(apaf, label_colors=label_colors_apaf, do_show=False)
def plot_tree(self): """show phylogenetic tree""" import pylab f = pylab.figure(figsize=(8,8)) ax = f.add_subplot(111) y = Phylo.draw(self.tree, axes=ax)
def test_draw(self): """Run the tree layout algorithm, but don't display it.""" pyplot.ioff() # Turn off interactive display dollo = Phylo.read(EX_DOLLO, 'phyloxml') apaf = Phylo.read(EX_APAF, 'phyloxml') Phylo.draw(dollo, do_show=False) Phylo.draw(apaf, do_show=False) # Fancier options Phylo.draw(apaf, do_show=False, branch_labels={apaf.root: 'Root'}) Phylo.draw(apaf, do_show=False, branch_labels=lambda c: c.branch_length)
def producePhylo(fig,gridspace1,tree_path): font= mpl.rcParams['font.size']=13.0 tree=Phylo.read(tree_path,"newick") ##rect_phyl = [-0.7, 0.3, 0.3, 0.8] phyl_ax=fig.add_subplot(gridspace1) ##phyl_ax = plt.axes(rect_phyl,frameon=True) phyl_ax.add_patch(Rectangle((5.6,16.7),10.2,16.8,edgecolor="brown", fill=False)) phyl_ax.add_patch(Rectangle((5.6,9.5),10.2,6.8,edgecolor="magenta", fill=False)) phyl_ax.add_patch(Rectangle((5.6,6.6),10.2,2.6,edgecolor="black", fill=False)) phyl_ax.add_patch(Rectangle((5.6,0.4),10.2,5.9,edgecolor="turquoise", fill=False)) Phylo.draw(tree, axes=phyl_ax, do_show=False,show_confidence=False) phyl_ax.set_xlim(0,16) phyl_ax.set(xlabel='',ylabel='') plt.setp(phyl_ax.get_xticklabels(),visible=False) plt.setp(phyl_ax.get_yticklabels(),visible=False) plt.setp(phyl_ax.get_xticklines(),visible=False) plt.setp(phyl_ax.get_yticklines(),visible=False) return phyl_ax
def tree_image(nwk_tree): f = open(nwk_tree) handle = StringIO(f.read().rstrip()) tree = Phylo.read(handle, "newick") Phylo.draw(tree) plt.savefig("%s.png" %nwk_tree) plt.clf() # tree = Tree(tree2.read()) # tree.render(nwk_tree+".png", w=183, units="mm") #def hgt_nwk_tree(ref_nwk_tree,positions,sequences): # """process the newick tree""" # f = open("ref_nwk_tree") # g = f.read() # pass # #make_core_tree("aligned.fasta_align_phylip",0.0,"GTR",0.0)