react-cheminfo 0.23.0 → 0.24.0

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 (260) hide show
  1. package/README.md +39 -26
  2. package/lib/chrome/ui/HeaderToggle.d.ts +52 -0
  3. package/lib/chrome/ui/HeaderToggle.d.ts.map +1 -0
  4. package/lib/chrome/ui/HeaderToggle.js +31 -0
  5. package/lib/chrome/ui/HeaderToggle.js.map +1 -0
  6. package/lib/chrome/ui/index.d.ts +2 -0
  7. package/lib/chrome/ui/index.d.ts.map +1 -1
  8. package/lib/chrome/ui/index.js +1 -0
  9. package/lib/chrome/ui/index.js.map +1 -1
  10. package/lib/citation/core/gfn2Paper.d.ts +10 -0
  11. package/lib/citation/core/gfn2Paper.d.ts.map +1 -0
  12. package/lib/citation/core/gfn2Paper.js +28 -0
  13. package/lib/citation/core/gfn2Paper.js.map +1 -0
  14. package/lib/citation/core/index.d.ts +1 -0
  15. package/lib/citation/core/index.d.ts.map +1 -1
  16. package/lib/citation/core/index.js +1 -0
  17. package/lib/citation/core/index.js.map +1 -1
  18. package/lib/clipboard/ui/ClickToCopy.d.ts +8 -7
  19. package/lib/clipboard/ui/ClickToCopy.d.ts.map +1 -1
  20. package/lib/clipboard/ui/ClickToCopy.js +3 -3
  21. package/lib/clipboard/ui/ClickToCopy.js.map +1 -1
  22. package/lib/conformers.d.ts +27 -0
  23. package/lib/conformers.d.ts.map +1 -0
  24. package/lib/conformers.js +19 -0
  25. package/lib/conformers.js.map +1 -0
  26. package/lib/core.d.ts +1 -0
  27. package/lib/core.d.ts.map +1 -1
  28. package/lib/core.js +1 -0
  29. package/lib/core.js.map +1 -1
  30. package/lib/credits/core/credits.d.ts +12 -0
  31. package/lib/credits/core/credits.d.ts.map +1 -1
  32. package/lib/credits/core/credits.js +14 -0
  33. package/lib/credits/core/credits.js.map +1 -1
  34. package/lib/ecosystem/core/index.d.ts +1 -1
  35. package/lib/ecosystem/core/index.d.ts.map +1 -1
  36. package/lib/ecosystem/core/index.js.map +1 -1
  37. package/lib/ecosystem/core/sites.d.ts +11 -1
  38. package/lib/ecosystem/core/sites.d.ts.map +1 -1
  39. package/lib/ecosystem/core/sites.js +7 -2
  40. package/lib/ecosystem/core/sites.js.map +1 -1
  41. package/lib/ecosystem/ui/EcosystemLinks.d.ts.map +1 -1
  42. package/lib/ecosystem/ui/EcosystemLinks.js +3 -1
  43. package/lib/ecosystem/ui/EcosystemLinks.js.map +1 -1
  44. package/lib/ecosystem/ui/SiteTile.d.ts.map +1 -1
  45. package/lib/ecosystem/ui/SiteTile.js +3 -1
  46. package/lib/ecosystem/ui/SiteTile.js.map +1 -1
  47. package/lib/format/core/index.d.ts +1 -1
  48. package/lib/format/core/index.d.ts.map +1 -1
  49. package/lib/format/core/index.js +1 -1
  50. package/lib/format/core/index.js.map +1 -1
  51. package/lib/format/core/words.d.ts +9 -0
  52. package/lib/format/core/words.d.ts.map +1 -1
  53. package/lib/format/core/words.js +16 -0
  54. package/lib/format/core/words.js.map +1 -1
  55. package/lib/language/core/index.d.ts +3 -0
  56. package/lib/language/core/index.d.ts.map +1 -0
  57. package/lib/language/core/index.js +3 -0
  58. package/lib/language/core/index.js.map +1 -0
  59. package/lib/language/core/languageName.d.ts +8 -0
  60. package/lib/language/core/languageName.d.ts.map +1 -0
  61. package/lib/language/core/languageName.js +16 -0
  62. package/lib/language/core/languageName.js.map +1 -0
  63. package/lib/language/core/languageParam.d.ts +35 -0
  64. package/lib/language/core/languageParam.d.ts.map +1 -0
  65. package/lib/language/core/languageParam.js +67 -0
  66. package/lib/language/core/languageParam.js.map +1 -0
  67. package/lib/language/ui/SiteLanguage.d.ts +20 -0
  68. package/lib/language/ui/SiteLanguage.d.ts.map +1 -0
  69. package/lib/language/ui/SiteLanguage.js +14 -0
  70. package/lib/language/ui/SiteLanguage.js.map +1 -0
  71. package/lib/language/ui/index.d.ts +4 -0
  72. package/lib/language/ui/index.d.ts.map +1 -0
  73. package/lib/language/ui/index.js +3 -0
  74. package/lib/language/ui/index.js.map +1 -0
  75. package/lib/language/ui/siteLanguageContext.d.ts +12 -0
  76. package/lib/language/ui/siteLanguageContext.d.ts.map +1 -0
  77. package/lib/language/ui/siteLanguageContext.js +19 -0
  78. package/lib/language/ui/siteLanguageContext.js.map +1 -0
  79. package/lib/orbital/core/atomicOrbitals.d.ts +9 -1
  80. package/lib/orbital/core/atomicOrbitals.d.ts.map +1 -1
  81. package/lib/orbital/core/atomicOrbitals.js +7 -0
  82. package/lib/orbital/core/atomicOrbitals.js.map +1 -1
  83. package/lib/orbital/core/hydrogenic.d.ts +2 -1
  84. package/lib/orbital/core/hydrogenic.d.ts.map +1 -1
  85. package/lib/orbital/core/hydrogenic.js +2 -1
  86. package/lib/orbital/core/hydrogenic.js.map +1 -1
  87. package/lib/orbital/core/index.d.ts +1 -0
  88. package/lib/orbital/core/index.d.ts.map +1 -1
  89. package/lib/orbital/core/index.js +1 -0
  90. package/lib/orbital/core/index.js.map +1 -1
  91. package/lib/orbital/core/slaterEnergy.d.ts +35 -0
  92. package/lib/orbital/core/slaterEnergy.d.ts.map +1 -0
  93. package/lib/orbital/core/slaterEnergy.js +66 -0
  94. package/lib/orbital/core/slaterEnergy.js.map +1 -0
  95. package/lib/shared/ui/MenuButton.d.ts +3 -2
  96. package/lib/shared/ui/MenuButton.d.ts.map +1 -1
  97. package/lib/shared/ui/MenuButton.js +1 -1
  98. package/lib/shared/ui/MenuButton.js.map +1 -1
  99. package/lib/structure/core/conformerMinimise.d.ts +36 -0
  100. package/lib/structure/core/conformerMinimise.d.ts.map +1 -0
  101. package/lib/structure/core/conformerMinimise.js +81 -0
  102. package/lib/structure/core/conformerMinimise.js.map +1 -0
  103. package/lib/structure/core/conformerMinimum.d.ts +62 -0
  104. package/lib/structure/core/conformerMinimum.d.ts.map +1 -0
  105. package/lib/structure/core/conformerMinimum.js +90 -0
  106. package/lib/structure/core/conformerMinimum.js.map +1 -0
  107. package/lib/structure/core/conformerOptions.d.ts +62 -0
  108. package/lib/structure/core/conformerOptions.d.ts.map +1 -0
  109. package/lib/structure/core/conformerOptions.js +83 -0
  110. package/lib/structure/core/conformerOptions.js.map +1 -0
  111. package/lib/structure/core/conformerRefine.d.ts +104 -0
  112. package/lib/structure/core/conformerRefine.d.ts.map +1 -0
  113. package/lib/structure/core/conformerRefine.js +130 -0
  114. package/lib/structure/core/conformerRefine.js.map +1 -0
  115. package/lib/structure/core/conformerRefineRank.d.ts +36 -0
  116. package/lib/structure/core/conformerRefineRank.d.ts.map +1 -0
  117. package/lib/structure/core/conformerRefineRank.js +69 -0
  118. package/lib/structure/core/conformerRefineRank.js.map +1 -0
  119. package/lib/structure/core/conformerSession.d.ts +50 -0
  120. package/lib/structure/core/conformerSession.d.ts.map +1 -0
  121. package/lib/structure/core/conformerSession.js +88 -0
  122. package/lib/structure/core/conformerSession.js.map +1 -0
  123. package/lib/structure/core/conformerShape.d.ts +36 -0
  124. package/lib/structure/core/conformerShape.d.ts.map +1 -0
  125. package/lib/structure/core/conformerShape.js +150 -0
  126. package/lib/structure/core/conformerShape.js.map +1 -0
  127. package/lib/structure/core/conformers.d.ts +107 -0
  128. package/lib/structure/core/conformers.d.ts.map +1 -0
  129. package/lib/structure/core/conformers.js +149 -0
  130. package/lib/structure/core/conformers.js.map +1 -0
  131. package/lib/structure/core/geometryRelaxer.d.ts +66 -0
  132. package/lib/structure/core/geometryRelaxer.d.ts.map +1 -0
  133. package/lib/structure/core/geometryRelaxer.js +2 -0
  134. package/lib/structure/core/geometryRelaxer.js.map +1 -0
  135. package/lib/structure/core/index.d.ts +4 -0
  136. package/lib/structure/core/index.d.ts.map +1 -1
  137. package/lib/structure/core/index.js +2 -0
  138. package/lib/structure/core/index.js.map +1 -1
  139. package/lib/structure/core/moleculeCoordinates.d.ts +31 -0
  140. package/lib/structure/core/moleculeCoordinates.d.ts.map +1 -0
  141. package/lib/structure/core/moleculeCoordinates.js +80 -0
  142. package/lib/structure/core/moleculeCoordinates.js.map +1 -0
  143. package/lib/structure/core/molfileExport.d.ts +42 -0
  144. package/lib/structure/core/molfileExport.d.ts.map +1 -0
  145. package/lib/structure/core/molfileExport.js +47 -0
  146. package/lib/structure/core/molfileExport.js.map +1 -0
  147. package/lib/structure/core/oclResources.d.ts +12 -0
  148. package/lib/structure/core/oclResources.d.ts.map +1 -0
  149. package/lib/structure/core/oclResources.js +35 -0
  150. package/lib/structure/core/oclResources.js.map +1 -0
  151. package/lib/translate/core/catalog.d.ts +61 -0
  152. package/lib/translate/core/catalog.d.ts.map +1 -0
  153. package/lib/translate/core/catalog.js +49 -0
  154. package/lib/translate/core/catalog.js.map +1 -0
  155. package/lib/translate/core/checkTranslation.d.ts +32 -0
  156. package/lib/translate/core/checkTranslation.d.ts.map +1 -0
  157. package/lib/translate/core/checkTranslation.js +98 -0
  158. package/lib/translate/core/checkTranslation.js.map +1 -0
  159. package/lib/translate/core/contribution.d.ts +88 -0
  160. package/lib/translate/core/contribution.d.ts.map +1 -0
  161. package/lib/translate/core/contribution.js +14 -0
  162. package/lib/translate/core/contribution.js.map +1 -0
  163. package/lib/translate/core/index.d.ts +19 -0
  164. package/lib/translate/core/index.d.ts.map +1 -0
  165. package/lib/translate/core/index.js +15 -0
  166. package/lib/translate/core/index.js.map +1 -0
  167. package/lib/translate/core/marker.d.ts +45 -0
  168. package/lib/translate/core/marker.d.ts.map +1 -0
  169. package/lib/translate/core/marker.js +79 -0
  170. package/lib/translate/core/marker.js.map +1 -0
  171. package/lib/translate/core/mergeMessages.d.ts +35 -0
  172. package/lib/translate/core/mergeMessages.d.ts.map +1 -0
  173. package/lib/translate/core/mergeMessages.js +69 -0
  174. package/lib/translate/core/mergeMessages.js.map +1 -0
  175. package/lib/translate/core/session.d.ts +102 -0
  176. package/lib/translate/core/session.d.ts.map +1 -0
  177. package/lib/translate/core/session.js +162 -0
  178. package/lib/translate/core/session.js.map +1 -0
  179. package/lib/translate/core/start.d.ts +70 -0
  180. package/lib/translate/core/start.d.ts.map +1 -0
  181. package/lib/translate/core/start.js +73 -0
  182. package/lib/translate/core/start.js.map +1 -0
  183. package/lib/translate/core/suggestions.d.ts +77 -0
  184. package/lib/translate/core/suggestions.d.ts.map +1 -0
  185. package/lib/translate/core/suggestions.js +143 -0
  186. package/lib/translate/core/suggestions.js.map +1 -0
  187. package/lib/translate/core/tables.d.ts +100 -0
  188. package/lib/translate/core/tables.d.ts.map +1 -0
  189. package/lib/translate/core/tables.js +51 -0
  190. package/lib/translate/core/tables.js.map +1 -0
  191. package/lib/translate.d.ts +2 -0
  192. package/lib/translate.d.ts.map +1 -0
  193. package/lib/translate.js +6 -0
  194. package/lib/translate.js.map +1 -0
  195. package/lib/ui.d.ts +1 -0
  196. package/lib/ui.d.ts.map +1 -1
  197. package/lib/ui.js +1 -0
  198. package/lib/ui.js.map +1 -1
  199. package/lib/xtb/core/xtbRelaxer.d.ts +61 -0
  200. package/lib/xtb/core/xtbRelaxer.d.ts.map +1 -0
  201. package/lib/xtb/core/xtbRelaxer.js +108 -0
  202. package/lib/xtb/core/xtbRelaxer.js.map +1 -0
  203. package/lib/xtb.d.ts +11 -0
  204. package/lib/xtb.d.ts.map +1 -0
  205. package/lib/xtb.js +10 -0
  206. package/lib/xtb.js.map +1 -0
  207. package/package.json +18 -11
  208. package/src/chrome/ui/HeaderToggle.tsx +100 -0
  209. package/src/chrome/ui/index.ts +2 -0
  210. package/src/citation/core/gfn2Paper.ts +32 -0
  211. package/src/citation/core/index.ts +1 -0
  212. package/src/clipboard/ui/ClickToCopy.tsx +8 -7
  213. package/src/conformers.ts +87 -0
  214. package/src/core.ts +1 -0
  215. package/src/credits/core/credits.ts +16 -0
  216. package/src/ecosystem/core/index.ts +1 -0
  217. package/src/ecosystem/core/sites.ts +20 -2
  218. package/src/ecosystem/ui/EcosystemLinks.tsx +3 -1
  219. package/src/ecosystem/ui/SiteTile.tsx +3 -1
  220. package/src/format/core/index.ts +1 -1
  221. package/src/format/core/words.ts +19 -0
  222. package/src/language/core/index.ts +7 -0
  223. package/src/language/core/languageName.ts +16 -0
  224. package/src/language/core/languageParam.ts +70 -0
  225. package/src/language/ui/SiteLanguage.tsx +26 -0
  226. package/src/language/ui/index.ts +3 -0
  227. package/src/language/ui/siteLanguageContext.ts +20 -0
  228. package/src/orbital/core/atomicOrbitals.ts +17 -1
  229. package/src/orbital/core/hydrogenic.ts +2 -1
  230. package/src/orbital/core/index.ts +1 -0
  231. package/src/orbital/core/slaterEnergy.ts +83 -0
  232. package/src/shared/ui/MenuButton.tsx +4 -3
  233. package/src/structure/core/conformerMinimise.ts +99 -0
  234. package/src/structure/core/conformerMinimum.ts +125 -0
  235. package/src/structure/core/conformerOptions.ts +125 -0
  236. package/src/structure/core/conformerRefine.ts +234 -0
  237. package/src/structure/core/conformerRefineRank.ts +89 -0
  238. package/src/structure/core/conformerSession.ts +123 -0
  239. package/src/structure/core/conformerShape.ts +181 -0
  240. package/src/structure/core/conformers.ts +282 -0
  241. package/src/structure/core/geometryRelaxer.ts +71 -0
  242. package/src/structure/core/index.ts +13 -0
  243. package/src/structure/core/moleculeCoordinates.ts +93 -0
  244. package/src/structure/core/molfileExport.ts +62 -0
  245. package/src/structure/core/oclResources.ts +41 -0
  246. package/src/translate/core/catalog.ts +83 -0
  247. package/src/translate/core/checkTranslation.ts +128 -0
  248. package/src/translate/core/contribution.ts +99 -0
  249. package/src/translate/core/index.ts +72 -0
  250. package/src/translate/core/marker.ts +85 -0
  251. package/src/translate/core/mergeMessages.ts +74 -0
  252. package/src/translate/core/session.ts +250 -0
  253. package/src/translate/core/start.ts +121 -0
  254. package/src/translate/core/suggestions.ts +211 -0
  255. package/src/translate/core/tables.ts +120 -0
  256. package/src/translate.ts +5 -0
  257. package/src/ui.ts +1 -0
  258. package/src/xtb/core/xtbRelaxer.ts +160 -0
  259. package/src/xtb.ts +16 -0
  260. package/styles/chrome.css +61 -30
@@ -0,0 +1,99 @@
1
+ import type { Molecule } from 'openchemlib';
2
+ import { ForceFieldMMFF94 } from 'openchemlib';
3
+
4
+ import { ordinal } from '../../format/core/words.ts';
5
+
6
+ import type { ConformerOptions } from './conformerOptions.ts';
7
+
8
+ /**
9
+ * Relax one conformer in place and return its energy.
10
+ *
11
+ * Minimisation is best-effort. MMFF94 cannot type every element (boron, for
12
+ * one), so a conformer it refuses gets a `null` energy and a warning rather
13
+ * than an exception. A non-zero return code from `minimise()` is *not* a
14
+ * failure: ethyne returns 2 with a perfect geometry.
15
+ *
16
+ * Warnings count the conformers the generator handed back, kept or not, so one
17
+ * number never means two things — a row number would, once one is dropped.
18
+ * @param conformer - The conformer to relax. Mutated.
19
+ * @param options - Force field and iteration budget.
20
+ * @param produced - 1-based position of the conformer in the generator's output.
21
+ * @param warnings - Receives one sentence when the energy is unknown.
22
+ * @returns Total energy in kcal/mol, or `null` when none was computed.
23
+ */
24
+ export function minimiseConformer(
25
+ conformer: Molecule,
26
+ options: ConformerOptions,
27
+ produced: number,
28
+ warnings: string[],
29
+ ): number | null {
30
+ const algorithm = options.minimisation;
31
+ if (algorithm === 'none') return null;
32
+ let energy: number;
33
+ try {
34
+ const forceField = new ForceFieldMMFF94(conformer, algorithm, {});
35
+ forceField.minimise({ maxIts: options.maxIterations });
36
+ energy = forceField.getTotalEnergy();
37
+ } catch (error) {
38
+ warnings.push(
39
+ `The ${ordinal(produced)} conformer produced could not be minimised with ${algorithm} (${readable(error)}), so its energy is unknown.`,
40
+ );
41
+ return null;
42
+ }
43
+ if (!Number.isFinite(energy)) {
44
+ warnings.push(
45
+ `The ${ordinal(produced)} conformer produced minimised to a non-finite ${algorithm} energy, so its energy is unknown.`,
46
+ );
47
+ return null;
48
+ }
49
+ return energy;
50
+ }
51
+
52
+ /** What {@link rankByEnergy} reads and rewrites on a conformer. */
53
+ export interface RankedConformer {
54
+ id: number;
55
+ energy: number | null;
56
+ relativeEnergy: number | null;
57
+ }
58
+
59
+ /**
60
+ * Order a set from the most stable conformer down, number it from one in that
61
+ * order, and measure every known energy from the lowest.
62
+ *
63
+ * A conformer without an energy ranks after every one that has one; equal
64
+ * energies, and conformers without one, keep the order they came in.
65
+ * @param conformers - The set. Each `id` and `relativeEnergy` is rewritten in place.
66
+ * @returns The same conformers, most stable first.
67
+ */
68
+ export function rankByEnergy<T extends RankedConformer>(
69
+ conformers: readonly T[],
70
+ ): T[] {
71
+ assignRelativeEnergies(conformers);
72
+ const ranked = conformers.toSorted(compareEnergy);
73
+ for (const [index, conformer] of ranked.entries()) conformer.id = index + 1;
74
+ return ranked;
75
+ }
76
+
77
+ function compareEnergy(a: RankedConformer, b: RankedConformer): number {
78
+ if (a.energy === null) return b.energy === null ? 0 : 1;
79
+ if (b.energy === null) return -1;
80
+ return a.energy - b.energy;
81
+ }
82
+
83
+ function assignRelativeEnergies(conformers: readonly RankedConformer[]): void {
84
+ let lowest = Number.POSITIVE_INFINITY;
85
+ for (const conformer of conformers) {
86
+ const energy = conformer.energy;
87
+ if (energy !== null && energy < lowest) lowest = energy;
88
+ }
89
+ if (lowest === Number.POSITIVE_INFINITY) return;
90
+ for (const conformer of conformers) {
91
+ const energy = conformer.energy;
92
+ if (energy !== null) conformer.relativeEnergy = energy - lowest;
93
+ }
94
+ }
95
+
96
+ function readable(error: unknown): string {
97
+ const message = error instanceof Error ? error.message : String(error);
98
+ return message.replace(/^\w+\$\w+: /, '');
99
+ }
@@ -0,0 +1,125 @@
1
+ import { Molecule } from 'openchemlib';
2
+
3
+ import type { ConformerShape } from './conformerShape.ts';
4
+ import { conformerShape } from './conformerShape.ts';
5
+
6
+ /** What {@link isKeptMinimum} and {@link minimisedShapes} read of a conformer. */
7
+ export interface KeptConformer {
8
+ /** Total energy in kcal/mol, `null` when it was not computed. */
9
+ energy: number | null;
10
+ /** The 3D structure, as molfile text. */
11
+ molfile: { data: string };
12
+ }
13
+
14
+ /**
15
+ * Energy gap, in kcal/mol, under which two minimised conformers can be the
16
+ * same minimum. Copies of one minimum agree to about 1e-7; distinct minima
17
+ * that close in energy differ in shape by more than a percent.
18
+ */
19
+ export const SAME_ENERGY_TOLERANCE = 1e-3;
20
+
21
+ /** Gap between principal moments, relative to the largest, under which two shapes are one. */
22
+ export const SAME_SHAPE_TOLERANCE = 1e-3;
23
+
24
+ /**
25
+ * Gap in chirality under which two shapes are one. Copies of one minimum agree
26
+ * to about 1e-4 and a shape that is its own mirror image reads within 1e-3 of
27
+ * zero, while the mirror images of a chiral minimum usually sit tenths apart.
28
+ */
29
+ export const SAME_CHIRALITY_TOLERANCE = 1e-2;
30
+
31
+ /**
32
+ * Whether two minimised conformers are one energy minimum: the same energy,
33
+ * the same principal moments and the same chirality. The two mirror-image
34
+ * gauche butanes are two minima; the two chairs of cyclohexane, each its own
35
+ * mirror image, are one.
36
+ * @param energyA - Total energy of the first conformer, kcal/mol.
37
+ * @param shapeA - Shape of the first conformer.
38
+ * @param energyB - Total energy of the second conformer, kcal/mol.
39
+ * @param shapeB - Shape of the second conformer.
40
+ * @returns `true` when both agree within {@link SAME_ENERGY_TOLERANCE}, {@link SAME_SHAPE_TOLERANCE} and {@link SAME_CHIRALITY_TOLERANCE}.
41
+ */
42
+ export function isSameMinimum(
43
+ energyA: number,
44
+ shapeA: ConformerShape,
45
+ energyB: number,
46
+ shapeB: ConformerShape,
47
+ ): boolean {
48
+ if (Math.abs(energyA - energyB) > SAME_ENERGY_TOLERANCE) return false;
49
+ return isSameShape(shapeA, shapeB);
50
+ }
51
+
52
+ /**
53
+ * Whether two conformers have the same shape: the same principal moments and
54
+ * the same chirality, energies not consulted. Two mirror images share the
55
+ * moments and not the chirality, so they are two shapes.
56
+ * @param shapeA - Shape of one conformer.
57
+ * @param shapeB - Shape of the other.
58
+ * @returns `true` when both agree within {@link SAME_SHAPE_TOLERANCE} and {@link SAME_CHIRALITY_TOLERANCE}.
59
+ */
60
+ export function isSameShape(
61
+ shapeA: ConformerShape,
62
+ shapeB: ConformerShape,
63
+ ): boolean {
64
+ if (
65
+ Math.abs(shapeA.chirality - shapeB.chirality) > SAME_CHIRALITY_TOLERANCE
66
+ ) {
67
+ return false;
68
+ }
69
+ const momentsA = shapeA.moments;
70
+ const momentsB = shapeB.moments;
71
+ const scale = Math.max(momentsA[2], momentsB[2]);
72
+ for (let axis = 0; axis < 3; axis++) {
73
+ const gap = Math.abs((momentsA[axis] ?? 0) - (momentsB[axis] ?? 0));
74
+ if (gap > SAME_SHAPE_TOLERANCE * scale) return false;
75
+ }
76
+ return true;
77
+ }
78
+
79
+ /**
80
+ * Whether a minimised conformer lands in a minimum one of `kept` already holds.
81
+ * @param energy - Total energy of the candidate, kcal/mol.
82
+ * @param shape - Shape of the candidate.
83
+ * @param kept - The conformers kept so far.
84
+ * @param keptShapes - Their shapes, index for index; `null` where there is no energy.
85
+ * @returns `true` when one of them is the same minimum.
86
+ */
87
+ export function isKeptMinimum(
88
+ energy: number,
89
+ shape: ConformerShape,
90
+ kept: readonly KeptConformer[],
91
+ keptShapes: ReadonlyArray<ConformerShape | null>,
92
+ ): boolean {
93
+ for (let index = 0; index < kept.length; index++) {
94
+ const keptEnergy = kept[index]?.energy ?? null;
95
+ const keptShape = keptShapes[index] ?? null;
96
+ if (
97
+ keptEnergy !== null &&
98
+ keptShape !== null &&
99
+ isSameMinimum(energy, shape, keptEnergy, keptShape)
100
+ ) {
101
+ return true;
102
+ }
103
+ }
104
+ return false;
105
+ }
106
+
107
+ /**
108
+ * Shapes of conformers read back from their molfiles, so a continuation
109
+ * compares new conformers with the ones a previous run kept.
110
+ * @param conformers - Conformers of an earlier run.
111
+ * @returns Their shapes, index for index; `null` for one without an energy.
112
+ */
113
+ export function minimisedShapes(
114
+ conformers: readonly KeptConformer[],
115
+ ): Array<ConformerShape | null> {
116
+ const shapes: Array<ConformerShape | null> = [];
117
+ for (const conformer of conformers) {
118
+ shapes.push(
119
+ conformer.energy === null
120
+ ? null
121
+ : conformerShape(Molecule.fromMolfile(conformer.molfile.data)),
122
+ );
123
+ }
124
+ return shapes;
125
+ }
@@ -0,0 +1,125 @@
1
+ /** The four torsion-set strategies OpenChemLib's `ConformerGenerator` offers. */
2
+ export const CONFORMER_STRATEGIES = [
3
+ 'adaptive-random',
4
+ 'likely-random',
5
+ 'pure-random',
6
+ 'likely-systematic',
7
+ ] as const;
8
+
9
+ /** One of {@link CONFORMER_STRATEGIES}. */
10
+ export type ConformerStrategy = (typeof CONFORMER_STRATEGIES)[number];
11
+
12
+ /** Short names for the strategy picker. */
13
+ export const STRATEGY_LABELS: Record<ConformerStrategy, string> = {
14
+ 'adaptive-random': 'Adaptive random',
15
+ 'likely-random': 'Likely random',
16
+ 'pure-random': 'Pure random',
17
+ 'likely-systematic': 'Likely systematic',
18
+ };
19
+
20
+ /** One sentence per strategy, shown as help text next to the picker. */
21
+ export const STRATEGY_DETAILS: Record<ConformerStrategy, string> = {
22
+ 'adaptive-random':
23
+ 'Draws torsions at random but steers away from the combinations that already collided, which covers a flexible molecule fastest.',
24
+ 'likely-random':
25
+ 'Draws torsions at random from the torsion database, weighted by how often each angle is seen in crystal structures.',
26
+ 'pure-random':
27
+ 'Draws every torsion uniformly at random, ignoring how likely the angle is — the widest spread and the least realistic set.',
28
+ 'likely-systematic':
29
+ 'Walks the torsion combinations in decreasing likelihood, so the first conformers are the most probable ones and the set is reproducible.',
30
+ };
31
+
32
+ /** Force fields offered for the optional minimisation step. */
33
+ export const MINIMISATION_ALGORITHMS = [
34
+ 'none',
35
+ 'MMFF94',
36
+ 'MMFF94s',
37
+ 'MMFF94s+',
38
+ ] as const;
39
+
40
+ /** One of {@link MINIMISATION_ALGORITHMS}. */
41
+ export type MinimisationAlgorithm = (typeof MINIMISATION_ALGORITHMS)[number];
42
+
43
+ /** Short names for the minimisation picker. */
44
+ export const MINIMISATION_LABELS: Record<MinimisationAlgorithm, string> = {
45
+ none: 'None (no energies)',
46
+ MMFF94: 'MMFF94',
47
+ MMFF94s: 'MMFF94s',
48
+ 'MMFF94s+': 'MMFF94s+',
49
+ };
50
+
51
+ /** Everything a conformer run needs, in one serialisable object. */
52
+ export interface ConformerOptions {
53
+ /** Which torsion-set strategy the generator follows. */
54
+ strategy: ConformerStrategy;
55
+ /** Upper bound on the number of conformers kept. */
56
+ maxConformers: number;
57
+ /** Upper bound on the distinct torsion sets the strategy will try. */
58
+ maxTorsionSets: number;
59
+ /** Rotate every rotatable bond in 60° steps instead of using the torsion database. */
60
+ use60DegreeSteps: boolean;
61
+ /** Seed of the generator's random source, so a run is reproducible. */
62
+ seed: number;
63
+ /** Force field used to relax each conformer; `none` leaves the energies unknown. */
64
+ minimisation: MinimisationAlgorithm;
65
+ /** Iteration budget of a single minimisation. */
66
+ maxIterations: number;
67
+ /** Wall-clock budget of the whole run, in seconds. */
68
+ timeoutSeconds: number;
69
+ }
70
+
71
+ /** The settings a first-time visitor gets. */
72
+ export const DEFAULT_CONFORMER_OPTIONS: ConformerOptions = {
73
+ strategy: 'adaptive-random',
74
+ maxConformers: 10,
75
+ maxTorsionSets: 10_000,
76
+ use60DegreeSteps: false,
77
+ seed: 42,
78
+ minimisation: 'MMFF94s+',
79
+ maxIterations: 4000,
80
+ timeoutSeconds: 10,
81
+ };
82
+
83
+ /**
84
+ * Whether two option sets would run the same calculation.
85
+ * @param first - One option set.
86
+ * @param second - The other.
87
+ * @returns `true` when every option is equal.
88
+ */
89
+ export function sameConformerOptions(
90
+ first: ConformerOptions,
91
+ second: ConformerOptions,
92
+ ): boolean {
93
+ for (const key of Object.keys(first) as Array<keyof ConformerOptions>) {
94
+ if (first[key] !== second[key]) return false;
95
+ }
96
+ return true;
97
+ }
98
+
99
+ /**
100
+ * Rotatable-bond count above which the conformer space is large enough that the
101
+ * run is worth warning about before it starts.
102
+ */
103
+ export const ROTATABLE_BOND_WARNING = 12;
104
+
105
+ /**
106
+ * Narrow an arbitrary string, typically a URL parameter or a stored preference,
107
+ * to a strategy id.
108
+ * @param value - Text to test.
109
+ * @returns Whether `value` is one of {@link CONFORMER_STRATEGIES}.
110
+ */
111
+ export function isConformerStrategy(value: string): value is ConformerStrategy {
112
+ return (CONFORMER_STRATEGIES as readonly string[]).includes(value);
113
+ }
114
+
115
+ /**
116
+ * Narrow an arbitrary string, typically a stored preference, to a force-field
117
+ * id.
118
+ * @param value - Text to test.
119
+ * @returns Whether `value` is one of {@link MINIMISATION_ALGORITHMS}.
120
+ */
121
+ export function isMinimisationAlgorithm(
122
+ value: string,
123
+ ): value is MinimisationAlgorithm {
124
+ return (MINIMISATION_ALGORITHMS as readonly string[]).includes(value);
125
+ }
@@ -0,0 +1,234 @@
1
+ import { Molecule } from 'openchemlib';
2
+
3
+ import { ordinal } from '../../format/core/words.ts';
4
+
5
+ import type { RefinedMinimum } from './conformerRefineRank.ts';
6
+ import {
7
+ isRefinedDuplicate,
8
+ rankByRefinedEnergy,
9
+ } from './conformerRefineRank.ts';
10
+ import { conformerShape } from './conformerShape.ts';
11
+ import type { Conformer, ConformerSet } from './conformers.ts';
12
+ import type {
13
+ GeometryRelaxer,
14
+ RelaxableGeometry,
15
+ RelaxerOptions,
16
+ } from './geometryRelaxer.ts';
17
+ import {
18
+ centredRmsd,
19
+ readRelaxableGeometry,
20
+ writeRelaxedCoordinates,
21
+ } from './moleculeCoordinates.ts';
22
+ import { toMolfileExport } from './molfileExport.ts';
23
+
24
+ /** What refining one conformer produced, on top of its force-field numbers. */
25
+ export interface ConformerRefinement {
26
+ /**
27
+ * The rank this conformer held before the refinement, which is the `id` it
28
+ * carried in the force-field set. Refinement hands the ids out again, so this
29
+ * is what a reader compares the new rank against and what a caller uses to
30
+ * find again the conformer that was on screen.
31
+ */
32
+ forceFieldId: number;
33
+ /** Total energy at the relaxed geometry, kcal/mol. */
34
+ energy: number;
35
+ /** Energy above the most stable refined conformer of the set, kcal/mol. */
36
+ relativeEnergy: number;
37
+ /**
38
+ * The dispersion term of {@link ConformerRefinement.energy}, kcal/mol, or
39
+ * `null` from a method that has none.
40
+ */
41
+ dispersionEnergy: number | null;
42
+ /**
43
+ * The force-field energy this conformer had before, kcal/mol, `null` when it
44
+ * had none. Kept so the two rankings can be read against each other, which is
45
+ * the whole reason to refine.
46
+ */
47
+ forceFieldEnergy: number | null;
48
+ /** How far the atoms moved from the force-field geometry, RMSD in ångström. */
49
+ rmsd: number;
50
+ /** Optimizer cycles spent. */
51
+ cycles: number;
52
+ /** Whether the optimizer met its convergence criteria. */
53
+ converged: boolean;
54
+ }
55
+
56
+ /** What a whole refinement pass produced, recorded on the set. */
57
+ export interface ConformerSetRefinement {
58
+ /** The method, as it is shown to a reader, e.g. `GFN2-xTB`. */
59
+ method: string;
60
+ /** Wall-clock duration of the pass, in milliseconds. */
61
+ elapsedMilliseconds: number;
62
+ /** How many conformers were dropped as duplicates of a kept minimum. */
63
+ merged: number;
64
+ /**
65
+ * How many conformers the refined ranking put at a different position,
66
+ * degenerate conformers swapping places included — so it is a fact about the
67
+ * two orders, not a measure of how much the methods disagree. The two relative
68
+ * energies are what a reader should be shown.
69
+ */
70
+ reordered: number;
71
+ }
72
+
73
+ /** How a refinement pass is tuned, watched and cancelled. */
74
+ export interface RefineConformersOptions extends RelaxerOptions {
75
+ /**
76
+ * Name of the method, for the label the UI shows.
77
+ * @default 'GFN2-xTB'
78
+ */
79
+ method?: string;
80
+ /**
81
+ * Energy gap in kcal/mol under which two refined conformers are taken to be
82
+ * one minimum.
83
+ * @default REFINED_SAME_ENERGY_TOLERANCE
84
+ */
85
+ sameEnergyTolerance?: number;
86
+ }
87
+
88
+ /**
89
+ * Energy gap in kcal/mol under which two relaxed conformers can be one minimum.
90
+ *
91
+ * Measured with `benchmark/conformerRefine.js scatter`: one butane minimum
92
+ * reached from six geometries perturbed by up to 0.02 Å lands within 4.9e-6
93
+ * kcal/mol, in 8 to 20 optimizer cycles. So 1e-4 is twenty times the scatter of
94
+ * one minimum, and ten times below the closest gap between two real minima seen
95
+ * on a flexible molecule — ibuprofen has distinct conformers 1e-3 kcal/mol
96
+ * apart. The energy is only the cheap half of the test in any case: what decides
97
+ * that two conformers are one minimum is the shape.
98
+ */
99
+ export const REFINED_SAME_ENERGY_TOLERANCE = 1e-4;
100
+
101
+ /** What is shown when a conformer set is ranked by the refined energies. */
102
+ export const REFINED_RANKING = 'refined';
103
+
104
+ /**
105
+ * Relax every conformer of a set with a better method and rank the set by what
106
+ * that method says.
107
+ *
108
+ * This is the opt-in second stage of a conformer search, opt-in because it costs
109
+ * seconds where the force field costs milliseconds. MMFF94 places the atoms well
110
+ * but ranks conformers badly — it has no dispersion and no electronic structure,
111
+ * and those are what decide which conformer is lowest. So both numbers are kept:
112
+ * `conformer.energy` stays the force field's, `conformer.refinement.energy` is
113
+ * the new one, and `set.rankedBy` says which of the two the order follows.
114
+ *
115
+ * Refinement changes the set, not only its order: conformers the force field
116
+ * held apart can relax into one minimum, and the first of them is the one that
117
+ * survives. Ids are handed out again over the ranked result, so a caller holding
118
+ * an id must look its conformer up again.
119
+ *
120
+ * A conformer without a force-field energy is refined like any other; its
121
+ * `forceFieldEnergy` is `null` and it takes its place by the refined energy.
122
+ * @param set - The set to refine. Not modified.
123
+ * @param relax - The engine, e.g. `xtbRelaxer` from `react-cheminfo/xtb`.
124
+ * @param options - Method name, duplicate tolerance, progress and cancellation.
125
+ * @returns A new set, ranked by the refined energies.
126
+ * @throws {Error} When the relaxer fails or is cancelled, or answers with the
127
+ * wrong number of geometries.
128
+ */
129
+ export async function refineConformers(
130
+ set: ConformerSet,
131
+ relax: GeometryRelaxer,
132
+ options: RefineConformersOptions = {},
133
+ ): Promise<ConformerSet> {
134
+ const startedAt = performance.now();
135
+ const method = options.method ?? 'GFN2-xTB';
136
+ if (set.conformers.length === 0) {
137
+ return {
138
+ ...set,
139
+ rankedBy: REFINED_RANKING,
140
+ refinement: { method, elapsedMilliseconds: 0, merged: 0, reordered: 0 },
141
+ };
142
+ }
143
+
144
+ const prepared: Array<{
145
+ conformer: Conformer;
146
+ molecule: Molecule;
147
+ geometry: RelaxableGeometry;
148
+ }> = [];
149
+ for (const conformer of set.conformers) {
150
+ const molecule = Molecule.fromMolfile(conformer.molfile.data);
151
+ prepared.push({
152
+ conformer,
153
+ molecule,
154
+ geometry: readRelaxableGeometry(molecule),
155
+ });
156
+ }
157
+
158
+ const relaxed = await relax(
159
+ prepared.map((entry) => entry.geometry),
160
+ {
161
+ onSettled: options.onSettled,
162
+ signal: options.signal,
163
+ },
164
+ );
165
+ if (relaxed.length !== set.conformers.length) {
166
+ throw new Error(
167
+ `The relaxer answered with ${relaxed.length} geometries for ${set.conformers.length} conformers.`,
168
+ );
169
+ }
170
+
171
+ const tolerance =
172
+ options.sameEnergyTolerance ?? REFINED_SAME_ENERGY_TOLERANCE;
173
+ const warnings = [...set.warnings];
174
+ const kept: Conformer[] = [];
175
+ const minima: RefinedMinimum[] = [];
176
+ let merged = 0;
177
+
178
+ for (const [index, entry] of prepared.entries()) {
179
+ const result = relaxed[index];
180
+ if (result === undefined) continue;
181
+ const { conformer, molecule, geometry } = entry;
182
+ writeRelaxedCoordinates(molecule, result.coordinates);
183
+ const candidate: RefinedMinimum = {
184
+ energy: result.energy,
185
+ shape: conformerShape(molecule),
186
+ };
187
+ if (isRefinedDuplicate(candidate, minima, tolerance)) {
188
+ merged++;
189
+ continue;
190
+ }
191
+ for (const warning of result.warnings) {
192
+ warnings.push(`The ${ordinal(conformer.id)} conformer: ${warning}`);
193
+ }
194
+ if (!result.converged) {
195
+ warnings.push(
196
+ `The ${ordinal(conformer.id)} conformer did not reach a ${method} minimum in ${result.cycles} cycles, so its refined energy is an upper bound.`,
197
+ );
198
+ }
199
+ kept.push({
200
+ ...conformer,
201
+ molfile: toMolfileExport(molecule),
202
+ refinement: {
203
+ forceFieldId: conformer.id,
204
+ energy: result.energy,
205
+ relativeEnergy: 0,
206
+ dispersionEnergy: result.dispersionEnergy,
207
+ forceFieldEnergy: conformer.energy,
208
+ rmsd: centredRmsd(geometry.coordinates, result.coordinates),
209
+ cycles: result.cycles,
210
+ converged: result.converged,
211
+ },
212
+ });
213
+ minima.push(candidate);
214
+ }
215
+
216
+ if (merged > 0) {
217
+ warnings.push(
218
+ `${merged} conformer(s) relaxed into a ${method} minimum the set already held, so they were dropped.`,
219
+ );
220
+ }
221
+ const ranked = rankByRefinedEnergy(kept);
222
+ return {
223
+ ...set,
224
+ conformers: ranked.conformers,
225
+ warnings,
226
+ rankedBy: REFINED_RANKING,
227
+ refinement: {
228
+ method,
229
+ elapsedMilliseconds: performance.now() - startedAt,
230
+ merged,
231
+ reordered: ranked.reordered,
232
+ },
233
+ };
234
+ }
@@ -0,0 +1,89 @@
1
+ import { isSameShape } from './conformerMinimum.ts';
2
+ import type { ConformerShape } from './conformerShape.ts';
3
+ import type { Conformer } from './conformers.ts';
4
+
5
+ /** A refined conformer's energy and shape, which is what tells two minima apart. */
6
+ export interface RefinedMinimum {
7
+ /** Refined total energy, kcal/mol. */
8
+ energy: number;
9
+ shape: ConformerShape;
10
+ }
11
+
12
+ /** A ranked set, and how far it moved from the order it came in. */
13
+ export interface RefinedRanking {
14
+ /** The conformers, most stable first, renumbered from one in that order. */
15
+ conformers: Conformer[];
16
+ /** How many conformers ended up at a different position than they came in at. */
17
+ reordered: number;
18
+ }
19
+
20
+ /**
21
+ * Order refined conformers from the most stable down, number them from one in
22
+ * that order, and measure every refined energy from the lowest.
23
+ *
24
+ * `reordered` counts the conformers whose position changed, which is the number
25
+ * worth showing: it says how much the better method disagreed with the force
26
+ * field.
27
+ * @param conformers - The refined conformers, in the force field's own order.
28
+ * @returns The ranked conformers and the disagreement count.
29
+ */
30
+ export function rankByRefinedEnergy(
31
+ conformers: readonly Conformer[],
32
+ ): RefinedRanking {
33
+ let lowest = Number.POSITIVE_INFINITY;
34
+ for (const conformer of conformers) {
35
+ const energy = conformer.refinement?.energy;
36
+ if (energy !== undefined && energy < lowest) lowest = energy;
37
+ }
38
+ const ranked = conformers.toSorted(compareRefined);
39
+ const result: Conformer[] = [];
40
+ let reordered = 0;
41
+ for (const [index, conformer] of ranked.entries()) {
42
+ if (conformer.id !== index + 1) reordered++;
43
+ const refinement = conformer.refinement;
44
+ result.push({
45
+ ...conformer,
46
+ id: index + 1,
47
+ refinement:
48
+ refinement === null
49
+ ? null
50
+ : { ...refinement, relativeEnergy: refinement.energy - lowest },
51
+ });
52
+ }
53
+ return { conformers: result, reordered };
54
+ }
55
+
56
+ /**
57
+ * Whether a refined conformer landed in a minimum one of `kept` already holds:
58
+ * the same energy within `tolerance`, and the same shape.
59
+ * @param candidate - The refined conformer's energy and shape.
60
+ * @param kept - The minima kept so far.
61
+ * @param tolerance - Energy gap in kcal/mol under which two energies are one.
62
+ * @returns `true` when one of them is the same minimum.
63
+ */
64
+ export function isRefinedDuplicate(
65
+ candidate: RefinedMinimum,
66
+ kept: readonly RefinedMinimum[],
67
+ tolerance: number,
68
+ ): boolean {
69
+ for (const entry of kept) {
70
+ if (Math.abs(candidate.energy - entry.energy) > tolerance) continue;
71
+ if (isSameShape(candidate.shape, entry.shape)) return true;
72
+ }
73
+ return false;
74
+ }
75
+
76
+ /**
77
+ * Order two conformers by refined energy; one without a refinement ranks after
78
+ * every one that has one.
79
+ * @param a - One conformer.
80
+ * @param b - The other.
81
+ * @returns Negative when `a` is the more stable, positive when `b` is.
82
+ */
83
+ function compareRefined(a: Conformer, b: Conformer): number {
84
+ const energyA = a.refinement?.energy;
85
+ const energyB = b.refinement?.energy;
86
+ if (energyA === undefined) return energyB === undefined ? 0 : 1;
87
+ if (energyB === undefined) return -1;
88
+ return energyA - energyB;
89
+ }