def solve_pathway(self, subs, prod, html_writer, pathway_name, pathway_prefix, max_levels=4, stop_after_first_solution=False): G_subs = compound2graph(subs) G_prod = compound2graph(prod) if (self.ignore_chirality): G_subs.reset_chiralities() G_prod.reset_chiralities() print >> self.outstream, "*** Starting: %s -> %s" % (subs, prod) html_writer.write(" <li>%s = %s" % (subs, prod)) (original_compound_map, possible_pathways, min_length) = self.find_shortest_pathway([G_subs], [G_prod], max_levels, stop_after_first_solution) if (possible_pathways != []): scene_list = self.get_all_possible_scenes(original_compound_map, possible_pathways) min_cost = min([m[0] for m in scene_list]) min_cost_string = " (minimal cost = %s)" % str(min_cost) sub_html_writer = html_writer.branch(pathway_name + "/" + pathway_prefix, min_cost_string) # add to the sub HTML only the pathways that achieve the minimal cost pathway_counter = 0 for (cost, pathway_scene) in scene_list: if (cost == min_cost): sub_html_writer.write_svg(pathway_scene, pathway_prefix + "/pathway_%d" % pathway_counter) sub_html_writer.write("<br><br>") pathway_counter += 1 print >> self.outstream, "*** Success: %s -> %s," % (subs, prod), print >> self.outstream, "found %d pathways with minimal cost of %s" % (pathway_counter, str(min_cost)) else: html_writer.write(" (minimal path-length > %d)" % max_levels) print >> self.outstream, "*** Failure: %s -> %s, after %d steps" % (subs, prod, max_levels) html_writer.write("</li>\n") print >> self.outstream, "-"*80
def find_shortest_pathways(self, substrate, product, max_levels, stop_after_first_solution=True): G_subs = compound2graph(substrate) G_prod = compound2graph(product) return self.pathfinder.find_shortest_pathway( [G_subs], [G_prod], max_levels=max_levels, stop_after_first_solution=stop_after_first_solution)
def solve_pathway(self, subs, prod, html_writer, pathway_name, pathway_prefix, max_levels=4, stop_after_first_solution=False): G_subs = compound2graph(subs) G_prod = compound2graph(prod) if (self.ignore_chirality): G_subs.reset_chiralities() G_prod.reset_chiralities() print >> self.outstream, "*** Starting: %s -> %s" % (subs, prod) html_writer.write(" <li>%s = %s" % (subs, prod)) (original_compound_map, possible_pathways, min_length) = self.find_shortest_pathway([G_subs], [G_prod], max_levels, stop_after_first_solution) if (possible_pathways != []): scene_list = self.get_all_possible_scenes(original_compound_map, possible_pathways) min_cost = min([m[0] for m in scene_list]) min_cost_string = " (minimal cost = %s)" % str(min_cost) sub_html_writer = html_writer.branch( pathway_name + "/" + pathway_prefix, min_cost_string) # add to the sub HTML only the pathways that achieve the minimal cost pathway_counter = 0 for (cost, pathway_scene) in scene_list: if (cost == min_cost): sub_html_writer.write_svg( pathway_scene, pathway_prefix + "/pathway_%d" % pathway_counter) sub_html_writer.write("<br><br>") pathway_counter += 1 print >> self.outstream, "*** Success: %s -> %s," % (subs, prod), print >> self.outstream, "found %d pathways with minimal cost of %s" % ( pathway_counter, str(min_cost)) else: html_writer.write(" (minimal path-length > %d)" % max_levels) print >> self.outstream, "*** Failure: %s -> %s, after %d steps" % ( subs, prod, max_levels) html_writer.write("</li>\n") print >> self.outstream, "-" * 80
def balance_reaction(self, substrate, product): """ Balances the reaction (by counting atoms) """ atom_gap = compound2graph(substrate).node_bag() - compounds2graph(product).node_bag() extra_bag = bag.Bag() extra_bag['CO2'] = atom_gap['C'] extra_bag['H2O'] = atom_gap['O'] + atom_gap['N'] - 2 * atom_gap['C'] extra_bag['PO3'] = atom_gap['PO3'] for (atom, count) in atom_gap.itercounts(): if (not atom in ['C', 'O', 'N', 'PO3'] and count != 0): raise Exception("cannot balance the number of '%s' atoms, between %s and %s" % (atom, substrate, product)) for (metabolite, count) in extra_bag.itercounts(): if (count > 0): product += (" + " + metabolite) * count if (count < 0): substrate += (" + " + metabolite) * (-count) return (substrate, product)
def test(self, comp=None): util._mkdir('../results') util._mkdir('../results/svg') if (comp == None): html = html_writer.HtmlWriter("../results/reaction_templates.html") counter = 0 for rid in self.forward_reaction_list: html.write(self.get_reaction_info(rid)) html.write_svg(self.template2svg(rid), "svg/reaction_template_%d" % counter) counter += 1 html.display() else: G = compound2graph(comp) html = html_writer.HtmlWriter("../results/reaction_example.html") html.write("<h1>Reactions for compound: " + comp + "</h1>") html.write("<p>") html.write_svg(G.svg(), "svg/compound") html.write("</p>") counter = 0 products_set = set() print "Products for " + comp + " : " for (G_new, rid, mapping) in self.apply_all_reactions(G, backward=False): products_set.add(G_new.hash(ignore_chirality=self.ignore_chirality)) print rid + ": " + graph2compound(G_new, ignore_chirality=False) html.write(self.get_reaction_info(rid, mapping)) html.write_svg(self.template2svg(rid), "svg/reaction_template_%d" % counter) html.write("</br>") html.write_svg(self.reaction2svg(G, G_new, rid), "svg/product_%d" % counter) counter += 1 product_file = open("../results/products.txt", 'w') product_file.write("\n".join(products_set)) product_file.close() html.display() return
def test(self, comp=None): util._mkdir("../results") util._mkdir("../results/svg") if comp == None: html = html_writer.HtmlWriter("../results/reaction_templates.html") counter = 0 for rid in self.forward_reaction_list: html.write(self.get_reaction_info(rid)) html.write_svg(self.template2svg(rid), "svg/reaction_template_%d" % counter) counter += 1 html.display() else: G = compound2graph(comp) html = html_writer.HtmlWriter("../results/reaction_example.html") html.write("<h1>Reactions for compound: " + comp + "</h1>") html.write("<p>") html.write_svg(G.svg(), "svg/compound") html.write("</p>") counter = 0 products_set = set() print "Products for " + comp + " : " for (G_new, rid, mapping) in self.apply_all_reactions(G, backward=False): products_set.add(G_new.hash(ignore_chirality=self.ignore_chirality)) print rid + ": " + graph2compound(G_new, ignore_chirality=False) html.write(self.get_reaction_info(rid, mapping)) html.write_svg(self.template2svg(rid), "svg/reaction_template_%d" % counter) html.write("</br>") html.write_svg(self.reaction2svg(G, G_new, rid), "svg/product_%d" % counter) counter += 1 product_file = open("../results/products.txt", "w") product_file.write("\n".join(products_set)) product_file.close() html.display() return
def balance_reaction(self, substrate, product): """ Balances the reaction (by counting atoms) """ atom_gap = compound2graph(substrate).node_bag() - compounds2graph( product).node_bag() extra_bag = bag.Bag() extra_bag['CO2'] = atom_gap['C'] extra_bag['H2O'] = atom_gap['O'] + atom_gap['N'] - 2 * atom_gap['C'] extra_bag['PO3'] = atom_gap['PO3'] for (atom, count) in atom_gap.itercounts(): if (not atom in ['C', 'O', 'N', 'PO3'] and count != 0): raise Exception( "cannot balance the number of '%s' atoms, between %s and %s" % (atom, substrate, product)) for (metabolite, count) in extra_bag.itercounts(): if (count > 0): product += (" + " + metabolite) * count if (count < 0): substrate += (" + " + metabolite) * (-count) return (substrate, product)
max_depth = 2 reaction_database_fname = "../rec/reaction_templates.dat" # the substrate and product must be listed in '../metacyc/mcard_sdf_extra.txt' # which uses the MOL format for describing molecules. # each one of them can also be a sum of more than one compound (i.e. ribitol + CO2) substrate, product = 'D-ribulose-5P', 'L-ribulose-5P' pathfinder = PathFinder(carbon_only=True, pruning_method=None, ignore_chirality=ignore_chirality, use_antimotifs=True, reaction_database_fname=reaction_database_fname) # This loop tries to find all the paths with lengths less than 'max_depth' for depth in xrange(1, max_depth+1): sys.stderr.write(substrate + " <=> " + product + ", depth = %d ... " % depth) G_subs = compound2graph(substrate) G_prod = compound2graph(product) (original_compound_map, possible_paths, D) = pathfinder.find_shortest_pathway([G_subs], [G_prod], max_levels=depth) for (substrate_pathways, product_pathways, h_bridge) in possible_paths: # the results are given as 3-tuples, characterized by the compound where the two ends # have met (denoted h_bridge). 'substrate_pathways' is a list of pathways leading from 'substrate' to 'h_bridge'. # 'product_pathways' is a list of pathways leading from 'h_bridge' to 'product'. for subs_path in substrate_pathways: G_subs2 = original_compound_map[subs_path[0]] subs_reaction_list = pathfinder.expand_rid_list(subs_path[1:]) (subs_log, G_last_subs) = pathfinder.pathway2text(G_subs2.clone(), subs_reaction_list) for prod_path in product_pathways: G_prod2 = original_compound_map[prod_path[0]]
# the substrate and product must be listed in '../metacyc/mcard_sdf_extra.txt' # which uses the MOL format for describing molecules. # each one of them can also be a sum of more than one compound (i.e. ribitol + CO2) substrate, product = 'D-ribulose-5P', 'L-ribulose-5P' pathfinder = PathFinder(carbon_only=True, pruning_method=None, ignore_chirality=ignore_chirality, use_antimotifs=True, reaction_database_fname=reaction_database_fname) # This loop tries to find all the paths with lengths less than 'max_depth' for depth in xrange(1, max_depth + 1): sys.stderr.write(substrate + " <=> " + product + ", depth = %d ... " % depth) G_subs = compound2graph(substrate) G_prod = compound2graph(product) (original_compound_map, possible_paths, D) = pathfinder.find_shortest_pathway([G_subs], [G_prod], max_levels=depth) for (substrate_pathways, product_pathways, h_bridge) in possible_paths: # the results are given as 3-tuples, characterized by the compound where the two ends # have met (denoted h_bridge). 'substrate_pathways' is a list of pathways leading from 'substrate' to 'h_bridge'. # 'product_pathways' is a list of pathways leading from 'h_bridge' to 'product'. for subs_path in substrate_pathways: G_subs2 = original_compound_map[subs_path[0]] subs_reaction_list = pathfinder.expand_rid_list(subs_path[1:]) (subs_log, G_last_subs) = pathfinder.pathway2text(G_subs2.clone(),
def get_distance(self, substrate, product, max_levels): G_subs = compound2graph(substrate) G_prod = compound2graph(product) return self.pathfinder.find_distance([G_subs], [G_prod], max_levels=max_levels)
def find_shortest_pathways(self, substrate, product, max_levels, stop_after_first_solution=True): G_subs = compound2graph(substrate) G_prod = compound2graph(product) return self.pathfinder.find_shortest_pathway([G_subs], [G_prod], max_levels=max_levels, stop_after_first_solution=stop_after_first_solution)