molfoundry.DG#
- class molfoundry.DG(labelSettings=LabelSettings(LabelType.String, LabelRelation.Isomorphism), graphDatabase=[])#
-
- property labelSettings: LabelSettings#
- findEdge(sources, targets)#
- print() None#
- print(printer: DGPrinter | None = None) None
- print(printer: DGPrinter | None = None, data: DGPrintData | None = None) None
Emit a static SVG visualization of the derivation graph.
Every DG vertex renders as an oval-framed molecule depiction (produced by the shared GraphSVGDrawer, in the mode / collapse-H setting picked up from printer) with the graph’s name captioned inside the oval. Every hyperedge with a single source and a single target collapses to a direct arrow labeled with e{id}; every multi-educt or multi-product hyperedge gets a small labeled square “junction” node with arrows from the educts into the junction and from the junction to the products.
Per-atom overlay markers (e.g. for atom tracing) come from the graph printer’s
GraphPrinter.pushVertexOverlay()resolver — configure it viaprinter.graphPrinterand it applies to every vertex’s molecule.- Parameters:
printer (DGPrinter | None)
data (DGPrintData | None)
- Return type:
None
- svg() str#
- svg(printer: DGPrinter | None) str
- svg(printer: DGPrinter | None = None, data: DGPrintData | None = None) str
Render the derivation graph to an SVG string and return it — the same visualization
printproduces, without the summary card wrapper.Per-atom overlay markers (e.g. for atom tracing) come from the graph printer’s
GraphPrinter.pushVertexOverlay()resolver.- Parameters:
printer (DGPrinter | None)
data (DGPrintData | None)
- Return type:
- getSBMLString()#
Serialize the derivation graph as SBML (Systems Biology Markup Language) Level 3 Version 2 text and return it.
Each DG vertex becomes a
<species>(named by its graph) and each hyperedge becomes an irreversible<reaction>whose reactant / product stoichiometries are the multiplicities of its source / target vertices. All species share one default compartment.- Returns:
The SBML document as a string.
- Return type:
- class Vertex#
- class HyperEdge#
- vertexMaps(limit=1, vertex_filter=None)#
- numMatches(rule)#
The number of distinct matches of
rulethat produce this hyperedge.That is how many ways the rule applies to these educts to give these products. The derivation keeps only one of each set of matches related by an educt automorphism; this counts all of them, walking only the matched atoms, so it stays cheap where enumerating
vertexMaps()would not.Every match counts: a rule with an automorphism contributes each of its orientations, and matches differing only in which copy of a species plays which role are distinct. Returns
0whenruledoes not produce this hyperedge.
- print() None#
- print(printer: GraphPrinter | None = None) None
- Parameters:
printer (GraphPrinter | None)
- Return type:
None
- svg() List[str]#
- svg(printer: GraphPrinter | None = None) List[str]
Render this hyperedge’s derivation(s) to SVG and return the list of SVG strings — the same depictions
printshows, without the summary card wrappers. A hyperedge can bundle several derivations, so this returns one SVG string per derivation.- Parameters:
printer (GraphPrinter | None)
- Return type:
- class Builder(dg)#
- Parameters:
dg (DG)
- execute(strategy, verbosity=2, ignoreRuleLabelTypes=False)#
- Parameters:
strategy (DGStrat) – The strategy to execute
verbosity (int) –
The verbosity defaults to level 2. The levels have the following meaning:
0 (or less): no information is printed.
2: Repetition strategies print information for each round.
4: All strategies print minimal information.
6: Derivation predicate strategies and filtering strategies also print their predicates.
8: Rule strategies print minimal information about graph binding.
10: Rule strategies print more information about graph binding, including failure due to derivation predicates.
50: Print information about morphism generation for rule composition.
60: Print rule composition information.
ignoreRuleLabelTypes – Whether rules in the strategy should be checked beforehand for whether they have an associated LabelType which matches the one in the underlying derivation graph.
- Return type:
- apply(graphs, r, mode=ApplyMode.UseAll, verbosity=0, *, onlyProper=None)#
Compute the direct derivations of
rongraphsand add them.modeselects which graphs a derivation may use (seeApplyMode):ApplyMode.UseAll(default): all ofgraphsmust be used in each direct derivation, each listed graph exactly once. A graph listed twice (or two identical graphs) is two copies, and no further copy is added, so a rule with two left components combines two molecules of a kind only when that kind is listed twice.ApplyMode.UseAny: a derivation may use any ofgraphs, each listed graph at most once, again with no further copy added.ApplyMode.UseAnyMultiplicity: a derivation may use any ofgraphs, each in as many copies as it needs.
MØD’s
onlyProperflag is still accepted: true isUseAll, falseUseAny, as in MØD. Returns the hyperedges of the derivations.Rule.numMatches()counts their matches without deriving.A rule with an empty left side has one match, the empty one. It uses no graph, so its derivation has no educts: a hyperedge without sources whose targets are the components the rule creates, derived under
UseAllonly whengraphsis empty. The empty rule derives the hyperedge without sources or targets, the identity on the empty graph. A rule strategy derives none of these, as each of its derivations uses a graph of the active subset.
- applyMatch(graphs, r, match)#
Derive
rat the givenmatchintographsand add the derivation.graphsis the concrete state: each listed graph is one copy, so a graph listed twice is two copies.matchmaps every vertex of the rule’s left side, by rule vertex id, to(graph index, vertex id): the position of its host graph ingraphsand the vertex’s id in that graph. The match alone decides which graphs take part; the ones it does not touch are not educts of the derivation. A rule with an empty left side takes the empty match{}, a derivation without educts.The match is checked as
apply()would admit it: it must embed the left side (labels, edges, the dangling condition, adjacency constraints), keep its components apart, satisfy the rule’s other constraints, and pass the checks of the rewrite itself (an edge the rule creates must not exist yet). RaisesLogicError, naming the failing check, otherwise.Returns a
MatchApplication: the hyperedge, whose targets are the products, and the atom map of this match, from which the surviving vertices’ correspondence is read:edge, vertexMap = builder.applyMatch([ethanol], hToN, {0: (0, 6), 1: (0, 5)}) survivors = [(e.source, e.target) for e in vertexMap if e.source is not None and e.target is not None]
- applyPartialMatch(graphs, r, match, mode=None, *, onlyProper=None)#
Derive
rat every match extending a partial one, and add the derivations.matchplaces some rule vertices as inapplyMatch()– rule vertex id to(graph index, vertex id)– and leaves the others free. Every matchapply()admits formodethat agrees with the placed vertices is derived; matches related by an automorphism of the educts give one derivation, as inapply().modereadsgraphsasapply()does (seeApplyMode) and decides where the free vertices may go. It defaults toApplyMode.UseAny: each listed graph is one copy, and a derivation uses the ones the match reaches. MØD’sonlyProperflag is accepted as inapply().Placed vertices keep their copies: placed on the same listed graph they share its copy, placed on two listings of the same graph they take different copies. A free vertex may land in a placed one’s copy or in another.
The placement is checked for shape as in
applyMatch()(rule vertices of the left side, valid positions, no vertex taken twice, one graph per left component), raisingLogicErrorotherwise. A placement that no admitted match extends is no error: it derives nothing.{}derives whatapply()does, and a complete placement whatapplyMatch()does. Returns the hyperedges:# Every H->N on ethanol's oxygen, wherever the hydrogen is. edges = builder.applyPartialMatch([water, ethanol], hToN, {0: (1, o.id)})
- addDerivation(d)#
- Parameters:
d (Derivations)
- Return type:
- removeVertex(v)#
Remove a vertex and every hyperedge incident to it.
The vertex’s stable id is retired, not reused, so ids held elsewhere stop resolving.
- Parameters:
v (Vertex)
- Return type:
None