Exemplo n.º 1
0
def _get_motif_tree(tree, data, circle=True, vmin=None, vmax=None):
    try:
        from ete3 import Tree, NodeStyle, TreeStyle
    except ImportError:
        print("Please install ete3 to use this functionality")
        sys.exit(1)

    t = Tree(tree)
    
    # Determine cutoff for color scale
    if not(vmin and vmax):
        for i in range(90, 101):
            minmax = np.percentile(data.values, i)
            if minmax > 0:
                break
    if not vmin:
        vmin = -minmax
    if not vmax:
        vmax = minmax
    
    norm = Normalize(vmin=vmin, vmax=vmax, clip=True)
    mapper = cm.ScalarMappable(norm=norm, cmap="RdBu_r")
    
    m = 25 / data.values.max()
    
    for node in t.traverse("levelorder"):
        val = data[[l.name for l in node.get_leaves()]].values.mean()
        style = NodeStyle()
        style["size"] = 0
        
        style["hz_line_color"] = to_hex(mapper.to_rgba(val))
        style["vt_line_color"] = to_hex(mapper.to_rgba(val))
        
        v = max(np.abs(m * val), 5)
        style["vt_line_width"] = v
        style["hz_line_width"] = v

        node.set_style(style)
    
    ts = TreeStyle()

    ts.layout_fn = _tree_layout
    ts.show_leaf_name= False
    ts.show_scale = False
    ts.branch_vertical_margin = 10

    if circle:
        ts.mode = "c"
        ts.arc_start = 180 # 0 degrees = 3 o'clock
        ts.arc_span = 180
    
    return t, ts
Exemplo n.º 2
0
        else:
            noisy_tree.write(format=1, features=["ND","T","C"],outfile="%s/annotated_tree_est.nhx"%(self.reptree),format_root_node=True)


        self.tree_fn_est     = self.reptree + "/noisy_root_tree.nhx"
        self.treeconv_fn_est = self.reptree + "/noisy_root_tree_conv.nhx"
        self.annotated_tree_fn_est = "%s/annotated_tree_est.nhx" %(self.reptree)

        return

# Basic tree style
tree_style = TreeStyle()
tree_style.show_leaf_name = True
tree_style.show_branch_length = True
tree_style.min_leaf_separation  = 4
tree_style.branch_vertical_margin   = 2

nstyle_T = NodeStyle()
nstyle_T["fgcolor"] = "orange"
#nstyle_T["shape"] = "square"
nstyle_T["size"] = 5
nstyle_T["vt_line_color"] = "orange"
nstyle_T["hz_line_color"] = "orange"
nstyle_T["vt_line_width"] = 2
nstyle_T["hz_line_width"] = 2


nstyle_C = NodeStyle()
nstyle_C["fgcolor"] = "orange"
nstyle_C["size"] = 5
nstyle_C["vt_line_color"] = "orange"
Exemplo n.º 3
0
                      position="branch-top")
#AciJub+PumCon
(t & "Anc1").add_features(label="5 ")
(t & "Anc1").add_face(TextFace((t & "Anc1").label, ftype='Arial'),
                      column=0,
                      position="branch-top")
#PriBen+PriViv
(t & "Anc2").add_features(label="15 ")
(t & "Anc2").add_face(TextFace((t & "Anc2").label, ftype='Arial'),
                      column=0,
                      position="branch-top")

for node in t.traverse():
    node.img_style['size'] = 0
    if node.is_leaf():
        name_face = TextFace(' ' + node.name,
                             ftype='Arial',
                             fstyle='italic',
                             fsize=10)
        node.add_face(name_face, column=0, position="branch-right")

#t.get_ascii(attributes=["name", "label"])
ts = TreeStyle()
ts.scale = 12000  # 12000 pixels per branch length unit
#t.show(tree_style=ts)
ts.show_leaf_name = False
ts.branch_vertical_margin = 10
#ts.show_branch_support = True
#t.get_ascii(attributes=["name", "label"])
t.render('tree.pdf', tree_style=ts)
Exemplo n.º 4
0
def plot_tree(tree_newick, tree_title):

    tree = Tree(tree_newick, format=1)
    # set tree parameters
    ts = TreeStyle()
    ts.mode = "r"  # tree model: 'r' for rectangular, 'c' for circular
    ts.show_leaf_name = 0
    # set tree title text parameters
    ts.title.add_face(TextFace(tree_title,
                               fsize=8,
                               fgcolor='black',
                               ftype='Arial',
                               tight_text=False),
                      column=0)  # tree title text setting
    # set layout parameters
    ts.rotation = 0  # from 0 to 360
    ts.show_scale = False
    ts.margin_top = 10  # top tree image margin
    ts.margin_bottom = 10  # bottom tree image margin
    ts.margin_left = 10  # left tree image margin
    ts.margin_right = 10  # right tree image margin
    ts.show_border = False  # set tree image border
    ts.branch_vertical_margin = 3  # 3 pixels between adjancent branches

    # set tree node style
    for each_node in tree.traverse():
        # leaf node parameters
        if each_node.is_leaf():
            ns = NodeStyle()
            ns["shape"] = "circle"  # dot shape: circle, square or sphere
            ns["size"] = 0  # dot size
            ns['hz_line_width'] = 0.5  # branch line width
            ns['vt_line_width'] = 0.5  # branch line width
            ns['hz_line_type'] = 0  # branch line type: 0 for solid, 1 for dashed, 2 for dotted
            ns['vt_line_type'] = 0  # branch line type
            ns["fgcolor"] = "blue"  # the dot setting
            each_node.add_face(TextFace(each_node.name,
                                        fsize=5,
                                        fgcolor='black',
                                        tight_text=False,
                                        bold=False),
                               column=0,
                               position='branch-right'
                               )  # leaf node the node name text setting

            each_node.set_style(ns)

        # non-leaf node parameters
        else:
            nlns = NodeStyle()
            nlns["size"] = 0  # dot size
            #nlns["rotation"] = 45
            each_node.add_face(
                TextFace(each_node.name[:12],
                         fsize=4,
                         fgcolor='black',
                         tight_text=False,
                         bold=False),
                column=5,
                position='branch-top')  # non-leaf node name text setting)

            each_node.set_style(nlns)

    tree.render('/Users/songweizhi/Desktop/Tree_' + tree_title + '.png',
                w=900,
                units="px",
                tree_style=ts)  # set figures size
Exemplo n.º 5
0
def plotting_tree(species, latin_names, original_tree, correction_table,
                  consensus_strategy_for_multi_outgroups, ortholog_db,
                  peak_stats, nextflow_flag):
    """
    Generate a PDF figure of the input tree with branch lengths equal to Ks distances.
    If it is not possible to compute the branch length for a branch, the branch line is dashed. This happens when some\\
    ortholog data to compute the branch-specific Ks contribution are missing.

    :param species: the current focal species
    :param latin_names: a dictionary-like data structure that associates each informal species name to its latin name
    :param original_tree: Newick tree format of the phylogenetic tree among the involved species
    :param correction_table: adjustment results in DataFrame format (contains both possible types of consensus strategy for how to deal with multiple outgroups)
    :param consensus_strategy_for_multi_outgroups: user choice about which consensus strategy to use when dealing with multiple outgroups
    :para ortholog_db: ortholog peak database used to get ortholog data for the relative rate test; if not available, will be ignored
    :param peak_stats: flag to specify whether the ortholog distribution peak is the mode or the median
    :param nextflow_flag: boolean flag to state whether the script is run in the Nextflow pipeline or not
    """
    # Get an equivalent tree where the focal species is the top leaf
    tree = reorder_tree_leaves(original_tree, species)
    node_and_branch_style(tree)

    species_node = get_species_node(species, tree)

    labeling_internal_nodes(species_node)
    species_history = get_species_history(species_node)
    rate_species_dict, rate_sister_dict = {}, {}

    for ancestor_node in species_history[:-2]:
        # NOTE: at the moment the following function is only used to fill in the dictionaries of branch-specific Ks contributions
        average_peak_of_divergence_event, margin_error_box, error_text = get_branch_length_and_errorbox(
            species, ancestor_node, correction_table,
            consensus_strategy_for_multi_outgroups, latin_names,
            rate_species_dict, rate_sister_dict)

        # Adding the branch length to the focal species node, otherwise it lacks it
        if ancestor_node.name == species:
            ancestor_node.dist = rate_species_dict[species]
            draw_branch_length_label(ancestor_node, known_distance=True)

        # Adding as TextFaces both the divergent Ks of the node (as mean) and the error range (left-most and right-most boundaries)
        divergence_node = ancestor_node.up  # getting parent node, where the current divergence takes place
        divergence_node.add_feature("rate_species", rate_species_dict[species])
        divergence_node.add_feature("avg_peak",
                                    round(average_peak_of_divergence_event, 2))
        divergence_node.add_feature("margins",
                                    f"({error_text[0]}, {error_text[1]})")
        ### divergence_node.add_face(AttrFace("margins", fsize=5), column=0, position="branch-right") [ NOT USED FOR NOW ]

    # Setting the branch length of the nodes belonging to the speciation history of the focal species
    for divergence_node in species_history[1:]:
        parent_node = divergence_node.up
        try:
            divergence_node.dist = round(
                parent_node.rate_species - divergence_node.rate_species, 3)
            draw_branch_length_label(divergence_node, known_distance=True)
        except Exception:
            divergence_node.dist = 10  # impossible number to flag an unknown length
            draw_branch_length_label(divergence_node, known_distance=False)
            unknown_branch_len_style(divergence_node)

    if ortholog_db.empty:  # branch-specific Ks contributions can be obtained only from adjustment_tables
        logging.info(
            "Getting branch-specific Ks contributions from rate-adjustment table data"
        )
    else:  # if the ortholog DB is available, we can try to compute the branch-specific Ks contributions from there too
        logging.info(
            "Getting branch-specific Ks contributions from rate-adjustment table data"
        )
        logging.info(
            "Computing branch-specific Ks contributions from ortholog peak data in database by applying principles of the relative rate test"
        )

    rate_dict = {}
    get_rates_from_current_analysis(rate_dict, correction_table, species,
                                    species_history, latin_names)

    # Setting the branch length of the other remaining nodes
    missing_ortholog_data_from_database = False
    missing_ortholog_data_from_correction_table = False

    for node in species_history[:-1]:
        sister_node = node.get_sisters(
        )  # is a list containing the sister NODE (it's only ONE node)

        if not ortholog_db.empty:  # if there is an ortholog database that can help with computing the missing branch lengths
            if len(sister_node[0].get_leaves()) > 1:
                missing_ortholog_data_from_database = get_rates_from_ortholog_peak_db(
                    rate_dict, sister_node, latin_names, ortholog_db,
                    peak_stats, missing_ortholog_data_from_database)
            else:
                if sister_node[0].name in rate_sister_dict.keys(
                ):  # if leaf has known length
                    sister_node[0].dist = rate_sister_dict[sister_node[0].name]
                    draw_branch_length_label(sister_node[0],
                                             known_distance=True)
                else:  # if the leaf has unknown length
                    sister_node[
                        0].dist = 10  # impossible number to flag an unknown length
                    draw_branch_length_label(sister_node[0],
                                             known_distance=False)
                    unknown_branch_len_style(sister_node[0])

        else:  # if ortholog database not available (the variable was previously set as an empty dataframe)
            if len(sister_node[0].get_leaves()) > 1:
                missing_ortholog_data_from_correction_table = True  # correction_tables is not enough to know all branch lengths!
                sister_node[
                    0].dist = 10  # impossible number to flag an unknown length
                draw_branch_length_label(sister_node[0], known_distance=False)
                unknown_branch_len_style(sister_node[0])
                for node in sister_node[0].get_descendants():
                    node.dist = 10  # impossible number to flag an unknown length
                    draw_branch_length_label(node, known_distance=False)
                    unknown_branch_len_style(node)
            else:
                leaf = sister_node[0].get_leaves()[0]  # there is only one leaf
                if leaf.name in rate_sister_dict.keys():
                    leaf.dist = rate_sister_dict[leaf.name]
                    draw_branch_length_label(leaf, known_distance=True)
                else:  # if the leaf has unknown length
                    leaf.dist = 10  # impossible number to flag an unknown length
                    draw_branch_length_label(leaf, known_distance=False)
                    unknown_branch_len_style(leaf)

    # If the ortholog peak database is lacking some required data (must have been deleted by the user) or
    # if the peak database has been deleted and only the correction_table has been used for the branch contributions, gives a warning
    if missing_ortholog_data_from_database or missing_ortholog_data_from_correction_table:
        logging.warning("")
        logging.warning(
            "One or more branch lengths are unknown (dashed line) due to missing ortholog distribution peak data"
        )

    # If in Nextflow mode, tell the user to wait until the pipeline is finished in order to have all branch lengths
    if nextflow_flag:
        if missing_ortholog_data_from_database:
            logging.info(
                f"As soon as new ortholog data will become available, the tree branch lengths will be updated"
            )
    # If manual mode, tell the user how to get a complete branch tree (probably they deleted some data in the peak database)
    else:
        if missing_ortholog_data_from_database or missing_ortholog_data_from_correction_table:
            logging.warning(
                f"It's necessary to run a new Nextflow (or manual) pipeline to complete the tree branch length information"
            )

    label_leaves_with_latin_names(tree, latin_names)
    adapt_unknown_branch_length(tree)

    ts = TreeStyle()
    # ts.title.add_face(TextFace("  Input tree with branch length equal to Ks distances  ", ftype="Arial", fsize=18), column=0)
    ts.orientation = 1
    ts.branch_vertical_margin = 14
    ts.show_leaf_name = False  # because there is a Face showing it
    ts.show_branch_length = False
    ts.margin_left = 25
    ts.margin_right = 25
    ts.margin_top = 25
    ts.scale = 200
    #ts.scale_length =  # to set a fixed scale branch length
    root_of_corrected_tree = species_history[-1]
    root_of_corrected_tree.render(os.path.join(
        "rate_adjustment", f"{species}",
        f"{_TREE_BRANCH_DISTANCES.format(species)}"),
                                  w=4.5,
                                  units="in",
                                  tree_style=ts)
Exemplo n.º 6
0
def draw_lifting_tree_inline(filename: str) -> None:

    ts = TreeStyle()
    ts.show_leaf_name = False
    ts.layout_fn = layout_lift
    ts.rotation = 90
    ts.branch_vertical_margin = 10
    ts.show_scale = False
    ts.scale = 50
    ts.title.add_face(TextFace(" ", fsize=20), column=0)

    ts.legend.add_face(TextFace("  "), column=0)
    ts.legend.add_face(TextFace("  "), column=1)
    ts.legend.add_face(TextFace("  "), column=2)
    ts.legend.add_face(TextFace("  "), column=3)

    ts.legend.add_face(TextFace("              "), column=0)
    ts.legend.add_face(TextFace("Node shape and size:"), column=1)
    ts.legend.add_face(TextFace("              "), column=2)
    ts.legend.add_face(TextFace("Node color - membership value:"), column=3)

    ts.legend.add_face(TextFace("  "), column=0)
    ts.legend.add_face(TextFace("  "), column=1)
    ts.legend.add_face(TextFace("  "), column=2)
    ts.legend.add_face(TextFace("  "), column=3)

    ts.legend.add_face(PieChartFace([100], 20, 20, colors=['white'], line_color='black'), column=0)
    ts.legend.add_face(TextFace("  topics that relate to the cluster"), column=1)

    ts.legend.add_face(RectFace(30, 10, "#90ee90", "#90ee90"), column=2)
    ts.legend.add_face(TextFace("  topic with minor membership 0<u(t)<=0.2"), column=3)

    ts.legend.add_face(TextFace("  "), column=0)
    ts.legend.add_face(TextFace("  "), column=1)
    ts.legend.add_face(TextFace("  "), column=2)
    ts.legend.add_face(TextFace("  "), column=3)

    ts.legend.add_face(PieChartFace([100], 40, 40, colors=['white'], line_color='black'), column=0)
    ts.legend.add_face(TextFace("  head subject"), column=1)

    ts.legend.add_face(RectFace(30, 10, "green", "green"), column=2)
    ts.legend.add_face(TextFace(u"  topic with medium membership 0.2<u(t)<=0.4   "), column=3)

    ts.legend.add_face(TextFace("  "), column=0)
    ts.legend.add_face(TextFace("  "), column=1)
    ts.legend.add_face(TextFace("  "), column=2)
    ts.legend.add_face(TextFace("  "), column=3)

    ts.legend.add_face(RectFace(20, 20, "black", "white"), column=0)
    ts.legend.add_face(TextFace("  topics that don't refer to cluster                     "), \
                       column=1)

    ts.legend.add_face(RectFace(30, 10, "#004000", "#004000"), column=2)
    ts.legend.add_face(TextFace("  topic with high membership u(t)>0.4"), column=3)

    ts.legend.add_face(TextFace("  "), column=0)
    ts.legend.add_face(TextFace("  "), column=1)
    ts.legend.add_face(TextFace("  "), column=2)
    ts.legend.add_face(TextFace("  "), column=3)

    ts.legend.add_face(TextFace("  "), column=1)
    ts.legend.add_face(TextFace("  "), column=1)

    ts.legend.add_face(RectFace(30, 10, "red", "red"), column=2)
    ts.legend.add_face(TextFace("  topic with no membership (u(t)=0)"), column=3)

    ts.legend_position = 3

    ete3_desc = read_ete3_from_file(filename)
    tree = Tree(ete3_desc, format=1)

    # tree.show(tree_style=ts)
    return tree.render("%%inline",tree_style=ts)
Exemplo n.º 7
0
def plot_species_tree(tree_newick, tree_type, gene_name, tree_file_name,
                      name_list, tree_image_folder):
    # set tree parameters
    tree = Tree(tree_newick, format=2)
    ts = TreeStyle()
    ts.mode = "r"  # tree model: 'r' for rectangular, 'c' for circular
    ts.show_leaf_name = False
    tree_title = tree_type + ' (' + gene_name + ')'  # define tree title
    # set tree title text parameters
    ts.title.add_face(TextFace(tree_title,
                               fsize=8,
                               fgcolor='black',
                               ftype='Arial',
                               tight_text=False),
                      column=0)  # tree title text setting
    # set layout parameters
    ts.rotation = 0  # from 0 to 360
    ts.show_scale = False
    ts.margin_top = 10  # top tree image margin
    ts.margin_bottom = 10  # bottom tree image margin
    ts.margin_left = 10  # left tree image margin
    ts.margin_right = 10  # right tree image margin
    ts.show_border = False  # set tree image border
    ts.branch_vertical_margin = 3  # 3 pixels between adjancent branches

    # set tree node style
    for each_node in tree.traverse():
        # leaf node parameters
        if each_node.is_leaf():
            ns = NodeStyle()
            ns['shape'] = 'circle'  # dot shape: circle, square or sphere
            ns['size'] = 0  # dot size
            ns['hz_line_width'] = 0.5  # branch line width
            ns['vt_line_width'] = 0.5  # branch line width
            ns['hz_line_type'] = 0  # branch line type: 0 for solid, 1 for dashed, 2 for dotted
            ns['vt_line_type'] = 0  # branch line type
            if each_node.name in name_list:
                ns['fgcolor'] = 'red'  # the dot setting
                each_node.add_face(
                    TextFace(each_node.name,
                             fsize=8,
                             fgcolor='red',
                             tight_text=False,
                             bold=False),
                    column=0,
                    position='branch-right')  # the node name text setting
                each_node.set_style(ns)
            else:
                ns['fgcolor'] = 'blue'  # the dot setting
                each_node.add_face(
                    TextFace(each_node.name,
                             fsize=8,
                             fgcolor='black',
                             tight_text=False,
                             bold=False),
                    column=0,
                    position='branch-right')  # the node name text setting
                each_node.set_style(ns)

        # non-leaf node parameters
        else:
            nlns = NodeStyle()
            nlns['size'] = 0  # dot size
            each_node.add_face(
                TextFace(each_node.name,
                         fsize=4,
                         fgcolor='black',
                         tight_text=False,
                         bold=False),
                column=5,
                position='branch-top')  # non-leaf node name text setting)
            each_node.set_style(nlns)
    # set figures size
    tree.render('%s/%s.png' % (tree_image_folder, tree_file_name),
                w=900,
                units='px',
                tree_style=ts)
Exemplo n.º 8
0
def create_tree(tree, tree_root_key, filepath, patient, phylogeny, drivers=None):
    """
    Takes a networkx tree and creates an ete3 tree and creates a PDF, SVG or PNG according to the given filename at
    the given path
    :param tree: inferred cancer phylogeny encoded as networkx tree
    :param tree_root_key: key of root in networkx tree
    :param filepath: path to output file (excluding file extension)
    :param patient: data structure around patient
    :param phylogeny: inferred cancer phylogeny
    :param drivers: defaultdict with mutation IDs and instance of driver class to be highlighted on edges
    :return path to the generated ete3 PNG file
    """

    # generate ete3 tree
    rt = Tree()   # generate root
    rt.name = 'Germline {}'.format(patient.name)
    _generate_ete_tree(tree, tree_root_key, rt, 0, patient, phylogeny,
                       gene_names=patient.gene_names, drivers=drivers)

    ts = TreeStyle()
    ts.layout_fn = ete_layout
    ts.show_leaf_name = False
    ts.show_branch_length = False
    ts.show_branch_support = False
    ts.show_scale = False
    ts.extra_branch_line_color = 'Black'
    # ts.complete_branch_lines_when_necessary = False

    # Find the maximal number of variants present in a single sample to determine the optimal ETE3 scale level
    max_muts = max(no_pres_vars for no_pres_vars in patient.no_present_vars.values())

    def round_to_1(x):
        """
        Round to one significant digit
        :param x:
        :return:
        """
        return round(x, -int(math.floor(math.log10(abs(x)))))

    # XX pixels per branch length unit
    # if max_muts > 20000:
    #     ts.scale = 0.02
    # elif max_muts > 10000:
    #     ts.scale = 0.05
    # elif max_muts > 5000:
    #     ts.scale = 0.1
    # elif max_muts > 2000:
    #     ts.scale = 0.2
    # elif max_muts > 1000:
    #     ts.scale = 0.5
    # elif max_muts > 500:
    #     ts.scale = 1
    # elif max_muts > 200:
    #     ts.scale = 2
    # elif max_muts > 100:
    #     ts.scale = 5
    # elif max_muts > 50:
    #     ts.scale = 5
    # else:
    #     ts.scale = 10

    ts.scale = round_to_1(600 / max_muts)

    ts.branch_vertical_margin = 15  # XX pixels between adjacent branches

    # rt.show(tree_style=ts)

    ete_tree_plot = filepath+'.png'
    rt.render(ete_tree_plot, w=183, units="mm", tree_style=ts, dpi=300)
    rt.render(filepath+'.pdf', w=183, units="mm", tree_style=ts, dpi=300)

    return ete_tree_plot
Exemplo n.º 9
0
def deepbiome_draw_phylogenetic_tree(
        log,
        network_info,
        path_info,
        num_classes,
        file_name="%%inline",
        img_w=500,
        branch_vertical_margin=20,
        arc_start=0,
        arc_span=360,
        node_name_on=True,
        name_fsize=10,
        tree_weight_on=True,
        tree_weight=None,
        tree_level_list=['Genus', 'Family', 'Order', 'Class', 'Phylum'],
        weight_opacity=0.4,
        weight_max_radios=10,
        phylum_background_color_on=True,
        phylum_color=[],
        phylum_color_legend=False,
        show_covariates=True,
        verbose=True):
    """
    Draw phylogenetic tree

    Parameters
    ----------
    log (logging instance) :
        python logging instance for logging
    network_info (dictionary) :
        python dictionary with network_information
    path_info (dictionary):
        python dictionary with path_information
    num_classes (int):
        number of classes for the network. 0 for regression, 1 for binary classificatin.
    file_name (str):
        name of the figure for save.
        - "*.png", "*.jpg"
        - "%%inline" for notebook inline output.
        default="%%inline"
    img_w (int):
        image width (pt)
        default=500
    branch_vertical_margin (int):
        vertical margin for branch
        default=20
    arc_start (int):
        angle that arc start
        default=0
    arc_span (int):
        total amount of angle for the arc span
        default=360
    node_name_on (boolean):
        show the name of the last leaf node if True
        default=False
    name_fsize (int):
        font size for the name of the last leaf node
        default=10
    tree_weight_on (boolean):
        show the amount and the direction of the weight for each edge in the tree by circle size and color.
        default=True
    tree_weight (ndarray):
        reference tree weights
        default=None
    tree_level_list (list):
        name of each level of the given reference tree weights
        default=['Genus', 'Family', 'Order', 'Class', 'Phylum']
    weight_opacity  (float):
        opacity for weight circle
        default= 0.4
    weight_max_radios (int):
        maximum radios for weight circle
        default= 10
    phylum_background_color_on (boolean):
        show the background color for each phylum based on `phylumn_color`.
        default= True
    phylum_color (list):
        specify the list of background colors for phylum level. If `phylumn_color` is empty, it will arbitrarily assign the color for each phylum.
        default= []
    phylum_color_legend (boolean):
        show the legend for the background colors for phylum level
        default= False
    show_covariates (boolean):
        show the effect of the covariates
        default= True
    verbose (boolean):
        show the log if True
        default=True
    Returns
    -------
    
    Examples
    --------
    Draw phylogenetic tree
    
    deepbiome_draw_phylogenetic_tree(log, network_info, path_info, num_classes, file_name = "%%inline")
    """

    os.environ[
        'QT_QPA_PLATFORM'] = 'offscreen'  # for tree figure (https://github.com/etetoolkit/ete/issues/381)
    reader_class = getattr(readers,
                           network_info['model_info']['reader_class'].strip())
    reader = reader_class(log, path_info, verbose=verbose)
    data_path = path_info['data_info']['data_path']
    try:
        count_path = path_info['data_info']['count_path']
        x_list = np.array(
            pd.read_csv(path_info['data_info']['count_list_path'],
                        header=None).iloc[:, 0])
        x_path = np.array([
            '%s/%s' % (count_path, x_list[fold])
            for fold in range(x_list.shape[0]) if '.csv' in x_list[fold]
        ])
    except:
        x_path = np.array([
            '%s/%s' % (data_path, path_info['data_info']['x_path'])
            for fold in range(1)
        ])

    reader.read_dataset(x_path[0], None, 0)

    network_class = getattr(
        build_network, network_info['model_info']['network_class'].strip())
    network = network_class(network_info,
                            path_info,
                            log,
                            fold=0,
                            num_classes=num_classes,
                            tree_level_list=tree_level_list,
                            is_covariates=reader.is_covariates,
                            covariate_names=reader.covariate_names,
                            verbose=False)

    if len(phylum_color) == 0:
        colors = mcolors.CSS4_COLORS
        colors_name = list(colors.keys())
        if reader.is_covariates and show_covariates:
            phylum_color = np.random.choice(
                colors_name,
                network.phylogenetic_tree_info['Phylum_with_covariates'].
                unique().shape[0])
        else:
            phylum_color = np.random.choice(
                colors_name,
                network.phylogenetic_tree_info['Phylum'].unique().shape[0])

    basic_st = NodeStyle()
    basic_st['size'] = weight_max_radios * 0.5
    basic_st['shape'] = 'circle'
    basic_st['fgcolor'] = 'black'

    t = Tree()
    root_st = NodeStyle()
    root_st["size"] = 0
    t.set_style(root_st)

    tree_node_dict = {}
    tree_node_dict['root'] = t

    upper_class = 'root'
    lower_class = tree_level_list[-1]
    lower_layer_names = tree_weight[-1].columns.to_list()

    layer_tree_node_dict = {}
    phylum_color_dict = {}
    for j, val in enumerate(lower_layer_names):
        t.add_child(name=val)
        leaf_t = t.get_leaves_by_name(name=val)[0]
        leaf_t.set_style(basic_st)
        layer_tree_node_dict[val] = leaf_t
        if lower_class == 'Phylum' and phylum_background_color_on:
            phylum_st = copy.deepcopy(basic_st)
            phylum_st["bgcolor"] = phylum_color[j]
            phylum_color_dict[val] = phylum_color[j]
            leaf_t.set_style(phylum_st)
    tree_node_dict[lower_class] = layer_tree_node_dict
    upper_class = lower_class
    upper_layer_names = lower_layer_names

    for i in range(len(tree_level_list) - 1):
        lower_class = tree_level_list[-2 - i]
        if upper_class == 'Disease' and show_covariates == False:
            lower_layer_names = network.phylogenetic_tree_info[
                lower_class].unique()
        else:
            lower_layer_names = tree_weight[-i - 1].index.to_list()

        layer_tree_node_dict = {}
        for j, val in enumerate(upper_layer_names):
            parient_t = tree_node_dict[upper_class][val]
            if upper_class == 'Disease':
                child_class = lower_layer_names
            else:
                child_class = network.phylogenetic_tree_info[lower_class][
                    network.phylogenetic_tree_info[upper_class] ==
                    val].unique()

            for k, child_val in enumerate(child_class):
                parient_t.add_child(name=child_val)
                leaf_t = parient_t.get_leaves_by_name(name=child_val)[0]
                if lower_class == 'Phylum' and phylum_background_color_on:
                    phylum_st = copy.deepcopy(basic_st)
                    phylum_st["bgcolor"] = phylum_color[k]
                    phylum_color_dict[child_val] = phylum_color[k]
                    leaf_t.set_style(phylum_st)
                else:
                    leaf_t.set_style(basic_st)
                if tree_weight_on:
                    edge_weights = np.array(tree_weight[-1 - i])
                    edge_weights *= (weight_max_radios / np.max(edge_weights))
                    if upper_class == 'Disease':
                        upper_num = 0
                    else:
                        upper_num = network.phylogenetic_tree_dict[
                            upper_class][val]
                    if upper_class == 'Disease' and reader.is_covariates == True and show_covariates:
                        lower_num = network.phylogenetic_tree_dict[
                            '%s_with_covariates' % lower_class][child_val]
                    else:
                        lower_num = network.phylogenetic_tree_dict[
                            lower_class][child_val]
                    leaf_t.add_features(weight=edge_weights[lower_num,
                                                            upper_num])
                layer_tree_node_dict[child_val] = leaf_t
        tree_node_dict[lower_class] = layer_tree_node_dict
        upper_class = lower_class
        upper_layer_names = lower_layer_names

    def layout(node):
        if "weight" in node.features:
            # Creates a sphere face whose size is proportional to node's
            # feature "weight"
            color = {1: "RoyalBlue", 0: "Red"}[int(node.weight > 0)]
            C = CircleFace(radius=node.weight, color=color, style="circle")
            # Let's make the sphere transparent
            C.opacity = weight_opacity
            # And place as a float face over the tree
            faces.add_face_to_node(C, node, 0, position="float")

        if node_name_on & node.is_leaf():
            # Add node name to laef nodes
            N = AttrFace("name", fsize=name_fsize, fgcolor="black")
            faces.add_face_to_node(N, node, 0)

    ts = TreeStyle()

    ts.show_leaf_name = False
    ts.mode = "c"
    ts.arc_start = arc_start
    ts.arc_span = arc_span
    ts.layout_fn = layout
    ts.branch_vertical_margin = branch_vertical_margin
    ts.show_scale = False

    if phylum_color_legend:
        for phylum_name in np.sort(list(phylum_color_dict.keys())):
            color_name = phylum_color_dict[phylum_name]
            ts.legend.add_face(CircleFace(weight_max_radios * 1, color_name),
                               column=0)
            ts.legend.add_face(TextFace(" %s" % phylum_name, fsize=name_fsize),
                               column=1)

    return t.render(file_name=file_name, w=img_w, tree_style=ts)


# #########################################################################################################################
# if __name__ == "__main__":
#     argdict = argv_parse(sys.argv)
#     try: gpu_memory_fraction = float(argdict['gpu_memory_fraction'][0])
#     except: gpu_memory_fraction = None
#     try: max_queue_size=int(argdict['max_queue_size'][0])
#     except: max_queue_size=10
#     try: workers=int(argdict['workers'][0])
#     except: workers=1
#     try: use_multiprocessing=argdict['use_multiprocessing'][0]=='True'
#     except: use_multiprocessing=False

#     ### Logger ############################################################################################
#     logger = logging_daily.logging_daily(argdict['log_info'][0])
#     logger.reset_logging()
#     log = logger.get_logging()
#     log.setLevel(logging_daily.logging.INFO)

#     log.info('Argument input')
#     for argname, arg in argdict.items():
#         log.info('    {}:{}'.format(argname,arg))

#     ### Configuration #####################################################################################
#     config_data = configuration.Configurator(argdict['path_info'][0], log)
#     config_data.set_config_map(config_data.get_section_map())
#     config_data.print_config_map()

#     config_network = configuration.Configurator(argdict['network_info'][0], log)
#     config_network.set_config_map(config_network.get_section_map())
#     config_network.print_config_map()

#     path_info = config_data.get_config_map()
#     network_info = config_network.get_config_map()
#     test_evaluation, train_evaluation, network = deepbiome_train(log, network_info, path_info, number_of_fold=20)
Exemplo n.º 10
0
    def show_dom_MSA_tree(self):
        '''
        Plot the gene tree with a MSA+domains aligned next to it,
        or alternatively dump the tree to a file (if self.out was defined)
        '''
        if not self.tree_path:
            # if no tree was specified, use a "star-shaped" newick tree
            # as a substitute (all species have the same relatedness)
            newick_star = '({});'.format(','.join(sorted(
                self.get_gene_list())))
            t = Tree(newick_star)
            ts = TreeStyle()
            ts.show_branch_length = False
            ts.show_branch_support = False
            ts.branch_vertical_margin = 10
            ts.scale = 0  # 0 pixels per branch length unit
            # make the tree disappear:
            nstyle = NodeStyle()
            nstyle['size'] = 0
            nstyle['hz_line_color'] = 'white'
            nstyle['vt_line_color'] = 'white'
            for n in t.traverse():
                n.set_style(nstyle)
        else:
            t = Tree(self.tree_path)
            ts = TreeStyle()
            ts.show_branch_length = True
            ts.show_branch_support = True
            ts.scale = 120  # 120 pixels per branch length unit
        ts.show_leaf_name = True
        ts.show_scale = False

        if self.outgroup_node_for_rooting:
            assert self.tree_path, ('Rooting doesn\'t make sense if '
                                    'you don\'t specify a tree file!')
            self.root_tree(t)

        if self.msa_path:
            self.add_background_color_to_nodes(t)
            self.add_msa_with_domains_to_tree(t)
        else:
            self.add_background_color_to_nodes(t)
            self.add_domains_to_tree(t)

        if self.hide_nodes:
            for word2hide in self.hide_nodes:
                nodes_hidden = []
                print(
                    '\nRemoving these terminal nodes because they contain the '
                    'word "{}":'.format(word2hide))
                for leaf in t.traverse():
                    if word2hide in leaf.name:
                        nodes_hidden.append(leaf.name)
                        leaf.delete()
                print(', '.join(nodes_hidden))

        if self.out:
            file_ext = os.path.splitext(self.out.upper())[1]
            if file_ext not in ('.SVG', '.PDF', '.PNG'):
                raise Exception('Output image file name must end with '
                                '".SVG", ".PDF" or ".PNG".')
            t.render(self.out, tree_style=ts)
            print('Wrote output image to', self.out)
        else:
            t.show(tree_style=ts)
        return
Exemplo n.º 11
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()
    tree._faces = _FaceAreas()
    for node in tree.get_descendants():
        node.set_style(style0)
        node.img_style["size"] = 0
        node._faces = _FaceAreas()
        #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)
Exemplo n.º 12
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()
    tree._faces = _FaceAreas()
    for node in tree.get_descendants():
        node.set_style(style0)
        node.img_style["size"] = 0
        node._faces = _FaceAreas()
        #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)
Exemplo n.º 13
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)
Exemplo n.º 14
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)
Exemplo n.º 15
0
            current_color %= len(colors)
            style = NodeStyle()
            style['vt_line_color'] = colors[current_color]
            style['hz_line_color'] = colors[current_color]
            style['size'] = 0
            style['fgcolor'] = '#000000'
            style["vt_line_width"] = 2
            style["hz_line_width"] = 2
            for gg in g.traverse():
                if not gg.coloured:
                    gg.set_style(style)
                    gg.coloured = True
            source_style = NodeStyle(style)
            source_style['size'] = 5
            source_style['fgcolor'] = '#C01000'
            g.set_style(source_style)

    tstyle = TreeStyle()
    #tstyle.show_leaf_name = False
    tstyle.scale = 28
    tstyle.branch_vertical_margin = 6
    tstyle.show_branch_length = False

    # tstyle.show_branch_support = True
    tstyle.show_scale = False

    if ultrametric == 1:
        gene_tree.convert_to_ultrametric()
    gene_tree.show(tree_style=tstyle)
    # species_tree.show()
Exemplo n.º 16
0
circular_style.show_branch_length = True
circular_style.show_branch_support = True
t.render("mytree.png", tree_style=circular_style)


nstyle = NodeStyle()
nstyle["hz_line_width"] = 3
nstyle["vt_line_width"] = 3

# Applies the same static style to all nodes in the tree. Note that,
# if "nstyle" is modified, changes will affect to all nodes
for n in t.traverse():
   n.set_style(nstyle)



ts = TreeStyle()
ts.branch_vertical_margin = 10
ts.show_leaf_name = True
ts.rotation = 90
ts.scale=100
t.render("tree_test100.png",tree_style=ts)

ts.scale=1000
t.render("tree_test1000.png",tree_style=ts)
## compare to

#t = Tree( '("[a,b]",c);' )
#t.show()

# []
Exemplo n.º 17
0
def get_example_tree():

    t = PhyloTree('partition.nxs.treefile')

    # delete nodes supported less than 70% in Bootstrap
    #for node in t.get_descendants():
    #if not node.is_leaf() and node.support <= 90:
    #node.delete()
    #set size to 0

    #list_remove = []

    #for node in t.get_descendants():
    #if node.dist>0:

    #list_remove.append(node.name)

    #str_list = [x for x in list_remove if x]

    #t.prune(str_list)

    #style2 = NodeStyle()
    #style2["fgcolor"] = "#000000"
    #style2["shape"] = "circle"
    #style2["size"]=0
    #for l in t.iter_leaves():
    #l.img_style = style2

    #set the box around the commmon ancestor

    #nst1 = NodeStyle()
    #nst1["bgcolor"] = "LightSteelBlue"
    #nst2 = NodeStyle()
    #nst2["bgcolor"] = "SteelBlue"
    #nst3 =NodeStyle()
    #nst3["bgcolor"] = "PaleTurquoise"
    #nst4 = NodeStyle()
    #nst4["bgcolor"] = "PowderBlue"

    #n1 = t.get_common_ancestor("CMW50197_Raffaelea_kentii_sp._nov.", "CMW49902_Raffaelea_kentii_sp._nov.")
    #n1.set_style(nst1)

    #n2 = t.get_common_ancestor("Raffaelea_sulphurea", "Raffaelea_quercivora")
    #n2.set_style(nst2)

    #n3 = t.get_common_ancestor("Raffaelea_cyclorhipidia", "Raffaelea_subalba")
    #n3.set_style(nst3)

    #n4 = t.get_common_ancestor("Raffaelea_lauricola", "Raffaelea_brunnea")
    #n4.set_style(nst4)

    # Set the path in which images are located
    #img_path = "./"
    #Raff_Ai = faces.ImgFace(img_path + "Fig2_CMW49901.png")

    # Specify the boundaries of the Image, how big do you want it
    # Raff_Ai.width = 200
    # Raff_Ai.height = 200
    # How much to the left do you want the Image
    # Raff_Ai.margin_left = 200

    ts = TreeStyle()
    ts.show_branch_length = False  # show branch length
    ts.show_branch_support = True  # show support
    ts.show_leaf_name = False
    ts.branch_vertical_margin = 1  # 1 pixels between adjacent branches
    ts.scale = 2000  # 2000 pixels per branch length unit
    ts.layout_fn = layout

    return t, ts