示例#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()
    # 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)
示例#2
0
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)
示例#3
0
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)
示例#4
0
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)
示例#5
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()
    # 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)
示例#6
0
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)
示例#7
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_nucleotide = {}
    # parse the column string
    for line in iterutils.stripped_lines(fs.column.splitlines()):
        name_string, nucleotide_string = SnippetUtil.get_state_value_pair(line)
        if nucleotide_string not in list('acgtACGT'):
            msg = '"%s" is not a valid nucleotide' % nucleotide_string
            raise HandlingError(msg)
        nucleotide_string = nucleotide_string.upper()
        if name_string in name_to_nucleotide:
            raise HandlingError('the name "%s" was duplicated' % name_string)
        name_to_nucleotide[name_string] = nucleotide_string
    # augment the tips with the nucleotide letters
    for name, nucleotide in name_to_nucleotide.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 = nucleotide
    # get the Jukes-Cantor rate matrix object
    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 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)
    # 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)
示例#8
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_nucleotide = {}
    # parse the column string
    for line in iterutils.stripped_lines(fs.column.splitlines()):
        name_string, nucleotide_string = SnippetUtil.get_state_value_pair(line)
        if nucleotide_string not in list('acgtACGT'):
            msg = '"%s" is not a valid nucleotide' % nucleotide_string
            raise HandlingError(msg)
        nucleotide_string = nucleotide_string.upper()
        if name_string in name_to_nucleotide:
            raise HandlingError('the name "%s" was duplicated' % name_string)
        name_to_nucleotide[name_string] = nucleotide_string
    # augment the tips with the nucleotide letters
    for name, nucleotide in name_to_nucleotide.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 = nucleotide
    # get the Jukes-Cantor rate matrix object
    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 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)
    # 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)
示例#9
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 rgb color
    name_to_rgb = {}
    # parse the coloration string
    for line in iterutils.stripped_lines(fs.coloration.splitlines()):
        # get the branch and its color
        name_string, rgb_string = SnippetUtil.get_state_value_pair(line)
        rgb_string = rgb_string.upper()
        # validate the rgb string
        if len(rgb_string) != 6:
            msg = 'expected each rgb string to be six characters long'
            raise HandlingError(msg)
        bad_letters = set(rgb_string) - set('0123456789ABCDEFabcdef')
        if bad_letters:
            msg = 'found invalid rgb characters: %s' % str(tuple(bad_letters))
            raise HandlingError(msg)
        # associate the branch with its color
        name_to_rgb[name_string] = rgb_string
    # color the branches
    for name, rgb in name_to_rgb.items():
        try:
            node = tree.get_unique_node(name)
        except Newick.NewickSearchError as e:
            raise HandlingError(e)
        node.branch_color = rgb
    # 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 DrawTreeImage.get_tree_image(tree, (640, 480), ext)
    except CairoUtil.CairoUtilError as e:
        raise HandlingError(e)
示例#10
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)
示例#11
0
def demo():
    import EqualArcLayout
    import DrawTreeImage
    # read a tree
    tree_string = Newick.daylight_example_tree
    tree = Newick.parse(tree_string, SpatialTree)
    # do the layout
    EqualArcLayout.do_layout(tree)
    # show some debugging information
    max_size = (640, 480)
    tree.fit(max_size)
    extents = tree.get_extents()
    print 'extents:', extents
    branches = list(tree.gen_branches())
    print 'branches:'
    for branch in branches:
        print branch.src_location, branch.dst_location
    # draw the image
    image_format = 'png'
    image_string = DrawTreeImage.get_tree_image(tree, max_size, image_format)
    # write the image file
    with open('test.%s' % image_format, 'wb') as fout:
        fout.write(image_string)
示例#12
0
def get_response_content(fs):
    # start writing the response type
    response_headers = []
    # get a properly formatted newick tree with branch lengths
    tree = NewickIO.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 selected taxa
    selected_taxa = Util.get_stripped_lines(fs.selection.splitlines())
    # verify that each name is present in the tree
    for name in selected_taxa:
        try:
            node = tree.get_unique_node(name)
        except NewickIO.NewickSearchError as e:
            raise HandlingError(e)
        if node.children:
            raise HandlingError('an internal node was selected: ' + name)
    # split the branches of the tree a couple of times
    split_branches(tree)
    split_branches(tree)
    # color the tree
    add_colors(tree, selected_taxa)
    # 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 DrawTreeImage.get_tree_image(tree, (640, 480), ext)
    except CairoUtil.CairoUtilError as e:
        raise HandlingError(e)
示例#13
0
def get_response_content(fs):
    # start writing the response type
    response_headers = []
    # get a properly formatted newick tree with branch lengths
    tree = NewickIO.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 selected taxa
    selected_taxa = Util.get_stripped_lines(fs.selection.splitlines())
    # verify that each name is present in the tree
    for name in selected_taxa:
        try:
            node = tree.get_unique_node(name)
        except NewickIO.NewickSearchError as e:
            raise HandlingError(e)
        if node.children:
            raise HandlingError('an internal node was selected: ' + name)
    # split the branches of the tree a couple of times
    split_branches(tree)
    split_branches(tree)
    # color the tree
    add_colors(tree, selected_taxa)
    # 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 DrawTreeImage.get_tree_image(tree, (640, 480), ext)
    except CairoUtil.CairoUtilError as e:
        raise HandlingError(e)
示例#14
0
def demo():
    import EqualArcLayout
    import DrawTreeImage

    # read a tree
    tree_string = Newick.daylight_example_tree
    tree = Newick.parse(tree_string, SpatialTree)
    # do the layout
    EqualArcLayout.do_layout(tree)
    # show some debugging information
    max_size = (640, 480)
    tree.fit(max_size)
    extents = tree.get_extents()
    print "extents:", extents
    branches = list(tree.gen_branches())
    print "branches:"
    for branch in branches:
        print branch.src_location, branch.dst_location
    # draw the image
    image_format = "png"
    image_string = DrawTreeImage.get_tree_image(tree, max_size, image_format)
    # write the image file
    with open("test.%s" % image_format, "wb") as fout:
        fout.write(image_string)
示例#15
0
def make_file_progressive(newick_string, filename_prefix, min_segment_count):
    import DrawTreeImage
    import Newick
    import SpatialTree
    # make a spatial tree
    tree = Newick.parse(newick_string, SpatialTree.SpatialTree)
    # do the layout
    layout = CurvedBranchLayout()
    layout.set_min_segment_count(min_segment_count)
    layout.do_layout(tree)
    # 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)
示例#16
0
def make_file_progressive(newick_string, filename_prefix, min_segment_count):
    import DrawTreeImage
    import Newick
    import SpatialTree
    # make a spatial tree
    tree = Newick.parse(newick_string, SpatialTree.SpatialTree)
    # do the layout
    layout = CurvedBranchLayout()
    layout.set_min_segment_count(min_segment_count)
    layout.do_layout(tree)
    # 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)