Esempio n. 1
0
def label_groups(t, ngroups):
    nstyles = {}
    for i in range(1, ngroups + 1):
        nstyle = NodeStyle()
        nstyle["size"] = 10
        color = random_color()
        nstyle["fgcolor"] = color
        # assign a node style for each group from 1...N
        nstyles["Group{}".format(i)] = nstyle

    for n in t.traverse():
        if n.name.startswith('Group'):
            n.set_style(nstyles[n.name])

    return nstyles
Esempio n. 2
0
def draw_tree(tree, conf, outfile):
    try:
        from ete3 import (add_face_to_node, AttrFace, TextFace, TreeStyle, RectFace, CircleFace,
                             SequenceFace, random_color, SeqMotifFace)
    except ImportError as e:
        print(e)
        return

    def ly_basic(node):
        if node.is_leaf():
            node.img_style['size'] = 0
        else:
            node.img_style['size'] = 0
            node.img_style['shape'] = 'square'
            if len(MIXED_RES) > 1 and hasattr(node, "tree_seqtype"):
                if node.tree_seqtype == "nt":
                    node.img_style["bgcolor"] = "#CFE6CA"
                    ntF = TextFace("nt", fsize=6, fgcolor='#444', ftype='Helvetica')
                    add_face_to_node(ntF, node, 10, position="branch-bottom")
            if len(NPR_TREES) > 1 and hasattr(node, "tree_type"):
                node.img_style['size'] = 4
                node.img_style['fgcolor'] = "steelblue"

        node.img_style['hz_line_width'] = 1
        node.img_style['vt_line_width'] = 1

    def ly_leaf_names(node):
        if node.is_leaf():
            spF = TextFace(node.species, fsize=10, fgcolor='#444444', fstyle='italic', ftype='Helvetica')
            add_face_to_node(spF, node, column=0, position='branch-right')
            if hasattr(node, 'genename'):
                geneF = TextFace(" (%s)" %node.genename, fsize=8, fgcolor='#777777', ftype='Helvetica')
                add_face_to_node(geneF, node, column=1, position='branch-right')

    def ly_supports(node):
        if not node.is_leaf() and node.up:
            supFace = TextFace("%0.2g" %(node.support), fsize=7, fgcolor='indianred')
            add_face_to_node(supFace, node, column=0, position='branch-top')

    def ly_tax_labels(node):
        if node.is_leaf():
            c = LABEL_START_COL
            largest = 0
            for tname in TRACKED_CLADES:
                if hasattr(node, "named_lineage") and tname in node.named_lineage:
                    linF = TextFace(tname, fsize=10, fgcolor='white')
                    linF.margin_left = 3
                    linF.margin_right = 2
                    linF.background.color = lin2color[tname]
                    add_face_to_node(linF, node, c, position='aligned')
                    c += 1

            for n in range(c, len(TRACKED_CLADES)):
                add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'), node, c, position='aligned')
                c+=1

    def ly_full_alg(node):
        pass

    def ly_block_alg(node):
        if node.is_leaf():
            if 'sequence' in node.features:
                seqFace = SeqMotifFace(node.sequence, [])
                # [10, 100, "[]", None, 10, "black", "rgradient:blue", "arial|8|white|domain Name"],
                motifs = []
                last_lt = None
                for c, lt in enumerate(node.sequence):
                    if lt != '-':
                        if last_lt is None:
                            last_lt = c
                        if c+1 == len(node.sequence):
                            start, end = last_lt, c
                            motifs.append([start, end, "()", 0, 12, "slategrey", "slategrey", None])
                            last_lt = None
                    elif lt == '-':
                        if last_lt is not None:
                            start, end = last_lt, c-1
                            motifs.append([start, end, "()", 0, 12, "grey", "slategrey", None])
                            last_lt = None

                seqFace = SeqMotifFace(node.sequence, motifs,
                                       intermotif_format="line",
                                       seqtail_format="line", scale_factor=ALG_SCALE)
                add_face_to_node(seqFace, node, ALG_START_COL, aligned=True)


    TRACKED_CLADES = ["Eukaryota", "Viridiplantae",  "Fungi",
                      "Alveolata", "Metazoa", "Stramenopiles", "Rhodophyta",
                      "Amoebozoa", "Crypthophyta", "Bacteria",
                      "Alphaproteobacteria", "Betaproteobacteria", "Cyanobacteria",
                      "Gammaproteobacteria",]

    # ["Opisthokonta",  "Apicomplexa"]

    colors = random_color(num=len(TRACKED_CLADES), s=0.45)
    lin2color = dict([(ln, colors[i]) for i, ln in enumerate(TRACKED_CLADES)])

    NAME_FACE = AttrFace('name', fsize=10, fgcolor='#444444')

    LABEL_START_COL = 10
    ALG_START_COL = 40
    ts = TreeStyle()
    ts.draw_aligned_faces_as_table = False
    ts.draw_guiding_lines = False
    ts.show_leaf_name = False
    ts.show_branch_support = False
    ts.scale = 160

    ts.layout_fn = [ly_basic, ly_leaf_names, ly_supports, ly_tax_labels]

    MIXED_RES = set()
    MAX_SEQ_LEN = 0
    NPR_TREES = []
    for n in tree.traverse():
        if hasattr(n, "tree_seqtype"):
            MIXED_RES.add(n.tree_seqtype)
        if hasattr(n, "tree_type"):
            NPR_TREES.append(n.tree_type)
        seq = getattr(n, "sequence", "")
        MAX_SEQ_LEN = max(len(seq), MAX_SEQ_LEN)

    if MAX_SEQ_LEN:
        ALG_SCALE = min(1, 1000./MAX_SEQ_LEN)
        ts.layout_fn.append(ly_block_alg)

    if len(NPR_TREES) > 1:
        rF = RectFace(4, 4, "steelblue", "steelblue")
        rF.margin_right = 10
        rF.margin_left = 10
        ts.legend.add_face(rF, 0)
        ts.legend.add_face(TextFace(" NPR node"), 1)
        ts.legend_position = 3

    if len(MIXED_RES) > 1:
        rF = RectFace(20, 20, "#CFE6CA", "#CFE6CA")
        rF.margin_right = 10
        rF.margin_left = 10
        ts.legend.add_face(rF, 0)
        ts.legend.add_face(TextFace(" Nucleotide based alignment"), 1)
        ts.legend_position = 3


    try:
        tree.set_species_naming_function(spname)
        annotate_tree_with_ncbi(tree)
        a = tree.search_nodes(species='Dictyostelium discoideum')[0]
        b = tree.search_nodes(species='Chondrus crispus')[0]
        #out = tree.get_common_ancestor([a, b])
        #out = tree.search_nodes(species='Haemophilus parahaemolyticus')[0].up
        tree.set_outgroup(out)
        tree.swap_children()
    except Exception:
        pass

    tree.render(outfile, tree_style=ts, w=170, units='mm', dpi=150)
    tree.render(outfile+'.svg', tree_style=ts, w=170, units='mm', dpi=150)
    tree.render(outfile+'.pdf', tree_style=ts, w=170, units='mm', dpi=150)
Esempio n. 3
0
def draw_tree(tree, conf, outfile):
    try:
        from ete3 import (add_face_to_node, AttrFace, TextFace, TreeStyle,
                          RectFace, CircleFace, SequenceFace, random_color,
                          SeqMotifFace)
    except ImportError as e:
        print(e)
        return

    def ly_basic(node):
        if node.is_leaf():
            node.img_style['size'] = 0
        else:
            node.img_style['size'] = 0
            node.img_style['shape'] = 'square'
            if len(MIXED_RES) > 1 and hasattr(node, "tree_seqtype"):
                if node.tree_seqtype == "nt":
                    node.img_style["bgcolor"] = "#CFE6CA"
                    ntF = TextFace("nt",
                                   fsize=6,
                                   fgcolor='#444',
                                   ftype='Helvetica')
                    add_face_to_node(ntF, node, 10, position="branch-bottom")
            if len(NPR_TREES) > 1 and hasattr(node, "tree_type"):
                node.img_style['size'] = 4
                node.img_style['fgcolor'] = "steelblue"

        node.img_style['hz_line_width'] = 1
        node.img_style['vt_line_width'] = 1

    def ly_leaf_names(node):
        if node.is_leaf():
            spF = TextFace(node.species,
                           fsize=10,
                           fgcolor='#444444',
                           fstyle='italic',
                           ftype='Helvetica')
            add_face_to_node(spF, node, column=0, position='branch-right')
            if hasattr(node, 'genename'):
                geneF = TextFace(" (%s)" % node.genename,
                                 fsize=8,
                                 fgcolor='#777777',
                                 ftype='Helvetica')
                add_face_to_node(geneF,
                                 node,
                                 column=1,
                                 position='branch-right')

    def ly_supports(node):
        if not node.is_leaf() and node.up:
            supFace = TextFace("%0.2g" % (node.support),
                               fsize=7,
                               fgcolor='indianred')
            add_face_to_node(supFace, node, column=0, position='branch-top')

    def ly_tax_labels(node):
        if node.is_leaf():
            c = LABEL_START_COL
            largest = 0
            for tname in TRACKED_CLADES:
                if hasattr(node,
                           "named_lineage") and tname in node.named_lineage:
                    linF = TextFace(tname, fsize=10, fgcolor='white')
                    linF.margin_left = 3
                    linF.margin_right = 2
                    linF.background.color = lin2color[tname]
                    add_face_to_node(linF, node, c, position='aligned')
                    c += 1

            for n in range(c, len(TRACKED_CLADES)):
                add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'),
                                 node,
                                 c,
                                 position='aligned')
                c += 1

    def ly_full_alg(node):
        pass

    def ly_block_alg(node):
        if node.is_leaf():
            if 'sequence' in node.features:
                seqFace = SeqMotifFace(node.sequence, [])
                # [10, 100, "[]", None, 10, "black", "rgradient:blue", "arial|8|white|domain Name"],
                motifs = []
                last_lt = None
                for c, lt in enumerate(node.sequence):
                    if lt != '-':
                        if last_lt is None:
                            last_lt = c
                        if c + 1 == len(node.sequence):
                            start, end = last_lt, c
                            motifs.append([
                                start, end, "()", 0, 12, "slategrey",
                                "slategrey", None
                            ])
                            last_lt = None
                    elif lt == '-':
                        if last_lt is not None:
                            start, end = last_lt, c - 1
                            motifs.append([
                                start, end, "()", 0, 12, "grey", "slategrey",
                                None
                            ])
                            last_lt = None

                seqFace = SeqMotifFace(node.sequence,
                                       motifs,
                                       intermotif_format="line",
                                       seqtail_format="line",
                                       scale_factor=ALG_SCALE)
                add_face_to_node(seqFace, node, ALG_START_COL, aligned=True)

    TRACKED_CLADES = [
        "Eukaryota",
        "Viridiplantae",
        "Fungi",
        "Alveolata",
        "Metazoa",
        "Stramenopiles",
        "Rhodophyta",
        "Amoebozoa",
        "Crypthophyta",
        "Bacteria",
        "Alphaproteobacteria",
        "Betaproteobacteria",
        "Cyanobacteria",
        "Gammaproteobacteria",
    ]

    # ["Opisthokonta",  "Apicomplexa"]

    colors = random_color(num=len(TRACKED_CLADES), s=0.45)
    lin2color = dict([(ln, colors[i]) for i, ln in enumerate(TRACKED_CLADES)])

    NAME_FACE = AttrFace('name', fsize=10, fgcolor='#444444')

    LABEL_START_COL = 10
    ALG_START_COL = 40
    ts = TreeStyle()
    ts.draw_aligned_faces_as_table = False
    ts.draw_guiding_lines = False
    ts.show_leaf_name = False
    ts.show_branch_support = False
    ts.scale = 160

    ts.layout_fn = [ly_basic, ly_leaf_names, ly_supports, ly_tax_labels]

    MIXED_RES = set()
    MAX_SEQ_LEN = 0
    NPR_TREES = []
    for n in tree.traverse():
        if hasattr(n, "tree_seqtype"):
            MIXED_RES.add(n.tree_seqtype)
        if hasattr(n, "tree_type"):
            NPR_TREES.append(n.tree_type)
        seq = getattr(n, "sequence", "")
        MAX_SEQ_LEN = max(len(seq), MAX_SEQ_LEN)

    if MAX_SEQ_LEN:
        ALG_SCALE = min(1, 1000. / MAX_SEQ_LEN)
        ts.layout_fn.append(ly_block_alg)

    if len(NPR_TREES) > 1:
        rF = RectFace(4, 4, "steelblue", "steelblue")
        rF.margin_right = 10
        rF.margin_left = 10
        ts.legend.add_face(rF, 0)
        ts.legend.add_face(TextFace(" NPR node"), 1)
        ts.legend_position = 3

    if len(MIXED_RES) > 1:
        rF = RectFace(20, 20, "#CFE6CA", "#CFE6CA")
        rF.margin_right = 10
        rF.margin_left = 10
        ts.legend.add_face(rF, 0)
        ts.legend.add_face(TextFace(" Nucleotide based alignment"), 1)
        ts.legend_position = 3

    try:
        tree.set_species_naming_function(spname)
        annotate_tree_with_ncbi(tree)
        a = tree.search_nodes(species='Dictyostelium discoideum')[0]
        b = tree.search_nodes(species='Chondrus crispus')[0]
        #out = tree.get_common_ancestor([a, b])
        #out = tree.search_nodes(species='Haemophilus parahaemolyticus')[0].up
        tree.set_outgroup(out)
        tree.swap_children()
    except Exception:
        pass

    tree.render(outfile, tree_style=ts, w=170, units='mm', dpi=150)
    tree.render(outfile + '.svg', tree_style=ts, w=170, units='mm', dpi=150)
    tree.render(outfile + '.pdf', tree_style=ts, w=170, units='mm', dpi=150)
Esempio n. 4
0
def double_or_triple():
    seqdict = {}
    ny_aligned = list(SeqIO.parse('final_ny_aligned_name_fixed.txt', "fasta"))
    for record in ny_aligned:
        # new = str(record.description).split(' ')
        # # print(record.description)
        # new = new[0] + '_' + new[1]
        # new = new.replace('|', '_')
        seqdict[record.id] = str(record.seq)

    t = Tree("beast_tree_newick.nh")

    place_list = [
        'albany', 'manhattan', 'richmond', 'bronx', 'montgomery', 'rockland',
        'brooklyn', 'broome', 'new_rochelle', 'schenecktady', 'cayuga',
        'new_york', 'staten_island', 'chenango', 'ononda', 'suffolk',
        'clinton', 'orange', 'ulster', 'dutchess', 'passiac', 'washington',
        'franklin', 'putnam', 'westchester', 'jefferson', 'queens'
    ]
    # color_list = ["red", "yellow", "blue", "green", "cyan", "magenta", "orange", "light blue", "grey", "purple", "brown",
    #                 "pink", "light green"]
    color_list = random_color(num=len(place_list))
    color_list_mut = random_color(num=9)

    mut_list = ['AAC', 'AAG', 'AGC', 'AGG', 'GAG', 'GGC', 'GAC', 'GGG']

    for n in t.traverse():
        if n.name == '' or n.name == None:
            nstyle = NodeStyle()
            nstyle["fgcolor"] = "light grey"
            nstyle["size"] = 0
            n.set_style(nstyle)
        else:
            n.name = n.name.replace("\'", "")
            nstyle = NodeStyle()
            sequence = seqdict[n.name][28432:28435]
            color = color_list_mut[mut_list.index(sequence)]
            nstyle["hz_line_color"] = color
            nstyle["fgcolor"] = color
            n.set_style(nstyle)

    ## --------------------------------------------------------------------------
    ##  FOR TRIPLE MUTANT
    ##
    ## Author: Arjun
    ## --------------------------------------------------------------------------
    for n in t.traverse():
        new_name = n.name.replace("\'", "")
        if n.name == None or '|' not in n.name:
            nstyle_blank = NodeStyle()
            nstyle_blank["fgcolor"] = color_list_mut[8]
            nstyle_blank["size"] = 0
            n.set_style(nstyle_blank)
            # print(n.name)
            # names = n.name.split('_', 1)
        elif seqdict[new_name][28432] == 'A' and seqdict[new_name][
                28433] == 'A' and seqdict[new_name][28434] == 'C':
            nstyle_AAC = NodeStyle()
            nstyle_AAC["fgcolor"] = "red"
            nstyle_AAC["hz_line_color"] = "red"
            # nstyle["fgcolor"] = color_list_mut[0]
            # nstyle["hz_line_color"] = color_list_mut[0]
            nstyle_AAC["size"] = 15
            n.set_style(nstyle_AAC)
            n.name = 'AAC'
        elif seqdict[new_name][28432] == 'A' and seqdict[new_name][
                28433] == 'A' and seqdict[new_name][28434] == 'G':
            nstyle_AAG = NodeStyle()
            # nstyle_AAG["fgcolor"] = "yellow"
            nstyle_AAG["fgcolor"] = color_list_mut[1]
            nstyle_AAG["hz_line_color"] = color_list_mut[1]
            nstyle_AAG["size"] = 15
            n.set_style(nstyle_AAG)
            n.name = 'AAG'
        elif seqdict[new_name][28432] == 'A' and seqdict[new_name][
                28433] == 'G' and seqdict[new_name][28434] == 'C':
            nstyle_AGC = NodeStyle()
            # nstyle_AGC["fgcolor"] = "purple"
            nstyle_AGC["fgcolor"] = color_list_mut[2]
            nstyle_AGC["hz_line_color"] = color_list_mut[2]
            nstyle_AGC["size"] = 15
            n.set_style(nstyle_AGC)
            n.name = 'AGC'
        elif seqdict[new_name][28432] == 'A' and seqdict[new_name][
                28433] == 'G' and seqdict[new_name][28434] == 'G':
            nstyle_AGG = NodeStyle()
            # nstyle_AGG["fgcolor"] = "green"
            nstyle_AGG["fgcolor"] = color_list_mut[3]
            nstyle_AGG["hz_line_color"] = color_list_mut[3]
            nstyle_AGG["size"] = 15
            n.set_style(nstyle_AGG)
            n.name = 'AGG'
        elif seqdict[new_name][28432] == 'G' and seqdict[new_name][
                28433] == 'A' and seqdict[new_name][28434] == 'G':
            nstyle_GAG = NodeStyle()
            # nstyle_GAG["fgcolor"] = "grey"
            nstyle_GAG["fgcolor"] = color_list_mut[4]
            nstyle_GAG["hz_line_color"] = color_list_mut[4]
            nstyle_GAG["size"] = 15
            n.set_style(nstyle_GAG)
            n.name = 'GAG'
        elif seqdict[new_name][28432] == 'G' and seqdict[new_name][
                28433] == 'G' and seqdict[new_name][28434] == 'C':
            nstyle_GGC = NodeStyle()
            # nstyle_GGC["fgcolor"] = "light blue"
            nstyle_GGC["fgcolor"] = color_list_mut[5]
            nstyle_GGC["hz_line_color"] = color_list_mut[5]
            nstyle_GGC["size"] = 15
            n.set_style(nstyle_GGC)
            n.name = 'GGC'
        elif seqdict[new_name][28432] == 'G' and seqdict[new_name][
                28433] == 'A' and seqdict[new_name][28434] == 'C':
            nstyle_GAC = NodeStyle()
            # nstyle_GAC["fgcolor"] = "orange"
            nstyle_GAC["fgcolor"] = color_list_mut[6]
            nstyle_GAC["hz_line_color"] = color_list_mut[6]
            nstyle_GAC["size"] = 15
            n.set_style(nstyle_GAC)
            n.name = 'GAC'
        elif seqdict[new_name][28432] == 'G' and seqdict[new_name][
                28433] == 'G' and seqdict[new_name][28434] == 'G':
            n.name = 'GGG'
            nstyle_GGG = NodeStyle()
            nstyle_GGG["fgcolor"] = color_list_mut[7]
            nstyle_GGG["hz_line_color"] = color_list_mut[7]
            nstyle_GGG["size"] = 15
            n.set_style(nstyle_GGG)
            # namesplit = new_name.split('|')
            # n.name = namesplit[-1]

    ## --------------------------------------------------------------------------
    ##  FOR DOUBLE MUTANT
    ##
    ## Author: Arjun
    ## --------------------------------------------------------------------------

    for n in t.traverse():
        new_name = n.name.replace("\'", "")
        if n.name == None or '|' not in n.name:
            nstyle_blank = NodeStyle()
            nstyle_blank["fgcolor"] = color_list_mut[8]
            nstyle_blank["size"] = 0
            n.set_style(nstyle_blank)
        elif seqdict[new_name][18549] == 'C' and seqdict[new_name][
                29091] == 'G':
            n.name = 'CG'
            nstyle_CG = NodeStyle()
            nstyle_CG["fgcolor"] = color_list_mut[0]
            nstyle_CG["hz_line_color"] = color_list_mut[0]
            nstyle_CG["size"] = 15
            n.set_style(nstyle_CG)
        elif seqdict[new_name][18549] == 'C' and seqdict[new_name][
                29091] == 'A':
            n.name = 'CA'
            nstyle_CA = NodeStyle()
            nstyle_CA["fgcolor"] = color_list_mut[1]
            nstyle_CA["hz_line_color"] = color_list_mut[1]
            nstyle_CA["size"] = 15
            n.set_style(nstyle_CA)
        elif seqdict[new_name][18549] == 'T' and seqdict[new_name][
                29091] == 'G':
            n.name = 'TG'
            nstyle_TG = NodeStyle()
            nstyle_TG["fgcolor"] = color_list_mut[2]
            nstyle_TG["hz_line_color"] = color_list_mut[2]
            nstyle_TG["size"] = 15
            n.set_style(nstyle_TG)
        elif seqdict[new_name][18549] == 'T' and seqdict[new_name][
                29091] == 'A':
            n.name = 'TA'
            nstyle_TA = NodeStyle()
            nstyle_TA["fgcolor"] = color_list_mut[3]
            nstyle_TA["hz_line_color"] = color_list_mut[3]
            nstyle_TA["size"] = 15
            n.set_style(nstyle_TA)

    # for n in t.traverse():
    #     if n.name != '':
    #         sequence = seqdict[n.name][28432:28435]
    #         if n.nstyle["fgcolor"] != color_list_mut[mut_list.index(sequence)]:

    # for bad in badlist:
    #     new_bad = bad.replace("\'", "")
    #     if new_bad in seqdict:
    #         print(seqdict[new_bad][28432:28435])
    #     else:
    #         print('Name: ' + str(bad))

    make_long = TreeStyle()
    make_long.scale = 1500
    # t.show(tree_style=make_long)
    # print(len(badlist))
    # print(badlist)
    # for n in t.traverse():
    #     if n.name == '':
    #         nstyle = NodeStyle()
    #         nstyle["fgcolor"] = "light grey"
    #         nstyle["size"] = 0
    #         n.set_style(nstyle)
    #     else:
    #         n.name = n.name.replace("\'", "")
    #         nstyle = NodeStyle()
    #         nstyle["hz_line_color"] = color_list[place_list.index(n.name.split('|')[-1])]
    #         nstyle["fgcolor"] = color_list[place_list.index(n.name.split('|')[-1])]
    #         n.set_style(nstyle)
    # make_long = TreeStyle()
    # make_long.scale = 15000
    collapse(t)
    collapse(t)
    collapse(t)
    collapse(t)
    collapse(t)
    # collapse(t)
    # collapse(t)
    t.show(tree_style=make_long)
Esempio n. 5
0
def run(args):
    if args.text_mode:
        from ete3 import Tree
        for tindex, tfile in enumerate(args.src_tree_iterator):
            #print tfile
            if args.raxml:
                nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]",
                            open(tfile).read())
                t = Tree(nw)
            else:
                t = Tree(tfile)

            print(
                t.get_ascii(show_internal=args.show_internal_names,
                            attributes=args.show_attributes))
        return

    import random
    import re
    import colorsys
    from collections import defaultdict
    from ete3 import (Tree, PhyloTree, TextFace, RectFace, faces, TreeStyle,
                      add_face_to_node, random_color)

    global FACES

    if args.face:
        FACES = parse_faces(args.face)
    else:
        FACES = []

    # VISUALIZATION
    ts = TreeStyle()
    ts.mode = args.mode
    ts.show_leaf_name = True
    ts.tree_width = args.tree_width

    for f in FACES:
        if f["value"] == "@name":
            ts.show_leaf_name = False
            break

    if args.as_ncbi:
        ts.show_leaf_name = False
        FACES.extend(
            parse_faces([
                'value:@sci_name, size:10, fstyle:italic',
                'value:@taxid, color:grey, size:6, format:" - %s"',
                'value:@sci_name, color:steelblue, size:7, pos:b-top, nodetype:internal',
                'value:@rank, color:indianred, size:6, pos:b-bottom, nodetype:internal',
            ]))

    if args.alg:
        FACES.extend(
            parse_faces([
                'value:@sequence, size:10, pos:aligned, ftype:%s' %
                args.alg_type
            ]))

    if args.heatmap:
        FACES.extend(
            parse_faces(['value:@name, size:10, pos:aligned, ftype:heatmap']))

    if args.bubbles:
        for bubble in args.bubbles:
            FACES.extend(
                parse_faces([
                    'value:@%s, pos:float, ftype:bubble, opacity:0.4' % bubble,
                ]))

    ts.branch_vertical_margin = args.branch_separation
    if args.show_support:
        ts.show_branch_support = True
    if args.show_branch_length:
        ts.show_branch_length = True
    if args.force_topology:
        ts.force_topology = True
    ts.layout_fn = lambda x: None

    for tindex, tfile in enumerate(args.src_tree_iterator):
        #print tfile
        if args.raxml:
            nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]",
                        open(tfile).read())
            t = PhyloTree(nw)
        else:
            t = PhyloTree(tfile)

        if args.alg:
            t.link_to_alignment(args.alg, alg_format=args.alg_format)

        if args.heatmap:
            DEFAULT_COLOR_SATURATION = 0.3
            BASE_LIGHTNESS = 0.7

            def gradient_color(value, max_value, saturation=0.5, hue=0.1):
                def rgb2hex(rgb):
                    return '#%02x%02x%02x' % rgb

                def hls2hex(h, l, s):
                    return rgb2hex(
                        tuple([
                            int(x * 255) for x in colorsys.hls_to_rgb(h, l, s)
                        ]))

                lightness = 1 - (value * BASE_LIGHTNESS) / max_value
                return hls2hex(hue, lightness, DEFAULT_COLOR_SATURATION)

            heatmap_data = {}
            max_value, min_value = None, None
            for line in open(args.heatmap):
                if line.startswith('#COLNAMES'):
                    pass
                elif line.startswith('#') or not line.strip():
                    pass
                else:
                    fields = line.split('\t')
                    name = fields[0].strip()

                    values = [float(x) if x else None for x in fields[1:]]

                    maxv = max(values)
                    minv = min(values)
                    if max_value is None or maxv > max_value:
                        max_value = maxv
                    if min_value is None or minv < min_value:
                        min_value = minv
                    heatmap_data[name] = values

            heatmap_center_value = 0
            heatmap_color_center = "white"
            heatmap_color_up = 0.3
            heatmap_color_down = 0.7
            heatmap_color_missing = "black"

            heatmap_max_value = abs(heatmap_center_value - max_value)
            heatmap_min_value = abs(heatmap_center_value - min_value)

            if heatmap_center_value <= min_value:
                heatmap_max_value = heatmap_min_value + heatmap_max_value
            else:
                heatmap_max_value = max(heatmap_min_value, heatmap_max_value)

        # scale the tree
        if not args.height:
            args.height = None
        if not args.width:
            args.width = None

        f2color = {}
        f2last_seed = {}
        for node in t.traverse():
            node.img_style['size'] = 0
            if len(node.children) == 1:
                node.img_style['size'] = 2
                node.img_style['shape'] = "square"
                node.img_style['fgcolor'] = "steelblue"

            ftype_pos = defaultdict(int)

            for findex, f in enumerate(FACES):
                if (f['nodetype'] == 'any'
                        or (f['nodetype'] == 'leaf' and node.is_leaf()) or
                    (f['nodetype'] == 'internal' and not node.is_leaf())):

                    # if node passes face filters
                    if node_matcher(node, f["filters"]):
                        if f["value"].startswith("@"):
                            fvalue = getattr(node, f["value"][1:], None)
                        else:
                            fvalue = f["value"]

                        # if node's attribute has content, generate face
                        if fvalue is not None:
                            fsize = f["size"]
                            fbgcolor = f["bgcolor"]
                            fcolor = f['color']

                            if fcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr:
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(
                                        node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(
                                        color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(
                                        0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fcolor = f2color.setdefault(
                                        color_bin, random_color(h=seed))

                            if fbgcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fbgcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr:
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(
                                        node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(
                                        color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(
                                        0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fbgcolor = f2color.setdefault(
                                        color_bin, random_color(h=seed))

                            if f["ftype"] == "text":
                                if f.get("format", None):
                                    fvalue = f["format"] % fvalue

                                F = TextFace(fvalue,
                                             fsize=fsize,
                                             fgcolor=fcolor or "black",
                                             fstyle=f.get('fstyle', None))

                            elif f["ftype"] == "fullseq":
                                F = faces.SeqMotifFace(seq=fvalue,
                                                       seq_format="seq",
                                                       seqtail_format="seq",
                                                       height=fsize)
                            elif f["ftype"] == "compactseq":
                                F = faces.SeqMotifFace(
                                    seq=fvalue,
                                    seq_format="compactseq",
                                    seqtail_format="compactseq",
                                    height=fsize)
                            elif f["ftype"] == "blockseq":
                                F = faces.SeqMotifFace(
                                    seq=fvalue,
                                    seq_format="blockseq",
                                    seqtail_format="blockseq",
                                    height=fsize,
                                    fgcolor=fcolor or "slategrey",
                                    bgcolor=fbgcolor or "slategrey",
                                    scale_factor=1.0)
                                fbgcolor = None
                            elif f["ftype"] == "bubble":
                                try:
                                    v = float(fvalue)
                                except ValueError:
                                    rad = fsize
                                else:
                                    rad = fsize * v
                                F = faces.CircleFace(radius=rad,
                                                     style="sphere",
                                                     color=fcolor
                                                     or "steelblue")

                            elif f["ftype"] == "heatmap":
                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                else:
                                    col = f["column"]

                                for i, value in enumerate(
                                        heatmap_data.get(node.name, [])):
                                    ftype_pos[f["pos"]] += 1

                                    if value is None:
                                        color = heatmap_color_missing
                                    elif value > heatmap_center_value:
                                        color = gradient_color(
                                            abs(heatmap_center_value - value),
                                            heatmap_max_value,
                                            hue=heatmap_color_up)
                                    elif value < heatmap_center_value:
                                        color = gradient_color(
                                            abs(heatmap_center_value - value),
                                            heatmap_max_value,
                                            hue=heatmap_color_down)
                                    else:
                                        color = heatmap_color_center
                                    node.add_face(RectFace(
                                        20, 20, color, color),
                                                  position="aligned",
                                                  column=col + i)
                                    # Add header
                                    # for i, name in enumerate(header):
                                    #    nameF = TextFace(name, fsize=7)
                                    #    nameF.rotation = -90
                                    #    tree_style.aligned_header.add_face(nameF, column=i)
                                F = None

                            elif f["ftype"] == "profile":
                                # internal profiles?
                                F = None
                            elif f["ftype"] == "barchart":
                                F = None
                            elif f["ftype"] == "piechart":
                                F = None

                            # Add the Face
                            if F:
                                F.opacity = f['opacity'] or 1.0

                                # Set face general attributes
                                if fbgcolor:
                                    F.background.color = fbgcolor

                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                    ftype_pos[f["pos"]] += 1
                                else:
                                    col = f["column"]
                                node.add_face(F, column=col, position=f["pos"])

        if args.image:
            t.render("t%d.%s" % (tindex, args.image),
                     tree_style=ts,
                     w=args.width,
                     h=args.height,
                     units=args.size_units)
        else:
            t.show(None, tree_style=ts)
Esempio n. 6
0
    # new = new[0] + '_' + new[1]
    # new = new.replace('|', '_')
    seqdict[record.id] = str(record.seq)

t = Tree("beast_tree_newick.nh")

place_list = [
    'albany', 'manhattan', 'richmond', 'bronx', 'montgomery', 'rockland',
    'brooklyn', 'broome', 'new_rochelle', 'schenecktady', 'cayuga', 'new_york',
    'staten_island', 'chenango', 'ononda', 'suffolk', 'clinton', 'orange',
    'ulster', 'dutchess', 'passiac', 'washington', 'franklin', 'putnam',
    'westchester', 'jefferson', 'queens'
]
# color_list = ["red", "yellow", "blue", "green", "cyan", "magenta", "orange", "light blue", "grey", "purple", "brown",
#                 "pink", "light green"]
color_list = random_color(num=len(place_list))
color_list_mut = random_color(num=9)

mut_list = ['AAC', 'AAG', 'AGC', 'AGG', 'GAG', 'GGC', 'GAC', 'GGG']

# for n in t.traverse():
#     if n.name == '' or n.name == None:
#         nstyle = NodeStyle()
#         nstyle["fgcolor"] = "light grey"
#         nstyle["size"] = 0
#         n.set_style(nstyle)
#     else:
#         n.name = n.name.replace("\'", "")
#         nstyle = NodeStyle()
#         sequence = seqdict[n.name][28432:28435]
#         color = color_list_mut[mut_list.index(sequence)]
Esempio n. 7
0
def run(args):
    if args.text_mode:
        from ete3 import Tree
        for tindex, tfile in enumerate(args.src_tree_iterator):
            #print tfile
            if args.raxml:
                nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]", open(tfile).read())
                t = Tree(nw)
            else:
                t = Tree(tfile)

            print(t.get_ascii(show_internal=args.show_internal_names,
                              attributes=args.show_attributes))
        return

    import random
    import re
    import colorsys
    from collections import defaultdict
    from ete3 import (Tree, PhyloTree, TextFace, RectFace, faces, TreeStyle,
                         add_face_to_node, random_color)

    global FACES

    if args.face:
        FACES = parse_faces(args.face)
    else:
        FACES = []

    # VISUALIZATION
    ts = TreeStyle()
    ts.mode = args.mode
    ts.show_leaf_name = True
    ts.tree_width = args.tree_width


    for f in FACES:
        if f["value"] == "@name":
            ts.show_leaf_name = False
            break

    if args.as_ncbi:
        ts.show_leaf_name = False
        FACES.extend(parse_faces(
            ['value:@sci_name, size:10, fstyle:italic',
             'value:@taxid, color:grey, size:6, format:" - %s"',
             'value:@sci_name, color:steelblue, size:7, pos:b-top, nodetype:internal',
             'value:@rank, color:indianred, size:6, pos:b-bottom, nodetype:internal',
         ]))


    if args.alg:
        FACES.extend(parse_faces(
            ['value:@sequence, size:10, pos:aligned, ftype:%s' %args.alg_type]
         ))

    if args.heatmap:
        FACES.extend(parse_faces(
            ['value:@name, size:10, pos:aligned, ftype:heatmap']
         ))

    if args.bubbles:
        for bubble in args.bubbles:
            FACES.extend(parse_faces(
                ['value:@%s, pos:float, ftype:bubble, opacity:0.4' %bubble,
             ]))

    ts.branch_vertical_margin = args.branch_separation
    if args.show_support:
        ts.show_branch_support = True
    if args.show_branch_length:
        ts.show_branch_length = True
    if args.force_topology:
        ts.force_topology = True
    ts.layout_fn = lambda x: None

    for tindex, tfile in enumerate(args.src_tree_iterator):
        #print tfile
        if args.raxml:
            nw = re.sub(":(\d+\.\d+)\[(\d+)\]", ":\\1[&&NHX:support=\\2]", open(tfile).read())
            t = PhyloTree(nw)
        else:
            t = PhyloTree(tfile)


        if args.alg:
            t.link_to_alignment(args.alg, alg_format=args.alg_format)

        if args.heatmap:
            DEFAULT_COLOR_SATURATION = 0.3
            BASE_LIGHTNESS = 0.7
            def gradient_color(value, max_value, saturation=0.5, hue=0.1):
                def rgb2hex(rgb):
                    return '#%02x%02x%02x' % rgb
                def hls2hex(h, l, s):
                    return rgb2hex( tuple([int(x*255) for x in colorsys.hls_to_rgb(h, l, s)]))

                lightness = 1 - (value * BASE_LIGHTNESS) / max_value
                return hls2hex(hue, lightness, DEFAULT_COLOR_SATURATION)


            heatmap_data = {}
            max_value, min_value = None, None
            for line in open(args.heatmap):
                if line.startswith('#COLNAMES'):
                    pass
                elif line.startswith('#') or not line.strip():
                    pass
                else:
                    fields = line.split('\t')
                    name = fields[0].strip()

                    values = [float(x) if x else None for x in fields[1:]]

                    maxv = max(values)
                    minv = min(values)
                    if max_value is None or maxv > max_value:
                        max_value = maxv
                    if min_value is None or minv < min_value:
                        min_value = minv
                    heatmap_data[name] = values

            heatmap_center_value = 0
            heatmap_color_center = "white"
            heatmap_color_up = 0.3
            heatmap_color_down = 0.7
            heatmap_color_missing = "black"

            heatmap_max_value = abs(heatmap_center_value - max_value)
            heatmap_min_value = abs(heatmap_center_value - min_value)

            if heatmap_center_value <= min_value:
                heatmap_max_value = heatmap_min_value + heatmap_max_value
            else:
                heatmap_max_value = max(heatmap_min_value, heatmap_max_value)



        # scale the tree
        if not args.height:
            args.height = None
        if not args.width:
            args.width = None

        f2color = {}
        f2last_seed = {}
        for node in t.traverse():
            node.img_style['size'] = 0
            if len(node.children) == 1:
                node.img_style['size'] = 2
                node.img_style['shape'] = "square"
                node.img_style['fgcolor'] = "steelblue"

            ftype_pos = defaultdict(int)

            for findex, f in enumerate(FACES):
                if (f['nodetype'] == 'any' or
                    (f['nodetype'] == 'leaf' and node.is_leaf()) or
                    (f['nodetype'] == 'internal' and not node.is_leaf())):


                    # if node passes face filters
                    if node_matcher(node, f["filters"]):
                        if f["value"].startswith("@"):
                            fvalue = getattr(node, f["value"][1:], None)
                        else:
                            fvalue = f["value"]

                        # if node's attribute has content, generate face
                        if fvalue is not None:
                            fsize = f["size"]
                            fbgcolor = f["bgcolor"]
                            fcolor = f['color']

                            if fcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr :
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fcolor = f2color.setdefault(color_bin, random_color(h=seed))

                            if fbgcolor:
                                # Parse color options
                                auto_m = re.search("auto\(([^)]*)\)", fbgcolor)
                                if auto_m:
                                    target_attr = auto_m.groups()[0].strip()
                                    if not target_attr :
                                        color_keyattr = f["value"]
                                    else:
                                        color_keyattr = target_attr

                                    color_keyattr = color_keyattr.lstrip('@')
                                    color_bin = getattr(node, color_keyattr, None)

                                    last_seed = f2last_seed.setdefault(color_keyattr, random.random())

                                    seed = last_seed + 0.10 + random.uniform(0.1, 0.2)
                                    f2last_seed[color_keyattr] = seed

                                    fbgcolor = f2color.setdefault(color_bin, random_color(h=seed))

                            if f["ftype"] == "text":
                                if f.get("format", None):
                                    fvalue = f["format"] % fvalue

                                F = TextFace(fvalue,
                                             fsize = fsize,
                                             fgcolor = fcolor or "black",
                                             fstyle = f.get('fstyle', None))

                            elif f["ftype"] == "fullseq":
                                F = faces.SeqMotifFace(seq=fvalue, seq_format="seq",
                                                       seqtail_format="seq",
                                                       height=fsize)
                            elif f["ftype"] == "compactseq":
                                F = faces.SeqMotifFace(seq=fvalue, seq_format="compactseq",
                                                       seqtail_format="compactseq",
                                                       height=fsize)
                            elif f["ftype"] == "blockseq":
                                F = faces.SeqMotifFace(seq=fvalue, seq_format="blockseq",
                                                   seqtail_format="blockseq",
                                                       height=fsize,
                                                       fgcolor=fcolor or "slategrey",
                                                       bgcolor=fbgcolor or "slategrey",
                                                       scale_factor = 1.0)
                                fbgcolor = None
                            elif f["ftype"] == "bubble":
                                try:
                                    v = float(fvalue)
                                except ValueError:
                                    rad = fsize
                                else:
                                    rad = fsize * v
                                F = faces.CircleFace(radius=rad, style="sphere",
                                                     color=fcolor or "steelblue")

                            elif f["ftype"] == "heatmap":
                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                else:
                                    col = f["column"]

                                for i, value in enumerate(heatmap_data.get(node.name, [])):
                                    ftype_pos[f["pos"]] += 1

                                    if value is None:
                                        color = heatmap_color_missing
                                    elif value > heatmap_center_value:
                                        color = gradient_color(abs(heatmap_center_value - value), heatmap_max_value, hue=heatmap_color_up)
                                    elif value < heatmap_center_value:
                                        color = gradient_color(abs(heatmap_center_value - value), heatmap_max_value, hue=heatmap_color_down)
                                    else:
                                        color = heatmap_color_center
                                    node.add_face(RectFace(20, 20, color, color), position="aligned", column=col + i)
                                    # Add header
                                    # for i, name in enumerate(header):
                                    #    nameF = TextFace(name, fsize=7)
                                    #    nameF.rotation = -90
                                    #    tree_style.aligned_header.add_face(nameF, column=i)
                                F = None

                            elif f["ftype"] == "profile":
                                # internal profiles?
                                F = None
                            elif f["ftype"] == "barchart":
                                F = None
                            elif f["ftype"] == "piechart":
                                F = None



                            # Add the Face
                            if F:
                                F.opacity = f['opacity'] or 1.0

                                # Set face general attributes
                                if fbgcolor:
                                    F.background.color = fbgcolor

                                if not f['column']:
                                    col = ftype_pos[f["pos"]]
                                    ftype_pos[f["pos"]] += 1
                                else:
                                    col = f["column"]
                                node.add_face(F, column=col, position=f["pos"])

        if args.image:
            t.render("t%d.%s" %(tindex, args.image),
                     tree_style=ts, w=args.width, h=args.height, units=args.size_units)
        else:
            t.show(None, tree_style=ts)