-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathextendFromMolfile.js
More file actions
56 lines (56 loc) · 1.92 KB
/
extendFromMolfile.js
File metadata and controls
56 lines (56 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
export function extendFromMolfile(Molecule) {
const _fromMolfile = Molecule.fromMolfile;
Molecule.fromMolfile = function fromMolfile(molfile, options = {}) {
const { customLabelPosition } = options;
const molecule = _fromMolfile.call(this, molfile);
// what is eol ? \r\n or \n ?
const eol = molfile.includes('\r\n') ? '\r\n' : '\n';
const lines = molfile.split(eol);
if (lines.length < 4 || !lines[3].includes('V2000')) {
return molecule; // nothing to do
}
const possibleLines = lines.slice(
4 + molecule.getAllAtoms() + molecule.getAllBonds(),
);
// we should not forget that for A lines the value is on the next line
for (let i = 0; i < possibleLines.length; i++) {
const line = possibleLines[i];
if (line.startsWith('A ')) {
const atom = Number(line.slice(3));
const valueLine = possibleLines[i + 1]?.trim();
i++;
if (
!Number.isNaN(atom) &&
atom <= molecule.getAllAtoms() &&
valueLine &&
!molecule.getAtomCustomLabel(atom - 1)
) {
molecule.setAtomCustomLabel(atom - 1, valueLine);
}
}
if (line.startsWith('V ')) {
const parts = line.split(' ').filter(Boolean);
if (parts.length >= 3) {
const atom = Number(parts[1]);
const label = parts.slice(2).join(' ');
if (
!Number.isNaN(atom) &&
atom <= molecule.getAllAtoms() &&
!molecule.getAtomCustomLabel(atom - 1)
) {
molecule.setAtomCustomLabel(atom - 1, label);
}
}
}
if (line.startsWith('M ZZC')) {
const atom = Number(line.slice(7, 10).trim());
const label = line.slice(10).trim();
if (atom && label) {
molecule.setAtomCustomLabel(atom - 1, label);
}
}
}
molecule.changeCustomLabelPosition(customLabelPosition);
return molecule;
};
}