| import { ParserOptionsSpec } from "./ParserOptionsSpec"; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function CDJSON(str: string, options: ParserOptionsSpec) { |
| var atoms: any[][] & Record<string, any> = [[]]; |
| if (typeof str === "string") { |
| |
| str = JSON.parse(str); |
| } |
| var molecules = (str as any).m; |
| var atomsInFile = molecules[0].a; |
| var bondsInFile = molecules[0].b; |
| |
| var styles = molecules[0].s; |
| var parseStyle = |
| options !== undefined && options.parseStyle !== undefined |
| ? options.parseStyle |
| : styles !== undefined; |
| |
| var offset = atoms[atoms.length - 1].length; |
| |
| |
| for (var i = 0; i < atomsInFile.length; i++) { |
| var currentAtom = atomsInFile[i]; |
| var atom: Record<string, any> = {}; |
| atom.id = currentAtom.i; |
| |
| atom.x = currentAtom.x; |
| atom.y = currentAtom.y; |
| atom.z = currentAtom.z || 0; |
| |
| atom.bonds = []; |
| atom.bondOrder = []; |
| |
| var elem = currentAtom.l || "C"; |
| atom.elem = elem[0].toUpperCase() + elem.substring(1).toLowerCase(); |
| |
| atom.serial = atoms[atoms.length - 1].length; |
| if (parseStyle) { |
| atom.style = styles[currentAtom.s || 0]; |
| } |
| atoms[atoms.length - 1].push(atom); |
| } |
| for (let i = 0; i < bondsInFile.length; i++) { |
| let currentBond = bondsInFile[i]; |
| let beginIndex = currentBond.b + offset; |
| let endIndex = currentBond.e + offset; |
| let bondOrder = currentBond.o || 1; |
| |
| let firstAtom = atoms[atoms.length - 1][beginIndex]; |
| let secondAtom = atoms[atoms.length - 1][endIndex]; |
| |
| firstAtom.bonds.push(endIndex); |
| firstAtom.bondOrder.push(bondOrder); |
| secondAtom.bonds.push(beginIndex); |
| secondAtom.bondOrder.push(bondOrder); |
| } |
| return atoms; |
| } |