def tree_reconstruction(phy_file, method, model, phyformat): '''Construct tree with given method and model''' aln = AlignIO.read(phy_file, 'phylip-' + phyformat) constructor = DistanceTreeConstructor() calculator = DistanceCalculator(model) dm = calculator.get_distance(aln) if method == 'upgma': tree = constructor.upgma(dm) elif method == 'nj': tree = constructor.nj(dm) tree.ladderize() for c in tree.find_clades(): if 'Inner' in c.name: c.name = '' Phylo.write(tree, args.output + '/tree.nwk', 'newick') plt.rcParams['font.style'] = 'italic' plt.rc('font', size=8) plt.rc('axes', titlesize=14) plt.rc('xtick', labelsize=10) plt.rc('ytick', labelsize=10) plt.rc('figure', titlesize=18) draw(tree, do_show=False) plt.savefig(args.output + "/tree.svg", format='svg', dpi=1200)
def draw_tree(self, show_scores: bool = False): """ Draws tree. show_scores: show branch parsimonious scores (optional); default is False """ self.show_scores = show_scores if self.tree is None: print("Please first build the tree.") else: draw(self.tree, label_func=self.get_label)
def draw_tree(self, show_branch_labels=False): """ Draws tree. show_branch_labels: show branch lengths; default is False """ if self.tree is None: print("Please first build the tree.") else: if show_branch_labels: draw(self.tree, label_func=self.get_label, branch_labels=(lambda clade: clade.branch_length)) else: draw(self.tree, label_func=self.get_label)
def plot_tree(tree, title, output_file): # build the plot # clear names of nodes that are not leaves for clade in tree.find_clades(): if clade.name[0:5] == 'Inner': clade.name = '' fig, ax = plt.subplots() axes = fig.add_subplot(1, 1, 1) fig = draw(tree, axes=axes, do_show=0) ax.get_xaxis().set_ticks([]) ax.get_yaxis().set_ticks([]) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) plt.gcf().subplots_adjust(right=0.99, left=0.01, top=0.9, bottom=0.1) plt.suptitle(title) plt.yticks([]) plt.ylabel('') plt.savefig(output_file, dpi=200) plt.close(fig)
def ex01(): alignments = get_alignments() calculator = DistanceCalculator('blosum62') constructor = DistanceTreeConstructor() for a, name in alignments: dist_matrix = calculator.get_distance(a) upgma_tree = constructor.upgma(dist_matrix) nj_tree = constructor.nj(dist_matrix) print("\n\n>>> {}".format(name)) # print(dist_matrix) # draw_ascii(upgma_tree) # draw_ascii(nj_tree) draw(upgma_tree) draw(nj_tree)
def draw_tree(tree, duplications): def branch_labels(node): if node in duplications: return '*' else: return '' def label_colors(label): mapping = { 'Mouse': 'red', 'Human': 'green', 'Beetle': 'blue', 'Fruitfly': 'purple' } for species, color in mapping.items(): if label.startswith(species): return color return 'black' from pylab import rcParams rcParams['figure.figsize'] = 15, 40 draw(tree, branch_labels=branch_labels, label_colors=label_colors)
import matplotlib.pyplot as plt # Vuelvo a leer el alineamiento, en realidad este paso se puede saltar si se ejecuta el script de una sola vez. En la variable align ya tenemos el alineamiento en clustal alignment = AlignIO.read(wd + 'sequencesAln.fa', 'fasta') # reading the alignment file # Creamos la matriz de distancias. No he encontrado la corrección de Jukes-Cantor calculator = DistanceCalculator('identity') dm = calculator.get_distance(alignment) # Available models: identity, blastn, trans, benner6, benner22, benner74, blosum100, blosum30, blosum35, blosum40, blosum45, blosum50, blosum55, blosum60, blosum62, blosum65, blosum70, blosum75, blosum80, blosum85, blosum90, blosum95, feng, fitch, genetic, gonnet, grant, ident, johnson, levin, mclach, miyata, nwsgappep, pam120, pam180, pam250, pam30, pam300, pam60, pam90, rao, risler, structure # Build with neighbour joining algorithm a tree from dm constructor = DistanceTreeConstructor() tree = constructor.nj(dm) # Build with upgma algorithm a tree from dm # tree = constructor.upgma(dm) # Write tree Phylo.write(tree, wd + 'TreeToCutOff.nwk', 'newick') #Plot tree plt.rc( 'font', size=0 ) # 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 draw(tree, do_show=False) plt.savefig(wd + "TreeToCutOff.svg", format='svg', dpi=1200)
clade_4413.branch_length = (8758 - 4413) clade_2356.branch_length = (8758 - 2356) clade_rat.branch_length = 2356 clade_mouse.branch_length = 2356 clade_8758.branch_length = (17735 - 8758) clade_7481.branch_length = (17735 - 7481) clade_horse.branch_length = 7481 clade_muntjak.branch_length = 7481 clade_17735.branch_length = 18960 - 17735 clade_cat.branch_length = 18960 tree = BaseTree.Tree(root, rooted=True) print(tree) draw(tree) ################################## # Non-coding clade_131584 = BaseTree.Clade(None, "") clade_120079 = BaseTree.Clade(None, "") clade_93169 = BaseTree.Clade(None, "") clade_55571 = BaseTree.Clade(None, "") clade_35195 = BaseTree.Clade(None, "") clade_22904 = BaseTree.Clade(None, "") clade_28998 = BaseTree.Clade(None, "") clade_horse = BaseTree.Clade(None, "horse") clade_cow = BaseTree.Clade(None, "cow") clade_gorilla = BaseTree.Clade(None, "gorilla") clade_orangutan = BaseTree.Clade(None, "orangutan")