def make_tree(treefile, image_file, clone_info):
    colour_list = ['MidnightBlue','RoyalBlue', 'LightSkyBlue', 'Aquamarine', 'SpringGreen', 'GreenYellow',\
                   'Gold','DarkOrange']
    weeks = ['16', '30', '38', '48', '59', '119', '176', '206']
    weeks = ['6', '14', '53', '92','144']
    t = Tree(treefile,format = 1)
    ts = TreeStyle()
    for i in range(5):
        ts.legend.add_face(CircleFace(20, colour_list[i]), column=0)
        ts.legend.add_face(TextFace('week' + weeks[i]), column=1)
    ts.legend_position = 2
    ts.show_leaf_name = True
    ts.branch_vertical_margin = 15
    ts.rotation = 90
    ns = NodeStyle()
    ns["size"] = 1
    ns.hz_line_width = 10
    ns.vt_line_width = 10
    edge = 0
    for node in t.traverse():
        node.name = node.name.replace("'", "")
        node.name = node.name.replace(".", ",")
        name = node.name.split(' ')[0]
        print name
        if name in clone_info.keys():
            style_node(node, colour_list[int(clone_info[name][0])-1], int(int(clone_info[name][1])/10)+5)
        if not node.is_leaf() and node.name != 'NoName':
                f = TextFace(node.name)
                f.margin_top = 2.5
                f.margin_bottom = 2.5
                f.margin_right = 2.5
                f.margin_left = 2.5
                node.add_face(f, column=0, position="branch-top")
    t.render(image_file, tree_style = ts)
示例#2
0
def main(argv):
    inputfile = ''
    outgroup = ''
    outputfile = ''

    ts = TreeStyle()
    ts.show_leaf_name = True
    ts.show_branch_length = False
    ts.show_branch_support = True

    ns = NodeStyle()
    ns["size"] = 0
    ns["vt_line_width"] = 2
    ns["hz_line_width"] = 2

    try:
        opts, args = getopt.getopt(argv, "hi:o:p:",
                                   ["input=", "outgroup=", "-prefix-svg="])
    except getopt.GetoptError:
        print 'print-single-nwk.py -i <inputfile> -o <outgroup> -p <prefixsvg>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'print-single-nwk.py -i <inputfile> -o <outgroup> -p <prefixsvg>'
            sys.exit()
        elif opt in ("-i", "--input"):
            inputfile = arg
        elif opt in ("-o", "--outgroup"):
            outgroup = arg
        elif opt in ("-p", "--prefix-svg"):
            outputfile = arg
    # read tree
    t = Tree(inputfile)
    t.set_outgroup(outgroup)
    t.ladderize(1)

    for node in t.traverse():
        node.set_style(ns)

    # print final tree
    t.render(outputfile + ".svg", tree_style=ts)
def applyNodeStyle2(t,
                    rootname="root",
                    linewidth=1,
                    bgcolor="White",
                    dot_size=3,
                    dot_color="Black"):
    #applies node style to sub-tree beginning with text name of root
    style = NodeStyle()
    style["bgcolor"] = bgcolor
    style["hz_line_width"] = linewidth
    style["vt_line_width"] = linewidth
    style["size"] = dot_size

    for n in t.traverse():
        if rootname in n.name:
            # n.set_style(style)
            # n.img_style["fgcolor"]=dot_color
            for x in n.traverse():
                x.set_style(style)
                x.img_style["fgcolor"] = dot_color
            break
示例#4
0
def render_tree(sk_tree,
                tip_hues,
                tip_lums,
                saturation=0.9,
                output_fp=None,
                supress_display=False):
    ete_tree = Tree(str(sk_tree))
    tree_tips = [x.name for x in sk_tree.tips()]

    for ete_leaf in ete_tree.iter_leaves():
        if (ete_leaf.name not in tree_tips) and (ete_leaf.name[1:-1]
                                                 in tree_tips):
            ete_leaf.name = ete_leaf.name[1:-1]
        elif ete_leaf.name not in tree_tips:
            print 'leaf {0} in ete-parsed tree not found in skbio tree {1}'.format(
                ete_leaf.name, str(sk_tree.ascii_art()))
            raise KeyError

    for n in ete_tree.traverse():
        if n.is_leaf():
            hex_color = hls_to_rgb_hex(tip_hues[n.name], tip_lums[n.name],
                                       saturation)
            n.add_features(tip_color=hex_color)

    style = NodeStyle()
    style["size"] = 0
    for l in ete_tree.traverse():
        l.set_style(style)

    ts = TreeStyle()
    ts.layout_fn = layout
    ts.show_leaf_name = False

    if output_fp:
        ete_tree.render(output_fp, tree_style=ts)

    if not supress_display:
        ete_tree.show(tree_style=ts)

    return
示例#5
0
    def __layout__(self, node):
        if node.is_leaf():
            # Add node name to laef nodes
            N = AttrFace("name", fsize=14, fgcolor="black")
            faces.add_face_to_node(N, node, 0)
            nstyle = NodeStyle()

            nstyle["size"] = 0
            node.set_style(nstyle)
        if "internalCount" in node.features:
            print node.name
            # Creates a sphere face whose size is proportional to node's
            # feature "weight"
            C = CircleFace(radius=int(node.internalCount) / 10,
                           color="RoyalBlue",
                           style="sphere")
            T = TextFace("10")
            # Let's make the sphere transparent
            C.opacity = 0.3
            # And place as a float face over the tree
            faces.add_face_to_node(C, node, 0, position="float")
            faces.add_face_to_node(T, node, 1)
示例#6
0
 def _colorize(self, palette):
     """
     Assigns faces and colours to the locus trees for pretty rendering.
     :param palette: list
         List of strings representing colours in hexadecimal format.
     :return:
     """
     from ete2 import NodeStyle, faces
     if not self.L:
         self.locus_tree()  # computes & stores the tree
     ncol = len(palette)
     iFace = faces.AttrFace("I", fsize=8, text_suffix='/')
     pFace = faces.AttrFace("P", fsize=8)
     # idFace = faces.AttrFace("id", fsize=8)
     # suppFace = faces.AttrFace("support", text_suffix=" ", formatter="%.2f", fsize=8)
     coloured = dict((i, False) for i, g in enumerate(self.L.traverse(strategy="postorder")))
     current_colour = -1
     for g in self.L.traverse(strategy="postorder"):
         if not g.is_leaf():
             # g.add_face(suppFace, position="branch-bottom", column=-2)
             g.add_face(iFace, position="branch-top", column=-1)
             g.add_face(pFace, position="branch-top", column=0)
         if g.source:
             current_colour += 1
             current_colour %= ncol
             style = NodeStyle()
             style['vt_line_color'] = palette[current_colour]
             style['hz_line_color'] = palette[current_colour]
             style['size'] = 0
             style['fgcolor'] = '#000000'
             style["vt_line_width"] = 2
             style["hz_line_width"] = 2
             for gg in g.traverse():
                 if not coloured[gg.nid]:
                     gg.set_style(style)
                     coloured[gg.nid] = True
示例#7
0
def rename_labels(tree,
                  label_dict,
                  circle_size_dict,
                  color_dict,
                  font_size=12):
    """Changes the OTU-naming to taxa-naming for the leaves"""

    for node in tree.traverse():

        nstyle = NodeStyle()

        if node.is_leaf():
            otu = node.name
            taxa_name = label_dict[otu]
            nstyle['size'] = circle_size_dict[otu]
            color = color_dict[otu]

            node.add_face(TextFace(taxa_name, fsize=font_size, fgcolor=color),
                          0)

        else:
            nstyle['size'] = 0

        node.set_style(nstyle)
示例#8
0
    def createLineageTrees(self,
                           fn=None,
                           width=None,
                           height=None,
                           circular=False,
                           withAppearing=True,
                           from_t=0,
                           to_t=0):
        from ete2 import Tree, NodeStyle, AttrFace

        tree = Tree()
        style = self.getNodeStyle()
        divisionStyle = self.getNodeStyle()

        invisibleNodeStyle = NodeStyle()
        invisibleNodeStyle["hz_line_color"] = "white"
        invisibleNodeStyle["vt_line_color"] = "white"
        invisibleNodeStyle["fgcolor"] = "white"

        distanceFromRoot = 0

        nodeMap = {}
        branchSize = {}

        # add all nodes which appear in the first frame
        for event in self.mainOperator.innerOperators[0].events[from_t]:
            if event.type != pgmlink.EventType.Appearance:
                label = event.traxel_ids[0]
                appNode = tree.add_child(name=self.getNodeName(0, label),
                                         dist=distanceFromRoot)
                nodeMap[str(self.getNodeName(0, label))] = appNode
                branchSize[str(self.getNodeName(0, label))] = 0
                # making the branches to the root node invisible
                n = appNode
                while n:
                    n.set_style(invisibleNodeStyle)
                    n = n.up
                appNode.set_style(invisibleNodeStyle)
                name = AttrFace("name")
                name.fsize = 6

        # add all lineages
        for t, events_at in enumerate(
                self.mainOperator.innerOperators[0].events[from_t:to_t + 1]):
            t = t + 1
            for event in events_at:
                if event.type == pgmlink.EventType.Appearance and withAppearing:
                    label = event.traxel_ids[0]
                    appNode = tree.add_child(name=self.getNodeName(t, label),
                                             dist=distanceFromRoot + t)
                    nodeMap[str(self.getNodeName(t, label))] = appNode
                    branchSize[str(self.getNodeName(t, label))] = 0
                    # making the branches to the root node invisible
                    n = appNode
                    while n:
                        n.set_style(invisibleNodeStyle)
                        n = n.up
                    appNode.set_style(invisibleNodeStyle)
                    name = AttrFace("name")
                    name.fsize = 6

                elif event.type == pgmlink.EventType.Disappearance:
                    label = event.traxel_ids[0]
                    if str(self.getNodeName(t - 1,
                                            str(label))) not in nodeMap.keys():
                        continue
                    if branchSize[str(self.getNodeName(t - 1,
                                                       str(label)))] == 0:
                        del nodeMap[str(self.getNodeName(t - 1, str(label)))]
                        del branchSize[str(self.getNodeName(t - 1,
                                                            str(label)))]
                        continue
                    newNode = nodeMap[str(self.getNodeName(
                        t - 1, str(label)))].add_child(
                            name=self.getNodeName(t - 1, str(label)),
                            dist=branchSize[str(
                                self.getNodeName(t - 1, str(label)))])
                    newNode.set_style(style)
                    del nodeMap[str(self.getNodeName(t - 1, str(label)))]
                    del branchSize[str(self.getNodeName(t - 1, str(label)))]

                elif event.type == pgmlink.EventType.Division:
                    labelOld = event.traxel_ids[0]
                    labelNew1 = event.traxel_ids[1]
                    labelNew2 = event.traxel_ids[2]
                    if str(self.getNodeName(
                            t - 1, str(labelOld))) not in nodeMap.keys():
                        continue
                    newNode = nodeMap[str(
                        self.getNodeName(t - 1, str(labelOld)))].add_child(
                            name=self.getNodeName(
                                t - 1,
                                str(self.getNodeName(t - 1, str(labelOld)))),
                            dist=branchSize[str(
                                self.getNodeName(t - 1, str(labelOld)))])
                    del nodeMap[str(self.getNodeName(t - 1, str(labelOld)))]
                    del branchSize[str(self.getNodeName(t - 1, str(labelOld)))]
                    newNode.set_style(divisionStyle)
                    nodeMap[str(self.getNodeName(t, str(labelNew1)))] = newNode
                    nodeMap[str(self.getNodeName(t, str(labelNew2)))] = newNode
                    branchSize[str(self.getNodeName(t, str(labelNew1)))] = 1
                    branchSize[str(self.getNodeName(t, str(labelNew2)))] = 1

                elif event.type == pgmlink.EventType.Move:
                    labelOld = event.traxel_ids[0]
                    labelNew = event.traxel_ids[1]
                    if str(self.getNodeName(
                            t - 1, str(labelOld))) not in nodeMap.keys():
                        continue
                    nodeMap[str(self.getNodeName(
                        t, str(labelNew)))] = nodeMap[str(
                            self.getNodeName(t - 1, str(labelOld)))]
                    del nodeMap[str(self.getNodeName(t - 1, str(labelOld)))]
                    branchSize[str(self.getNodeName(
                        t, str(labelNew)))] = branchSize[str(
                            self.getNodeName(t - 1, str(labelOld)))] + 1
                    del branchSize[str(self.getNodeName(t - 1, str(labelOld)))]

                else:
                    raise Exception, "lineage tree generation not implemented for event type " + str(
                        event.type)

        for label in nodeMap.keys():
            newNode = nodeMap[label].add_child(name=label,
                                               dist=branchSize[label])

        self.plotTree(tree,
                      out_fn=fn,
                      rotation=270,
                      show_leaf_name=False,
                      show_branch_length=False,
                      circularTree=circular,
                      show_division_nodes=False,
                      distance_between_branches=4,
                      width=width,
                      height=height)
示例#9
0
def main():
    args = parse_args()

    if args.data:
        print "\nReading tree from " + args.tree + " and data matrix from " + args.data
        tree = ClusterTree(args.tree, text_array=args.data)
    else:
        print "\nReading tree from " + args.tree
        tree = Tree(args.tree)

    if args.midpoint:
        R = tree.get_midpoint_outgroup()
        tree.set_outgroup(R)
        print "- Applying midpoint rooting"
    elif args.outgroup:
        tree.set_outgroup(tree & args.outgroup)
        print "- Rooting using outgroup " + args.outgroup

    if not args.no_ladderize:
        tree.ladderize()
        print "- Ladderizing tree"

    table, column_list, column_values = readtable(args, tree.get_leaf_names())

    labels = []
    if args.labels:
        print "\nThese labels will be printed next to each strain:"
        for label in args.labels:
            if label in column_list:
                labels.append(label)
                print " " + label
            else:
                print "WARNING: specified label " + label + " was not found in the columns of the info file provided, " + args.info

    # set node styles
    # start by setting all to no shapes, black labels
    for n in tree.traverse():
        nstyle = NodeStyle()
        nstyle["fgcolor"] = "black"
        nstyle["size"] = 0
        n.set_style(nstyle)

    # add colour tags next to nodes
    if args.colour_tags:
        colour_tags = []
        print "\nThese columns will be used to generate colour tags:"
        for label in args.colour_tags:
            if label in column_list:
                colour_tags.append(label)
                print " " + label
            else:
                print "\tWARNING: specified label for colour tagging, " + label + ", was not found in the columns of the info file provided, " + args.info
        print(colour_tags)
        print("here")
        for i in range(0, len(colour_tags)):
            label = colour_tags[i]
            print(label)
            colour_dict = getColourPalette(column_values[label], args, label)

            print "- Adding colour tag for " + label

            for node in tree.get_leaves():
                this_face = Face()
                this_face.margin_left = args.padding
                node.add_face(this_face, column=0, position="aligned")

                if node.name in table:
                    this_label = table[node.name][label]
                    this_colour = colour_dict[this_label]
                else:
                    this_colour = "white"
                this_face = Face()
                this_face.background.color = this_colour
                this_face.margin_right = args.margin_right
                this_face.margin_left = args.margin_left
                this_face.margin_top = args.margin_top
                this_face.margin_bottom = args.margin_bottom
                this_face.border.width = args.border_width
                this_face.border.color = "white"
                node.add_face(this_face, column=i + 1, position="aligned")
        print
    else:
        colour_tags = []

    # add labels as columns
    for i in range(0, len(labels)):

        label = labels[i]

        print "- Adding label " + label
        if label == args.colour_nodes_by:
            print "  also colouring nodes by these values"
        print(column_values)
        print(args)
        colour_dict = getColourPalette(column_values[label], args, label)

        for node in tree.get_leaves():
            if node.name in table:
                this_label = table[node.name][label]
                this_colour = colour_dict[this_label]
            else:
                this_label = ""
                this_colour = "black"

            this_face = TextFace(text=this_label, fsize=args.font_size)
            if args.tags:
                this_face.background.color = this_colour
            elif label == args.colour_nodes_by:
                this_face.fgcolor = this_colour
            this_face.margin_right = args.padding
            if i == 0:
                this_face.margin_left = args.padding
            node.add_face(this_face,
                          column=i + len(colour_tags) + 1,
                          position="aligned")

            # set leaves to coloured circles
            node.img_style["size"] = args.node_size
            if label == args.colour_nodes_by:
                node.img_style["fgcolor"] = this_colour

    if args.colour_branches_by or args.colour_backgrounds_by or args.branch_support_colour:
        if args.colour_branches_by:
            print "- Colouring branches by label " + args.colour_branches_by
            colour_dict_br = getColourPalette(
                column_values[args.colour_branches_by], args,
                args.colour_branches_by)
        if args.colour_backgrounds_by:
            print "- Colouring node backgrounds by label " + args.colour_backgrounds_by
            colour_dict_bg = getColourPalette(
                column_values[args.colour_backgrounds_by], args,
                args.colour_backgrounds_by)
        if args.branch_support_colour:
            print "- Colouring branches by support values"
            # colours extracted from R using rgb( colorRamp(c("white","red", "black"))(seq(0, 1, length = 100)), max = 255)
            # support_colours = {0.0:"#FFFFFF",0.01:"#FFFFFF", 0.02:"#FFF9F9", 0.03:"#FFF4F4", 0.04:"#FFEFEF", 0.05:"#FFEAEA", 0.06:"#FFE5E5", 0.07:"#FFE0E0", 0.08:"#FFDADA", 0.09:"#FFD5D5", 0.1:"#FFD0D0", 0.11:"#FFCBCB", 0.12:"#FFC6C6", 0.13:"#FFC1C1", 0.14:"#FFBCBC", 0.15:"#FFB6B6", 0.16:"#FFB1B1", 0.17:"#FFACAC", 0.18:"#FFA7A7", 0.19:"#FFA2A2", 0.2:"#FF9D9D", 0.21:"#FF9797", 0.22:"#FF9292", 0.23:"#FF8D8D", 0.24:"#FF8888", 0.25:"#FF8383", 0.26:"#FF7E7E", 0.27:"#FF7979", 0.28:"#FF7373", 0.29:"#FF6E6E", 0.3:"#FF6969", 0.31:"#FF6464", 0.32:"#FF5F5F", 0.33:"#FF5A5A", 0.34:"#FF5454", 0.35:"#FF4F4F", 0.36:"#FF4A4A", 0.37:"#FF4545", 0.38:"#FF4040", 0.39:"#FF3B3B", 0.4:"#FF3636", 0.41:"#FF3030", 0.42:"#FF2B2B", 0.43:"#FF2626", 0.44:"#FF2121", 0.45:"#FF1C1C", 0.46:"#FF1717", 0.47:"#FF1212", 0.48:"#FF0C0C", 0.49:"#FF0707", 0.5:"#FF0202", 0.51:"#FC0000", 0.52:"#F70000", 0.53:"#F20000", 0.54:"#EC0000", 0.55:"#E70000", 0.56:"#E20000", 0.57:"#DD0000", 0.58:"#D80000", 0.59:"#D30000", 0.6:"#CE0000", 0.61:"#C80000", 0.62:"#C30000", 0.63:"#BE0000", 0.64:"#B90000", 0.65:"#B40000", 0.66:"#AF0000", 0.67:"#A90000", 0.68:"#A40000", 0.69:"#9F0000", 0.7:"#9A0000", 0.71:"#950000", 0.72:"#900000", 0.73:"#8B0000", 0.74:"#850000", 0.75:"#800000", 0.76:"#7B0000", 0.77:"#760000", 0.78:"#710000", 0.79:"#6C0000", 0.8:"#670000", 0.81:"#610000", 0.82:"#5C0000", 0.83:"#570000", 0.84:"#520000", 0.85:"#4D0000", 0.86:"#480000", 0.87:"#420000", 0.88:"#3D0000", 0.89:"#380000", 0.9:"#330000", 0.91:"#2E0000", 0.92:"#290000", 0.93:"#240000", 0.94:"#1E0000", 0.95:"#190000", 0.96:"#140000", 0.97:"#0F0000", 0.98:"#0A0000", 0.99:"#050000", 1:"#000000"}
            # rgb( colorRamp(c("red", "black"))(seq(0, 1, length = 100)), max = 255))
            support_colours = {}

            if args.branch_support_cutoff:
                for i in range(0, args.branch_support_cutoff):
                    support_colours[i] = "#FF0000"
                for i in range(args.branch_support_cutoff, 101):
                    support_colours[i] = "#000000"
            else:
                if args.branch_support_percent:
                    support_colours = {
                        0: "#FF0000",
                        1: "#FF0000",
                        2: "#FC0000",
                        3: "#F90000",
                        4: "#F70000",
                        5: "#F40000",
                        6: "#F20000",
                        7: "#EF0000",
                        8: "#EC0000",
                        9: "#EA0000",
                        10: "#E70000",
                        11: "#E50000",
                        12: "#E20000",
                        13: "#E00000",
                        14: "#DD0000",
                        15: "#DA0000",
                        16: "#D80000",
                        17: "#D50000",
                        18: "#D30000",
                        19: "#D00000",
                        20: "#CE0000",
                        21: "#CB0000",
                        22: "#C80000",
                        23: "#C60000",
                        24: "#C30000",
                        25: "#C10000",
                        26: "#BE0000",
                        27: "#BC0000",
                        28: "#B90000",
                        29: "#B60000",
                        30: "#B40000",
                        31: "#B10000",
                        32: "#AF0000",
                        33: "#AC0000",
                        34: "#AA0000",
                        35: "#A70000",
                        36: "#A40000",
                        37: "#A20000",
                        38: "#9F0000",
                        39: "#9D0000",
                        40: "#9A0000",
                        41: "#970000",
                        42: "#950000",
                        43: "#920000",
                        44: "#900000",
                        45: "#8D0000",
                        46: "#8B0000",
                        47: "#880000",
                        48: "#850000",
                        49: "#830000",
                        50: "#800000",
                        51: "#7E0000",
                        52: "#7B0000",
                        53: "#790000",
                        54: "#760000",
                        55: "#730000",
                        56: "#710000",
                        57: "#6E0000",
                        58: "#6C0000",
                        59: "#690000",
                        60: "#670000",
                        61: "#640000",
                        62: "#610000",
                        63: "#5F0000",
                        64: "#5C0000",
                        65: "#5A0000",
                        66: "#570000",
                        67: "#540000",
                        68: "#520000",
                        69: "#4F0000",
                        70: "#4D0000",
                        71: "#4A0000",
                        72: "#480000",
                        73: "#450000",
                        74: "#420000",
                        75: "#400000",
                        76: "#3D0000",
                        77: "#3B0000",
                        78: "#380000",
                        79: "#360000",
                        80: "#330000",
                        81: "#300000",
                        82: "#2E0000",
                        83: "#2B0000",
                        84: "#290000",
                        85: "#260000",
                        86: "#240000",
                        87: "#210000",
                        88: "#1E0000",
                        89: "#1C0000",
                        90: "#190000",
                        91: "#170000",
                        92: "#140000",
                        93: "#120000",
                        94: "#0F0000",
                        95: "#0C0000",
                        96: "#0A0000",
                        97: "#070000",
                        98: "#050000",
                        99: "#020000",
                        100: "#000000"
                    }
                else:
                    support_colours = {
                        0.0: "#FF0000",
                        0.01: "#FF0000",
                        0.02: "#FC0000",
                        0.03: "#F90000",
                        0.04: "#F70000",
                        0.05: "#F40000",
                        0.06: "#F20000",
                        0.07: "#EF0000",
                        0.08: "#EC0000",
                        0.09: "#EA0000",
                        0.1: "#E70000",
                        0.11: "#E50000",
                        0.12: "#E20000",
                        0.13: "#E00000",
                        0.14: "#DD0000",
                        0.15: "#DA0000",
                        0.16: "#D80000",
                        0.17: "#D50000",
                        0.18: "#D30000",
                        0.19: "#D00000",
                        0.2: "#CE0000",
                        0.21: "#CB0000",
                        0.22: "#C80000",
                        0.23: "#C60000",
                        0.24: "#C30000",
                        0.25: "#C10000",
                        0.26: "#BE0000",
                        0.27: "#BC0000",
                        0.28: "#B90000",
                        0.29: "#B60000",
                        0.3: "#B40000",
                        0.31: "#B10000",
                        0.32: "#AF0000",
                        0.33: "#AC0000",
                        0.34: "#AA0000",
                        0.35: "#A70000",
                        0.36: "#A40000",
                        0.37: "#A20000",
                        0.38: "#9F0000",
                        0.39: "#9D0000",
                        0.4: "#9A0000",
                        0.41: "#970000",
                        0.42: "#950000",
                        0.43: "#920000",
                        0.44: "#900000",
                        0.45: "#8D0000",
                        0.46: "#8B0000",
                        0.47: "#880000",
                        0.48: "#850000",
                        0.49: "#830000",
                        0.5: "#800000",
                        0.51: "#7E0000",
                        0.52: "#7B0000",
                        0.53: "#790000",
                        0.54: "#760000",
                        0.55: "#730000",
                        0.56: "#710000",
                        0.57: "#6E0000",
                        0.58: "#6C0000",
                        0.59: "#690000",
                        0.6: "#670000",
                        0.61: "#640000",
                        0.62: "#610000",
                        0.63: "#5F0000",
                        0.64: "#5C0000",
                        0.65: "#5A0000",
                        0.66: "#570000",
                        0.67: "#540000",
                        0.68: "#520000",
                        0.69: "#4F0000",
                        0.7: "#4D0000",
                        0.71: "#4A0000",
                        0.72: "#480000",
                        0.73: "#450000",
                        0.74: "#420000",
                        0.75: "#400000",
                        0.76: "#3D0000",
                        0.77: "#3B0000",
                        0.78: "#380000",
                        0.79: "#360000",
                        0.8: "#330000",
                        0.81: "#300000",
                        0.82: "#2E0000",
                        0.83: "#2B0000",
                        0.84: "#290000",
                        0.85: "#260000",
                        0.86: "#240000",
                        0.87: "#210000",
                        0.88: "#1E0000",
                        0.89: "#1C0000",
                        0.9: "#190000",
                        0.91: "#170000",
                        0.92: "#140000",
                        0.93: "#120000",
                        0.94: "#0F0000",
                        0.95: "#0C0000",
                        0.96: "#0A0000",
                        0.97: "#070000",
                        0.98: "#050000",
                        0.99: "#020000",
                        1.0: "#000000"
                    }
        for node in tree.traverse():
            nstyle = NodeStyle()
            nstyle["size"] = 0
            if node.name in table:
                #print "Colouring individual " + node.name
                if args.colour_branches_by:
                    nstyle["vt_line_color"] = colour_dict_br[table[node.name][
                        args.colour_branches_by]]  # set branch colour
                    nstyle["hz_line_color"] = colour_dict_br[table[node.name][
                        args.colour_branches_by]]
                if args.colour_backgrounds_by:
                    if args.colour_branches_by in table[node.name]:
                        if table[node.name][args.colour_branches_by] != "none":
                            if not args.colour_branches_by:
                                nstyle["hz_line_color"] = "black"
                                nstyle["vt_line_color"] = "black"
                            nstyle["bgcolor"] = colour_dict_bg[table[node.name][
                                args.
                                colour_backgrounds_by]]  # set background colour
                node.set_style(nstyle)
            else:
                # internal node
                descendants = node.get_leaves()
                descendant_labels_br = []
                descendant_labels_bg = []
                for d in descendants:
                    if args.colour_branches_by:
                        if d.name in table:
                            this_label_br = table[d.name][
                                args.colour_branches_by]
                            if this_label_br not in descendant_labels_br:
                                descendant_labels_br.append(this_label_br)
                        elif "none" not in descendant_labels_br:
                            descendant_labels_br.append("none")
                    if args.colour_backgrounds_by:
                        if d.name in table:
                            this_label_bg = table[d.name][
                                args.colour_backgrounds_by]
                            if this_label_bg not in descendant_labels_bg:
                                descendant_labels_bg.append(this_label_bg)
                        elif "none" not in descendant_labels_bg:
                            descendant_labels_bg.append("none")
#				nstyle = NodeStyle()
#				nstyle["size"] = 0
                if len(descendant_labels_br
                       ) == 1 and descendant_labels_br[0] != "none":
                    this_colour = colour_dict_br[descendant_labels_br[0]]
                    nstyle["vt_line_color"] = this_colour  # set branch colour
                    nstyle["hz_line_color"] = this_colour
                elif args.branch_support_colour and not node.is_leaf():
                    if int(node.support) in support_colours:
                        nstyle["vt_line_color"] = support_colours[int(
                            node.support)]  # take colour from support value
                        nstyle["hz_line_color"] = support_colours[int(
                            node.support)]
                    else:
                        print "  WARNING support values don't make sense. Note scale is assumed to be 0-1 unless using the --branch_support_percent flag."
                if len(descendant_labels_bg
                       ) == 1 and descendant_labels_bg[0] != "none":
                    this_colour = colour_dict_bg[descendant_labels_bg[0]]
                    nstyle["bgcolor"] = this_colour  # set background colour
                node.set_style(nstyle)

    if args.colour_nodes_by:
        if args.colour_nodes_by not in labels:
            print "- Colouring nodes by label " + args.colour_nodes_by
            colour_dict = getColourPalette(column_values[args.colour_nodes_by],
                                           args, args.colour_nodes_by)
            for node in tree.get_leaves():
                if node.name in table:
                    this_label = table[node.name][args.colour_nodes_by]
                    this_colour = colour_dict[this_label]
                    if this_colour != "None":
                        node.img_style["fgcolor"] = this_colour
                        node.img_style["size"] = args.node_size

    for node in tree.traverse():
        node.img_style["hz_line_width"] = args.branch_thickness
        node.img_style["vt_line_width"] = args.branch_thickness

    # matches = search_by_size(tree, 75)
    # print("matches")
    # node = matches[0]
    # node.swap_children()


########### COMENTADO PARA VER COMO QUEDA CON EL BARCODE. PORQUE ESE NO TIENE TODOS LOS NODOS

# rotate specific nodes
# matches = search_by_size(tree, 463)
# print("matches")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 601)
# print("matches")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(node, 7)
# print("matches")
# node = matches[1]
# node.swap_children()

# matches = search_by_size(node, 5)
# print("matches 5")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 8)
# print("matches 8")
# node = matches[5]
# node.swap_children()

# matches = search_by_size(tree, 349)
# print("matches 349")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 192)
# print("matches 192")
# node = matches[1]
# node.swap_children()

# matches = search_by_size(tree, 182)
# print("matches 182")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 180)
# print("matches 180")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 175)
# print("matches 175")
# node = matches[0]
# node.swap_children()

# matches = search_by_size(tree, 34)
# print("matches 34")
# node = matches[0]
# node.swap_children()

#### FIN ####

#for n in matches:
#	print n

# set tree style
    ts = TreeStyle()

    ts.draw_guiding_lines = True
    ts.guiding_lines_color = 'black'

    if args.show_leaf_names:
        ts.show_leaf_name = True
    else:
        ts.show_leaf_name = False

    if args.length_scale:
        ts.scale = args.length_scale

    if args.branch_padding:
        ts.branch_vertical_margin = args.branch_padding

    if args.branch_support_print:
        ts.show_branch_support = True

    if args.fan:
        ts.mode = "c"
        print "\nPrinting circular tree (--fan)"
    else:
        print "\nPrinting rectangular tree, to switch to circular use --fan"

    if args.title:
        title = TextFace(args.title, fsize=20)
        title.margin_left = 20
        title.margin_top = 20
        ts.title.add_face(title, column=1)

    if args.no_guiding_lines:
        ts.draw_guiding_lines = False

    if args.data:
        print "\nPrinting data matrix as " + args.data_type + " with range (" + str(
            args.mindata) + "->" + str(args.maxdata) + ";" + str(
                args.centervalue) + "), height " + str(
                    args.data_height) + ", width " + str(args.data_width)
        profileFace = ProfileFace(min_v=args.mindata,
                                  max_v=args.maxdata,
                                  center_v=args.centervalue,
                                  width=args.data_width,
                                  height=args.data_height,
                                  style=args.data_type)

        def mylayout(node):
            if node.is_leaf():
                add_face_to_node(profileFace, node, 0, aligned=True)

        ts.layout_fn = mylayout

    # set root branch length to zero
    tree.dist = 0

    if args.delete_branches:
        #print "Branches "+ args.delete_branches + " will not be shown"
        for branch in args.delete_branches:
            leaf = tree.get_leaves_by_name(branch)[0]  #SRR1173284
            leaf.delete()

    # render tree
    tree.render(args.output, w=args.width, dpi=400, units="mm", tree_style=ts)
    print "\n FINISHED! Tree plot printed to file " + args.output
    print

    if args.print_colour_dict:
        print colour_dict
        if args.colour_branches_by:
            print colour_dict_br
        if args.colour_backgrounds_by:
            print colour_dict_bg

    if args.interactive:
        print "\nEntering interactive mode..."
        tree.show(tree_style=ts)
示例#10
0
def bct_dendrogram(corpus,
                   tree,
                   outputfile=None,
                   fontsize=5,
                   save_newick=True,
                   mode='c',
                   show=False,
                   color_leafs=False,
                   save=False,
                   return_svg=True):
    """
    Draw a dendrogram of the texts in the corpus using ETE.
    
    Parameters
    ----------
    corpus : `Corpus` instance
        The corpus to be plotted.
    tree : `(VN)Clusterer` object
        The clusterer object which was
        applied to the corpus.
    outputfile : str
        The path where the plot should be saved.
    color_leafs: boolean, default=True,
        If true, will color the text labels
        according to their category.
    fontsize : int, default=5
        The fontsize of the labels on the axes.
    save_newick : boolean, default=True
        Whether to dump a representation of the
        tree in newick-format, which can later
        be modified using software like FigTree:
        http://tree.bio.ed.ac.uk/software/figtree/
    mode : str, default='c'
        The type of tree to be drawn. Supports:
        - 'c': circular dendrogram
        - 'r': traditional, rectangular dendrogram
    save : boolean, default=False
        Whether to save the plot to `outputfile`.
    return_svg : boolean, default=True
        Whether to return the plot in SVG-format.
        Useful for the GUI.
    """

    for leaf in tree.get_leaves():
        leaf.name = leaf.name.replace("'", '')

    def layout(node):
        if node.is_leaf():
            N = AttrFace("name", fsize=7)
            faces.add_face_to_node(faces.AttrFace("name", "Arial", 10, None),
                                   node,
                                   0,
                                   position='branch-right')
            # problems: aligment of labels to branch, left padding of labels

    ts = TreeStyle()
    ts.mode = mode
    ts.show_leaf_name = False
    ts.scale = 120
    ts.show_scale = False
    ts.branch_vertical_margin = 10

    nstyle = NodeStyle()
    nstyle["fgcolor"] = "#0f0f0f"
    nstyle["size"] = 0
    nstyle["vt_line_color"] = "#0f0f0f"
    nstyle["hz_line_color"] = "#0f0f0f"
    nstyle["vt_line_width"] = 1
    nstyle["hz_line_width"] = 1
    nstyle["vt_line_type"] = 0
    nstyle["hz_line_type"] = 0

    for n in tree.traverse():
        n.set_style(nstyle)

    ts.layout_fn = layout

    if outputfile:
        outputfile = os.path.expanduser(outputfile)

    if save_newick:  # save tree in newick format for later manipulation in e.g. FigTree:
        tree.write(outfile=os.path.splitext(outputfile)[0] + '.newick')

    if save:
        tree.render(outputfile, tree_style=ts)
    if show:
        tree.show(tree_style=ts)
    if return_svg:  # return the SVG as a string
        return tree.render("%%return")[0]
示例#11
0
parser.add_argument(
    '--outgroups',
    required=True,
    help=
    'A text document with your outgroups listed. The line should start with the word Outgroup1 followed by a list of all the species in the outgroup with everything separated by spaces. You can specify Outgroup2 and Outgroup3 on other lines as backup outgroups if no species from your outgroup are present.'
)
args = parser.parse_args()

###############
## Functions
###############

#### ETE TOOLKIT STYLES:
ts = TreeStyle()
ts.scale = 200
RED = NodeStyle()
RED["size"] = 0
RED["vt_line_width"] = 1
RED["hz_line_width"] = 1
RED["vt_line_type"] = 1  # 0 solid, 1 dashed, 2 dotted
RED["hz_line_type"] = 1
RED["bgcolor"] = "#c74d52"
BLUE = NodeStyle()
BLUE["size"] = 0
BLUE["vt_line_width"] = 1
BLUE["hz_line_width"] = 1
BLUE["vt_line_type"] = 1  # 0 solid, 1 dashed, 2 dotted
BLUE["hz_line_type"] = 1
BLUE["bgcolor"] = "#dedede"
YELLOW = NodeStyle()
YELLOW["size"] = 0
示例#12
0
文件: wejscie.py 项目: ejdzi/biol_sys
def find_modules_and_draw_tree(drzewo, graph):
    t = Tree(str(drzewo) + ";")
    t.img_style["bgcolor"] = "lavender"
    ts = TreeStyle()

    special = set()  # defaultdict(list)
    for l in t.iter_leaves():
        tmp = l
        special.add(tmp)
        up = True
        while up:
            up = False
            if tmp.is_root():
                break
            else:
                for children in tmp.up.children:
                    if children.is_leaf():
                        tmp = tmp.up
                        special.add(tmp)
                        up = True
                        break
    modules = set()
    for node in t.traverse('preorder'):
        if node in special:
            good = True
            for n in node.traverse('preorder'):
                if n not in special:
                    good = False
                    break
            if good:
                if not node.is_leaf(): modules.add(node)
                for n in node.traverse('preorder'):
                    special.remove(n)

    # below: merging modules by pre-defined cutoff
    def check_modules(i, j, graph, cutoff):
        for ii in i.iter_leaves():
            for jj in j.iter_leaves():
                if ii.name[1:-1] in graph[jj.name[1:-1]] and graph[
                        jj.name[1:-1]][ii.name[1:-1]]["com"] > cutoff:
                    return True
        return False

    while True:
        j = False
        breakkk = False
        for i in modules:
            if breakkk: break
            for j in modules:
                if i != j and (
                        not i.is_root()) and i.up == j.up and check_modules(
                            i, j, graph, 0.1):  # CUTOFF HERE
                    breakkk = True
                    break
        if breakkk:
            modules.add(i.up)
            modules.remove(i)
            modules.remove(j)
        else:
            break  # heh...

    ts.show_leaf_name = True
    for i in modules:
        style_ca = NodeStyle()
        style_ca["bgcolor"] = color()
        i.set_style(style_ca)
    t.show(tree_style=ts)
示例#13
0
	if not flag:
		break
	driver_in_tree.write('\t'.join(lines)+'\n')
driver_in_tree.close()


ts = TreeStyle()
ts.show_leaf_name = True
ts.margin_bottom = 40
ts.margin_top = 40
ts.margin_left = 10
ts.margin_right = 10
ts.show_scale = False
ts.scale=0.3

style1 = NodeStyle()
style1['size'] = 4
style1['fgcolor'] = '#5500ff'
style1['hz_line_color'] = '#55aa00'
style1['vt_line_color'] = '#55aa00'
style1['hz_line_width'] = 2
style1['vt_line_width'] = 2
style2 = NodeStyle()
style2['size'] = 6
style2['fgcolor'] = '#0055ff'
style2['hz_line_color'] = '#55aa00'
style2['vt_line_color'] = '#55aa00'
style2['hz_line_width'] = 2
style2['vt_line_width'] = 2
t0 = t&'node1'
t0.add_face(TextFace('1'),column=0,position='branch-right')
示例#14
0
def showTree(delimitation, scale = 500, render = False, fout = "", form = "svg", show_support = False):
	"""delimitation: species_setting class"""
	tree = delimitation.root
	style0 = NodeStyle()
	style0["fgcolor"] = "#000000"
	style0["vt_line_color"] = "#0000aa"
	style0["hz_line_color"] = "#0000aa"
	style0["vt_line_width"] = 2
	style0["hz_line_width"] = 2
	style0["vt_line_type"] = 0 
	style0["hz_line_type"] = 0
	style0["size"] = 0
	
	tree.clear_face()
	for node in tree.get_descendants():
		node.set_style(style0)
		node.img_style["size"] = 0
		node.clear_face()
	
	tree.set_style(style0)
	tree.img_style["size"] = 0
	
	style1 = NodeStyle()
	style1["fgcolor"] = "#000000"
	style1["vt_line_color"] = "#ff0000"
	style1["hz_line_color"] = "#0000aa"
	style1["vt_line_width"] = 2
	style1["hz_line_width"] = 2
	style1["vt_line_type"] = 0 
	style1["hz_line_type"] = 0
	style1["size"] = 0
	
	style2 = NodeStyle()
	style2["fgcolor"] = "#0f0f0f"
	style2["vt_line_color"] = "#ff0000"
	style2["hz_line_color"] = "#ff0000"
	style2["vt_line_width"] = 2
	style2["hz_line_width"] = 2
	style2["vt_line_type"] = 0 
	style2["hz_line_type"] = 0
	style2["size"] = 0
	
	for node in delimitation.active_nodes:
		node.set_style(style1)
		node.img_style["size"] = 0
		for des in node.get_descendants():
			des.set_style(style2)
			des.img_style["size"] = 0
	
	for node in delimitation.root.traverse(strategy='preorder'):
		if show_support and hasattr(node, "bs"):
			if node.bs == 0.0:
				node.add_face(TextFace("0", fsize = 8), column=0, position = "branch-top")
			else:
				node.add_face(TextFace("{0:.2f}".format(node.bs), fsize = 8), column=0, position = "branch-top")
			
	ts = TreeStyle()
	"""scale pixels per branch length unit"""
	ts.scale =  scale 
	ts.branch_vertical_margin = 7
	if render:
		tree.render(fout+"."+form, tree_style=ts)
	else:
		tree.show(tree_style=ts)
示例#15
0
            clade.name = str(idx)
        names[clade.name] = clade
    return names
# Search tree by clade name
def lookup_by_names(TREE):
    names = {}
    for clade in TREE.find_clades():
        if clade.name:
            if clade.name in names:
                raise ValueError("Duplicate key: %s" % clade.name)
            names[clade.name] = clade
    return names
#### ETE TOOLKIT BASED FUNCTIONS AND STYLES:
ts = TreeStyle()
ts.scale = 200
RED = NodeStyle()
RED["size"] = 0
RED["vt_line_width"] = 1
RED["hz_line_width"] = 1
RED["vt_line_type"] = 1 # 0 solid, 1 dashed, 2 dotted
RED["hz_line_type"] = 1
RED["bgcolor"] = "#dedede"
nstyle = NodeStyle()
nstyle["size"] = 0
#### STATISTICS FUNCTIONS:
# Median Absolute Deviation Statistic (MAD)
def mad(data, axis=None):
    return median(absolute(data - median(data, axis)), axis)
# Cut off score
def cut(data, axis=None):
    return median(data, axis) + median(absolute(data - median(data, axis)), axis)*MULTI
示例#16
0
def cluster():
    fileManager = managers.utility.loadFileManager()
    leq = '≤'.decode('utf-8')

    if request.method == "GET":
        # "GET" request occurs when the page is first loaded.
        if 'analyoption' not in session:
            session['analyoption'] = constants.DEFAULT_ANALIZE_OPTIONS
        if 'hierarchyoption' not in session:
            session['hierarchyoption'] = constants.DEFAULT_HIERARCHICAL_OPTIONS
        labels = fileManager.getActiveLabels()
        thresholdOps = {}
        return render_template('cluster.html',
                               labels=labels,
                               thresholdOps=thresholdOps)

    if 'getdendro' in request.form:
        labelDict = fileManager.getActiveLabels()
        labels = []
        for ind, label in labelDict.items():
            labels.append(label)
        # Apply re-tokenisation and filters to DTM
        #countMatrix = fileManager.getMatrix(ARGUMENTS OMITTED)

        # Get options from request.form
        orientation = str(request.form['orientation'])
        title = request.form['title']
        pruning = request.form['pruning']
        pruning = int(request.form['pruning']) if pruning else 0
        linkage = str(request.form['linkage'])
        metric = str(request.form['metric'])

        # Get active files
        allContents = []  # list of strings-of-text for each segment
        tempLabels = []  # list of labels for each segment
        for lFile in fileManager.files.values():
            if lFile.active:
                contentElement = lFile.loadContents()
                allContents.append(contentElement)

                if request.form["file_" + str(lFile.id)] == lFile.label:
                    tempLabels.append(lFile.label.encode("utf-8"))
                else:
                    newLabel = request.form["file_" +
                                            str(lFile.id)].encode("utf-8")
                    tempLabels.append(newLabel)

        # More options
        ngramSize = int(request.form['tokenSize'])
        useWordTokens = request.form['tokenType'] == 'word'
        try:
            useFreq = request.form['normalizeType'] == 'freq'

            useTfidf = request.form['normalizeType'] == 'tfidf'  # if use TF/IDF
            normOption = "N/A"  # only applicable when using "TF/IDF", set default value to N/A
            if useTfidf:
                if request.form['norm'] == 'l1':
                    normOption = u'l1'
                elif request.form['norm'] == 'l2':
                    normOption = u'l2'
                else:
                    normOption = None
        except:
            useFreq = useTfidf = False
            normOption = None

        onlyCharGramsWithinWords = False
        if not useWordTokens:  # if using character-grams
            # this option is disabled on the GUI, because countVectorizer count front and end markers as ' ' if this is true
            onlyCharGramsWithinWords = 'inWordsOnly' in request.form

        greyWord = 'greyword' in request.form
        MostFrequenWord = 'mfwcheckbox' in request.form
        Culling = 'cullcheckbox' in request.form

        showDeletedWord = False
        if 'greyword' or 'mfwcheckbox' or 'cullcheckbox' in request.form:
            if 'onlygreyword' in request.form:
                showDeletedWord = True

        if useWordTokens:
            tokenType = u'word'
        else:
            tokenType = u'char'
            if onlyCharGramsWithinWords:
                tokenType = u'char_wb'

        from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
        vectorizer = CountVectorizer(input=u'content',
                                     encoding=u'utf-8',
                                     min_df=1,
                                     analyzer=tokenType,
                                     token_pattern=ur'(?u)\b[\w\']+\b',
                                     ngram_range=(ngramSize, ngramSize),
                                     stop_words=[],
                                     dtype=float,
                                     max_df=1.0)

        # make a (sparse) Document-Term-Matrix (DTM) to hold all counts
        DocTermSparseMatrix = vectorizer.fit_transform(allContents)
        dtm = DocTermSparseMatrix.toarray()

        from sklearn.metrics.pairwise import euclidean_distances
        from scipy.cluster.hierarchy import ward

        import matplotlib.pyplot as plt
        from scipy.cluster.hierarchy import average, weighted, ward, single, complete, dendrogram
        from scipy.cluster import hierarchy
        from scipy.spatial.distance import pdist

        if orientation == "left":
            orientation = "right"
        if orientation == "top":
            LEAF_ROTATION_DEGREE = 90
        else:
            LEAF_ROTATION_DEGREE = 0

        if linkage == "ward":
            dist = euclidean_distances(dtm)
            np.round(dist, 1)
            linkage_matrix = ward(dist)
            dendrogram(linkage_matrix,
                       orientation=orientation,
                       leaf_rotation=LEAF_ROTATION_DEGREE,
                       labels=labels)
            Z = linkage_matrix
        else:
            Y = pdist(dtm, metric)
            Z = hierarchy.linkage(Y, method=linkage)
            dendrogram(Z,
                       orientation=orientation,
                       leaf_rotation=LEAF_ROTATION_DEGREE,
                       labels=labels)

        plt.tight_layout()  # fixes margins

        ## Conversion to Newick/ETE
        # Stuff we need
        from scipy.cluster.hierarchy import average, linkage, to_tree
        #from hcluster import linkage, to_tree
        from ete2 import Tree, TreeStyle, NodeStyle

        # Change it to a distance matrix
        T = to_tree(Z)

        # ete2 section
        root = Tree()
        root.dist = 0
        root.name = "root"
        item2node = {T: root}

        to_visit = [T]
        while to_visit:
            node = to_visit.pop()
            cl_dist = node.dist / 2.0
            for ch_node in [node.left, node.right]:
                if ch_node:
                    ch = Tree()
                    ch.dist = cl_dist
                    ch.name = str(ch_node.id)
                    item2node[node].add_child(ch)
                    item2node[ch_node] = ch
                    to_visit.append(ch_node)

        # This is the ETE tree structure
        tree = root
        ts = TreeStyle()
        ts.show_leaf_name = True
        ts.show_branch_length = True
        ts.show_scale = False
        ts.scale = None
        if orientation == "top":
            ts.rotation = 90
            ts.branch_vertical_margin = 10  # 10 pixels between adjacent branches

        # Draws nodes as small red spheres of diameter equal to 10 pixels
        nstyle = NodeStyle()
        nstyle["size"] = 0

        # Replace the node labels
        for leaf in tree:
            k = leaf.name
            k = int(k)
            leaf.name = labels[k]

        # Apply node styles to nodes
        for n in tree.traverse():
            n.set_style(nstyle)

        # Convert the ETE tree to Newick
        newick = tree.write()
        f = open(
            'C:\\Users\\Scott\\Documents\\GitHub\\d3-dendro\\newickStr.txt',
            'w')
        f.write(newick)
        f.close()

        # Save the image as .png...
        from os import path, makedirs

        # Using ETE
        folder = pathjoin(session_manager.session_folder(),
                          constants.RESULTS_FOLDER)
        if (not os.path.isdir(folder)):
            makedirs(folder)

        # saves dendrogram as a .png with pyplot
        plt.savefig(path.join(folder, constants.DENDROGRAM_PNG_FILENAME))
        plt.close()
        # if orientation == "top":
        #     plt.figure(figsize=(20,80))
        # else:
        #     plt.figure(figsize=(80,20))

        pdfPageNumber, score, inconsistentMax, maxclustMax, distanceMax, distanceMin, monocritMax, monocritMin, threshold = utility.generateDendrogram(
            fileManager)
        session['dengenerated'] = True
        labels = fileManager.getActiveLabels()

        inconsistentOp = "0 " + leq + " t " + leq + " " + str(inconsistentMax)
        maxclustOp = "2 " + leq + " t " + leq + " " + str(maxclustMax)
        distanceOp = str(distanceMin) + " " + leq + " t " + leq + " " + str(
            distanceMax)
        monocritOp = str(monocritMin) + " " + leq + " t " + leq + " " + str(
            monocritMax)

        thresholdOps = {
            "inconsistent": inconsistentOp,
            "maxclust": maxclustOp,
            "distance": distanceOp,
            "monocrit": monocritOp
        }

        managers.utility.saveFileManager(fileManager)
        session_manager.cacheAnalysisOption()
        session_manager.cacheHierarchyOption()
        import random
        ver = random.random() * 100
        return render_template('cluster.html',
                               labels=labels,
                               pdfPageNumber=pdfPageNumber,
                               score=score,
                               inconsistentMax=inconsistentMax,
                               maxclustMax=maxclustMax,
                               distanceMax=distanceMax,
                               distanceMin=distanceMin,
                               monocritMax=monocritMax,
                               monocritMin=monocritMin,
                               threshold=threshold,
                               thresholdOps=thresholdOps,
                               ver=ver)
示例#17
0
ts = TreeStyle()
ts.show_leaf_name = True
ts.show_branch_length = True
ts.show_scale = False
ts.scale = None

ts.mode = "c"  # draw tree in circular style
ts.arc_start = 0
ts.arc_span = 360

if orientation == "top":
    ts.rotation = 90
    ts.branch_vertical_margin = 10  # 10 pixels between adjacent branches

# Remove the default small red spheres for each node
nstyle = NodeStyle()
nstyle["size"] = 0
for leaf in tree:
    k = leaf.name
    k = int(k)
    leaf.name = labels[k]

# Apply node styles to nodes
for n in tree.traverse():
    n.set_style(nstyle)

# Plot the tree
tree.show(tree_style=ts)

# Or save it
ete_output_path = "eteTree.png"
示例#18
0
def main(argv):
    inputfile1 = ''
    inputfile2 = ''
    outgroup = ''
    outputfile1 = ''
    outputfile2 = ''

    ts = TreeStyle()
    ts.show_leaf_name = True
    ts.show_branch_length = False
    ts.show_branch_support = True

    ns1 = NodeStyle()
    ns1["size"] = 0
    ns1["vt_line_width"] = 2
    ns1["hz_line_width"] = 2
    ns1["vt_line_type"] = 2  # 0 solid, 1 dashed, 2 dotted
    ns1["hz_line_type"] = 2  # 0 solid, 1 dashed, 2 dotted
    ns1["vt_line_color"] = "#FF0000"
    ns1["hz_line_color"] = "#FF0000"

    ns2 = NodeStyle()
    ns2["size"] = 0
    ns2["vt_line_width"] = 2
    ns2["hz_line_width"] = 2
    ns2["vt_line_type"] = 0  # 0 solid, 1 dashed, 2 dotted
    ns2["hz_line_type"] = 0  # 0 solid, 1 dashed, 2 dotted

    try:
        opts, args = getopt.getopt(argv, "hi:j:o:p:q:", [
            "input1=", "input2=", "outgroup=", "-prefix-svg1=", "-prefix-svg2="
        ])
    except getopt.GetoptError:
        print 'print-two-nwk.py -i <inputfile1> -j <inputfile2> -o <outgroup> -p <prefixsvg1> -q <prefixsvg2>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'print-two-nwk.py -i <inputfile1> -j <inputfile2> -o <outgroup> -p <prefixsvg1> -q <prefixsvg2>'
            sys.exit()
        elif opt in ("-i", "--input1"):
            inputfile1 = arg
        elif opt in ("-j", "--input2"):
            inputfile2 = arg
        elif opt in ("-o", "--outgroup"):
            outgroup = arg
        elif opt in ("-p", "--prefix-svg1"):
            outputfile1 = arg
        elif opt in ("-q", "--prefix-svg2"):
            outputfile2 = arg
    # read tree
    t1 = Tree(inputfile1)
    t1.set_outgroup(outgroup)
    t1.ladderize(1)

    t2 = Tree(inputfile2)
    t2.set_outgroup(outgroup)
    t2.ladderize(1)

    # check clades
    clades = dict()

    for node in t1.traverse():
        if not node.is_leaf():
            # Get leaf names for a "node"
            names = map(lambda n: n.name, node.iter_leaves())
            names.sort()
            cladename = ",".join(names)
            if cladename in clades:
                clades[cladename] += 1
            else:
                clades[cladename] = 1

    for node in t2.traverse():
        if not node.is_leaf():
            # Get leaf names for a "node"
            names = map(lambda n: n.name, node.iter_leaves())
            names.sort()
            cladename = ",".join(names)
            if cladename in clades:
                clades[cladename] += 1
            else:
                clades[cladename] = 1

    for node in t1.traverse():
        if not node.is_leaf():
            # Get leaf names for a "node"
            names = map(lambda n: n.name, node.iter_leaves())
            names.sort()
            cladename = ",".join(names)
            if clades[cladename] == 2:
                node.set_style(ns2)
            else:
                node.set_style(ns1)
        else:
            node.set_style(ns2)

    for node in t2.traverse():
        if not node.is_leaf():
            # Get leaf names for a "node"
            names = map(lambda n: n.name, node.iter_leaves())
            names.sort()
            cladename = ",".join(names)
            if clades[cladename] == 2:
                node.set_style(ns2)
            else:
                node.set_style(ns1)
        else:
            node.set_style(ns2)

    # print final tree
    t1.render(outputfile1 + ".svg", tree_style=ts)
    t2.render(outputfile2 + ".svg", tree_style=ts)
示例#19
0
def plot_blast_result(tree_file,
                      blast_result_file_list,
                      id2description,
                      id2mlst,
                      check_overlap,
                      ordered_queries,
                      fasta_file2accession,
                      id_cutoff=80,
                      reference_accession='-',
                      accession2hit_filter=False,
                      show_identity_values=True):
    '''
    Projet Staph aureus PVL avec Laure Jaton
    Script pour afficher une phylogénie et la conservation de facteurs de virulence côte à côte
    Nécessite résultats MLST, ensemble des résultats tblastn (facteurs de virulence vs chromosomes),
    ainsi qu'une correspondance entre les accession des génomes et les noms qu'on veut afficher dans la phylogénie. Icemn
    pour les identifiants molis des patients, on les remplace par CHUV n.
    :param tree_file: phylogénie au format newick avec identifiants correspondants à tous les dico utilisés
    :param blast_result_file_list: résultats tblastn virulence factors vs chromosome (seulement best blast)
    :param id2description: identifiants génome utiisé dans l'arbre et description correspondante (i.e S aureus Newman)
    :param id2mlst: identitifiants arbre 2 S. aureus ST type
    :return:
    '''
    import blast_utils
    blast2data, queries = blast_utils.remove_blast_redundancy(
        blast_result_file_list, check_overlap)

    queries_count = {}

    for query in queries:
        queries_count[query] = 0
        for one_blast in blast2data:
            if query in blast2data[one_blast]:
                #print blast2data[one_blast][query]
                if float(blast2data[one_blast][query][0]) > id_cutoff:
                    queries_count[query] += 1
                else:
                    del blast2data[one_blast][query]

    print queries_count
    for query in queries:
        print "Hit counts: %s\t%s" % (query, queries_count[query])
        if queries_count[query] == 0:
            queries.pop(queries.index(query))

    print 'delete columns with no matches ok'
    '''             
    rm_genes = ['selv','spsmA1','psmB1','psmB2','ses','set','sel','selX','sek','sel2','LukF', 'LukM', 'hly', 'hld'
        , 'hlgA', 'hlgB', 'hlgC', 'sed', 'sej', 'ser', 'selq1', 'sec3', 'sek2', 'seq2', 'lukD', 'lukE']
    #rm_genes = ['icaR','icaA','icaB','icaC','icaD', 'sdrF', 'sdrH']

    for gene in rm_genes:
        queries.pop(queries.index(gene))
    '''
    #queries = ['selv']
    t1 = Tree(tree_file)
    tss = TreeStyle()
    #tss.show_branch_support = True
    # Calculate the midpoint node
    R = t1.get_midpoint_outgroup()
    t1.set_outgroup(R)
    t1.ladderize()

    ordered_queries_filtered = []
    for query in ordered_queries:
        hit_count = 0
        for lf2 in t1.iter_leaves():
            try:
                accession = fasta_file2accession[lf2.name]
                tmpidentity = blast2data[accession][query][0]
                if float(tmpidentity) > float(id_cutoff):
                    hit_count += 1
            except:
                continue
        if hit_count > 0:
            ordered_queries_filtered.append(query)
            #print 'skippink-----------'

    head = True
    print 'drawing tree'
    print 'n initial queries: %s n kept: %s' % (len(ordered_queries),
                                                len(ordered_queries_filtered))
    for lf in t1.iter_leaves():
        #lf.add_face(AttrFace("name", fsize=20), 0, position="branch-right")
        lf.branch_vertical_margin = 0
        #data = [random.randint(0,2) for x in xrange(3)]
        accession = fasta_file2accession[lf.name]
        for col, value in enumerate(ordered_queries_filtered):

            if head:
                if show_identity_values:
                    #'first row, print gene names'
                    #print 'ok!'
                    n = TextFace(' %s ' % str(value))
                    n.margin_top = 2
                    n.margin_right = 2
                    n.margin_left = 2
                    n.margin_bottom = 2
                    n.rotation = 270
                    n.vt_align = 2
                    n.hz_align = 2
                    n.inner_background.color = "white"
                    n.opacity = 1.
                    #lf.add_face(n, col, position="aligned")
                    tss.aligned_header.add_face(n, col)
                else:
                    n = TextFace(' %s ' % str(value), fsize=6)
                    n.margin_top = 0
                    n.margin_right = 0
                    n.margin_left = 0
                    n.margin_bottom = 0
                    n.rotation = 270
                    n.vt_align = 2
                    n.hz_align = 2
                    n.inner_background.color = "white"
                    n.opacity = 1.
                    #lf.add_face(n, col, position="aligned")
                    tss.aligned_header.add_face(n, col)
            try:
                identity_value = blast2data[accession][value][0]
                #print 'identity', lf.name, value, identity_value

                if lf.name != reference_accession:
                    if not accession2hit_filter:
                        # m_red
                        color = rgb2hex(m_blue.to_rgba(float(identity_value)))
                    else:
                        # if filter, color hits that are not in the filter in green

                        if accession in accession2hit_filter:
                            if value in accession2hit_filter[accession]:
                                # mred
                                color = rgb2hex(
                                    m_green.to_rgba(float(identity_value)))

                            else:
                                color = rgb2hex(
                                    m_blue.to_rgba(float(identity_value)))
                        else:
                            color = rgb2hex(
                                m_blue.to_rgba(float(identity_value)))
                else:
                    # reference taxon, blue scale
                    color = rgb2hex(m_blue.to_rgba(float(identity_value)))
                #if not show_identity_values:
                #    color = rgb2hex(m_blue.to_rgba(float(identity_value)))

            except:
                identity_value = 0
                color = "white"
            if show_identity_values:
                if float(identity_value) >= float(id_cutoff):

                    if str(identity_value) == '100.00' or str(
                            identity_value) == '100.0':
                        identity_value = '100'
                        n = TextFace("%s   " % identity_value)
                    else:
                        #    identity_value = str(round(float(identity_value), 1))

                        n = TextFace("%.2f" % round(float(identity_value), 2))
                    if float(identity_value) > 95:
                        n.fgcolor = "white"

                    n.opacity = 1.
                else:
                    identity_value = '-'
                    n = TextFace(' %s ' % str(identity_value))
                    n.opacity = 1.
                n.margin_top = 2
                n.margin_right = 2
                n.margin_left = 2
                n.margin_bottom = 2
                n.inner_background.color = color
                lf.add_face(n, col, position="aligned")
            else:

                if float(identity_value) >= float(id_cutoff):

                    # don't show identity values
                    n = TextFace('  ')

                    n.margin_top = 0
                    n.margin_right = 0
                    n.margin_left = 0
                    n.margin_bottom = 0
                    #n.color = color
                    n.inner_background.color = color
                    lf.add_face(n, col, position="aligned")

        try:
            accession = fasta_file2accession[lf.name]
            lf.name = ' %s (%s)' % (id2description[accession],
                                    id2mlst[lf.name])
        except KeyError:
            print '--------', id2description
            lf.name = ' %s (%s)' % (lf.name, id2mlst[lf.name])
        head = False

    for n in t1.traverse():
        nstyle = NodeStyle()
        if n.support < 0.9:
            #mundo = TextFace("%s" % str(n.support))
            #n.add_face(mundo, column=1, position="branch-bottom")
            nstyle["fgcolor"] = "blue"
            nstyle["size"] = 6
            n.set_style(nstyle)
        else:
            nstyle["fgcolor"] = "red"
            nstyle["size"] = 0
            n.set_style(nstyle)

    print 'rendering tree'
    t1.render("profile.svg", dpi=1000, h=400, tree_style=tss)
示例#20
0
文件: GMYC.py 项目: sangeeta97/pGMYC
	def get_species(self):
		sp_list = []
		for sp in self.species_list:
			spe = []
			for taxa in sp:
				spe.append(taxa.name)
			sp_list.append(spe)
		
		all_taxa_name = []
		
		#self.tree.convert_to_ultrametric(tree_length = 1.0, strategy='balanced')
		
		for leaf in self.tree.get_leaves():
			all_taxa_name.append(leaf.name)
		
		
		style0 = NodeStyle()
		style0["fgcolor"] = "#000000"
		#style2["shape"] = "circle"
		style0["vt_line_color"] = "#0000aa"
		style0["hz_line_color"] = "#0000aa"
		style0["vt_line_width"] = 2
		style0["hz_line_width"] = 2
		style0["vt_line_type"] = 0 # 0 solid, 1 dashed, 2 dotted
		style0["hz_line_type"] = 0
		style0["size"] = 0
		
		for node in self.tree.get_descendants():
			node.set_style(style0)
			node.img_style["size"] = 0
		self.tree.set_style(style0)
		self.tree.img_style["size"] = 0
		
		
		style1 = NodeStyle()
		style1["fgcolor"] = "#000000"
		#style2["shape"] = "circle"
		style1["vt_line_color"] = "#ff0000"
		style1["hz_line_color"] = "#0000aa"
		style1["vt_line_width"] = 2
		style1["hz_line_width"] = 2
		style1["vt_line_type"] = 0 # 0 solid, 1 dashed, 2 dotted
		style1["hz_line_type"] = 0
		style1["size"] = 0
		
		style2 = NodeStyle()
		style2["fgcolor"] = "#0f0f0f"
		#style2["shape"] = "circle"
		style2["vt_line_color"] = "#ff0000"
		style2["hz_line_color"] = "#ff0000"
		style2["vt_line_width"] = 2
		style2["hz_line_width"] = 2
		style2["vt_line_type"] = 0 # 0 solid, 1 dashed, 2 dotted
		style2["hz_line_type"] = 0
		style2["size"] = 0
		
		for node in self.coa_roots:
			node.set_style(style1)
			node.img_style["size"] = 0
			for des in node.get_descendants():
				des.set_style(style2)
				des.img_style["size"] = 0
		
		return [all_taxa_name], sp_list