tree_type = "nexus" elif first.find("<") > -1: tree_type = "phyloxml" else: tree_type = "newick" f.close() if tree_type == "newick": # only one tree per file, so get a tree tree = dendropy.Tree.get_from_path(filename, tree_type) dpy_trees[tree.label] = tree dpy_tree_list.append(tree) else: # the others can have more than one per, # so get a TreeList t = dendropy.TreeList.get_from_path(filename, tree_type) if not t is None: for i in t: dpy_trees[i.label] = i dpy_tree_list.append(i) trees = {} tree_list = [] """ At this point, dpy_trees is full, now we need to convert these dendropy trees to the detangle trees which are optimized for rotations. Luckily, dendropy trees have a utility function that provides us with exactly the simple Newick string detangle.tree likes.""" for i in dpy_tree_list: tr = tree("tree " + i.label + " = [&U] " + i.as_newick_string()) trees[tr.name] = tr tree_list.append(tr) process_trees(tree_list, output_filename=args.output_filename)
ct.move_to(x_pos + (branch_length * (max_depth-depth)), y_pos + upper) ct.line_to(x_pos + (branch_length * (max_depth-depth)), y_pos + lower) ct.stroke() if current == tr.root: return (max_depth+1) * branch_length else: return (top, upper + ((lower - upper)/2.00)) if __name__=='__main__': """ Loop over all files, reading in all available trees. """ for line in fileinput.input(): #line = line.trim() if line[0:4] == 'tree': tr = tree(line) if first_tree == None: first_tree = tr.name trees[tr.name] = tr tree_list.append(tr) twists[tr.name] = tr.get_twists() #print trees combos = list(itertools.combinations(tree_list,2)) f = open('output.svg', 'w') surf = cairo.SVGSurface(f, len(combos) * (120 + line_gap+ line_region_width) * 2, len(tr.leaves()) * 40) ct = cairo.Context(surf) ct.translate(10,16) ct.set_source_rgb(0.0, 0.0, 0.0) ct.select_font_face("Georgia", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
print args bio_trees = {} bio_tree_list = [] for filename in args.infiles: """ Work out what format the file is, then parse the trees out """ f = open(filename, 'r') first = f.readline() if first.find('#nexus') > -1 or first.find('#NEXUS') > -1: tree_type='nexus' elif first.find('<') > -1: tree_type='phyloxml' else: tree_type='newick' f.close() temp = list(Phylo.parse(filename,tree_type)) if not temp is None: for i in temp: bio_trees[i.name] = i bio_tree_list.append(i) trees = {} tree_list = [ ] """ At this point, bio_trees is full, now we need to convert these Phylo trees to the detangle trees which are optimized for rotations. """ for i in bio_tree_list: tr = tree() tr.init_from_phylo(i) trees[tr.name]=tr tree_list.append(tr) process_trees(tree_list, output_filename = args.output_filename)