react-cheminfo 0.134.0 → 0.135.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.
- package/README.md +13 -0
- package/lib/crystal/core/bondGrid.d.ts +26 -0
- package/lib/crystal/core/bondGrid.d.ts.map +1 -0
- package/lib/crystal/core/bondGrid.js +76 -0
- package/lib/crystal/core/bondGrid.js.map +1 -0
- package/lib/crystal/core/cellBonds.d.ts +21 -0
- package/lib/crystal/core/cellBonds.d.ts.map +1 -0
- package/lib/crystal/core/cellBonds.js +100 -0
- package/lib/crystal/core/cellBonds.js.map +1 -0
- package/lib/crystal/core/cif/element.d.ts +9 -0
- package/lib/crystal/core/cif/element.d.ts.map +1 -0
- package/lib/crystal/core/cif/element.js +41 -0
- package/lib/crystal/core/cif/element.js.map +1 -0
- package/lib/crystal/core/cif/index.d.ts +4 -0
- package/lib/crystal/core/cif/index.d.ts.map +1 -0
- package/lib/crystal/core/cif/index.js +3 -0
- package/lib/crystal/core/cif/index.js.map +1 -0
- package/lib/crystal/core/cif/number.d.ts +27 -0
- package/lib/crystal/core/cif/number.d.ts.map +1 -0
- package/lib/crystal/core/cif/number.js +57 -0
- package/lib/crystal/core/cif/number.js.map +1 -0
- package/lib/crystal/core/cif/read.d.ts +12 -0
- package/lib/crystal/core/cif/read.d.ts.map +1 -0
- package/lib/crystal/core/cif/read.js +183 -0
- package/lib/crystal/core/cif/read.js.map +1 -0
- package/lib/crystal/core/cif/readGeometry.d.ts +17 -0
- package/lib/crystal/core/cif/readGeometry.d.ts.map +1 -0
- package/lib/crystal/core/cif/readGeometry.js +93 -0
- package/lib/crystal/core/cif/readGeometry.js.map +1 -0
- package/lib/crystal/core/cif/tags.d.ts +62 -0
- package/lib/crystal/core/cif/tags.d.ts.map +1 -0
- package/lib/crystal/core/cif/tags.js +115 -0
- package/lib/crystal/core/cif/tags.js.map +1 -0
- package/lib/crystal/core/cif/types.d.ts +158 -0
- package/lib/crystal/core/cif/types.d.ts.map +1 -0
- package/lib/crystal/core/cif/types.js +2 -0
- package/lib/crystal/core/cif/types.js.map +1 -0
- package/lib/crystal/core/components.d.ts +56 -0
- package/lib/crystal/core/components.d.ts.map +1 -0
- package/lib/crystal/core/components.js +132 -0
- package/lib/crystal/core/components.js.map +1 -0
- package/lib/crystal/core/covalentRadii.d.ts +24 -0
- package/lib/crystal/core/covalentRadii.d.ts.map +1 -0
- package/lib/crystal/core/covalentRadii.js +55 -0
- package/lib/crystal/core/covalentRadii.js.map +1 -0
- package/lib/crystal/core/disorder.d.ts +59 -0
- package/lib/crystal/core/disorder.d.ts.map +1 -0
- package/lib/crystal/core/disorder.js +168 -0
- package/lib/crystal/core/disorder.js.map +1 -0
- package/lib/crystal/core/expand.d.ts +62 -0
- package/lib/crystal/core/expand.d.ts.map +1 -0
- package/lib/crystal/core/expand.js +61 -0
- package/lib/crystal/core/expand.js.map +1 -0
- package/lib/crystal/core/index.d.ts +18 -0
- package/lib/crystal/core/index.d.ts.map +1 -0
- package/lib/crystal/core/index.js +12 -0
- package/lib/crystal/core/index.js.map +1 -0
- package/lib/crystal/core/moleculeOf.d.ts +31 -0
- package/lib/crystal/core/moleculeOf.d.ts.map +1 -0
- package/lib/crystal/core/moleculeOf.js +128 -0
- package/lib/crystal/core/moleculeOf.js.map +1 -0
- package/lib/crystal/core/moleculeTypes.d.ts +80 -0
- package/lib/crystal/core/moleculeTypes.d.ts.map +1 -0
- package/lib/crystal/core/moleculeTypes.js +2 -0
- package/lib/crystal/core/moleculeTypes.js.map +1 -0
- package/lib/crystal/core/molecules.d.ts +39 -0
- package/lib/crystal/core/molecules.d.ts.map +1 -0
- package/lib/crystal/core/molecules.js +140 -0
- package/lib/crystal/core/molecules.js.map +1 -0
- package/lib/crystal/core/operations.d.ts +19 -0
- package/lib/crystal/core/operations.d.ts.map +1 -0
- package/lib/crystal/core/operations.js +68 -0
- package/lib/crystal/core/operations.js.map +1 -0
- package/lib/crystal/core/packing.d.ts +43 -0
- package/lib/crystal/core/packing.d.ts.map +1 -0
- package/lib/crystal/core/packing.js +89 -0
- package/lib/crystal/core/packing.js.map +1 -0
- package/lib/crystal/core/periodicBonds.d.ts +57 -0
- package/lib/crystal/core/periodicBonds.d.ts.map +1 -0
- package/lib/crystal/core/periodicBonds.js +167 -0
- package/lib/crystal/core/periodicBonds.js.map +1 -0
- package/lib/crystal/core/resolve.d.ts +61 -0
- package/lib/crystal/core/resolve.d.ts.map +1 -0
- package/lib/crystal/core/resolve.js +149 -0
- package/lib/crystal/core/resolve.js.map +1 -0
- package/lib/crystal/core/spaceGroups/index.d.ts +5 -0
- package/lib/crystal/core/spaceGroups/index.d.ts.map +1 -0
- package/lib/crystal/core/spaceGroups/index.js +4 -0
- package/lib/crystal/core/spaceGroups/index.js.map +1 -0
- package/lib/crystal/core/spaceGroups/spaceGroupOperations.d.ts +14 -0
- package/lib/crystal/core/spaceGroups/spaceGroupOperations.d.ts.map +1 -0
- package/lib/crystal/core/spaceGroups/spaceGroupOperations.js +24 -0
- package/lib/crystal/core/spaceGroups/spaceGroupOperations.js.map +1 -0
- package/lib/crystal/core/spaceGroups/spaceGroupRows.d.ts +3 -0
- package/lib/crystal/core/spaceGroups/spaceGroupRows.d.ts.map +1 -0
- package/lib/crystal/core/spaceGroups/spaceGroupRows.js +536 -0
- package/lib/crystal/core/spaceGroups/spaceGroupRows.js.map +1 -0
- package/lib/crystal/core/spaceGroups/spaceGroupSymbols.d.ts +43 -0
- package/lib/crystal/core/spaceGroups/spaceGroupSymbols.d.ts.map +1 -0
- package/lib/crystal/core/spaceGroups/spaceGroupSymbols.js +169 -0
- package/lib/crystal/core/spaceGroups/spaceGroupSymbols.js.map +1 -0
- package/lib/crystal/core/spaceGroups/spaceGroups.d.ts +78 -0
- package/lib/crystal/core/spaceGroups/spaceGroups.d.ts.map +1 -0
- package/lib/crystal/core/spaceGroups/spaceGroups.js +83 -0
- package/lib/crystal/core/spaceGroups/spaceGroups.js.map +1 -0
- package/lib/crystal/core/symmetry/group.d.ts +15 -0
- package/lib/crystal/core/symmetry/group.d.ts.map +1 -0
- package/lib/crystal/core/symmetry/group.js +37 -0
- package/lib/crystal/core/symmetry/group.js.map +1 -0
- package/lib/crystal/core/symmetry/index.d.ts +8 -0
- package/lib/crystal/core/symmetry/index.d.ts.map +1 -0
- package/lib/crystal/core/symmetry/index.js +6 -0
- package/lib/crystal/core/symmetry/index.js.map +1 -0
- package/lib/crystal/core/symmetry/integerMatrix.d.ts +45 -0
- package/lib/crystal/core/symmetry/integerMatrix.d.ts.map +1 -0
- package/lib/crystal/core/symmetry/integerMatrix.js +139 -0
- package/lib/crystal/core/symmetry/integerMatrix.js.map +1 -0
- package/lib/crystal/core/symmetry/lattice.d.ts +64 -0
- package/lib/crystal/core/symmetry/lattice.d.ts.map +1 -0
- package/lib/crystal/core/symmetry/lattice.js +113 -0
- package/lib/crystal/core/symmetry/lattice.js.map +1 -0
- package/lib/crystal/core/symmetry/operation.d.ts +91 -0
- package/lib/crystal/core/symmetry/operation.d.ts.map +1 -0
- package/lib/crystal/core/symmetry/operation.js +184 -0
- package/lib/crystal/core/symmetry/operation.js.map +1 -0
- package/lib/crystal/core/symmetry/parseOperation.d.ts +18 -0
- package/lib/crystal/core/symmetry/parseOperation.d.ts.map +1 -0
- package/lib/crystal/core/symmetry/parseOperation.js +145 -0
- package/lib/crystal/core/symmetry/parseOperation.js.map +1 -0
- package/lib/crystal/core/symmetry/types.d.ts +29 -0
- package/lib/crystal/core/symmetry/types.d.ts.map +1 -0
- package/lib/crystal/core/symmetry/types.js +5 -0
- package/lib/crystal/core/symmetry/types.js.map +1 -0
- package/lib/crystal/core/symmetryClasses.d.ts +28 -0
- package/lib/crystal/core/symmetryClasses.d.ts.map +1 -0
- package/lib/crystal/core/symmetryClasses.js +93 -0
- package/lib/crystal/core/symmetryClasses.js.map +1 -0
- package/lib/crystal.d.ts +12 -0
- package/lib/crystal.d.ts.map +1 -0
- package/lib/crystal.js +12 -0
- package/lib/crystal.js.map +1 -0
- package/lib/server/spaSite.d.ts.map +1 -1
- package/lib/server/spaSite.js +9 -0
- package/lib/server/spaSite.js.map +1 -1
- package/package.json +3 -1
- package/src/crystal/core/bondGrid.ts +98 -0
- package/src/crystal/core/cellBonds.ts +121 -0
- package/src/crystal/core/cif/element.ts +40 -0
- package/src/crystal/core/cif/index.ts +12 -0
- package/src/crystal/core/cif/number.ts +62 -0
- package/src/crystal/core/cif/read.ts +218 -0
- package/src/crystal/core/cif/readGeometry.ts +104 -0
- package/src/crystal/core/cif/tags.ts +143 -0
- package/src/crystal/core/cif/types.ts +164 -0
- package/src/crystal/core/components.ts +183 -0
- package/src/crystal/core/covalentRadii.ts +56 -0
- package/src/crystal/core/disorder.ts +190 -0
- package/src/crystal/core/expand.ts +105 -0
- package/src/crystal/core/index.ts +44 -0
- package/src/crystal/core/moleculeOf.ts +169 -0
- package/src/crystal/core/moleculeTypes.ts +85 -0
- package/src/crystal/core/molecules.ts +165 -0
- package/src/crystal/core/operations.ts +79 -0
- package/src/crystal/core/packing.ts +116 -0
- package/src/crystal/core/periodicBonds.ts +234 -0
- package/src/crystal/core/resolve.ts +187 -0
- package/src/crystal/core/spaceGroups/index.ts +16 -0
- package/src/crystal/core/spaceGroups/spaceGroupOperations.ts +32 -0
- package/src/crystal/core/spaceGroups/spaceGroupRows.ts +536 -0
- package/src/crystal/core/spaceGroups/spaceGroupSymbols.ts +179 -0
- package/src/crystal/core/spaceGroups/spaceGroups.ts +154 -0
- package/src/crystal/core/symmetry/group.ts +42 -0
- package/src/crystal/core/symmetry/index.ts +23 -0
- package/src/crystal/core/symmetry/integerMatrix.ts +153 -0
- package/src/crystal/core/symmetry/lattice.ts +152 -0
- package/src/crystal/core/symmetry/operation.ts +223 -0
- package/src/crystal/core/symmetry/parseOperation.ts +180 -0
- package/src/crystal/core/symmetry/types.ts +32 -0
- package/src/crystal/core/symmetryClasses.ts +105 -0
- package/src/crystal.ts +12 -0
- package/src/server/spaSite.ts +9 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { CrystalSite } from './cif/types.ts';
|
|
2
|
+
import { disorderGroupOf } from './disorder.ts';
|
|
3
|
+
import type { ExpandedAtom } from './expand.ts';
|
|
4
|
+
import type { PeriodicPairs } from './periodicBonds.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The atoms to drop because another one already stands in their place.
|
|
8
|
+
* @param atoms - The atoms of one cell.
|
|
9
|
+
* @param coincident - The pairs of atoms closer than an atom to itself.
|
|
10
|
+
* @returns One flag per atom, 1 for an atom dropped.
|
|
11
|
+
*/
|
|
12
|
+
export function writtenTwice(
|
|
13
|
+
atoms: readonly ExpandedAtom[],
|
|
14
|
+
coincident: Int32Array,
|
|
15
|
+
): Uint8Array {
|
|
16
|
+
const removed = new Uint8Array(atoms.length);
|
|
17
|
+
for (let pair = 0; pair < coincident.length; pair += 2) {
|
|
18
|
+
const first = coincident[pair] ?? 0;
|
|
19
|
+
const second = coincident[pair + 1] ?? 0;
|
|
20
|
+
if (removed[first] === 1 || removed[second] === 1) continue;
|
|
21
|
+
const higher =
|
|
22
|
+
(atoms[second] as ExpandedAtom).occupancy >
|
|
23
|
+
(atoms[first] as ExpandedAtom).occupancy;
|
|
24
|
+
removed[higher ? first : second] = 1;
|
|
25
|
+
}
|
|
26
|
+
return removed;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The pairs kept as bonds: none to an atom dropped, none between the copies of
|
|
31
|
+
* a disorder about a special position, and one per hydrogen.
|
|
32
|
+
* @param pairs - The bonded pairs of the cell.
|
|
33
|
+
* @param atoms - The atoms of the cell.
|
|
34
|
+
* @param sites - The asymmetric unit.
|
|
35
|
+
* @param removed - One flag per atom, 1 for an atom dropped.
|
|
36
|
+
* @returns One flag per pair, 1 to keep it.
|
|
37
|
+
*/
|
|
38
|
+
export function keptPairs(
|
|
39
|
+
pairs: PeriodicPairs,
|
|
40
|
+
atoms: readonly ExpandedAtom[],
|
|
41
|
+
sites: readonly CrystalSite[],
|
|
42
|
+
removed: Uint8Array,
|
|
43
|
+
): Uint8Array {
|
|
44
|
+
const keep = new Uint8Array(pairs.count);
|
|
45
|
+
for (let pair = 0; pair < pairs.count; pair++) {
|
|
46
|
+
const first = pairs.from[pair] ?? 0;
|
|
47
|
+
const second = pairs.to[pair] ?? 0;
|
|
48
|
+
if (removed[first] === 1 || removed[second] === 1) continue;
|
|
49
|
+
if (keptApart(atoms[first], atoms[second], sites)) continue;
|
|
50
|
+
keep[pair] = 1;
|
|
51
|
+
}
|
|
52
|
+
keepNearestToHydrogen(pairs, atoms, keep);
|
|
53
|
+
return keep;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Whether two atoms are copies of a disorder that lies about a special
|
|
58
|
+
* position — a negative group, as SHELX writes it — made by different
|
|
59
|
+
* operations: such copies overlap on purpose, and are never bonded.
|
|
60
|
+
* @param a - One atom of the cell.
|
|
61
|
+
* @param b - Another one.
|
|
62
|
+
* @param sites - The asymmetric unit.
|
|
63
|
+
* @returns Whether they are kept apart.
|
|
64
|
+
*/
|
|
65
|
+
function keptApart(
|
|
66
|
+
a: ExpandedAtom | undefined,
|
|
67
|
+
b: ExpandedAtom | undefined,
|
|
68
|
+
sites: readonly CrystalSite[],
|
|
69
|
+
): boolean {
|
|
70
|
+
if (a === undefined || b === undefined) return false;
|
|
71
|
+
if (a.operationIndex === b.operationIndex) return false;
|
|
72
|
+
const groupA = disorderGroupOf(sites[a.siteIndex] as CrystalSite);
|
|
73
|
+
const groupB = disorderGroupOf(sites[b.siteIndex] as CrystalSite);
|
|
74
|
+
return (
|
|
75
|
+
groupA !== null &&
|
|
76
|
+
groupB !== null &&
|
|
77
|
+
(groupA.startsWith('-') || groupB.startsWith('-'))
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A hydrogen holds one bond, to its nearest neighbour: one sitting between two
|
|
83
|
+
* oxygens of a short hydrogen bond would otherwise chain the molecules.
|
|
84
|
+
* @param pairs - The bonded pairs of the cell.
|
|
85
|
+
* @param atoms - The atoms of the cell.
|
|
86
|
+
* @param keep - One flag per pair, cleared where a hydrogen has a nearer partner.
|
|
87
|
+
*/
|
|
88
|
+
function keepNearestToHydrogen(
|
|
89
|
+
pairs: PeriodicPairs,
|
|
90
|
+
atoms: readonly ExpandedAtom[],
|
|
91
|
+
keep: Uint8Array,
|
|
92
|
+
) {
|
|
93
|
+
const nearest = new Int32Array(atoms.length).fill(-1);
|
|
94
|
+
for (let pair = 0; pair < pairs.count; pair++) {
|
|
95
|
+
if (keep[pair] === 0) continue;
|
|
96
|
+
for (let side = 0; side < 2; side++) {
|
|
97
|
+
const atom = (side === 0 ? pairs.from[pair] : pairs.to[pair]) ?? 0;
|
|
98
|
+
if ((atoms[atom] as ExpandedAtom).element !== 'H') continue;
|
|
99
|
+
const best = nearest[atom] ?? -1;
|
|
100
|
+
if (
|
|
101
|
+
best === -1 ||
|
|
102
|
+
(pairs.lengths[pair] ?? 0) < (pairs.lengths[best] ?? 0)
|
|
103
|
+
) {
|
|
104
|
+
nearest[atom] = pair;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (let pair = 0; pair < pairs.count; pair++) {
|
|
109
|
+
if (keep[pair] === 0) continue;
|
|
110
|
+
const from = pairs.from[pair] ?? 0;
|
|
111
|
+
const to = pairs.to[pair] ?? 0;
|
|
112
|
+
const fromHydrogen = (atoms[from] as ExpandedAtom).element === 'H';
|
|
113
|
+
const toHydrogen = (atoms[to] as ExpandedAtom).element === 'H';
|
|
114
|
+
if (
|
|
115
|
+
(fromHydrogen && nearest[from] !== pair) ||
|
|
116
|
+
(toHydrogen && nearest[to] !== pair)
|
|
117
|
+
) {
|
|
118
|
+
keep[pair] = 0;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { elementBySymbol } from '../../../periodic/core/elements.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Read the element of an atom site: the type symbol's, else the label's.
|
|
5
|
+
* @param typeSymbol - `_atom_site_type_symbol`, or `null` when absent.
|
|
6
|
+
* @param label - `_atom_site_label`.
|
|
7
|
+
* @returns The chemical symbol.
|
|
8
|
+
* @throws {Error} When neither opens with an element symbol.
|
|
9
|
+
*/
|
|
10
|
+
export function elementOf(typeSymbol: string | null, label: string): string {
|
|
11
|
+
const fromType = typeSymbol === null ? null : symbolOf(typeSymbol);
|
|
12
|
+
if (fromType !== null) return fromType;
|
|
13
|
+
const fromLabel = symbolOf(label);
|
|
14
|
+
if (fromLabel !== null) return fromLabel;
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Atom site "${label}" names no element: neither its type symbol nor its label opens with one.`,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Read the chemical symbol a CIF token opens with.
|
|
22
|
+
*
|
|
23
|
+
* A type symbol carries the oxidation state (`Na1+`, `Cl1-`) and a label
|
|
24
|
+
* carries an index in whatever case the refinement wrote it (`NA1`, `CL2`), so
|
|
25
|
+
* the letters are taken, recapitalised and checked against the periodic table.
|
|
26
|
+
* Deuterium, which a neutron structure writes `D`, is hydrogen.
|
|
27
|
+
* @param token - a type symbol or an atom-site label.
|
|
28
|
+
* @returns the symbol, or `null` when the letters name no element.
|
|
29
|
+
*/
|
|
30
|
+
function symbolOf(token: string): string | null {
|
|
31
|
+
const letters = /^(?<letters>[A-Za-z]{1,2})/.exec(token)?.groups?.letters;
|
|
32
|
+
if (letters === undefined) return null;
|
|
33
|
+
for (let length = letters.length; length >= 1; length--) {
|
|
34
|
+
const symbol =
|
|
35
|
+
letters.charAt(0).toUpperCase() + letters.slice(1, length).toLowerCase();
|
|
36
|
+
if (elementBySymbol(symbol) !== undefined) return symbol;
|
|
37
|
+
if (symbol === 'D') return 'H';
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { formatCifNumber, formatNumber, parseCifNumber } from './number.ts';
|
|
2
|
+
export { readCif } from './read.ts';
|
|
3
|
+
export type {
|
|
4
|
+
CifGeometryBond,
|
|
5
|
+
CifGeometryTorsion,
|
|
6
|
+
CifNumber,
|
|
7
|
+
CrystalCell,
|
|
8
|
+
CrystalSite,
|
|
9
|
+
CrystalSource,
|
|
10
|
+
CrystalSpaceGroup,
|
|
11
|
+
CrystalStructure,
|
|
12
|
+
} from './types.ts';
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { CifNumber } from './types.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Read a CIF numeric token.
|
|
5
|
+
*
|
|
6
|
+
* `cif-to-json` never converts anything: every field arrives as a string, with
|
|
7
|
+
* the standard uncertainty still attached and with `.` and `?` already
|
|
8
|
+
* collapsed to the empty string.
|
|
9
|
+
* @param token - the raw field, e.g. `0.30475(3)`, `90`, `.5`, `-1.5e2`, `.`, `?` or ``.
|
|
10
|
+
* @returns the number, or `null` when the field is absent, unknown or not a number.
|
|
11
|
+
*/
|
|
12
|
+
export function parseCifNumber(
|
|
13
|
+
token: string | null | undefined,
|
|
14
|
+
): CifNumber | null {
|
|
15
|
+
if (token === null || token === undefined) return null;
|
|
16
|
+
const raw = token.trim();
|
|
17
|
+
if (MISSING.has(raw)) return null;
|
|
18
|
+
const match = NUMBER.exec(raw);
|
|
19
|
+
if (match?.groups === undefined) return null;
|
|
20
|
+
|
|
21
|
+
const { sign, mantissa, digits, exponent } = match.groups;
|
|
22
|
+
if (mantissa === undefined) return null;
|
|
23
|
+
const power = exponent === undefined ? 0 : Number(exponent);
|
|
24
|
+
const value = Number(`${sign ?? ''}${mantissa}e${power}`);
|
|
25
|
+
if (!Number.isFinite(value)) return null;
|
|
26
|
+
|
|
27
|
+
const dot = mantissa.indexOf('.');
|
|
28
|
+
const decimals = dot === -1 ? 0 : mantissa.length - dot - 1;
|
|
29
|
+
const su =
|
|
30
|
+
digits === undefined ? null : Number(`${digits}e${power - decimals}`);
|
|
31
|
+
return { value, su, raw };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Write a CIF numeric token, re-attaching the standard uncertainty.
|
|
36
|
+
* @param number - the value to write.
|
|
37
|
+
* @returns `0.30475(3)` for `{ value: 0.30475, su: 0.00003 }`.
|
|
38
|
+
*/
|
|
39
|
+
export function formatCifNumber(number: CifNumber): string {
|
|
40
|
+
const text = formatNumber(number.value);
|
|
41
|
+
if (number.su === null) return text;
|
|
42
|
+
const dot = text.indexOf('.');
|
|
43
|
+
const decimals = dot === -1 ? 0 : text.length - dot - 1;
|
|
44
|
+
return `${text}(${Math.round(number.su * 10 ** decimals)})`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Write a plain number the way a CIF field wants it.
|
|
49
|
+
*
|
|
50
|
+
* `String` gives the shortest text that reads back as the same double, so a
|
|
51
|
+
* value written here and read by `parseCifNumber` is bit-identical.
|
|
52
|
+
* @param value - the value to write.
|
|
53
|
+
* @returns the field text.
|
|
54
|
+
*/
|
|
55
|
+
export function formatNumber(value: number): string {
|
|
56
|
+
return Object.is(value, -0) ? '0' : String(value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const MISSING = new Set(['', '.', '?']);
|
|
60
|
+
|
|
61
|
+
const NUMBER =
|
|
62
|
+
/^(?<sign>[+-]?)(?<mantissa>\d+\.?\d*|\.\d+)(?:\((?<digits>\d+)\))?(?:[Ee](?<exponent>[+-]?\d+))?$/;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { parseCifBlocks } from 'cif-to-json';
|
|
2
|
+
|
|
3
|
+
import { elementOf } from './element.ts';
|
|
4
|
+
import { parseCifNumber } from './number.ts';
|
|
5
|
+
import { readGeometryBonds, readGeometryTorsions } from './readGeometry.ts';
|
|
6
|
+
import type { CifIndex, CifRowIndex } from './tags.ts';
|
|
7
|
+
import {
|
|
8
|
+
HALL_TAGS,
|
|
9
|
+
HM_TAGS,
|
|
10
|
+
IT_NUMBER_TAGS,
|
|
11
|
+
SYMOP_TAGS,
|
|
12
|
+
indexCif,
|
|
13
|
+
loopWith,
|
|
14
|
+
optionalRowTag,
|
|
15
|
+
rowTag,
|
|
16
|
+
scalarTag,
|
|
17
|
+
} from './tags.ts';
|
|
18
|
+
import type {
|
|
19
|
+
CrystalCell,
|
|
20
|
+
CrystalSite,
|
|
21
|
+
CrystalSource,
|
|
22
|
+
CrystalSpaceGroup,
|
|
23
|
+
CrystalStructure,
|
|
24
|
+
} from './types.ts';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Read one crystal structure from a CIF.
|
|
28
|
+
* @param text - CIF file content, one `data_` block.
|
|
29
|
+
* @returns the structure, with every value converted from the strings
|
|
30
|
+
* `cif-to-json` returns. `geometryBonds` and `geometryTorsions` are present
|
|
31
|
+
* only when the file carries the loop.
|
|
32
|
+
* @throws {Error} when the file holds more than one data block, carries no unit cell,
|
|
33
|
+
* or names an atom whose element cannot be read.
|
|
34
|
+
*/
|
|
35
|
+
export function readCif(text: string): CrystalStructure {
|
|
36
|
+
const blocks = parseCifBlocks(text);
|
|
37
|
+
if (blocks.length > 1) {
|
|
38
|
+
const names = blocks.map((block) => block.name).join(', ');
|
|
39
|
+
throw new Error(
|
|
40
|
+
`This file holds ${blocks.length} data blocks (${names}). Split it and load one structure at a time.`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
const [block] = blocks;
|
|
44
|
+
const index = indexCif(block?.data ?? {});
|
|
45
|
+
const geometryBonds = readGeometryBonds(index);
|
|
46
|
+
const geometryTorsions = readGeometryTorsions(index);
|
|
47
|
+
return {
|
|
48
|
+
name: block?.name ?? '',
|
|
49
|
+
formula:
|
|
50
|
+
scalarTag(index, ['_chemical_formula_sum', '_chemical_formula_moiety']) ??
|
|
51
|
+
'',
|
|
52
|
+
cell: readCell(index),
|
|
53
|
+
spaceGroup: readSpaceGroup(index),
|
|
54
|
+
symopsXyz: readSymops(index),
|
|
55
|
+
sites: readSites(index),
|
|
56
|
+
source: readSource(index),
|
|
57
|
+
...(geometryBonds === null ? {} : { geometryBonds }),
|
|
58
|
+
...(geometryTorsions === null ? {} : { geometryTorsions }),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function readCell(index: CifIndex): CrystalCell {
|
|
63
|
+
return {
|
|
64
|
+
a: cellNumber(index, '_cell_length_a'),
|
|
65
|
+
b: cellNumber(index, '_cell_length_b'),
|
|
66
|
+
c: cellNumber(index, '_cell_length_c'),
|
|
67
|
+
alpha: cellNumber(index, '_cell_angle_alpha'),
|
|
68
|
+
beta: cellNumber(index, '_cell_angle_beta'),
|
|
69
|
+
gamma: cellNumber(index, '_cell_angle_gamma'),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function cellNumber(index: CifIndex, tag: string): number {
|
|
74
|
+
const parsed = parseCifNumber(scalarTag(index, [tag]));
|
|
75
|
+
if (parsed === null) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`This CIF has no unit cell: ${tag} is missing or is not a number.`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return parsed.value;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function readSpaceGroup(index: CifIndex): CrystalSpaceGroup {
|
|
84
|
+
const parsed = parseCifNumber(scalarTag(index, IT_NUMBER_TAGS));
|
|
85
|
+
const number = parsed?.value ?? null;
|
|
86
|
+
return {
|
|
87
|
+
number:
|
|
88
|
+
number !== null &&
|
|
89
|
+
Number.isInteger(number) &&
|
|
90
|
+
number >= 1 &&
|
|
91
|
+
number <= 230
|
|
92
|
+
? number
|
|
93
|
+
: null,
|
|
94
|
+
hm: scalarTag(index, HM_TAGS),
|
|
95
|
+
hall: scalarTag(index, HALL_TAGS),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function readSymops(index: CifIndex): string[] {
|
|
100
|
+
const rows = loopWith(index, SYMOP_TAGS);
|
|
101
|
+
if (rows === null) return [];
|
|
102
|
+
const operations: string[] = [];
|
|
103
|
+
for (const row of rows) {
|
|
104
|
+
const xyz = rowTag(row, SYMOP_TAGS);
|
|
105
|
+
if (xyz !== null) operations.push(xyz);
|
|
106
|
+
}
|
|
107
|
+
return operations;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function readSites(index: CifIndex): CrystalSite[] {
|
|
111
|
+
const rows = loopWith(index, FRACT_X_TAGS);
|
|
112
|
+
if (rows === null) return [];
|
|
113
|
+
const sites: CrystalSite[] = [];
|
|
114
|
+
for (const row of rows) sites.push(readSite(row));
|
|
115
|
+
return sites;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function readSite(row: CifRowIndex): CrystalSite {
|
|
119
|
+
const typeSymbol = rowTag(row, TYPE_SYMBOL_TAGS);
|
|
120
|
+
const label = rowTag(row, LABEL_TAGS) ?? typeSymbol;
|
|
121
|
+
if (label === null) {
|
|
122
|
+
throw new Error(
|
|
123
|
+
'An atom site of this CIF carries neither a label nor a type symbol.',
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
const occupancy = parseCifNumber(rowTag(row, OCCUPANCY_TAGS));
|
|
127
|
+
const disorderGroup = optionalRowTag(row, DISORDER_GROUP_TAGS);
|
|
128
|
+
const disorderAssembly = optionalRowTag(row, DISORDER_ASSEMBLY_TAGS);
|
|
129
|
+
const calcFlag = optionalRowTag(row, CALC_FLAG_TAGS);
|
|
130
|
+
return {
|
|
131
|
+
label,
|
|
132
|
+
element: elementOf(typeSymbol, label),
|
|
133
|
+
x: siteNumber(row, FRACT_X_TAGS, label),
|
|
134
|
+
y: siteNumber(row, FRACT_Y_TAGS, label),
|
|
135
|
+
z: siteNumber(row, FRACT_Z_TAGS, label),
|
|
136
|
+
occupancy: occupancy?.value ?? 1,
|
|
137
|
+
uiso: readUiso(row),
|
|
138
|
+
...(disorderGroup === null ? {} : { disorderGroup }),
|
|
139
|
+
...(disorderAssembly === null ? {} : { disorderAssembly }),
|
|
140
|
+
...(calcFlag === null ? {} : { calcFlag }),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function siteNumber(
|
|
145
|
+
row: CifRowIndex,
|
|
146
|
+
aliases: readonly string[],
|
|
147
|
+
label: string,
|
|
148
|
+
): number {
|
|
149
|
+
const parsed = parseCifNumber(rowTag(row, aliases));
|
|
150
|
+
if (parsed === null) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
`Atom site "${label}" has no fractional coordinate on ${aliases[0] ?? ''}.`,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
return parsed.value;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Read the isotropic displacement parameter, in Ų.
|
|
160
|
+
*
|
|
161
|
+
* A file that gives `_atom_site_B_iso_or_equiv` instead is converted with
|
|
162
|
+
* `U = B / 8π²`, which is the definition of the two quantities.
|
|
163
|
+
* @param row - the atom-site row.
|
|
164
|
+
* @returns U_iso, or `null` when the file gives neither.
|
|
165
|
+
*/
|
|
166
|
+
function readUiso(row: CifRowIndex): number | null {
|
|
167
|
+
const u = parseCifNumber(rowTag(row, U_ISO_TAGS));
|
|
168
|
+
if (u !== null) return u.value;
|
|
169
|
+
const b = parseCifNumber(rowTag(row, B_ISO_TAGS));
|
|
170
|
+
return b === null ? null : b.value / (8 * Math.PI * Math.PI);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function readSource(index: CifIndex): CrystalSource {
|
|
174
|
+
const source: { doi?: string; cod?: string; note?: string } = {};
|
|
175
|
+
const doi = scalarTag(index, ['_journal_paper_doi']);
|
|
176
|
+
if (doi !== null) source.doi = doi;
|
|
177
|
+
const cod = scalarTag(index, ['_cod_database_code']);
|
|
178
|
+
if (cod !== null) source.cod = cod;
|
|
179
|
+
const note = scalarTag(index, [
|
|
180
|
+
'_chemical_name_mineral',
|
|
181
|
+
'_chemical_name_common',
|
|
182
|
+
]);
|
|
183
|
+
if (note !== null) source.note = note;
|
|
184
|
+
return source;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const FRACT_X_TAGS = ['_atom_site_fract_x', '_atom_site.fract_x'] as const;
|
|
188
|
+
const FRACT_Y_TAGS = ['_atom_site_fract_y', '_atom_site.fract_y'] as const;
|
|
189
|
+
const FRACT_Z_TAGS = ['_atom_site_fract_z', '_atom_site.fract_z'] as const;
|
|
190
|
+
const LABEL_TAGS = ['_atom_site_label', '_atom_site.label'] as const;
|
|
191
|
+
const TYPE_SYMBOL_TAGS = [
|
|
192
|
+
'_atom_site_type_symbol',
|
|
193
|
+
'_atom_site.type_symbol',
|
|
194
|
+
] as const;
|
|
195
|
+
const OCCUPANCY_TAGS = [
|
|
196
|
+
'_atom_site_occupancy',
|
|
197
|
+
'_atom_site.occupancy',
|
|
198
|
+
] as const;
|
|
199
|
+
const U_ISO_TAGS = [
|
|
200
|
+
'_atom_site_u_iso_or_equiv',
|
|
201
|
+
'_atom_site.u_iso_or_equiv',
|
|
202
|
+
] as const;
|
|
203
|
+
const DISORDER_GROUP_TAGS = [
|
|
204
|
+
'_atom_site_disorder_group',
|
|
205
|
+
'_atom_site.disorder_group',
|
|
206
|
+
] as const;
|
|
207
|
+
const DISORDER_ASSEMBLY_TAGS = [
|
|
208
|
+
'_atom_site_disorder_assembly',
|
|
209
|
+
'_atom_site.disorder_assembly',
|
|
210
|
+
] as const;
|
|
211
|
+
const CALC_FLAG_TAGS = [
|
|
212
|
+
'_atom_site_calc_flag',
|
|
213
|
+
'_atom_site.calc_flag',
|
|
214
|
+
] as const;
|
|
215
|
+
const B_ISO_TAGS = [
|
|
216
|
+
'_atom_site_b_iso_or_equiv',
|
|
217
|
+
'_atom_site.b_iso_or_equiv',
|
|
218
|
+
] as const;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { parseCifNumber } from './number.ts';
|
|
2
|
+
import type { CifIndex, CifRowIndex } from './tags.ts';
|
|
3
|
+
import { loopWith, optionalRowTag, rowTag } from './tags.ts';
|
|
4
|
+
import type { CifGeometryBond, CifGeometryTorsion } from './types.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Read the `_geom_bond` loop: the distances the authors reported.
|
|
8
|
+
* @param index - the folded CIF.
|
|
9
|
+
* @returns the bonds in file order, or `null` when the file has no such loop.
|
|
10
|
+
* A row whose labels or distance cannot be read is left out.
|
|
11
|
+
*/
|
|
12
|
+
export function readGeometryBonds(index: CifIndex): CifGeometryBond[] | null {
|
|
13
|
+
const rows = loopWith(index, BOND_LABEL_1);
|
|
14
|
+
if (rows === null) return null;
|
|
15
|
+
const bonds: CifGeometryBond[] = [];
|
|
16
|
+
for (const row of rows) {
|
|
17
|
+
const label1 = rowTag(row, BOND_LABEL_1);
|
|
18
|
+
const label2 = rowTag(row, BOND_LABEL_2);
|
|
19
|
+
const distance = parseCifNumber(rowTag(row, BOND_DISTANCE));
|
|
20
|
+
if (label1 === null || label2 === null || distance === null) continue;
|
|
21
|
+
bonds.push({
|
|
22
|
+
label1,
|
|
23
|
+
label2,
|
|
24
|
+
distance,
|
|
25
|
+
...symmetryCode(row, 'siteSymmetry1', BOND_SYMMETRY_1),
|
|
26
|
+
...symmetryCode(row, 'siteSymmetry2', BOND_SYMMETRY_2),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return bonds;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Read the `_geom_torsion` loop: the torsion angles the authors reported.
|
|
34
|
+
* @param index - the folded CIF.
|
|
35
|
+
* @returns the torsions in file order, or `null` when the file has no such
|
|
36
|
+
* loop. A row whose labels or angle cannot be read is left out.
|
|
37
|
+
*/
|
|
38
|
+
export function readGeometryTorsions(
|
|
39
|
+
index: CifIndex,
|
|
40
|
+
): CifGeometryTorsion[] | null {
|
|
41
|
+
const rows = loopWith(index, torsionTags('atom_site_label_1'));
|
|
42
|
+
if (rows === null) return null;
|
|
43
|
+
const torsions: CifGeometryTorsion[] = [];
|
|
44
|
+
for (const row of rows) {
|
|
45
|
+
const label1 = rowTag(row, torsionTags('atom_site_label_1'));
|
|
46
|
+
const label2 = rowTag(row, torsionTags('atom_site_label_2'));
|
|
47
|
+
const label3 = rowTag(row, torsionTags('atom_site_label_3'));
|
|
48
|
+
const label4 = rowTag(row, torsionTags('atom_site_label_4'));
|
|
49
|
+
const angle = parseCifNumber(rowTag(row, TORSION_ANGLE));
|
|
50
|
+
if (
|
|
51
|
+
label1 === null ||
|
|
52
|
+
label2 === null ||
|
|
53
|
+
label3 === null ||
|
|
54
|
+
label4 === null ||
|
|
55
|
+
angle === null
|
|
56
|
+
) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
torsions.push({
|
|
60
|
+
label1,
|
|
61
|
+
label2,
|
|
62
|
+
label3,
|
|
63
|
+
label4,
|
|
64
|
+
angle,
|
|
65
|
+
...symmetryCode(row, 'siteSymmetry1', torsionTags('site_symmetry_1')),
|
|
66
|
+
...symmetryCode(row, 'siteSymmetry2', torsionTags('site_symmetry_2')),
|
|
67
|
+
...symmetryCode(row, 'siteSymmetry3', torsionTags('site_symmetry_3')),
|
|
68
|
+
...symmetryCode(row, 'siteSymmetry4', torsionTags('site_symmetry_4')),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return torsions;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function symmetryCode<K extends string>(
|
|
75
|
+
row: CifRowIndex,
|
|
76
|
+
key: K,
|
|
77
|
+
aliases: readonly string[],
|
|
78
|
+
): Partial<Record<K, string>> {
|
|
79
|
+
const code = optionalRowTag(row, aliases);
|
|
80
|
+
return code === null ? {} : ({ [key]: code } as Record<K, string>);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function torsionTags(column: string): readonly string[] {
|
|
84
|
+
return [`_geom_torsion_${column}`, `_geom_torsion.${column}`];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const BOND_LABEL_1 = [
|
|
88
|
+
'_geom_bond_atom_site_label_1',
|
|
89
|
+
'_geom_bond.atom_site_label_1',
|
|
90
|
+
] as const;
|
|
91
|
+
const BOND_LABEL_2 = [
|
|
92
|
+
'_geom_bond_atom_site_label_2',
|
|
93
|
+
'_geom_bond.atom_site_label_2',
|
|
94
|
+
] as const;
|
|
95
|
+
const BOND_DISTANCE = ['_geom_bond_distance', '_geom_bond.distance'] as const;
|
|
96
|
+
const BOND_SYMMETRY_1 = [
|
|
97
|
+
'_geom_bond_site_symmetry_1',
|
|
98
|
+
'_geom_bond.site_symmetry_1',
|
|
99
|
+
] as const;
|
|
100
|
+
const BOND_SYMMETRY_2 = [
|
|
101
|
+
'_geom_bond_site_symmetry_2',
|
|
102
|
+
'_geom_bond.site_symmetry_2',
|
|
103
|
+
] as const;
|
|
104
|
+
const TORSION_ANGLE = ['_geom_torsion', '_geom_torsion.value'] as const;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { CifJson } from 'cif-to-json';
|
|
2
|
+
|
|
3
|
+
/** One `loop_` row, keyed on the lowercased tag of each column. */
|
|
4
|
+
export type CifRowIndex = ReadonlyMap<string, string>;
|
|
5
|
+
|
|
6
|
+
/** A parsed CIF, with every tag folded to lower case so lookups are case-blind. */
|
|
7
|
+
export interface CifIndex {
|
|
8
|
+
/** Every scalar tag of the block. */
|
|
9
|
+
readonly scalars: ReadonlyMap<string, string>;
|
|
10
|
+
/** Every `loop_` of the block, in file order. */
|
|
11
|
+
readonly loops: ReadonlyArray<readonly CifRowIndex[]>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Fold a parsed CIF so every tag can be looked up whatever case it was written in.
|
|
16
|
+
*
|
|
17
|
+
* CIF tags are case-insensitive and real files disagree: COD writes
|
|
18
|
+
* `_symmetry_Int_Tables_number`, other producers write it all in lower case.
|
|
19
|
+
* `cif-to-json` stores the tag verbatim, so the folding has to happen here.
|
|
20
|
+
* @param parsed - the output of `cifParser`.
|
|
21
|
+
* @returns the folded index.
|
|
22
|
+
*/
|
|
23
|
+
export function indexCif(parsed: CifJson): CifIndex {
|
|
24
|
+
const scalars = new Map<string, string>();
|
|
25
|
+
const loops: Array<readonly CifRowIndex[]> = [];
|
|
26
|
+
for (const [tag, value] of Object.entries(parsed)) {
|
|
27
|
+
if (typeof value === 'string') {
|
|
28
|
+
scalars.set(tag.toLowerCase(), value);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const rows: CifRowIndex[] = [];
|
|
32
|
+
for (const source of value) {
|
|
33
|
+
const row = new Map<string, string>();
|
|
34
|
+
for (const [column, cell] of Object.entries(source)) {
|
|
35
|
+
row.set(column.toLowerCase(), cell);
|
|
36
|
+
}
|
|
37
|
+
rows.push(row);
|
|
38
|
+
}
|
|
39
|
+
loops.push(rows);
|
|
40
|
+
}
|
|
41
|
+
return { scalars, loops };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Read a scalar tag, trying each alias in turn.
|
|
46
|
+
* @param index - the folded CIF.
|
|
47
|
+
* @param aliases - the tags to try, lowercased, most current first.
|
|
48
|
+
* @returns the trimmed value, or `null` when no alias carries one.
|
|
49
|
+
*/
|
|
50
|
+
export function scalarTag(
|
|
51
|
+
index: CifIndex,
|
|
52
|
+
aliases: readonly string[],
|
|
53
|
+
): string | null {
|
|
54
|
+
for (const alias of aliases) {
|
|
55
|
+
const value = index.scalars.get(alias);
|
|
56
|
+
if (value !== undefined && value.trim() !== '') return value.trim();
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Find the loop that carries one of these columns.
|
|
63
|
+
*
|
|
64
|
+
* `cif-to-json` keys a loop on the longest common prefix of its column tags, so
|
|
65
|
+
* the key changes with the optional columns a file happened to write: the same
|
|
66
|
+
* operation list arrives as `_space_group_symop`, `_symmetry_equiv_pos` or the
|
|
67
|
+
* full tag. Searching the columns instead of the key is immune to that.
|
|
68
|
+
* @param index - the folded CIF.
|
|
69
|
+
* @param aliases - the column tags to look for, lowercased.
|
|
70
|
+
* @returns the loop's rows, or `null` when no loop carries any of them.
|
|
71
|
+
*/
|
|
72
|
+
export function loopWith(
|
|
73
|
+
index: CifIndex,
|
|
74
|
+
aliases: readonly string[],
|
|
75
|
+
): readonly CifRowIndex[] | null {
|
|
76
|
+
for (const rows of index.loops) {
|
|
77
|
+
const first = rows[0];
|
|
78
|
+
if (first === undefined) continue;
|
|
79
|
+
for (const alias of aliases) {
|
|
80
|
+
if (first.has(alias)) return rows;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Read one cell of a loop row, trying each alias in turn.
|
|
88
|
+
* @param row - the row.
|
|
89
|
+
* @param aliases - the column tags to try, lowercased.
|
|
90
|
+
* @returns the trimmed value, or `null` when no alias carries one.
|
|
91
|
+
*/
|
|
92
|
+
export function rowTag(
|
|
93
|
+
row: CifRowIndex,
|
|
94
|
+
aliases: readonly string[],
|
|
95
|
+
): string | null {
|
|
96
|
+
for (const alias of aliases) {
|
|
97
|
+
const value = row.get(alias);
|
|
98
|
+
if (value !== undefined && value.trim() !== '') return value.trim();
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Read one cell of a loop row that may be left unknown or inapplicable.
|
|
105
|
+
* @param row - the row.
|
|
106
|
+
* @param aliases - the column tags to try, lowercased.
|
|
107
|
+
* @returns the trimmed value, or `null` when it is absent, `.` or `?`.
|
|
108
|
+
*/
|
|
109
|
+
export function optionalRowTag(
|
|
110
|
+
row: CifRowIndex,
|
|
111
|
+
aliases: readonly string[],
|
|
112
|
+
): string | null {
|
|
113
|
+
const value = rowTag(row, aliases);
|
|
114
|
+
return value === null || ['.', '?'].includes(value) ? null : value;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The four tags a file may carry its symmetry operations under. */
|
|
118
|
+
export const SYMOP_TAGS = [
|
|
119
|
+
'_space_group_symop_operation_xyz',
|
|
120
|
+
'_space_group_symop.operation_xyz',
|
|
121
|
+
'_symmetry_equiv_pos_as_xyz',
|
|
122
|
+
'_symmetry_equiv_pos.as_xyz',
|
|
123
|
+
] as const;
|
|
124
|
+
|
|
125
|
+
/** The two tags a file may carry its International Tables number under. */
|
|
126
|
+
export const IT_NUMBER_TAGS = [
|
|
127
|
+
'_space_group_it_number',
|
|
128
|
+
'_symmetry_int_tables_number',
|
|
129
|
+
] as const;
|
|
130
|
+
|
|
131
|
+
/** The tags a file may carry its Hermann-Mauguin symbol under. */
|
|
132
|
+
export const HM_TAGS = [
|
|
133
|
+
'_space_group_name_h-m_alt',
|
|
134
|
+
'_space_group_name_h-m_full',
|
|
135
|
+
'_symmetry_space_group_name_h-m',
|
|
136
|
+
'_cod_original_sg_symbol_h-m',
|
|
137
|
+
] as const;
|
|
138
|
+
|
|
139
|
+
/** The tags a file may carry its Hall symbol under. */
|
|
140
|
+
export const HALL_TAGS = [
|
|
141
|
+
'_space_group_name_hall',
|
|
142
|
+
'_symmetry_space_group_name_hall',
|
|
143
|
+
] as const;
|