def median(self) -> ReconciliationWrapper: """ Return one of the best ReconciliationWrapper that best represents the reconciliation graph. The function internally uses random and is not deterministic. """ postorder_parasite_tree, parasite_tree_root, _ = diameter.reformat_tree( self.recon_input.parasite_dict, "pTop") postorder_host_tree, _, _ = diameter.reformat_tree( self.recon_input.host_dict, "hTop") # Compute the median reconciliation graph median_reconciliation, n_meds, roots_for_median = median.get_median_graph( self.recongraph, postorder_parasite_tree, postorder_host_tree, parasite_tree_root, self.roots) med_counts_dict = median.get_med_counts(median_reconciliation, roots_for_median) random_median = median.choose_random_median_wrapper( median_reconciliation, roots_for_median, med_counts_dict) median_root = _find_roots(random_median)[0] return ReconciliationWrapper(random_median, median_root, self.recon_input, self.dup_cost, self.trans_cost, self.loss_cost, self.total_cost, self.event_frequencies, self.node_frequencies)
def calc_histogram(tree_data, d, t, l, time_it, normalize=False, zero_loss=False): """ Compute the PDV from a .newick file :param tree_data <_ReconInput> - Output of newickFormatReader.getInput() :param d <float> - the cost of a duplication :param t <float> - ^^ transfer :param l <float> - ^^ loss :param time_it <bool> - collect timing info :param normalize <bool> - normalize the histogram by the size of the gene tree :param zero_loss <bool> - ignore loss events :return diameter_alg_hist <Histogram> - the PDV for the given .newick :return elapsed <float> - the time it took to compute the PDV None if time_it is False """ # From the newick tree create the reconciliation graph edge_species_tree, edge_gene_tree, dtl_recon_graph, mpr_count, best_roots \ = recongraph_tools.reconcile(tree_data, d, t, l) # If we want to know the number of MPRs #print(mpr_count) # Reformat the host and parasite tree to use it with the histogram algorithm gene_tree, gene_tree_root, gene_node_count = diameter.reformat_tree( edge_gene_tree, "pTop") species_tree, species_tree_root, species_node_count \ = diameter.reformat_tree(edge_species_tree, "hTop") if time_it: start = time.time() # Calculate the histogram via histogram algorithm diameter_alg_hist = histogram_alg.diameter_algorithm( species_tree, gene_tree, gene_tree_root, dtl_recon_graph, dtl_recon_graph, False, zero_loss) if time_it: end = time.time() elapsed = end - start else: elapsed = None if normalize: # Number of internal gene tree nodes gene_tree_nodes = int(math.ceil(len(gene_tree) / 2.0)) diameter_alg_hist = diameter_alg_hist.xscale(1.0 / (2 * gene_tree_nodes)) return diameter_alg_hist, elapsed
def draw_on(self, axes: plt.Axes): """ Draw Pairwise Distance Histogram on axes """ # Reformat the host and parasite tree to use it with the histogram algorithm parasite_tree, parasite_tree_root, parasite_node_count = diameter.reformat_tree( self.recon_input.parasite_dict, "pTop") host_tree, host_tree_root, host_node_count \ = diameter.reformat_tree(self.recon_input.host_dict, "hTop") hist = histogram_alg.diameter_algorithm(host_tree, parasite_tree, parasite_tree_root, self.recongraph, self.recongraph, False, False) histogram_display.plot_histogram_to_ax(axes, hist.histogram_dict)
def set_event_frequencies(self): """ Set self.event_scores, event_scores is a dictionary that maps events nodes to their frequencies in all the optimal reconciliations indicated by the recongraph """ postorder_parasite_tree, parasite_tree_root, _ = diameter.reformat_tree( self.recon_input.parasite_dict, "pTop") postorder_host_tree, _, _ = diameter.reformat_tree( self.recon_input.host_dict, "hTop") postorder_mapping_node_list = median.mapping_node_sort( postorder_parasite_tree, postorder_host_tree, list(self.recongraph.keys())) event_scores = median.generate_scores( postorder_mapping_node_list[::-1], self.recongraph, parasite_tree_root)[0] self.event_scores = event_scores
def get_tree_info(newick, d, t, l): """ Reconcile the trees and return all the relevant info. :param newick <_ReconInput>: Output of newickFormatReader.getInput() :params d,t,l <float> - the relative DTL costs :return gene_tree <tree> :return species_tree <tree> :return gene_root <node> :return dtl_recon_graph <recon_graph> :return mpr_count <int> - the number of MPRs for the recon graph :return best_roots [<mapping_node>] - the sources of the recon graph """ # From the newick tree create the reconciliation graph edge_species_tree, edge_gene_tree, dtl_recon_graph, mpr_count, best_roots \ = recongraph_tools.reconcile(newick, d, t, l) # Reformat the host and parasite tree to use it with the histogram algorithm gene_tree, gene_root, gene_node_count = diameter.reformat_tree( edge_gene_tree, "pTop") species_tree, species_tree_root, species_node_count \ = diameter.reformat_tree(edge_species_tree, "hTop") return gene_tree, species_tree, gene_root, dtl_recon_graph, mpr_count, best_roots