molfoundry.atomtracing#
Atom tracing over derivation graphs: a bridge from a DG
through DGVertexMapper into the vendored saturate-tracer
engine.
Saturation-based atom tracing for derivation graphs.
This subpackage vendors the saturate-tracer engine (AstrorEnales/saturate-tracer)
and adds DGAtomTracing, which builds those inputs directly from a
DG using the atom-atom maps that DGVertexMapper
recovers from each derivation.
from molfoundry import DG, smiles, ruleGMLString, addSubset from molfoundry.atomtracing import DGAtomTracing, by_element
dg = … # a built derivation graph inputs = DGAtomTracing.from_dg(dg, vertex_filter=by_element(“C”)) sat = inputs.build_tracer() sat.run()
The drawing helpers need the optional drawing extra (rdkit, cairosvg, matplotlib,
networkx, pygraphviz). Their heavy imports are lazy, so importing this package never
requires the extra; only calling a draw function does.
- class molfoundry.atomtracing.SaturateTracer(reaction_rules, injected_template_instances=None, custom_initial_configuration=None, label_sets=None, templates=None, remove_duplicate_rules=True, log_duplicate_rules=True, verbose=False)#
Bases:
object- Parameters:
- save_config(file_path, indent=2)#
Save the configuration as a JSON file: templates, label sets, reaction rules, injected template instances, and custom initial configuration.
- classmethod load_config(file_path, **kwargs)#
Load a configuration previously written by
save_config()and return a newSaturateTracer. Any additional keyword arguments are forwarded to__init__(e.g.verbose,remove_duplicate_rules,log_duplicate_rules).- Parameters:
file_path (str)
- Return type:
- get_all_origin_id_positions(origin_id)#
Find all position IDs where the origin ID has been traced to.
- get_all_origin_species_targets(species_origin_ids)#
Find all targets the origin specie(s) has been traced to.
- get_all_position_id_labelings(position_id)#
Find all origin IDs of a specific position ID.
- get_all_species_labelings(position_ids)#
Find all template instances of a specific template.
- get_all_origin_target_labelings(origin_position_ids, target_position_ids)#
Find all template instances of a specific target template and from a specific origin template.
- class molfoundry.atomtracing.ReactionRule(educts, products, name=None, color=None)#
Bases:
object- Parameters:
- class molfoundry.atomtracing.Template(ids, name=None, smiles=None)#
Bases:
object- identity_instance()#
- unlabeled_instance()#
- class molfoundry.atomtracing.DGAtomTracing#
Bases:
objectStatic helpers translating a DG into atom-tracing inputs.
from_dg()converts a whole derivation graph;from_hyperedges()converts an explicit subset of its hyperedges. Both returnTracerInputs.- static from_dg(dg, *, vertex_filter=None, maps_per_edge=65535, on_unbalanced='skip', verbose=False)#
Build
TracerInputsfrom every hyperedge ofdg.- Parameters:
dg (DG) – the DG to translate; each hyperedge becomes one or more reaction rules and contributes its educt/product compounds as templates.
vertex_filter (Callable[[Vertex], bool] | None) – which atoms to trace.
Nonetraces every atom (hydrogens included); pass e.g.by_element()to restrict to an element. Only atoms this selects get position IDs, appear in templates, and constrain conservation.maps_per_edge (int) – how many atom-atom maps to enumerate per hyperedge. One map is the canonical labeling; a symmetric molecule admits several maps that give genuinely different atom traces (e.g. the two ends of succinate), so raise this to capture that symmetry. Each map becomes a rule sharing the hyperedge’s name; the tracer removes any that turn out identical. The orbit of maps can be large, hence the cap.
vertex_filteris handed to the mapper, so the maps enumerated are only those distinct on the traced atoms: symmetry among untraced atoms (hydrogen relabelings, say) is pruned before a map is ever produced, not deduplicated afterward.on_unbalanced (str) – what to do when a hyperedge creates or destroys a traced atom and so cannot be represented.
"skip"(default) records it inskipped_edgesand moves on;"error"raisesValueError.verbose (bool) – print a line for every skipped hyperedge.
- Return type:
- static from_hyperedges(edges, *, vertex_filter=None, maps_per_edge=65535, on_unbalanced='skip', verbose=False)#
Build
TracerInputsfrom a collection of hyperedges.- Parameters:
edges (Iterable[HyperEdge]) – the hyperedges to translate; each becomes one or more reaction rules and contributes its educt/product compounds as templates.
vertex_filter (Callable[[Vertex], bool] | None) – which atoms to trace.
Nonetraces every atom (hydrogens included); pass e.g.by_element()to restrict to an element. Only atoms this selects get position IDs, appear in templates, and constrain conservation.maps_per_edge (int) – how many atom-atom maps to enumerate per hyperedge. One map is the canonical labeling; a symmetric molecule admits several maps that give genuinely different atom traces (e.g. the two ends of succinate), so raise this to capture that symmetry. Each map becomes a rule sharing the hyperedge’s name; the tracer removes any that turn out identical. The orbit of maps can be large, hence the cap.
vertex_filteris handed to the mapper, so the maps enumerated are only those distinct on the traced atoms: symmetry among untraced atoms (hydrogen relabelings, say) is pruned before a map is ever produced, not deduplicated afterward.on_unbalanced (str) – what to do when a hyperedge creates or destroys a traced atom and so cannot be represented.
"skip"(default) records it inskipped_edgesand moves on;"error"raisesValueError.verbose (bool) – print a line for every skipped hyperedge.
- Return type:
- class molfoundry.atomtracing.TracerInputs(templates, rules, template_by_graph_id, position_id_by_atom, skipped_edges)#
Bases:
objectEverything needed to trace a network, produced by
DGAtomTracing.templatesandrulesare the two arguments aSaturateTracertakes; the remaining fields expose the bookkeeping so callers can build label sets and initial configurations that target specific compounds or atoms, and can map traced positions back to the DG.- Parameters:
- rules: List[ReactionRule]#
One
ReactionRuleper hyperedge per atom-atom map that could be traced.
- position_id_by_atom: Dict[Tuple[int, int], int]#
(graph id, atom vertex id)-> the atom’s global position ID.
- skipped_edges: List[Tuple[int, str]]#
(hyperedge id, reason)for every hyperedge that produced no rule.
- build_tracer(**kwargs)#
Construct a
SaturateTracerfrom these templates and rules.Extra keyword arguments (
label_sets,custom_initial_configuration,verbose, …) are forwarded toSaturateTracerunchanged.The rule list is copied first, because
SaturateTracerremoves duplicate rules from the list it is given, and we do not want to mutateself.rules.- Return type:
- template_of(vertex)#
The template of a compound, given its DG vertex, its graph, or one of its atom vertices. Returns
Nonewhen that compound has no traced atoms.
- position_id(atom)#
The global position ID of an atom vertex, or
Nonewhen it is not traced.
- molfoundry.atomtracing.by_element(*symbols)#
A
VertexFilterkeeping only atoms of the given element symbol(s).by_element("C")traces carbon;by_element("C", "N")traces carbon and nitrogen. Vertices without a valid element (e.g. electrons in an EFG graph) never match.
- molfoundry.atomtracing.heavy_atoms(v)#
A
VertexFilterkeeping every atom except hydrogen.Useful when a graph carries explicit hydrogens (as SMILES-derived graphs do) and you want to trace the heavy-atom skeleton without hydrogens exploding the maps.
- molfoundry.atomtracing.get_origin_id_color_map(ids)#
- molfoundry.atomtracing.draw(tracer, file_path=None, origin_id_colors_map=None, highlight_position_ids=None, title=None, style=DefaultDrawStyle, horizontal=False, layout='dot', graphviz_graph_attr=None)#
- molfoundry.atomtracing.draw_reaction_network(tracer, file_path=None, origin_id_colors_map=None, highlight_position_ids=None, title=None, style=('name', 'pid', 'image'), horizontal=False, layout='dot', graphviz_graph_attr=None)#
- molfoundry.atomtracing.draw_filtered_target(tracer, target_ids, file_path=None, origin_id_colors_map=None, highlight_position_ids=None, title=None, style=DefaultDrawStyle, horizontal=False, layout='dot', graphviz_graph_attr=None)#
- molfoundry.atomtracing.draw_filtered_source(tracer, origin_ids, file_path=None, origin_id_colors_map=None, highlight_position_ids=None, title=None, style=DefaultDrawStyle, horizontal=False, layout='dot', graphviz_graph_attr=None)#
- molfoundry.atomtracing.draw_filtered_source_target(tracer, origin_ids, target_ids, file_path=None, origin_id_colors_map=None, highlight_position_ids=None, title=None, style=DefaultDrawStyle, horizontal=False, layout='dot', graphviz_graph_attr=None)#