def calc_mcc_tree(tree_path, burnin): treeannotater_cmd = 'treeannotator -burnin {0} {1} {2}'.format( burnin, tree_path, tree_path.replace('.trees', '_mcc.tre')) subprocess.run(shlex.split(treeannotater_cmd)) myTree = bt.loadNexus(tree_path.replace('.trees', '_mcc.tre'), absoluteTime=False) max_time = max([ float(item.name.split('_')[1]) for item in myTree.Objects if item.branchType == 'leaf' ]) myTree.setAbsoluteTime(max_time) return (myTree)
def test_beast2_traits(self): print('Testing BEAST v2 trait parsing') ll=bt.loadNexus('./tests/data/MERS.mcc.tree') ll.traverse_tree() print('Test if branches have correct number of traits') assert len(ll.root.traits)==10 print('Root has correct number of traits') for k in ll.Objects: if k.is_node() and k!=ll.root: assert len(k.traits)==13 elif k.is_leaf(): assert len(k.traits) in [9,12] print('Branches have correct number of traits')
def test_beast1_traits(self): print('Testing BEAST v1 trait parsing') ll=bt.loadNexus('./tests/data/miniFluB.mcc.tree',tip_regex='_([0-9-]+)') ll.traverse_tree() print('Test if branches have correct number of traits') assert len(ll.root.traits)==77 print('Root has correct number of traits') for k in ll.Objects: if k.is_node() and k!=ll.root: assert len(k.traits) in [77,74,71,41] elif k.is_leaf(): assert len(k.traits) in [76,73,70] print('Branches have correct number of traits')
except ImportError: from io import StringIO as sio from io import BytesIO as csio import requests from Bio import Phylo path = ##pathway to json file treepath = ##pathway to mcc tree # treeFile = 'aligned.tree' # bstree = Phylo.read(path + treeFile, 'newick') nx_tree = json.load(open(path + '/finalproject_filename.json','r')) # do mcc stuff ll = bt.loadNexus(treepath + '/file_mcc.tree') tree = nx_tree['tree'] # tree data meta = nx_tree['meta'] # metadata # print(tree) traitName = 'country_exposure' # trait used for colouring the tree xspan = (0, 0.003) typeface = 'Times New Roman' mpl.rcParams['font.weight'] = 300 mpl.rcParams['axes.labelweight'] = 300 mpl.rcParams['font.family'] = typeface mpl.rcParams['font.size'] = 12
from matplotlib.gridspec import GridSpec #import requests from io import StringIO as sio from io import BytesIO as csio # Can add more tree files for additional genome segments fig_name = 'CoVSequentialTanglegram.png' tree_file = "sars-like-CoVs-sub_" segments=['1-2936','2937-4936','4937-6870','6871-8473','8474-19706','19707-20428'] "Load trees into tree dict" trees={} ## dict for segment in segments: print(segment,) treeFile = tree_file + segment + ".nexus" ll=bt.loadNexus(treeFile,absoluteTime=False) ll.setAbsoluteTime(2020.0) trees[segment]=ll print('\nDone!') "Rescale tree heights so they are all equal" tree_heights = [] for t,tr in enumerate(trees.keys()): ## iterate over trees cur_tree=trees[tr] ## fetch tree object tree_heights.append(cur_tree.treeHeight) max_height_cap = max(tree_heights) for t,tr in enumerate(trees.keys()): ## iterate over trees cur_tree=trees[tr] ## fetch tree object for k in cur_tree.Objects: ## iterate over a flat list of branches k.length = k.length * (max_height_cap/cur_tree.treeHeight) #print(k.height)
def test_nexus(self): tree = bt.loadNexus('./tests/data/2020-04-13_treetime/divergence_tree.nexus') tree.treeStats() pass