Typifier¶
The typifier module is responsible for atom typing and chemical perception.
Atomistic¶
OplsAngleTypifier ¶
OplsAngleTypifier(forcefield)
Bases: TypifierBase[Angle]
Match angle type based on atom types of three atoms in Angle
Source code in src/molpy/typifier/atomistic.py
150 151 152 | |
typify ¶
typify(angle)
Assign type to angle
Source code in src/molpy/typifier/atomistic.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
OplsAtomTypifier ¶
OplsAtomTypifier(forcefield, strict=False)
Bases: TypifierBase['Atomistic']
Assign atom types using SMARTS matcher (support type references and dependency resolution)
Initialize OPLS atom typifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forcefield
|
ForceField
|
Force field to use for typing |
required |
strict
|
bool
|
If True, raise error when atoms cannot be typed. If False (default), silently skip untyped atoms. |
False
|
Source code in src/molpy/typifier/atomistic.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
typify ¶
typify(struct)
Assign types to all atoms in Atomistic structure (using dependency-aware layered matching)
Source code in src/molpy/typifier/atomistic.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
OplsAtomisticTypifier ¶
OplsAtomisticTypifier(forcefield, skip_atom_typing=False, skip_pair_typing=False, skip_bond_typing=False, skip_angle_typing=False, skip_dihedral_typing=False, strict_typing=True)
Bases: TypifierBase[Atomistic]
Assign all types (bond, angle, dihedral) to entire Atomistic structure
Note: This class assumes atoms are already assigned types. If you need to assign atom types simultaneously, use OplsAtomTypifier first, or use skip_atom_typing=False parameter.
Initialize OPLS atomistic typifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forcefield
|
ForceField
|
Force field to use for typing |
required |
skip_atom_typing
|
bool
|
If True, skip atom type assignment |
False
|
skip_pair_typing
|
bool
|
If True, skip pair type assignment |
False
|
skip_bond_typing
|
bool
|
If True, skip bond type assignment |
False
|
skip_angle_typing
|
bool
|
If True, skip angle type assignment |
False
|
skip_dihedral_typing
|
bool
|
If True, skip dihedral type assignment |
False
|
strict_typing
|
bool
|
If True (default), raise error when atoms cannot be typed. If False, emit warnings for untyped atoms. |
True
|
Source code in src/molpy/typifier/atomistic.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
typify ¶
typify(struct)
Assign types to all bonds, angles, dihedrals in Atomistic structure
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
struct
|
Atomistic
|
Atomistic structure |
required |
Prerequisites
- If skip_atom_typing=True (default), all atoms must already have 'type' attribute
- If skip_atom_typing=False, will assign atom types first
Source code in src/molpy/typifier/atomistic.py
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |
OplsBondTypifier ¶
OplsBondTypifier(forcefield)
Bases: TypifierBase[Bond]
Match bond type based on atom types at both ends of the bond.
Strategy: Build class_to_types table and manually compare each AtomType
Source code in src/molpy/typifier/atomistic.py
60 61 62 | |
typify ¶
typify(bond)
Assign type to bond
Source code in src/molpy/typifier/atomistic.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
OplsDihedralTypifier ¶
OplsDihedralTypifier(forcefield)
Bases: TypifierBase[Dihedral]
Match dihedral type based on atom types of four atoms in Dihedral
Source code in src/molpy/typifier/atomistic.py
237 238 239 | |
typify ¶
typify(dihedral)
Assign type to dihedral
Source code in src/molpy/typifier/atomistic.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
PairTypifier ¶
PairTypifier(forcefield)
Bases: TypifierBase[Atom]
Assign nonbonded parameters (charge, sigma, epsilon) to atoms based on their types.
This typifier reads PairType parameters from the forcefield and assigns them to atoms.
Source code in src/molpy/typifier/atomistic.py
346 347 348 | |
typify ¶
typify(atom)
Assign nonbonded parameters to atom based on its type
Source code in src/molpy/typifier/atomistic.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
atomtype_matches ¶
atomtype_matches(atomtype, type_str)
Check if an AtomType matches a given type string.
Matching rules: 1. If atomtype has a specific type (not "*"), compare by type 2. If type doesn't match, compare by class
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atomtype
|
AtomType
|
AtomType instance |
required |
type_str
|
str
|
Type string to match (from Atom.data["type"] or class name) |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if matches, False otherwise |
Source code in src/molpy/typifier/atomistic.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
Adapter¶
Adapter utilities for converting molecules to graphs for matching.
This module provides functions to convert Atomistic structures into igraph.Graph representations suitable for SMARTS pattern matching.
build_mol_graph ¶
build_mol_graph(structure)
Convert Atomistic structure to igraph.Graph for matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Atomistic
|
Atomistic structure with atoms and bonds |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
Tuple of (graph, vs_to_atomid, atomid_to_vs) where: |
dict[int, int]
|
|
dict[int, int]
|
|
tuple[Graph, dict[int, int], dict[int, int]]
|
|
Vertex attributes set
- element: str (e.g., "C", "N", "O")
- number: int (atomic number)
- is_aromatic: bool
- charge: int
- degree: int (number of bonds)
- hyb: int | None (1=sp, 2=sp2, 3=sp3)
- in_ring: bool
- cycles: set of tuples (ring membership)
Edge attributes set
- order: int | str (1, 2, 3, or ":")
- is_aromatic: bool
- is_in_ring: bool
Source code in src/molpy/typifier/adapter.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
Dependency Analyzer¶
Dependency analysis for SMARTS patterns with type references.
DependencyAnalyzer ¶
DependencyAnalyzer(patterns)
Analyzes dependencies between SMARTS patterns and computes matching levels.
Attributes:
| Name | Type | Description |
|---|---|---|
patterns |
Dictionary mapping atom type names to their SMARTSGraph patterns |
|
dependency_graph |
dict[str, set[str]]
|
Adjacency list of dependencies (type -> depends_on) |
levels |
dict[str, int]
|
Dictionary mapping atom type names to their topological levels |
circular_groups |
list[set[str]]
|
List of sets containing types with circular dependencies |
Initialize dependency analyzer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
dict[str, SMARTSGraph]
|
Dictionary of {atom_type_name: SMARTSGraph} |
required |
Source code in src/molpy/typifier/dependency_analyzer.py
18 19 20 21 22 23 24 25 26 27 28 29 30 | |
get_max_level ¶
get_max_level()
Get the maximum level number.
Returns:
| Type | Description |
|---|---|
int
|
Maximum level, or -1 if no patterns |
Source code in src/molpy/typifier/dependency_analyzer.py
158 159 160 161 162 163 164 | |
get_patterns_by_level ¶
get_patterns_by_level(level)
Get all patterns at a specific level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
Topological level number |
required |
Returns:
| Type | Description |
|---|---|
list[SMARTSGraph]
|
List of SMARTSGraph patterns at that level |
Source code in src/molpy/typifier/dependency_analyzer.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
has_circular_dependencies ¶
has_circular_dependencies()
Check if there are any circular dependencies.
Returns:
| Type | Description |
|---|---|
bool
|
True if circular dependencies exist |
Source code in src/molpy/typifier/dependency_analyzer.py
166 167 168 169 170 171 172 | |
Graph¶
Module for SMARTSGraph and SMARTS matching logic.
SMARTSGraph ¶
SMARTSGraph(smarts_string=None, parser=None, name=None, atomtype_name=None, priority=0, target_vertices=None, source='', overrides=None, *args, **kwargs)
Bases: Graph
A graph representation of a SMARTS pattern.
This class supports two modes of construction: 1. From SMARTS string (legacy mode) 2. From predicates (new predicate-based mode)
Attributes¶
atomtype_name : str The atom type this pattern assigns priority : int Priority for conflict resolution (higher wins) target_vertices : list[int] Which pattern vertices should receive the atom type (empty = all) source : str Source identifier for debugging smarts_string : str | None The SMARTS string (if constructed from string) ir : SmartsIR | None The intermediate representation (if constructed from string)
Notes¶
SMARTSGraph inherits from igraph.Graph
Vertex attributes
- preds: list[Callable] - list of predicates that must all pass
Edge attributes
- preds: list[Callable] - list of predicates that must all pass
Graph attributes
- atomtype_name: str
- priority: int
- target_vertices: list[int]
- source: str
- specificity_score: int (computed)
Source code in src/molpy/typifier/graph.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
calc_signature ¶
calc_signature(graph)
Calculate graph signatures for pattern matching.
Source code in src/molpy/typifier/graph.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | |
extract_dependencies ¶
extract_dependencies()
Extract type references from SMARTS IR.
Finds all has_label primitives that reference atom types (e.g., %opls_154). These are parsed by Lark as AtomPrimitiveIR(type="has_label", value="%opls_154").
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of referenced atom type names (e.g., {'opls_154', 'opls_135'}) |
Source code in src/molpy/typifier/graph.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
find_matches ¶
find_matches(graph)
Return sets of atoms that match this SMARTS pattern in a topology.
Parameters¶
structure : TopologyGraph The topology that we are trying to atomtype. typemap : dict The target typemap being used/edited
Notes¶
When this function gets used in atomtyper.py, we actively modify the
white- and blacklists of the atoms in topology after finding a match.
This means that between every successive call of
subgraph_isomorphisms_iter(), the topology against which we are
matching may have actually changed. Currently, we take advantage of this
behavior in some edges cases (e.g. see test_hexa_coordinated in
test_smarts.py).
Source code in src/molpy/typifier/graph.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
from_igraph
classmethod
¶
from_igraph(graph, atomtype_name, priority=0, target_vertices=None, source='')
Create SmartsGraph from an existing igraph.Graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
igraph.Graph with vertex/edge predicates |
required |
atomtype_name
|
str
|
Atom type this pattern assigns |
required |
priority
|
int
|
Priority for conflict resolution |
0
|
target_vertices
|
list[int] | None
|
Which vertices should be typed (empty = all) |
None
|
source
|
str
|
Source identifier |
''
|
Returns:
| Type | Description |
|---|---|
SMARTSGraph
|
SMARTSGraph instance |
Source code in src/molpy/typifier/graph.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
get_priority ¶
get_priority()
Get priority value (supports both new and legacy modes).
Source code in src/molpy/typifier/graph.py
214 215 216 217 218 219 220 221 | |
get_specificity_score ¶
get_specificity_score()
Compute specificity score for this pattern.
Scoring heuristic
+0 per element predicate (baseline) +1 per charge/degree/hyb constraint +2 per aromatic/in_ring constraint +3 per bond order predicate +4 per custom predicate
Returns:
| Type | Description |
|---|---|
int
|
Specificity score (higher = more specific) |
Source code in src/molpy/typifier/graph.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
override ¶
override(overrides)
Set the priority of this SMART
Source code in src/molpy/typifier/graph.py
201 202 203 204 205 206 207 208 209 210 211 212 | |
plot ¶
plot(*args, **kwargs)
Plot the SMARTS graph.
Source code in src/molpy/typifier/graph.py
195 196 197 198 199 | |
SMARTSMatcher ¶
SMARTSMatcher(G1, G2, node_match_fn, edge_match_fn=None)
Inherits and implements VF2 for a SMARTSGraph.
Source code in src/molpy/typifier/graph.py
501 502 503 504 505 | |
candidate_pairs_iter ¶
candidate_pairs_iter()
Iterate over candidate pairs of nodes in G1 and G2.
Source code in src/molpy/typifier/graph.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
subgraph_isomorphisms ¶
subgraph_isomorphisms()
Iterate over all subgraph isomorphisms between G1 and G2.
Source code in src/molpy/typifier/graph.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
Layered Engine¶
Layered typing engine for dependency-aware SMARTS matching.
LayeredTypingEngine ¶
LayeredTypingEngine(patterns)
Orchestrates level-by-level atom typing with dependency resolution.
This engine handles: 1. Dependency analysis and topological sorting 2. Layered matching (level 0 first, then level 1, etc.) 3. Conflict resolution within each level 4. Fixed-point iteration for circular dependencies
Attributes:
| Name | Type | Description |
|---|---|---|
patterns |
Dictionary mapping atom type names to SMARTSGraph patterns |
|
matcher |
SmartsMatcher instance for pattern matching |
|
analyzer |
DependencyAnalyzer for computing levels |
Initialize layered typing engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
dict[str, SMARTSGraph]
|
Dictionary of {atom_type_name: SMARTSGraph} |
required |
Source code in src/molpy/typifier/layered_engine.py
25 26 27 28 29 30 31 32 33 34 35 36 | |
get_explain_data ¶
get_explain_data(mol_graph, vs_to_atomid)
Generate detailed explanation of typing process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mol_graph
|
Graph
|
Molecule graph |
required |
vs_to_atomid
|
dict[int, int]
|
Vertex to atom ID mapping |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with detailed typing information |
Source code in src/molpy/typifier/layered_engine.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
typify ¶
typify(mol_graph, vs_to_atomid, max_iterations=10)
Perform layered atom typing with dependency resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mol_graph
|
Graph
|
Molecule graph with vertex/edge attributes |
required |
vs_to_atomid
|
dict[int, int]
|
Mapping from vertex index to atom ID |
required |
max_iterations
|
int
|
Maximum iterations for circular dependency resolution |
10
|
Returns:
| Type | Description |
|---|---|
dict[int, str]
|
Dictionary mapping atom_id -> atom_type |
Source code in src/molpy/typifier/layered_engine.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
Matcher¶
SMARTS matcher for atomtyping with conflict resolution.
This module provides the main SmartsMatcher class that: 1. Finds all candidate atom type assignments from SMARTS patterns 2. Resolves conflicts using deterministic priority rules 3. Provides explain functionality for debugging
Candidate
dataclass
¶
Candidate(atom_id, atomtype, source, priority, score, pattern_size, definition_order)
Represents a candidate atom type assignment.
The ordering is designed for conflict resolution: 1. Higher priority wins 2. Higher specificity score wins 3. Larger pattern size wins 4. Later definition order wins
ScoringPolicy ¶
Policy for computing pattern specificity scores.
The default policy uses predicate weights
+0 per element predicate (baseline) +1 per charge/degree/hyb constraint +2 per aromatic/in_ring constraint +3 per bond order predicate +4 per custom predicate
custom
staticmethod
¶
custom(pattern, vertex_weight=1.0, edge_weight=1.5)
Compute custom specificity score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
SMARTSGraph
|
SmartsGraph pattern |
required |
vertex_weight
|
float
|
Multiplier for vertex predicate weights |
1.0
|
edge_weight
|
float
|
Multiplier for edge predicate weights |
1.5
|
Returns:
| Type | Description |
|---|---|
int
|
Specificity score |
Source code in src/molpy/typifier/matcher.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
default
staticmethod
¶
default(pattern)
Compute default specificity score for a pattern.
Uses pattern size (vertices + edges) as specificity metric. Larger patterns are considered more specific.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
SMARTSGraph
|
SmartsGraph pattern |
required |
Returns:
| Type | Description |
|---|---|
int
|
Specificity score (higher = more specific) |
Source code in src/molpy/typifier/matcher.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
SmartsMatcher ¶
SmartsMatcher(patterns, scoring=None)
Main matcher for atomtyping with SMARTS patterns.
This class finds all candidate atom type assignments from a list of SMARTS patterns and resolves conflicts using a deterministic priority system.
Example
patterns = [pattern1, pattern2, pattern3] matcher = SmartsMatcher(patterns) mol_graph = build_mol_graph(structure) candidates = matcher.find_candidates(mol_graph, vs_to_atomid) result = matcher.resolve(candidates)
Initialize matcher with patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
list[SMARTSGraph]
|
List of SmartsGraph patterns |
required |
scoring
|
ScoringPolicy | None
|
Scoring policy (default: ScoringPolicy.default) |
None
|
Source code in src/molpy/typifier/matcher.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
explain ¶
explain(candidates)
Generate explain data for debugging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidates
|
list[Candidate]
|
List of Candidate objects |
required |
Returns:
| Type | Description |
|---|---|
dict[int, Any]
|
Dict mapping atom_id -> explain data with all candidates |
dict[int, Any]
|
and their ordering keys |
Source code in src/molpy/typifier/matcher.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
find_candidates ¶
find_candidates(mol_graph, vs_to_atomid, type_assignments=None)
Find all candidate atom type assignments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mol_graph
|
Graph
|
Molecule graph with vertex/edge attributes |
required |
vs_to_atomid
|
dict[int, int]
|
Mapping from vertex index to atom ID |
required |
type_assignments
|
dict[int, str] | None
|
Current type assignments (for reference checking) |
None
|
Returns:
| Type | Description |
|---|---|
list[Candidate]
|
List of Candidate objects |
Source code in src/molpy/typifier/matcher.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
resolve ¶
resolve(candidates)
Resolve conflicts and return final atom type assignments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidates
|
list[Candidate]
|
List of Candidate objects |
required |
Returns:
| Type | Description |
|---|---|
dict[int, str]
|
Dict mapping atom_id -> atomtype |
Source code in src/molpy/typifier/matcher.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |