예제 #1
0
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()
    # get the dictionary mapping the branch name to the nucleotide
    name_to_nt = {}
    lines = Util.get_stripped_lines(fs.column.splitlines())
    if lines:
        name_to_nt = SnippetUtil.get_generic_dictionary(lines, 'name',
                'nucleotide', list('acgtACGT'))
    # augment the tips with the nucleotide letters
    for name, nt in name_to_nt.items():
        try:
            node = tree.get_unique_node(name)
        except Newick.NewickSearchError as e:
            raise HandlingError(e)
        if node.children:
            msg = 'constraints on internal nodes are not implemented'
            raise HandlingError(msg)
        node.state = nt.upper()
    # read the rate matrix
    R = fs.matrix
    # convert the rate matrix to a rate matrix object
    states = list('ACGT')
    rate_matrix_object = RateMatrix.RateMatrix(R.tolist(), states)
    # simulate the ancestral nucleotides
    rate_matrix_object.simulate_ancestral_states(tree)
    # simulate a path on each branch
    # this breaks up the branch into a linear sequence of nodes and adds color
    for node in tree.gen_non_root_nodes():
        simulate_branch_path(tree, node, rate_matrix_object)
    # do the layout
    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)
예제 #2
0
def get_response_content(fs):
    # get the tree
    tree = Newick.parse(fs.tree, Newick.NewickTree)
    tree.assert_valid()
    # read the ordered states
    ordered_states = Util.get_stripped_lines(StringIO(fs.states))
    # read the matrix from the form data
    R = fs.rate_matrix
    if len(R) < 2:
        raise HandlingError('the rate matrix should have at least two rows')
    if len(ordered_states) != len(R):
        msg_a = 'the number of ordered states should be the same '
        msg_b = 'as the number of rows in the matrix'
        raise HandlingError(msg_a + msg_b)
    # get the dictionary mapping taxa to states
    taxon_to_state = SnippetUtil.get_generic_dictionary(
            StringIO(fs.assignments), 'taxon name', 'state name',
            ordered_states)
    # set the states for each of the tree tips
    for node in tree.gen_tips():
        node.state = taxon_to_state[node.name]
    # create the rate matrix object
    rate_matrix_object = RateMatrix.RateMatrix(R.tolist(), ordered_states)
    # repeatedly reroot and calculate root state distributions
    internal_nodes = list(tree.gen_internal_nodes())
    for node in internal_nodes:
        tree.reroot(node)
        rate_matrix_object.add_probabilities(tree)
        weights = [node.state_to_subtree_prob[state]
                for state in ordered_states]
        node.state_distribution = Util.weights_to_distribution(weights)
    # define the response
    out = StringIO()
    # show the ancestral state distributions
    for node in tree.gen_internal_nodes():
        if node.name:
            name = '\t'.join(str(p) for p in node.state_distribution)
            print >> out, node.name, ':', name
    # write the response
    return out.getvalue()
예제 #3
0
파일: 20080624a.py 프로젝트: BIGtigr/xgcode
def get_response_content(fs):
    # get the tree
    tree = Newick.parse(fs.tree, Newick.NewickTree)
    tree.assert_valid()
    # read the ordered states
    ordered_states = Util.get_stripped_lines(StringIO(fs.states))
    # read the matrix from the form data
    R = fs.rate_matrix
    if len(R) < 2:
        raise HandlingError('the rate matrix should have at least two rows')
    if len(ordered_states) != len(R):
        msg_a = 'the number of ordered states should be the same '
        msg_b = 'as the number of rows in the matrix'
        raise HandlingError(msg_a + msg_b)
    # get the dictionary mapping taxa to states
    taxon_to_state = SnippetUtil.get_generic_dictionary(
        StringIO(fs.assignments), 'taxon name', 'state name', ordered_states)
    # set the states for each of the tree tips
    for node in tree.gen_tips():
        node.state = taxon_to_state[node.name]
    # create the rate matrix object
    rate_matrix_object = RateMatrix.RateMatrix(R.tolist(), ordered_states)
    # repeatedly reroot and calculate root state distributions
    internal_nodes = list(tree.gen_internal_nodes())
    for node in internal_nodes:
        tree.reroot(node)
        rate_matrix_object.add_probabilities(tree)
        weights = [
            node.state_to_subtree_prob[state] for state in ordered_states
        ]
        node.state_distribution = Util.weights_to_distribution(weights)
    # define the response
    out = StringIO()
    # show the ancestral state distributions
    for node in tree.gen_internal_nodes():
        if node.name:
            name = '\t'.join(str(p) for p in node.state_distribution)
            print >> out, node.name, ':', name
    # write the response
    return out.getvalue()