def get_response_content(fs): # Read the newick trees. true_tree = Newick.parse(fs.true_tree, Newick.NewickTree) test_tree = Newick.parse(fs.test_tree, Newick.NewickTree) flags = get_flags(fs) # Get a list of maps from node id to harmonic extension. true_tree_leaf_ids = set(id(x) for x in true_tree.gen_tips()) nleaves = len(true_tree_leaf_ids) id_to_full_val_list = [ Harmonic.get_harmonic_valuations(true_tree, i) for i in range(1, nleaves) ] id_map = get_true_leaf_id_to_test_leaf_id(true_tree, test_tree) test_id_to_adj = get_id_to_adj(test_tree) test_id_to_name = get_id_to_name(test_tree) # Get the list of id to val maps with respect to leaf ids of the test tree. test_tree_internal_ids = set(id(x) for x in test_tree.gen_internal_nodes()) test_tree_leaf_ids = set(id(x) for x in test_tree.gen_tips()) id_to_val_list = [] for id_to_full_val in id_to_full_val_list: d = {} for x in true_tree_leaf_ids: value = id_to_full_val[x] if abs(value) < 1e-8: raise ValueError('the true tree is too symmetric') elif value < 0: s = -1 else: s = 1 d[id_map[x]] = s for x in test_tree_internal_ids: d[x] = None id_to_val_list.append(d) # Attempt to find a sign assignment. id_to_vals = SeekEigenLacing.rec_eigen(test_id_to_adj, id_to_val_list, flags) # Reorder the leaf and the internal node ids according to name order. leaf_pair = sorted((test_id_to_name[x], x) for x in test_tree_leaf_ids) internal_pair = sorted( (test_id_to_name[x], x) for x in test_tree_internal_ids) reordered_leaf_ids = zip(*leaf_pair)[1] reordered_internal_ids = zip(*internal_pair)[1] # Check for a failure to find a certificate. if not id_to_vals: return 'no non-rejection certificate was found' # Start writing the response. out = StringIO() print >> out, 'leaf sign valuations:' for x in reordered_leaf_ids: print >> out, test_id_to_name[x], get_sign_string(id_to_vals[x]) print >> out print >> out, 'vertex sign compatible internal vertex valuations:' for x in reordered_internal_ids: print >> out, test_id_to_name[x], get_sign_string(id_to_vals[x]) return out.getvalue()
def get_response_content(fs): # Read the newick trees. true_tree = Newick.parse(fs.true_tree, Newick.NewickTree) test_tree = Newick.parse(fs.test_tree, Newick.NewickTree) flags = get_flags(fs) # Get a list of maps from node id to harmonic extension. true_tree_leaf_ids = set(id(x) for x in true_tree.gen_tips()) nleaves = len(true_tree_leaf_ids) id_to_full_val_list = [Harmonic.get_harmonic_valuations( true_tree, i) for i in range(1, nleaves)] id_map = get_true_leaf_id_to_test_leaf_id(true_tree, test_tree) test_id_to_adj = get_id_to_adj(test_tree) test_id_to_name = get_id_to_name(test_tree) # Get the list of id to val maps with respect to leaf ids of the test tree. test_tree_internal_ids = set(id(x) for x in test_tree.gen_internal_nodes()) test_tree_leaf_ids = set(id(x) for x in test_tree.gen_tips()) id_to_val_list = [] for id_to_full_val in id_to_full_val_list: d = {} for x in true_tree_leaf_ids: value = id_to_full_val[x] if abs(value) < 1e-8: raise ValueError('the true tree is too symmetric') elif value < 0: s = -1 else: s = 1 d[id_map[x]] = s for x in test_tree_internal_ids: d[x] = None id_to_val_list.append(d) # Attempt to find a sign assignment. id_to_vals = SeekEigenLacing.rec_eigen( test_id_to_adj, id_to_val_list, flags) # Reorder the leaf and the internal node ids according to name order. leaf_pair = sorted( (test_id_to_name[x], x) for x in test_tree_leaf_ids) internal_pair = sorted( (test_id_to_name[x], x) for x in test_tree_internal_ids) reordered_leaf_ids = zip(*leaf_pair)[1] reordered_internal_ids = zip(*internal_pair)[1] # Check for a failure to find a certificate. if not id_to_vals: return 'no non-rejection certificate was found' # Start writing the response. out = StringIO() print >> out, 'leaf sign valuations:' for x in reordered_leaf_ids: print >> out, test_id_to_name[x], get_sign_string(id_to_vals[x]) print >> out print >> out, 'vertex sign compatible internal vertex valuations:' for x in reordered_internal_ids: print >> out, test_id_to_name[x], get_sign_string(id_to_vals[x]) return out.getvalue()
def get_form(): """ @return: the body of a form """ # define the tree string tree_string = Newick.daylight_example_tree tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.IntegerInterval('first_index', 'last_index', 'eigenfunction index range (1 means Fiedler)', 0, 12, low=0), Form.CheckGroup('check_options', 'output options', [ Form.CheckItem('reflect_trees', 'reflect trees', True), Form.CheckItem('draw_background', 'draw background', True), Form.CheckItem('draw_labels', 'draw labels', True) ]), Form.Integer('width', 'physical width', 880, low=10, high=1000), Form.Integer('height', 'physical height', 680, low=10, high=1000), Form.Integer('inner_margin', 'inner margin', 10, low=0, high=1000), Form.Integer('outer_margin', 'outer margin', 0, low=0, high=1000), Form.ImageFormat() ] return form_objects
def get_form(): """ @return: the body of a form """ # define the tree string tree_string = '(((Human:0.1, Chimpanzee:0.2):0.8, Gorilla:0.3):0.7, Orangutan:0.4, Gibbon:0.5);' tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.Integer('ncols', 'sample this many columns', 100, low=1, high=1000), Form.Matrix('matrix_a', 'first nucleotide rate matrix', g_nt_matrix_a), Form.Float('weight_a', 'first mixture weight', g_weight_a, low_inclusive=0), Form.Matrix('matrix_b', 'second nucleotide rate matrix', g_nt_matrix_b), Form.Float('weight_b', 'second mixture weight', g_weight_b, low_inclusive=0), Form.Matrix('matrix_c', 'third nucleotide rate matrix', g_nt_matrix_c), Form.Float('weight_c', 'third mixture weight', g_weight_c, low_inclusive=0) ] return form_objects
def get_response_content(fs): tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # Draw the tree using a simple indented format # if the user chose the first option. # Otherwise create a drawing object and draw fancier ascii art. if fs.choice1: return str(tree) + '\n' else: # customize the tree drawing object according to the user choice drawer = DrawTree.DrawTree() if fs.choice2: drawer.use_branch_lengths = True drawer.force_ultrametric = False drawer.vertical_spacing = 1 drawer.horizontal_spacing = 1 elif fs.choice3: drawer.use_branch_lengths = False drawer.force_ultrametric = False drawer.vertical_spacing = 1 drawer.horizontal_spacing = 1 elif fs.choice4: drawer.use_branch_lengths = False drawer.force_ultrametric = True drawer.vertical_spacing = 3 drawer.horizontal_spacing = 6 # draw the tree return drawer.draw(tree) + '\n'
def get_response_content(fs): # get a properly formatted newick tree with branch lengths tree = Newick.parse(fs.tree, SpatialTree.SpatialTree) tree.assert_valid() if tree.has_negative_branch_lengths(): msg = 'drawing a tree with negative branch lengths is not implemented' raise HandlingError(msg) tree.add_branch_lengths() # do the layout if fs.daylight: try: layout = FastDaylightLayout.StraightBranchLayout() layout.do_layout(tree) except RuntimeError as e: pass elif fs.curved: try: layout = FastDaylightLayout.CurvedBranchLayout() layout.set_min_segment_count(400) layout.do_layout(tree) except RuntimeError as e: pass elif fs.arc: EqualArcLayout.do_layout(tree) # draw the image try: ext = Form.g_imageformat_to_ext[fs.imageformat] return DrawTreeImage.get_tree_image(tree, (640, 480), ext) except CairoUtil.CairoUtilError as e: raise HandlingError(e)
def main(): import EqualArcLayout import Newick import SpatialTree # make a spatial tree tree = Newick.parse(Newick.daylight_example_tree, SpatialTree.SpatialTree) EqualArcLayout.do_layout(tree) max_size = (640, 480) image_formats = ('png', 'pdf', 'ps', 'svg') for image_format in image_formats: # get the image string image_string = get_tree_image(tree, max_size, image_format) # write the image file image_filename = 'test.%s' % image_format with open(image_filename, 'wb') as fout: fout.write(image_string) print 'created', image_filename # write an html file html_filename = 'test.html' with open(html_filename, 'w') as fout: print >> fout, '<html><body>' for image_format in image_formats: image_filename = 'test.%s' % image_format print >> fout, '<a href="%s">%s</a><br/>' % (image_filename, image_filename) print >> fout, '</body></html>' print 'created', html_filename
def main(): # create the alignment object print 'creating the alignment...' alignment_string = Fasta.brown_example_alignment.strip() alignment = Fasta.Alignment(StringIO(alignment_string)) # create a tree object print 'creating the tree...' tree_string = Newick.brown_example_tree tree = Newick.parse(tree_string, Newick.NewickTree) # create a rate matrix object print 'creating the rate matrix object...' distribution = {'A': .25, 'C': .25, 'G': .25, 'T': .25} kappa = 2.0 row_major_rate_matrix = RateMatrix.get_unscaled_hky85_rate_matrix( distribution, kappa).get_row_major_rate_matrix() rate_matrix = RateMatrix.FastRateMatrix(row_major_rate_matrix, list('ACGT')) rate_matrix.normalize() # get the mle_rates print 'getting the mle rates...' mle_rates = get_mle_rates(tree, alignment, rate_matrix) print 'mle rates:' print mle_rates print 'stockholm string:' print get_stockholm_string(tree, alignment, mle_rates)
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() tree.add_branch_lengths() if tree.has_negative_branch_lengths(): msg_a = 'calculating weights for a tree ' msg_b = 'with negative branch lengths is not implemented' raise HandlingError(msg_a + msg_b) # get the selected names selection = Util.get_stripped_lines(fs.selection.splitlines()) selected_name_set = set(selection) possible_name_set = set(node.get_name() for node in tree.gen_tips()) extra_names = selected_name_set - possible_name_set if extra_names: msg_a = 'the following selected names are not valid tips: ' msg_b = str(tuple(extra_names)) raise HandlingError(msg_a + msg_b) # prune the tree for name in set(node.name for node in tree.gen_tips()) - set(selection): try: node = tree.get_unique_node(name) except NewickSearchError as e: raise HandlingError(e) tree.prune(node) # get the weights if fs.stone: name_weight_pairs = LeafWeights.get_stone_weights(tree) elif fs.thompson: name_weight_pairs = LeafWeights.get_thompson_weights(tree) # report the weights lines = ['%s: %f' % pair for pair in name_weight_pairs] return '\n'.join(lines) + '\n'
def get_form(): """ @return: the body of a form """ # define the tree string tree_string = Newick.daylight_example_tree tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.IntegerInterval('first_index', 'last_index', 'eigenfunction index range (1 means Fiedler)', 0, 4, low=0), Form.CheckGroup('check_options', 'output options', [ Form.CheckItem('draw_background', 'draw background', True), Form.CheckItem('draw_vertices', 'draw vertices'), Form.CheckItem('draw_labels', 'draw labels', True) ]), Form.ImageFormat() ] return form_objects
def get_response_content(fs): # get a properly formatted newick tree with branch lengths tree_string = '((1:1, 2:1)6:1, 3:1, (4:1, 5:1)7:1)8;' tree = Newick.parse(tree_string, SpatialTree.SpatialTree) # get the fiedler-like vertex valuation fiedler = [1, 1, -1, -1, -1, 1, -1, -1] # create a node id map ids = [None] * 8 for node in tree.preorder(): index = int(node.name) - 1 ids[index] = id(node) # convert fiedler into a dictionary v1 = dict((ids[i], float(fiedler[i])) for i in range(8)) # convert the annotations into dictionaries v2s = [dict((ids[i], float(v[i])) for i in range(8)) for v in g_annotation] # do the layout try: layout = FastDaylightLayout.StraightBranchLayout() layout.do_layout(tree) except RuntimeError as e: pass # draw the image try: ext = Form.g_imageformat_to_ext[fs.imageformat] return DrawEigenLacing.get_eg2_image(tree, (640, 480), ext, v1, v2s, fs.draw_background, fs.draw_vertices, fs.draw_labels) except CairoUtil.CairoUtilError as e: raise HandlingError(e)
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the minimum number of segments min_segment_count = fs.segments # determine the maximum allowed branch length total_branch_length = tree.get_total_length() max_branch_length = total_branch_length / float(min_segment_count) # any branch longer than the max branch length will be broken in half while True: old_nodes = list(tree.preorder()) for node in old_nodes: if node is tree.root: if node.blen is not None: msg = 'the root node should not have a branch length' raise HandlingError(msg) elif node.blen is None: msg = 'each non-root node should have a branch length' raise HandlingError(msg) elif node.blen > max_branch_length: # create a new node and set its attributes new = Newick.NewickNode() new.name = node.name # insert the new node tree.insert_node(new, node.parent, node, .5) # if no node was added then break out of the loop if len(old_nodes) == len(list(tree.preorder())): break # return the response return tree.get_newick_string() + '\n'
def get_response_content(fs): # get a properly formatted newick tree with branch lengths tree = Newick.parse(fs.tree, SpatialTree.SpatialTree) tree.assert_valid() if tree.has_negative_branch_lengths(): msg = 'drawing a tree with negative branch lengths is not implemented' raise HandlingError(msg) tree.add_branch_lengths() # do the layout if fs.daylight: try: layout = FastDaylightLayout.StraightBranchLayout() layout.do_layout(tree) except RuntimeError as e: pass elif fs.curved: try: layout = FastDaylightLayout.CurvedBranchLayout() layout.set_min_segment_count(400) layout.do_layout(tree) except RuntimeError as e: pass elif fs.arc: EqualArcLayout.do_layout(tree) # draw the image try: ext = Form.g_imageformat_to_ext[fs.imageformat] return DrawTreeImage.get_tree_image(tree, (640, 480), ext) except CairoUtil.CairoUtilError as e: raise HandlingError(e)
def get_form(): """ @return: the body of a form """ # define the default newick tree tree_string = '((((a:.05, b:.05)L1:.15, c:.2)L2:.8, x:1)L3:.5, (((m:.05, n:.05)R1:.15, p:.2)R2:.8, y:1)R3:.5)root;' tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the default rate matrix and its ordered states R = np.array([[-.3, .1, .1, .1], [.1, -.3, .1, .1], [.1, .1, -.3, .1], [.1, .1, .1, -.3]]) ordered_states = list('ACGT') # define the default leaf state assigments leaf_assignment_lines = [ 'a : A', 'b : A', 'c : A', 'x : A', 'm : C', 'n : C', 'p : C', 'y : C' ] # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree with branch lengths', formatted_tree_string), Form.Matrix('rate_matrix', 'rate matrix', R, MatrixUtil.assert_rate_matrix), Form.MultiLine('states', 'ordered states', '\n'.join(ordered_states)), Form.MultiLine('assignments', 'leaf states', '\n'.join(leaf_assignment_lines)) ] return form_objects
def get_test_image_format_and_string(): import DrawTreeImage import Newick import SpatialTree # make a spatial tree tree_string = """ ((((((((((((A:2)A:2, (B:2)B:2):3):3, (C:2.5)C:2.5):4):4, (D:3)D:3):1.5):1.5, (E:10.5)E:10.5):5):5, (((((F:2)F:2, (G:6)G:6):7):7, (H:4)H:4):6.5):6.5):6.5):6.5, (((((I:2.5)I:2.5, (J:1)J:1):15):15, (((K:5.5)K:5.5, (L:5.5)L:5.5):1):1):8.5):8.5, (M:28)M:28);""" tree = Newick.parse(tree_string, SpatialTree.SpatialTree) try: layout = EqualDaylightLayout() layout.force_straight_branches = False layout.do_layout(tree) except EqualDaylightLayoutError as e: print e # color some of the branches red = 'ff0000' green = '00ff00' blue = '0000ff' for child, color in zip(tree.root.children, (red, green, blue)): for node in child.preorder(): node.branch_color = color # get the image string image_format = 'png' image_string = DrawTreeImage.get_tree_image(tree, (640, 480), image_format) return (image_format, image_string)
def get_sample_tree(): """ @return: a mixture model that is used to generate the default nexus data """ tree_string = '(((Human:0.1, Chimpanzee:0.2):0.8, Gorilla:0.3):0.7, Orangutan:0.4, Gibbon:0.5);' tree = Newick.parse(tree_string, Newick.NewickTree) return tree
def get_sample_tree(): """ @return: a mixture model that is used to generate the default nexus data """ tree_string = '(((Human:0.1, Chimpanzee:0.2):0.8, Gorilla:0.3):0.7, Orangutan:0.4, Gibbon:0.5);' tree = Newick.parse(tree_string, Newick.NewickTree) return tree
def get_form(): """ @return: the body of a form """ # define the tree string tree_string = '(((Human:0.1, Chimpanzee:0.2):0.8, Gorilla:0.3):0.7, Orangutan:0.4, Gibbon:0.5);' tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.MultiLine('model', 'DNA frequency mixture model', get_sample_xml_string().strip()), Form.Integer('ncols', 'sample this many nucleotide columns', 200, low=1, high=1000), Form.Integer('seed', 'random number seed', 314159, low=0), Form.RadioGroup('format', 'output format', [ Form.RadioItem('fastaformat', 'fasta alignment'), Form.RadioItem('nexusformat', 'nexus alignment', True) ]) ] return form_objects
def get_form(): """ @return: the body of a form """ # define the default tree string tree_string = Newick.brown_example_tree tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the default alignment string default_alignment_string = Fasta.brown_example_alignment.strip() # define the default nucleotide distribution weights default_nt_lines = ['A : 1', 'C : 1', 'G : 1', 'T : 1'] # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.MultiLine('alignment', 'nucleotide alignment', default_alignment_string), Form.MultiLine('weights', 'nucleotide weights', '\n'.join(default_nt_lines)), Form.Float('kappa', 'transition transversion rate ratio', 2, low_inclusive=0) ] return form_objects
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the set of names selection = Util.get_stripped_lines(fs.names.splitlines()) selected_name_set = set(selection) possible_name_set = set(node.get_name() for node in tree.gen_tips()) extra_names = selected_name_set - possible_name_set if extra_names: msg_a = 'the following selected names ' msg_b = 'are not valid tips: ' + ', '.join(extra_names) raise HandlingError(msg_a + msg_b) # get the list of tip nodes to remove if fs.remove: nodes_to_remove = [ node for node in tree.gen_tips() if node.name in selection ] elif fs.keep: nodes_to_remove = [ node for node in tree.gen_tips() if node.name not in selection ] # prune the tree for node in nodes_to_remove: tree.prune(node) # merge segmented branches internal_nodes_to_remove = [ node for node in tree.preorder() if node.get_child_count() == 1 ] for node in internal_nodes_to_remove: tree.remove_node(node) # return the response return tree.get_newick_string() + '\n'
def get_form(): """ @return: the body of a form """ # define the tree string tree_string = Newick.daylight_example_tree tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.IntegerInterval( 'first_index', 'last_index', 'eigenfunction index range (1 means Fiedler)', 0, 12, low=0), Form.CheckGroup('check_options', 'output options', [ Form.CheckItem('reflect_trees', 'reflect trees', True), Form.CheckItem('draw_background', 'draw background', True), Form.CheckItem('draw_labels', 'draw labels', True)]), Form.Integer('width', 'physical width', 880, low=10, high=1000), Form.Integer('height', 'physical height', 680, low=10, high=1000), Form.Integer('inner_margin', 'inner margin', 10, low=0, high=1000), Form.Integer('outer_margin', 'outer margin', 0, low=0, high=1000), Form.ImageFormat()] return form_objects
def get_test_image_format_and_string(): import DrawTreeImage import Newick import SpatialTree # make a spatial tree tree_string = """ ((((((((((((A:2)A:2, (B:2)B:2):3):3, (C:2.5)C:2.5):4):4, (D:3)D:3):1.5):1.5, (E:10.5)E:10.5):5):5, (((((F:2)F:2, (G:6)G:6):7):7, (H:4)H:4):6.5):6.5):6.5):6.5, (((((I:2.5)I:2.5, (J:1)J:1):15):15, (((K:5.5)K:5.5, (L:5.5)L:5.5):1):1):8.5):8.5, (M:28)M:28);""" tree = Newick.parse(tree_string, SpatialTree.SpatialTree) try: layout = EqualDaylightLayout() layout.force_straight_branches = False layout.do_layout(tree) except EqualDaylightLayoutError as e: print e # color some of the branches red = 'ff0000' green = '00ff00' blue = '0000ff' for child, color in zip(tree.root.children, (red, green, blue)): for node in child.preorder(): node.branch_color = color # get the image string image_format = 'png' image_string = DrawTreeImage.get_tree_image(tree, (640, 480), image_format) return (image_format, image_string)
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the mixture weights weights = [fs.weight_a, fs.weight_b, fs.weight_c] # get the matrices matrices = [fs.matrix_a, fs.matrix_b, fs.matrix_c] for R in matrices: if R.shape != (4,4): msg = 'expected each nucleotide rate matrix to be 4x4' raise HandlingError(msg) # get the nucleotide alignment try: alignment = Fasta.Alignment(fs.alignment.splitlines()) alignment.force_nucleotide() except Fasta.AlignmentError as e: raise HandlingError(e) # create the mixture proportions weight_sum = sum(weights) mixture_proportions = [weight / weight_sum for weight in weights] # create the rate matrix objects ordered_states = list('ACGT') rate_matrix_objects = [] for R in matrices: rate_matrix_object = RateMatrix.RateMatrix(R.tolist(), ordered_states) rate_matrix_objects.append(rate_matrix_object) # create the mixture model mixture_model = SubModel.MixtureModel(mixture_proportions, rate_matrix_objects) # normalize the mixture model mixture_model.normalize() # return the html string return do_analysis(mixture_model, alignment, tree) + '\n'
def get_response_content(fs): """ @param fs: a FieldStorage object containing the cgi arguments @return: a (response_headers, response_text) pair """ # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # find nodes with a branch length of zero zero_blen_nodes = [node for node in tree.preorder() if node.blen == 0.0] # deal with these nodes appropriately for node in zero_blen_nodes: if node.blen != 0.0: # it is possible that this node no longer has branch length zero continue elif node.has_children(): # remove internal nodes with zero branch length tree.remove_node(node) else: # prune terminal nodes with zero branch length intersection = tree.prune(node) # remove intersection nodes that have a single child if intersection.get_child_count() == 1: tree.remove_node(intersection) # return the response return tree.get_newick_string() + '\n'
def get_response_content(fs): tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # Draw the tree using a simple indented format # if the user chose the first option. # Otherwise create a drawing object and draw fancier ascii art. if fs.choice1: return str(tree) + '\n' else: # customize the tree drawing object according to the user choice drawer = DrawTree.DrawTree() if fs.choice2: drawer.use_branch_lengths = True drawer.force_ultrametric = False drawer.vertical_spacing = 1 drawer.horizontal_spacing = 1 elif fs.choice3: drawer.use_branch_lengths = False drawer.force_ultrametric = False drawer.vertical_spacing = 1 drawer.horizontal_spacing = 1 elif fs.choice4: drawer.use_branch_lengths = False drawer.force_ultrametric = True drawer.vertical_spacing = 3 drawer.horizontal_spacing = 6 # draw the tree return drawer.draw(tree) + '\n'
def get_response_content(fs): """ @param fs: a FieldStorage object containing the cgi arguments @return: a (response_headers, response_text) pair """ # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # find nodes with a branch length of zero zero_blen_nodes = [node for node in tree.preorder() if node.blen == 0.0] # deal with these nodes appropriately for node in zero_blen_nodes: if node.blen != 0.0: # it is possible that this node no longer has branch length zero continue elif node.has_children(): # remove internal nodes with zero branch length tree.remove_node(node) else: # prune terminal nodes with zero branch length intersection = tree.prune(node) # remove intersection nodes that have a single child if intersection.get_child_count() == 1: tree.remove_node(intersection) # return the response return tree.get_newick_string() + '\n'
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the sequence order if it exists ordered_names = Util.get_stripped_lines(fs.order.splitlines()) if ordered_names: observed_name_set = set(ordered_names) expected_name_set = set(node.get_name() for node in tree.gen_tips()) extra_names = observed_name_set - expected_name_set missing_names = expected_name_set - observed_name_set if extra_names: msg_a = 'the list of ordered names includes these names ' msg_b = 'not found in the tree: %s' % str(tuple(extra_names)) raise HandlingError(msg_a + msg_b) if missing_names: msg_a = 'the tree includes these names not found in the list ' msg_b = 'of ordered names: %s' % str(tuple(missing_names)) raise HandlingError(msg_a + msg_b) else: ordered_names = list(tip.get_name() for name in tree.gen_tips()) # do the sampling sampled_sequences = JC69.sample_sequences(tree, ordered_names, fs.length) alignment = Fasta.create_alignment(ordered_names, sampled_sequences) # return the response return alignment.to_fasta_string() + '\n'
def get_form(): """ @return: the body of a form """ # define the tree string tree_string = Newick.daylight_example_tree tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.IntegerInterval( 'first_index', 'last_index', 'eigenfunction index range (1 means Fiedler)', 0, 12, low=0), Form.CheckGroup('check_options', 'output options', [ Form.CheckItem('reflect_trees', 'reflect trees across the vertical axis'), Form.CheckItem('show_subfigure_labels', 'show subfigure labels', True), Form.CheckItem('show_vertex_labels', 'show vertex labels')]), Form.Float('width', 'width (centimeters)', 12, low_inclusive=1, high_exclusive=1000), Form.Float('height', 'height (centimeters)', 8, low_inclusive=1, high_exclusive=1000), Form.Float('inner_margin', 'inner margin (centimeters)', 0.25, low_inclusive=0, high_exclusive=1000), Form.RadioGroup('radio_options', 'tree layout options', [ Form.RadioItem('equal_daylight_layout', 'equal daylight layout', True), Form.RadioItem('equal_arc_layout', 'equal arc layout')]), Form.TikzFormat()] return form_objects
def __call__(self, X_logs): """ The vth entry of X corresponds to the log rate of the branch above v. Return the quantity to be minimized (the neg log likelihood). @param X: vector of branch rate logs @return: negative log likelihood """ X = [math.exp(x) for x in X_logs] B_subs = {} for v_parent, v_child in self.R: edge = frozenset([v_parent, v_child]) r = X[v_child] t = self.B[edge] B_subs[edge] = r * t newick_string = FtreeIO.RBN_to_newick(self.R, B_subs, self.N_leaves) tree = Newick.parse(newick_string, Newick.NewickTree) # define the rate matrix object; horrible dictionary_rate_matrix = RateMatrix.get_jukes_cantor_rate_matrix() ordered_states = list('ACGT') row_major_rate_matrix = MatrixUtil.dict_to_row_major( dictionary_rate_matrix, ordered_states, ordered_states) rate_matrix_object = RateMatrix.RateMatrix( row_major_rate_matrix, ordered_states) # get the log likelihood ll = PhyLikelihood.get_log_likelihood( tree, self.alignment, rate_matrix_object) return -ll
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the alignment try: alignment = Fasta.Alignment(fs.fasta.splitlines()) alignment.force_nucleotide() except Fasta.AlignmentError as e: raise HandlingError(e) # define the jukes cantor rate matrix dictionary_rate_matrix = RateMatrix.get_jukes_cantor_rate_matrix() ordered_states = list('ACGT') row_major_rate_matrix = MatrixUtil.dict_to_row_major( dictionary_rate_matrix, ordered_states, ordered_states) rate_matrix_object = RateMatrix.RateMatrix(row_major_rate_matrix, ordered_states) # simulate the ancestral alignment try: alignment = PhyLikelihood.simulate_ancestral_alignment( tree, alignment, rate_matrix_object) except PhyLikelihood.SimulationError as e: raise HandlingError(e) # get the alignment string using an ordering defined by the tree arr = [] for node in tree.preorder(): arr.append(alignment.get_fasta_sequence(node.name)) # return the response return '\n'.join(arr) + '\n'
def get_form(): """ @return: the body of a form """ # define the tree string tree_string = '(((Human:0.1, Chimpanzee:0.2):0.8, Gorilla:0.3):0.7, Orangutan:0.4, Gibbon:0.5);' tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.MultiLine('alignment', 'nucleotide alignment', g_sample_alignment_string.strip()), Form.Matrix('matrix_a', 'first nucleotide rate matrix', g_nt_matrix_a), Form.Float('weight_a', 'first mixture weight', 1, low_inclusive=0), Form.Matrix('matrix_b', 'second nucleotide rate matrix', g_nt_matrix_b), Form.Float('weight_b', 'second mixture weight', 2, low_inclusive=0), Form.Matrix('matrix_c', 'third nucleotide rate matrix', g_nt_matrix_c), Form.Float('weight_c', 'third mixture weight', 3, low_inclusive=0)] return form_objects
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the alignment try: alignment = Fasta.Alignment(fs.fasta.splitlines()) alignment.force_nucleotide() except Fasta.AlignmentError as e: raise HandlingError(e) # define the jukes cantor rate matrix dictionary_rate_matrix = RateMatrix.get_jukes_cantor_rate_matrix() ordered_states = list('ACGT') row_major_rate_matrix = MatrixUtil.dict_to_row_major( dictionary_rate_matrix, ordered_states, ordered_states) rate_matrix_object = RateMatrix.RateMatrix( row_major_rate_matrix, ordered_states) # simulate the ancestral alignment try: alignment = PhyLikelihood.simulate_ancestral_alignment( tree, alignment, rate_matrix_object) except PhyLikelihood.SimulationError as e: raise HandlingError(e) # get the alignment string using an ordering defined by the tree arr = [] for node in tree.preorder(): arr.append(alignment.get_fasta_sequence(node.name)) # return the response return '\n'.join(arr) + '\n'
def get_tikz_lines(newick, eigenvector_index, yaw, pitch): """ @param eigenvector_index: 1 is Fiedler """ tree = Newick.parse(newick, SpatialTree.SpatialTree) # change the node names and get the new tree string for node in tree.preorder(): node.name = 'n' + str(id(node)) newick = NewickIO.get_newick_string(tree) # do the layout layout = FastDaylightLayout.StraightBranchLayout() layout.do_layout(tree) tree.fit((g_xy_scale, g_xy_scale)) name_to_location = dict(( x.name, tree._layout_to_display(x.location)) for x in tree.preorder()) T, B, N = FtreeIO.newick_to_TBN(newick) # get some vertices leaves = Ftree.T_to_leaves(T) internal = Ftree.T_to_internal_vertices(T) vertices = leaves + internal # get the locations v_to_location = dict((v, name_to_location[N[v]]) for v in vertices) # get the valuations w, V = Ftree.TB_to_harmonic_extension(T, B, leaves, internal) index_to_val = V[:, eigenvector_index-1] v_to_val = dict( (vertices[i], g_z_scale*val) for i, val in enumerate(index_to_val)) # get the coordinates v_to_xyz = get_v_to_xyz(yaw, v_to_location, v_to_val) # add intersection vertices add_intersection_vertices(T, B, v_to_xyz) intersection_vertices = sorted(v for v in v_to_xyz if v not in vertices) # get lines of the tikz file return xyz_to_tikz_lines(T, B, pitch, v_to_xyz, leaves, internal, intersection_vertices)
def get_response_content(fs): flags = get_flags(fs) nseconds = 5 tm = time.time() rejected_s = None nerrors = 0 nchecked = 0 while time.time() < tm + nseconds and not rejected_s: nchecked += 1 # Sample a Newick tree. true_f = TreeSampler.sample_tree(fs.nleaves, 0, 1.0) true_s = NewickIO.get_newick_string(true_f) true_tree = Newick.parse(true_s, Newick.NewickTree) # Get the leaf and internal vertex ids for the true tree. internal_ids = set(id(x) for x in true_tree.gen_internal_nodes()) leaf_ids = set(id(x) for x in true_tree.gen_tips()) nleaves = len(leaf_ids) # Get the harmonic valuations for all vertices of the tree. id_to_full_val_list = [ Harmonic.get_harmonic_valuations(true_tree, i) for i in range(1, nleaves) ] # Check for small valuations at the leaves. try: for id_to_full_val in id_to_full_val_list: for x in leaf_ids: value = id_to_full_val[x] if abs(value) < 1e-8: raise CheckTreeError('the true tree is too symmetric') except CheckTreeError as e: nerrors += 1 continue # Assign the leaf values and assign None to internal values. id_to_val_list = [] for id_to_full_val in id_to_full_val_list: d = {} for x in leaf_ids: s = -1 if id_to_full_val[x] < 0 else 1 d[x] = s for x in internal_ids: d[x] = None id_to_val_list.append(d) # Define the topology in a different format. id_to_adj = get_id_to_adj(true_tree) # Check the tree for self-compatibility under the given conditions. id_to_vals = SeekEigenLacing.rec_eigen(id_to_adj, id_to_val_list, flags) if not id_to_vals: rejected_s = true_s # make the report out = StringIO() if rejected_s: print >> out, 'rejected a true tree:' print >> out, rejected_s else: print >> out, 'no true tree was rejected' print >> out print >> out, nchecked, 'trees were sampled total' print >> out, nerrors, 'trees were too symmetric' return out.getvalue()
def get_tikz_lines(newick, eigenvector_index, yaw, pitch): """ @param eigenvector_index: 1 is Fiedler """ tree = Newick.parse(newick, SpatialTree.SpatialTree) # change the node names and get the new tree string for node in tree.preorder(): node.name = 'n' + str(id(node)) newick = NewickIO.get_newick_string(tree) # do the layout layout = FastDaylightLayout.StraightBranchLayout() layout.do_layout(tree) tree.fit((g_xy_scale, g_xy_scale)) name_to_location = dict( (x.name, tree._layout_to_display(x.location)) for x in tree.preorder()) T, B, N = FtreeIO.newick_to_TBN(newick) # get some vertices leaves = Ftree.T_to_leaves(T) internal = Ftree.T_to_internal_vertices(T) vertices = leaves + internal # get the locations v_to_location = dict((v, name_to_location[N[v]]) for v in vertices) # get the valuations w, V = Ftree.TB_to_harmonic_extension(T, B, leaves, internal) index_to_val = V[:, eigenvector_index - 1] v_to_val = dict( (vertices[i], g_z_scale * val) for i, val in enumerate(index_to_val)) # get the coordinates v_to_xyz = get_v_to_xyz(yaw, v_to_location, v_to_val) # add intersection vertices add_intersection_vertices(T, B, v_to_xyz) intersection_vertices = sorted(v for v in v_to_xyz if v not in vertices) # get lines of the tikz file return xyz_to_tikz_lines(T, B, pitch, v_to_xyz, leaves, internal, intersection_vertices)
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the minimum number of segments min_segment_count = fs.segments # determine the maximum allowed branch length total_branch_length = tree.get_total_length() max_branch_length = total_branch_length / float(min_segment_count) # any branch longer than the max branch length will be broken in half while True: old_nodes = list(tree.preorder()) for node in old_nodes: if node is tree.root: if node.blen is not None: msg = 'the root node should not have a branch length' raise HandlingError(msg) elif node.blen is None: msg = 'each non-root node should have a branch length' raise HandlingError(msg) elif node.blen > max_branch_length: # create a new node and set its attributes new = Newick.NewickNode() new.name = node.name # insert the new node tree.insert_node(new, node.parent, node, .5) # if no node was added then break out of the loop if len(old_nodes) == len(list(tree.preorder())): break # return the response return tree.get_newick_string() + '\n'
def process_line(self, line): """ Possibly add an attribute. The attribute can be a floating point value or a newick tree. """ self.processed_lines.append(line) arr = [x.strip() for x in line.split('=')] if len(arr) > 1: var, value = arr[0], arr[-1] var_arr = var.split() # read the log likelihood if var == 'Log Likelihood' and value.endswith(';'): try: fvalue = float(value[:-1]) setattr(self, 'lnL', fvalue) return except ValueError: pass # read a tree assignment if len(var_arr) == 2 and var_arr[0] == 'Tree': treename = var_arr[1] tree_string = value tree = Newick.parse(tree_string, Newick.NewickTree) setattr(self, treename, tree) return # read a standard variable assignment if len(var_arr) == 1: try: fvalue = float(value) setattr(self, var, fvalue) return except ValueError: pass
def get_form(): """ @return: a list of form objects """ # reformat the tree string tree = Newick.parse(g_tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 75) # define the form objects form_objects = [ Form.MultiLine('tree_string', 'newick tree', formatted_tree_string), Form.Float('scaling_factor', 'figure scaling factor', 3.0, low_exclusive=0), Form.RadioGroup('label_mode', 'label mode', [ Form.RadioItem('label_mode_suppressed', 'suppress all vertex labels', True), Form.RadioItem('label_mode_leaf_only', 'draw leaf names'), Form.RadioItem('label_mode_show', 'draw arbitrary short names at all vertices')]), Form.RadioGroup('sanitization_options', 'taxon label sanitization options', [ Form.RadioItem('no_sanitization', 'allow TikZ to interpret the taxon labels', True), Form.RadioItem('sanitization', 'attempt an ad-hoc sanitization of taxon labels')]), Form.RadioGroup('tree_layout', 'tree layout options', [ Form.RadioItem('equal_arc', 'equal arc layout', True), Form.RadioItem('equal_daylight', 'equal daylight layout')]), Form.LatexFormat()] return form_objects
def get_form(): """ @return: the body of a form """ # define the tree string tree = Newick.parse(g_tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the default tip data lines default_tip_data_lines = [ 'a : A', 'b : A', 'c : C', 'd : T', 'e : T', 'f : T'] # define the default rate matrix lines R = np.array([ [-1, 1/3.0, 1/3.0, 1/3.0], [1/3.0, -1, 1/3.0, 1/3.0], [1/3.0, 1/3.0, -1, 1/3.0], [1/3.0, 1/3.0, 1/3.0, -1]]) # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.MultiLine('column', 'tip data', '\n'.join(default_tip_data_lines)), Form.Matrix('matrix', 'rate matrix', R, MatrixUtil.assert_rate_matrix), Form.ImageFormat()] return form_objects
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() tree.add_branch_lengths() if tree.has_negative_branch_lengths(): msg_a = 'calculating weights for a tree ' msg_b = 'with negative branch lengths is not implemented' raise HandlingError(msg_a + msg_b) # get the selected names selection = Util.get_stripped_lines(fs.selection.splitlines()) selected_name_set = set(selection) possible_name_set = set(node.get_name() for node in tree.gen_tips()) extra_names = selected_name_set - possible_name_set if extra_names: msg_a = 'the following selected names are not valid tips: ' msg_b = str(tuple(extra_names)) raise HandlingError(msg_a + msg_b) # prune the tree for name in set(node.name for node in tree.gen_tips()) - set(selection): try: node = tree.get_unique_node(name) except NewickSearchError as e: raise HandlingError(e) tree.prune(node) # get the weights if fs.stone: name_weight_pairs = LeafWeights.get_stone_weights(tree) elif fs.thompson: name_weight_pairs = LeafWeights.get_thompson_weights(tree) # report the weights lines = ['%s: %f' % pair for pair in name_weight_pairs] return '\n'.join(lines) + '\n'
def make_file(newick_string, filename_prefix, iterations, min_segments): import DrawTreeImage import Newick import SpatialTree # make a spatial tree tree = Newick.parse(newick_string, SpatialTree.SpatialTree) # break the tree into pieces segment_tree(tree, min_segments, SpatialTree.SpatialTreeNode) print 'broke the tree into', sum(1 for node in tree.preorder()), 'nodes' # do the layout do_layout(tree, iterations) print 'did the layout' # color some of the branches red = 'ff0000' green = '00ff00' blue = '0000ff' for child, color in zip(tree.root.children, (red, green, blue)): for node in child.preorder(): node.branch_color = color # get the image string image_format = 'png' image_string = DrawTreeImage.get_tree_image(tree, (640, 480), image_format) print 'created the image string' # write the image file with open('%s.%s' % (filename_prefix, image_format), 'wb') as fout: fout.write(image_string)
def process_line(self, line): """ Possibly add an attribute. The attribute can be a floating point value or a newick tree. """ self.processed_lines.append(line) arr = [x.strip() for x in line.split('=')] if len(arr) > 1: var, value = arr[0], arr[-1] var_arr = var.split() # read the log likelihood if var == 'Log Likelihood' and value.endswith(';'): try: fvalue = float(value[:-1]) setattr(self, 'lnL', fvalue) return except ValueError: pass # read a tree assignment if len(var_arr) == 2 and var_arr[0] == 'Tree': treename = var_arr[1] tree_string = value tree = Newick.parse(tree_string, Newick.NewickTree) setattr(self, treename, tree) return # read a standard variable assignment if len(var_arr) == 1: try: fvalue = float(value) setattr(self, var, fvalue) return except ValueError: pass
def make_file(newick_string, filename_prefix, iterations, min_segments): import DrawTreeImage import Newick import SpatialTree # make a spatial tree tree = Newick.parse(newick_string, SpatialTree.SpatialTree) # break the tree into pieces segment_tree(tree, min_segments, SpatialTree.SpatialTreeNode) print 'broke the tree into', sum(1 for node in tree.preorder()), 'nodes' # do the layout do_layout(tree, iterations) print 'did the layout' # color some of the branches red = 'ff0000' green = '00ff00' blue = '0000ff' for child, color in zip(tree.root.children, (red, green, blue)): for node in child.preorder(): node.branch_color = color # get the image string image_format = 'png' image_string = DrawTreeImage.get_tree_image(tree, (640, 480), image_format) print 'created the image string' # write the image file with open('%s.%s' % (filename_prefix, image_format), 'wb') as fout: fout.write(image_string)
def get_form(): """ @return: the body of a form """ # define the default tree string tree_string = Newick.brown_example_tree tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the default alignment string default_alignment_string = Fasta.brown_example_alignment.strip() # define the default nucleotide distribution weights default_nt_lines = [ 'A : 1', 'C : 1', 'G : 1', 'T : 1'] # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.MultiLine('alignment', 'nucleotide alignment', default_alignment_string), Form.MultiLine('weights', 'nucleotide weights', '\n'.join(default_nt_lines)), Form.Float('kappa', 'transition transversion rate ratio', 2, low_inclusive=0)] return form_objects
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the sequence order if it exists ordered_names = Util.get_stripped_lines(fs.order.splitlines()) if ordered_names: observed_name_set = set(ordered_names) expected_name_set = set(node.get_name() for node in tree.gen_tips()) extra_names = observed_name_set - expected_name_set missing_names = expected_name_set - observed_name_set if extra_names: msg_a = 'the list of ordered names includes these names ' msg_b = 'not found in the tree: %s' % str(tuple(extra_names)) raise HandlingError(msg_a + msg_b) if missing_names: msg_a = 'the tree includes these names not found in the list ' msg_b = 'of ordered names: %s' % str(tuple(missing_names)) raise HandlingError(msg_a + msg_b) else: ordered_names = list(tip.get_name() for name in tree.gen_tips()) # do the sampling sampled_sequences = JC69.sample_sequences(tree, ordered_names, fs.length) alignment = Fasta.create_alignment(ordered_names, sampled_sequences) # return the response return alignment.to_fasta_string() + '\n'
def main(): # create the alignment object print 'creating the alignment...' alignment_string = Fasta.brown_example_alignment.strip() alignment = Fasta.Alignment(StringIO(alignment_string)) # create a tree object print 'creating the tree...' tree_string = Newick.brown_example_tree tree = Newick.parse(tree_string, Newick.NewickTree) # create a rate matrix object print 'creating the rate matrix object...' distribution = {'A': .25, 'C': .25, 'G': .25, 'T': .25} kappa = 2.0 row_major_rate_matrix = RateMatrix.get_unscaled_hky85_rate_matrix( distribution, kappa).get_row_major_rate_matrix() rate_matrix = RateMatrix.FastRateMatrix( row_major_rate_matrix, list('ACGT')) rate_matrix.normalize() # get the mle_rates print 'getting the mle rates...' mle_rates = get_mle_rates(tree, alignment, rate_matrix) print 'mle rates:' print mle_rates print 'stockholm string:' print get_stockholm_string(tree, alignment, mle_rates)
def main(): import EqualArcLayout import Newick import SpatialTree # make a spatial tree tree = Newick.parse(Newick.daylight_example_tree, SpatialTree.SpatialTree) EqualArcLayout.do_layout(tree) max_size = (640, 480) image_formats = ('png', 'pdf', 'ps', 'svg') for image_format in image_formats: # get the image string image_string = get_tree_image(tree, max_size, image_format) # write the image file image_filename = 'test.%s' % image_format with open(image_filename, 'wb') as fout: fout.write(image_string) print 'created', image_filename # write an html file html_filename = 'test.html' with open(html_filename, 'w') as fout: print >> fout, '<html><body>' for image_format in image_formats: image_filename = 'test.%s' % image_format print >> fout, '<a href="%s">%s</a><br/>' % ( image_filename, image_filename) print >> fout, '</body></html>' print 'created', html_filename
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the set of names selection = Util.get_stripped_lines(fs.names.splitlines()) selected_name_set = set(selection) possible_name_set = set(node.get_name() for node in tree.gen_tips()) extra_names = selected_name_set - possible_name_set if extra_names: msg_a = 'the following selected names ' msg_b = 'are not valid tips: ' + ', '.join(extra_names) raise HandlingError(msg_a + msg_b) # get the list of tip nodes to remove if fs.remove: nodes_to_remove = [ node for node in tree.gen_tips() if node.name in selection] elif fs.keep: nodes_to_remove = [ node for node in tree.gen_tips() if node.name not in selection] # prune the tree for node in nodes_to_remove: tree.prune(node) # merge segmented branches internal_nodes_to_remove = [ node for node in tree.preorder() if node.get_child_count() == 1] for node in internal_nodes_to_remove: tree.remove_node(node) # return the response return tree.get_newick_string() + '\n'
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # get the mixture weights weights = [fs.weight_a, fs.weight_b, fs.weight_c] # get the matrices matrices = [fs.matrix_a, fs.matrix_b, fs.matrix_c] for R in matrices: if R.shape != (4, 4): msg = 'expected each nucleotide rate matrix to be 4x4' raise HandlingError(msg) # get the nucleotide alignment try: alignment = Fasta.Alignment(fs.alignment.splitlines()) alignment.force_nucleotide() except Fasta.AlignmentError as e: raise HandlingError(e) # create the mixture proportions weight_sum = sum(weights) mixture_proportions = [weight / weight_sum for weight in weights] # create the rate matrix objects ordered_states = list('ACGT') rate_matrix_objects = [] for R in matrices: rate_matrix_object = RateMatrix.RateMatrix(R.tolist(), ordered_states) rate_matrix_objects.append(rate_matrix_object) # create the mixture model mixture_model = SubModel.MixtureModel(mixture_proportions, rate_matrix_objects) # normalize the mixture model mixture_model.normalize() # return the html string return do_analysis(mixture_model, alignment, tree) + '\n'
def get_form(): """ @return: the body of a form """ # define the tree string tree_string = '(((Human:0.1, Chimpanzee:0.2):0.8, Gorilla:0.3):0.7, Orangutan:0.4, Gibbon:0.5);' tree = Newick.parse(tree_string, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # define the form objects form_objects = [ Form.MultiLine('tree', 'newick tree', formatted_tree_string), Form.Integer('ncols', 'sample this many nucleotide columns', 100, low=1, high=1000), Form.MultiLine('frequency_a', 'first component frequencies', get_frequency_string(0)), Form.Float('kappa_a', 'first component kappa', get_kappa(0), low_inclusive=0), Form.Float('weight_a', 'first component weight', get_weight(0), low_inclusive=0), Form.MultiLine('frequency_b', 'second component frequencies', get_frequency_string(1)), Form.Float('kappa_b', 'second component kappa', get_kappa(1), low_inclusive=0), Form.Float('weight_b', 'second component weight', get_weight(1), low_inclusive=0), Form.RadioGroup('fmt', 'output format options', [ Form.RadioItem('fasta', 'fasta'), Form.RadioItem('nex', 'nexus', True)])] return form_objects
def get_form(): """ @return: the body of a form """ # format the default tree string tree = Newick.parse(g_default_tree, Newick.NewickTree) formatted_tree_string = Newick.get_narrow_newick_string(tree, 60) # make a real distance matrix D_s = [line.split() for line in g_default_D_lines] D_default = np.array([[float(x) for x in row] for row in D_s]) # define the form objects form_objects = [ Form.MultiLine('test_tree', 'harmonic extension tree', formatted_tree_string), Form.Matrix('D', 'leaf distance matrix', D_default, MatrixUtil.assert_symmetric), Form.MultiLine('names', 'ordered leaf names', '\n'.join(g_default_leaf_names)), Form.Float('scale', 'scale the image of the tree by this factor', 200.0, low_exclusive=0.0), Form.ImageFormat() ] return form_objects
def get_response_content(fs): # get a properly formatted newick tree with branch lengths tree_string = '((1:1, 2:1)6:1, 3:1, (4:1, 5:1)7:1)8;' tree = Newick.parse(tree_string, SpatialTree.SpatialTree) # get the fiedler-like vertex valuation fiedler = [1, 1, -1, -1, -1, 1, -1, -1] # create a node id map ids = [None]*8 for node in tree.preorder(): index = int(node.name) - 1 ids[index] = id(node) # convert fiedler into a dictionary v1 = dict((ids[i], float(fiedler[i])) for i in range(8)) # convert the annotations into dictionaries v2s = [dict((ids[i], float(v[i])) for i in range(8)) for v in g_annotation] # do the layout try: layout = FastDaylightLayout.StraightBranchLayout() layout.do_layout(tree) except RuntimeError as e: pass # draw the image try: ext = Form.g_imageformat_to_ext[fs.imageformat] return DrawEigenLacing.get_eg2_image( tree, (640, 480), ext, v1, v2s, fs.draw_background, fs.draw_vertices, fs.draw_labels) except CairoUtil.CairoUtilError as e: raise HandlingError(e)
def get_response_content(fs): flags = get_flags(fs) nseconds = 5 tm = time.time() rejected_s = None nerrors = 0 nchecked = 0 while time.time() < tm + nseconds and not rejected_s: nchecked += 1 # Sample a Newick tree. true_f = TreeSampler.sample_tree(fs.nleaves, 0, 1.0) true_s = NewickIO.get_newick_string(true_f) true_tree = Newick.parse(true_s, Newick.NewickTree) # Get the leaf and internal vertex ids for the true tree. internal_ids = set(id(x) for x in true_tree.gen_internal_nodes()) leaf_ids = set(id(x) for x in true_tree.gen_tips()) nleaves = len(leaf_ids) # Get the harmonic valuations for all vertices of the tree. id_to_full_val_list = [Harmonic.get_harmonic_valuations( true_tree, i) for i in range(1, nleaves)] # Check for small valuations at the leaves. try: for id_to_full_val in id_to_full_val_list: for x in leaf_ids: value = id_to_full_val[x] if abs(value) < 1e-8: raise CheckTreeError('the true tree is too symmetric') except CheckTreeError as e: nerrors += 1 continue # Assign the leaf values and assign None to internal values. id_to_val_list = [] for id_to_full_val in id_to_full_val_list: d = {} for x in leaf_ids: s = -1 if id_to_full_val[x] < 0 else 1 d[x] = s for x in internal_ids: d[x] = None id_to_val_list.append(d) # Define the topology in a different format. id_to_adj = get_id_to_adj(true_tree) # Check the tree for self-compatibility under the given conditions. id_to_vals = SeekEigenLacing.rec_eigen( id_to_adj, id_to_val_list, flags) if not id_to_vals: rejected_s = true_s # make the report out = StringIO() if rejected_s: print >> out, 'rejected a true tree:' print >> out, rejected_s else: print >> out, 'no true tree was rejected' print >> out print >> out, nchecked, 'trees were sampled total' print >> out, nerrors, 'trees were too symmetric' return out.getvalue()
def test_vingron_ultra(self): tree = Newick.parse(g_vingron_ultra_tree, Newick.NewickTree) name_weight_pairs = get_thompson_weights(tree) int_weight_pairs = [ (int(name), weight) for name, weight in name_weight_pairs] weights = [weight for name, weight in sorted(int_weight_pairs)] #print 'vingron ultra:' #print weights pass
def get_response_content(fs): # get the tree tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() # modify the tree segmenting_nodes = [p for p in tree.preorder() if len(p.children) == 1] for node in segmenting_nodes: tree.remove_node(node) # return the response return tree.get_newick_string() + '\n'
def test_likelihood_calculation(self): # get a tree tree = Newick.parse(sample_tree_string, Newick.NewickTree) # get a model input_xml_string = get_sample_xml_string() model = deserialize_mixture_model(input_xml_string) # get an alignment alignment = Fasta.CodonAlignment(StringIO(long_sample_codon_alignment_string)) # get the likelihood log_likelihood = PhyLikelihood.get_log_likelihood(tree, alignment, model)