def render(host_dict: dict, parasite_dict: dict, recon_dict: dict, event_scores: Dict[tuple, float] = None, show_internal_labels=False, show_freq=False, axes: Union[plt.Axes, None] = None): """ Renders a reconciliation using matplotlib :param host_dict: Host tree represented in dictionary format :param parasite_dict: Parasite tree represented in dictionary format :param recon_dict: Reconciliation represented in dictionary format :param axes: If specified, draw on the axes instead of creating a new figure """ # convert host and parasite dicts to objects and populate nodes with their temporal order host_tree, parasite_tree, consistency_type = utils.build_trees_with_temporal_order( host_dict, parasite_dict, recon_dict) recon = utils.dict_to_reconciliation(recon_dict, event_scores) fig = plot_tools.FigureWrapper(consistency_type, axes) if consistency_type != utils.ConsistencyType.NO_CONSISTENCY: render_host(fig, host_tree, show_internal_labels) host_lookup = host_tree.name_to_node_dict() render_parasite(fig, parasite_tree, recon, host_lookup, show_internal_labels, show_freq) return fig
def render(host_dict: dict, parasite_dict: dict, tip_mapping: dict, show_internal_labels: bool, ax: plt.Axes = None) \ -> plot_tools.FigureWrapper: """ Render tanglegram :param host_dict - host tree (dictionary representation) :param parasite_dict - parasite tree (dictionary representation) :param tip_mapping - tip mapping dictionary :param show_internal_labels - boolean indicator of whether internal node names should be displayed :param ax - draw on Axes instead if available :return FigureWrapper object """ global _g_host_counter global _g_parasite_counter _g_host_counter = 0 _g_parasite_counter = 0 fig = plot_tools.FigureWrapper("Host | Parasite", axes=ax) host_tree = utils.dict_to_tree(host_dict, tree.TreeType.HOST) parasite_tree = utils.dict_to_tree(parasite_dict, tree.TreeType.PARASITE) _render_helper_host(fig, host_tree.root_node, show_internal_labels) _render_helper_parasite(fig, parasite_tree.root_node, show_internal_labels) host_dict = {} for host in host_tree.leaf_list(): host_dict[host.name] = host # connect hosts leaves to parasite leaves for leaf in parasite_tree.leaf_list(): parasite = leaf host = host_dict[tip_mapping[leaf.name]] fig.line((host.layout.col, host.layout.row), (parasite.layout.col, parasite.layout.row), col=render_settings.GRAY, linestyle='--') return fig
def render( host_dict: dict, parasite_dict: dict, recon_dict: dict, event_frequencies: Dict[tuple, float] = None, show_internal_labels: bool = False, show_freq: bool = False, show_legend: bool = False, node_font_size: float = 0.3, axes: Union[plt.Axes, None] = None, ): """ Renders a reconciliation using matplotlib :param host_dict: Host tree represented in dictionary format :param parasite_dict: Parasite tree represented in dictionary format :param recon_dict: Reconciliation represented in dictionary format :param event_frequencies: Dictionary that maps event tuples to their frequencies :param show_internal_labels: Boolean that determines wheter internal labels are shown or not :param show_freq: Boolean that determines whether event frequencies are shown or not :param axes: If specified, draw on the axes instead of creating a new figure :return Figure Object """ host_tree, parasite_tree, consistency_type = utils.build_trees_with_temporal_order( host_dict, parasite_dict, recon_dict) recon_obj = utils.dict_to_reconciliation(recon_dict, event_frequencies) # Checks to see if the trees(or reconciliation) are empty if host_tree is None or parasite_tree is None or recon_obj is None: return None fig = plot_tools.FigureWrapper(render_settings.TREE_TITLE, axes) if show_legend: _create_legend(fig, consistency_type) font_size = node_font_size root = parasite_tree.root_node host_lookup = host_tree.name_to_node_dict() parasite_lookup = parasite_tree.name_to_node_dict() # Populate Host Nodes with track count _populate_host_tracks(root, recon_obj, host_lookup) # Render Host Tree _render_host(fig, host_tree, show_internal_labels, font_size) # Sets the offsets between tracks on each host node _set_offsets(host_tree) # Determine the length of the longest string in the host tree's leaf list longest_host_name = max([len(leaf.name) for leaf in host_tree.leaf_list()]) # Render Parasite Tree _render_parasite(fig, parasite_tree, recon_obj, host_lookup, parasite_lookup, show_internal_labels, show_freq, font_size, longest_host_name) return fig