protomol 0.0.2__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. {protomol-0.0.2 → protomol-0.0.3}/PKG-INFO +1 -1
  2. {protomol-0.0.2 → protomol-0.0.3}/pyproject.toml +22 -2
  3. {protomol-0.0.2 → protomol-0.0.3}/pyproject.toml.orig +11 -2
  4. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/__init__.py +5 -2
  5. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_canon.py +26 -1
  6. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_geometry.py +53 -1
  7. protomol-0.0.3/src/protomol/_low_t.py +120 -0
  8. protomol-0.0.3/src/protomol/_smiles.py +578 -0
  9. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/__init__.py +1 -1
  10. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/rdmol2graph.py +96 -46
  11. protomol-0.0.2/src/protomol/_smiles.py +0 -258
  12. {protomol-0.0.2 → protomol-0.0.3}/LICENSE +0 -0
  13. {protomol-0.0.2 → protomol-0.0.3}/README.md +0 -0
  14. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_backend.py +0 -0
  15. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_ccv.py +0 -0
  16. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_compare.py +0 -0
  17. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_display.py +0 -0
  18. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_json.py +0 -0
  19. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_lewis.py +0 -0
  20. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_reaction.py +0 -0
  21. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_stereo.py +0 -0
  22. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_types.py +0 -0
  23. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/LICENSE +0 -0
  24. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/__init__.py +0 -0
  25. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/algorithms/__init__.py +0 -0
  26. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/algorithms/_bond_orders.py +0 -0
  27. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/algorithms/canon.py +0 -0
  28. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/algorithms/circular.py +0 -0
  29. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/algorithms/isomorphism.py +0 -0
  30. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/algorithms/symmetry.py +0 -0
  31. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/coords.py +0 -0
  32. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/experimental/__init__.py +0 -0
  33. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/experimental/_isomers.py +0 -0
  34. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/experimental/_json.py +0 -0
  35. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/graph2rdmol.py +0 -0
  36. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/graphs/__init__.py +0 -0
  37. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/graphs/crg.py +0 -0
  38. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/graphs/mg.py +0 -0
  39. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/graphs/scrg.py +0 -0
  40. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/graphs/smg.py +0 -0
  41. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/ipython.py +0 -0
  42. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/periodic_table.py +0 -0
  43. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/py.typed +0 -0
  44. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/stereodescriptors.py +0 -0
  45. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/_vendor/stereomolgraph/xyz2graph.py +0 -0
  46. {protomol-0.0.2 → protomol-0.0.3}/src/protomol/examples.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: protomol
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Author: Andreas V. Copan
5
5
  Author-email: Andreas V. Copan <avcopan@uga.edu>
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "protomol"
3
- version = "0.0.2"
3
+ version = "0.0.3"
4
4
  readme = "README.md"
5
5
  license = "MIT"
6
6
  license-files = ["LICENSE"]
@@ -55,6 +55,26 @@ ignore = [
55
55
  "T201",
56
56
  "INP001",
57
57
  ]
58
+ "benchmarks/**.py" = [
59
+ "T201",
60
+ "INP001",
61
+ "E501",
62
+ "PLC0415",
63
+ "ANN401",
64
+ "FBT001",
65
+ "ARG001",
66
+ "N818",
67
+ "S311",
68
+ "S324",
69
+ "DTZ011",
70
+ "C901",
71
+ "PLR0912",
72
+ "PLR0913",
73
+ "PLR0915",
74
+ "PLR0917",
75
+ "S603",
76
+ "S607",
77
+ ]
58
78
 
59
79
  [tool.ruff.lint.pydocstyle]
60
80
  convention = "google"
@@ -77,7 +97,7 @@ root_package = "protomol"
77
97
  name = "Module Layering"
78
98
  type = "layers"
79
99
  layers = [
80
- "protomol._geometry",
100
+ "protomol._geometry | protomol._low_t",
81
101
  "protomol._smiles | protomol._display | protomol._canon | protomol._compare",
82
102
  "protomol._backend | protomol._json | protomol._lewis",
83
103
  "protomol._types",
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "protomol"
3
- version = "0.0.2"
3
+ version = "0.0.3"
4
4
  readme = "README.md" # the PyPI page
5
5
  license = "MIT"
6
6
  license-files = ["LICENSE"]
@@ -47,6 +47,15 @@ ignore = [
47
47
  [tool.ruff.lint.per-file-ignores]
48
48
  "tests/**.py" = ["S101", "PLR2004"] # assert, magic values
49
49
  "scripts/**.py" = ["T201", "INP001"] # print, not a package
50
+ "benchmarks/**.py" = [ # one-off research scripts, not part of the package
51
+ "T201", # print
52
+ "INP001", # not a package
53
+ "E501", # long lines in docstrings and SMARTS
54
+ "PLC0415", # imports inside the pool workers
55
+ "ANN401", "FBT001", "ARG001", "N818", "S311", "S324", "DTZ011",
56
+ "C901", "PLR0912", "PLR0913", "PLR0915", "PLR0917", # long check functions
57
+ "S603", "S607", # the report runs its own reproducers and git
58
+ ]
50
59
 
51
60
  [tool.ruff.lint.pydocstyle]
52
61
  convention = "google"
@@ -71,7 +80,7 @@ root_package = "protomol"
71
80
  name = "Module Layering"
72
81
  type = "layers"
73
82
  layers = [
74
- "protomol._geometry",
83
+ "protomol._geometry | protomol._low_t",
75
84
  "protomol._smiles | protomol._display | protomol._canon | protomol._compare",
76
85
  "protomol._backend | protomol._json | protomol._lewis",
77
86
  "protomol._types",
@@ -1,14 +1,15 @@
1
1
  """protomol: placeholder graph types for AutoStorage and AutoEngine."""
2
2
 
3
- __version__ = "0.0.2"
3
+ __version__ = "0.0.3"
4
4
 
5
5
  from . import examples
6
- from ._canon import canonical_form, canonical_json
6
+ from ._canon import canonical_direction, canonical_form, canonical_json
7
7
  from ._ccv import ts_graphs
8
8
  from ._compare import deduplicate, isomorphism
9
9
  from ._display import display
10
10
  from ._geometry import embed, from_geometry
11
11
  from ._json import from_json, to_json
12
+ from ._low_t import low_t
12
13
  from ._reaction import (
13
14
  products,
14
15
  reactants,
@@ -64,6 +65,7 @@ __all__ = [
64
65
  "StereoReactionGraph",
65
66
  "Tetrahedral",
66
67
  "TrigonalBipyramidal",
68
+ "canonical_direction",
67
69
  "canonical_form",
68
70
  "canonical_json",
69
71
  "deduplicate",
@@ -77,6 +79,7 @@ __all__ = [
77
79
  "is_determined",
78
80
  "isomers",
79
81
  "isomorphism",
82
+ "low_t",
80
83
  "neighbors",
81
84
  "products",
82
85
  "reactants",
@@ -14,7 +14,7 @@ depend on the input numbering.
14
14
 
15
15
  from ._backend import canon_numbering
16
16
  from ._json import to_json
17
- from ._reaction import relabel
17
+ from ._reaction import relabel, reverse
18
18
  from ._types import MolGraph, ReactionGraph, StereoMolGraph, StereoReactionGraph
19
19
 
20
20
 
@@ -51,3 +51,28 @@ def canonical_json[G: (MolGraph, StereoMolGraph, ReactionGraph, StereoReactionGr
51
51
  """
52
52
  canonical, _ = canonical_form(gra)
53
53
  return to_json(canonical)
54
+
55
+
56
+ def canonical_direction[R: (ReactionGraph, StereoReactionGraph)](
57
+ gra: R,
58
+ ) -> tuple[R, bool]:
59
+ """`gra` or its reverse with the smaller canonical JSON, and whether reversed.
60
+
61
+ A reaction graph and its `reverse` describe one TS from the two sides, and their
62
+ canonical forms differ. This picks one of them by comparing `canonical_json`, so
63
+ the result is the same for both, up to atom numbering if the two are equal (a
64
+ symmetric self-reaction, where a tie keeps `gra`). Atom indices are kept, so an
65
+ atom map from `canonical_form` of the result applies to the input's atoms as
66
+ they are. `canonical_form` itself stays faithful to its input.
67
+
68
+ >>> from protomol import examples
69
+ >>> ts = examples.abstraction_ts()
70
+ >>> chosen, flipped = canonical_direction(ts)
71
+ >>> chosen2, flipped2 = canonical_direction(reverse(ts))
72
+ >>> chosen == chosen2, flipped != flipped2
73
+ (True, True)
74
+ """
75
+ rev = reverse(gra)
76
+ if canonical_json(rev) < canonical_json(gra):
77
+ return rev, True
78
+ return gra, False
@@ -27,6 +27,7 @@ _MIN_VOLUME = 0.3 # |det| of three unit-vector differences; ideal tetrahedron:
27
27
  _MIN_COSINE = 0.3 # |cos| (planar) or |sin| (axial) of the dihedral between the ends
28
28
 
29
29
  _EMBED_ATTEMPTS = 100 # ETKDG random seeds 0, 1, …
30
+ _PYRAMID_HEIGHT = 0.35 # Å of a pyramidal centre above its neighbours' plane
30
31
  _FRAGMENT_GAP = 3.0 # Å between the fragments of a disconnected graph, along x
31
32
  _FLIP_TAG = {
32
33
  Chem.ChiralType.CHI_TETRAHEDRAL_CW: Chem.ChiralType.CHI_TETRAHEDRAL_CCW,
@@ -107,7 +108,7 @@ def embed(gra: MolGraph | StereoMolGraph) -> Geometry:
107
108
  params.randomSeed = seed
108
109
  if rdDistGeom.EmbedMolecule(mol, params) != 0:
109
110
  continue
110
- xyz = mol.GetConformer().GetPositions()
111
+ xyz = _pyramidalized(mol.GetConformer().GetPositions(), lss)
111
112
  wrong = [ls for ls in lss if _ls_from_coords(ls, xyz) != ls]
112
113
  if not wrong:
113
114
  coords = _spread_fragments(mol, xyz)
@@ -159,6 +160,35 @@ def _set_stereo_tags(mol: Chem.RWMol, lss: list[Stereo]) -> dict[int, Chem.Chira
159
160
  return tags
160
161
 
161
162
 
163
+ def _pyramidalized(xyz: _Coords, lss: list[Stereo]) -> _Coords:
164
+ """`xyz` with each flat centre of a determined three-neighbour LS made pyramidal.
165
+
166
+ ETKDG keeps an atom flat if RDKit calls it sp2, whatever its chiral tag: the N of an
167
+ N-acyl aziridine, or a radical centre. Each `Tetrahedral` with a `None` slot (a lone
168
+ pair or an unpaired electron) whose centre the coordinates leave undetermined is
169
+ moved `_PYRAMID_HEIGHT` out of its neighbours' plane, to the side of its parity.
170
+ """
171
+ xyz = np.array(xyz, dtype=np.float64)
172
+ for ls in lss:
173
+ if (
174
+ not isinstance(ls, Tetrahedral)
175
+ or None not in ls.atoms
176
+ or _tetrahedral_from_coords(ls, xyz).parity is not None
177
+ ):
178
+ continue
179
+ center = ls.atoms[0]
180
+ nbrs = [n for n in ls.atoms[1:] if n is not None]
181
+ normal = _unit(
182
+ np.cross(xyz[nbrs[1]] - xyz[nbrs[0]], xyz[nbrs[2]] - xyz[nbrs[0]])
183
+ )
184
+ foot = xyz[center] - np.dot(xyz[center] - xyz[nbrs[0]], normal) * normal
185
+ for sign in (1, -1):
186
+ xyz[center] = foot + sign * _PYRAMID_HEIGHT * normal
187
+ if _tetrahedral_from_coords(ls, xyz) == ls:
188
+ break
189
+ return xyz
190
+
191
+
162
192
  def _spread_fragments(mol: Chem.Mol, xyz: _Coords) -> _Coords:
163
193
  """Each fragment centred, then set along x with `_FRAGMENT_GAP` between them."""
164
194
  coords = np.array(xyz, dtype=np.float64)
@@ -172,6 +202,28 @@ def _spread_fragments(mol: Chem.Mol, xyz: _Coords) -> _Coords:
172
202
  return coords
173
203
 
174
204
 
205
+ def _with_parities_from_geometry(gra: StereoMolGraph, geo: Geometry) -> StereoMolGraph:
206
+ """`gra` with each undetermined LS's parity read from `geo`, atom i on atom i.
207
+
208
+ How a scheme's added sites get their parities (P57): `smg_low_t@1` applies it to
209
+ `low_t(gra)` (P53). It uses `from_geometry`'s readers, and a site whose coordinates
210
+ don't decide it stays undetermined.
211
+
212
+ Raises:
213
+ GraphError: If the geometry's symbols differ from the graph's.
214
+ """
215
+ if tuple(geo.symbols) != gra.symbols:
216
+ msg = f"The geometry's atoms {geo.symbols} differ from the graph's"
217
+ raise GraphError(msg)
218
+ xyz = np.asarray(geo.coordinates, dtype=np.float64)
219
+ lss = [
220
+ _ls_from_coords(ls, xyz)
221
+ for ls in (*gra.atom_stereo, *gra.bond_stereo)
222
+ if ls.parity is None
223
+ ]
224
+ return with_stereo(gra, *lss)
225
+
226
+
175
227
  def _with_parities_from(gra: StereoMolGraph, xyz: _Coords) -> StereoMolGraph:
176
228
  """`gra` with each LS's parity read from the coordinates (undetermined if unclear).
177
229
 
@@ -0,0 +1,120 @@
1
+ """`low_t(g)`: the low-T scheme's sites on any graph (P54, P57).
2
+
3
+ The sites come from a perceived Lewis structure (`_lewis`) through the pipeline
4
+ `from_smiles` and `from_geometry` share (`_from_rdkit_mol`); the given graph's parities
5
+ are kept where it has the same site.
6
+ """
7
+
8
+ from typing import Any, overload
9
+
10
+ from ._lewis import _lewis
11
+ from ._reaction import products, reactants
12
+ from ._smiles import _from_rdkit_mol
13
+ from ._stereo import AtropBond, PlanarBond, StereoChange, site
14
+ from ._types import (
15
+ _LS,
16
+ MolGraph,
17
+ ReactionGraph,
18
+ StereoMolGraph,
19
+ StereoReactionGraph,
20
+ _Graph,
21
+ _site,
22
+ _site_key,
23
+ _stereo_fields,
24
+ )
25
+
26
+
27
+ @overload
28
+ def low_t(g: MolGraph | StereoMolGraph) -> StereoMolGraph: ...
29
+ @overload
30
+ def low_t(g: ReactionGraph | StereoReactionGraph) -> StereoReactionGraph: ...
31
+ def low_t(
32
+ g: MolGraph | StereoMolGraph | ReactionGraph | StereoReactionGraph,
33
+ ) -> StereoMolGraph | StereoReactionGraph:
34
+ """The graph with the low-T scheme's stereo sites (P54), keeping its parities.
35
+
36
+ The sites are perceived on a Lewis structure of the graph. A site the graph already
37
+ has keeps its LS and parity; a site the scheme drops loses its LS; a site it adds
38
+ is undetermined, or fixed if it is trivial. No parity is invented, so a plain graph
39
+ gets the sites with only the trivial ones fixed. `low_t` is idempotent, and leaves
40
+ the output of `from_smiles` and `from_geometry` as it is, except where the rules
41
+ depend on which Lewis structure is read (open question 11).
42
+
43
+ A reaction graph's two sides are reassigned separately: each `StereoChange` slot
44
+ follows its side's site, and a change left with no LS in any slot is removed. The
45
+ TS slots are kept as they are (TS sites come with Phase 5).
46
+
47
+ >>> from protomol import from_smiles, without_stereo
48
+ >>> gra = from_smiles("C[C@H](N)CC")
49
+ >>> low_t(without_stereo(gra)).atom_stereo[1].parity is None # C1: undetermined
50
+ True
51
+ >>> low_t(gra) == gra
52
+ True
53
+
54
+ Raises:
55
+ GraphError: If no Lewis structure fits the bonds, charge and spin.
56
+ """
57
+ if isinstance(g, MolGraph | StereoMolGraph):
58
+ return _low_t_species(g)
59
+ rct = _low_t_species(reactants(g))
60
+ prd = _low_t_species(products(g))
61
+ rls = {_site(ls): ls for ls in (*rct.atom_stereo, *rct.bond_stereo)}
62
+ pls = {_site(ls): ls for ls in (*prd.atom_stereo, *prd.bond_stereo)}
63
+ ts = (
64
+ {_site(c): c.ts for c in g.stereo_changes if c.ts is not None}
65
+ if isinstance(g, StereoReactionGraph)
66
+ else {}
67
+ )
68
+ items: list[_LS | StereoChange] = []
69
+ for key in sorted(rls.keys() | pls.keys() | ts.keys(), key=_site_key):
70
+ r, p, t = rls.get(key), pls.get(key), ts.get(key)
71
+ if r == p and t is None:
72
+ assert r is not None # noqa: S101 (for the type checker)
73
+ items.append(r)
74
+ else:
75
+ items.append(StereoChange(reactant=r, product=p, ts=t))
76
+ data: dict[str, Any] = {
77
+ **_base_fields(g),
78
+ "breaking": g.breaking,
79
+ "forming": g.forming,
80
+ }
81
+ return StereoReactionGraph(**data, **_stereo_fields(items, reaction=True))
82
+
83
+
84
+ def _low_t_species(gra: MolGraph | StereoMolGraph) -> StereoMolGraph:
85
+ """`low_t` of a species graph."""
86
+ perceived = _from_rdkit_mol(_lewis(gra), charge=gra.charge, spin=gra.spin)
87
+ if not isinstance(gra, StereoMolGraph):
88
+ return perceived
89
+ given = {site(ls): ls for ls in (*gra.atom_stereo, *gra.bond_stereo)}
90
+ lss = [
91
+ _kept(given.get(site(ls)), ls)
92
+ for ls in (*perceived.atom_stereo, *perceived.bond_stereo)
93
+ ]
94
+ return StereoMolGraph(**_base_fields(gra), **_stereo_fields(lss, reaction=False))
95
+
96
+
97
+ def _base_fields(g: _Graph) -> dict[str, Any]:
98
+ """The atoms, bonds, charge and spin of a graph."""
99
+ return {name: getattr(g, name) for name in ("symbols", "bonds", "charge", "spin")}
100
+
101
+
102
+ def _kept(given: _LS | None, perceived: _LS) -> _LS:
103
+ """The given LS if it is the perceived site's LS with a parity, else the perceived.
104
+
105
+ The same site means the same kind on the same atoms (and chain): the given LS
106
+ keeps its parity (and its atom order). A given LS without a parity gives way to
107
+ the perceived one, which may be fixed as trivial.
108
+ """
109
+ if (
110
+ given is None
111
+ or given.parity is None
112
+ or type(given) is not type(perceived)
113
+ or set(given.atoms) != set(perceived.atoms)
114
+ ):
115
+ return perceived
116
+ if isinstance(given, PlanarBond | AtropBond):
117
+ assert isinstance(perceived, PlanarBond | AtropBond) # noqa: S101
118
+ if set(given.chain) != set(perceived.chain):
119
+ return perceived
120
+ return given