| import { IFileInfo } from "@/FileSystem/Types"; |
| import { EasyParserParent } from "./EasyParserParent"; |
| import { IAtom } from "@/UI/Navigation/TreeView/TreeInterfaces"; |
| import { GLModel } from "@/UI/Panels/Viewer/GLModelType"; |
|
|
| interface ISDFBond { |
| atomIndex1: number; |
| atomIndex2: number; |
| bondType: number; |
| } |
|
|
| |
| |
| |
| |
| |
| export class EasyParserSDF extends EasyParserParent { |
| private _parsedBonds: ISDFBond[] = []; |
| |
| private _atomOrderToSdfIndex: Map<number, number> = new Map(); |
| |
| private _sdfIndexToAtomOrder: Map<number, number> = new Map(); |
| private _isV3000: boolean; |
|
|
| constructor(src: IFileInfo | GLModel | IAtom[]) { |
| super(undefined); |
| this._isV3000 = (src as IFileInfo).contents.includes("V3000"); |
| this._load(src); |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| _load(src: IFileInfo | GLModel | IAtom[]): void { |
| |
| if (!this._parsedBonds) { |
| this._parsedBonds = []; |
| } |
| if (!this._atomOrderToSdfIndex) { |
| this._atomOrderToSdfIndex = new Map(); |
| } |
| if (!this._sdfIndexToAtomOrder) { |
| this._sdfIndexToAtomOrder = new Map(); |
| } |
|
|
| const lines = (src as IFileInfo).contents.split(/\r?\n/); |
| this._atoms = []; |
| this._parsedBonds = []; |
| this._atomOrderToSdfIndex.clear(); |
| this._sdfIndexToAtomOrder.clear(); |
| if (this._isV3000) { |
| this._parseV3000(lines); |
| } else { |
| this._parseV2000(lines); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| private _parseV3000(lines: string[]): void { |
| let inAtomBlock = false; |
| let inBondBlock = false; |
| let atomOrder = 0; |
|
|
| for (const line of lines) { |
| if (line.startsWith("M V30 END ATOM")) { |
| inAtomBlock = false; |
| continue; |
| } |
| if (line.startsWith("M V30 END BOND")) { |
| inBondBlock = false; |
| continue; |
| } |
|
|
| if (inAtomBlock) { |
| this._atoms.push(line); |
| |
| const parts = line.trim().match(/\S+/g); |
| if (parts && parts.length >= 3) { |
| const sdfIndex = parseInt(parts[2], 10); |
| if (!isNaN(sdfIndex)) { |
| this._atomOrderToSdfIndex.set(atomOrder, sdfIndex); |
| this._sdfIndexToAtomOrder.set(sdfIndex, atomOrder); |
| atomOrder++; |
| } |
| } |
| } else if (inBondBlock) { |
| |
| const parts = line.trim().match(/\S+/g); |
| if (parts && parts.length >= 6) { |
| const bondType = parseInt(parts[3], 10); |
| const sdfAtomIdx1 = parseInt(parts[4], 10); |
| const sdfAtomIdx2 = parseInt(parts[5], 10); |
|
|
| const atomOrderIdx1 = this._sdfIndexToAtomOrder.get(sdfAtomIdx1); |
| const atomOrderIdx2 = this._sdfIndexToAtomOrder.get(sdfAtomIdx2); |
|
|
| if ( |
| atomOrderIdx1 !== undefined && |
| atomOrderIdx2 !== undefined && |
| !isNaN(bondType) |
| ) { |
| this._parsedBonds.push({ |
| atomIndex1: atomOrderIdx1, |
| atomIndex2: atomOrderIdx2, |
| bondType: bondType, |
| }); |
| } |
| } |
| } |
|
|
| if (line.startsWith("M V30 BEGIN ATOM")) { |
| inAtomBlock = true; |
| } |
| if (line.startsWith("M V30 BEGIN BOND")) { |
| inBondBlock = true; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| private _parseV2000(lines: string[]): void { |
| let atomCount = 0; |
| let bondCount = 0; |
| let countsLineIndex = -1; |
| const currentAtomBlockLines: string[] = []; |
|
|
| |
| for (let i = 0; i < lines.length; i++) { |
| const line = lines[i]; |
| if (i >= 3 && line.trim().endsWith("V2000")) { |
| const parts = line.trim().split(/\s+/); |
| if (parts.length >= 2) { |
| const numAtoms = parseInt(parts[0], 10); |
| const numBonds = parseInt(parts[1], 10); |
| if (!isNaN(numAtoms) && !isNaN(numBonds)) { |
| atomCount = numAtoms; |
| bondCount = numBonds; |
| countsLineIndex = i; |
| break; |
| } |
| } |
| } |
| if (line.trim() === "$$$$") { |
| |
| break; |
| } |
| } |
|
|
| if (countsLineIndex === -1) { |
| |
| let inMoleculeBlock = false; |
| for (let i = 0; i < lines.length; ++i) { |
| const line = lines[i]; |
| if (line.includes("M END") || line.trim() === "$$$$") { |
| break; |
| } |
| |
| |
| if ( |
| line.match( |
| /^\s*-?\d+\.\d{4}\s+-?\d+\.\d{4}\s+-?\d+\.\d{4}\s+\w/ |
| ) |
| ) { |
| |
| if (!inMoleculeBlock && i > 2) { |
| inMoleculeBlock = true; |
| } |
| if (inMoleculeBlock) { |
| currentAtomBlockLines.push(line); |
| } |
| } else if ( |
| inMoleculeBlock && |
| currentAtomBlockLines.length > 0 && |
| !line.match(/^\s*\d+\s+\d+\s+\d+/) |
| ) { |
| |
| break; |
| } |
| } |
| this._atoms = currentAtomBlockLines; |
| |
| return; |
| } |
|
|
| |
| const atomBlockStartIndex = countsLineIndex + 1; |
| for (let i = 0; i < atomCount; i++) { |
| const atomLineIndex = atomBlockStartIndex + i; |
| if (atomLineIndex < lines.length) { |
| currentAtomBlockLines.push(lines[atomLineIndex]); |
| |
| this._atomOrderToSdfIndex.set(i, i + 1); |
| } else { |
| |
| break; |
| } |
| } |
| this._atoms = currentAtomBlockLines; |
|
|
| |
| const bondBlockStartIndex = atomBlockStartIndex + atomCount; |
| for (let i = 0; i < bondCount; i++) { |
| const bondLineIndex = bondBlockStartIndex + i; |
| if (bondLineIndex < lines.length) { |
| const line = lines[bondLineIndex]; |
| |
| |
| const atomIdx1 = parseInt(line.substring(0, 3).trim(), 10) - 1; |
| const atomIdx2 = parseInt(line.substring(3, 6).trim(), 10) - 1; |
| const bondType = parseInt(line.substring(6, 9).trim(), 10); |
|
|
| if (!isNaN(atomIdx1) && !isNaN(atomIdx2) && !isNaN(bondType)) { |
| this._parsedBonds.push({ |
| atomIndex1: atomIdx1, |
| atomIndex2: atomIdx2, |
| bondType, |
| }); |
| } |
| } else { |
| |
| break; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| _parseAtomStr( |
| atomStr: string, |
| atomParserIndex?: number |
| ): IAtom | undefined { |
| if (this._isV3000) { |
| return this._parseV3000AtomStr(atomStr, atomParserIndex); |
| } else { |
| return this._parseV2000AtomStr(atomStr, atomParserIndex); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| private _parseV3000AtomStr( |
| atomStr: string, |
| atomParserIndex?: number |
| ): IAtom | undefined { |
| |
| const parts = atomStr.trim().match(/\S+/g); |
| if (!parts || parts.length < 7) return undefined; |
| const sdfIndex = parseInt(parts[2], 10); |
| const elem = parts[3]; |
| const x = parseFloat(parts[4]); |
| const y = parseFloat(parts[5]); |
| const z = parseFloat(parts[6]); |
| |
|
|
| let charge = 0; |
| |
| for (let i = 8; i < parts.length; i++) { |
| if (parts[i].startsWith("CHG=")) { |
| charge = parseInt(parts[i].substring(4), 10); |
| if (isNaN(charge)) charge = 0; |
| break; |
| } |
| } |
|
|
| if (isNaN(x) || isNaN(y) || isNaN(z)) return undefined; |
|
|
| const bonds: number[] = []; |
| const bondOrder: number[] = []; |
|
|
| if (atomParserIndex !== undefined) { |
| this._parsedBonds.forEach((bond) => { |
| if (bond.atomIndex1 === atomParserIndex) { |
| bonds.push(bond.atomIndex2); |
| bondOrder.push(bond.bondType); |
| } else if (bond.atomIndex2 === atomParserIndex) { |
| bonds.push(bond.atomIndex1); |
| bondOrder.push(bond.bondType); |
| } |
| }); |
| } |
|
|
| return { |
| x, |
| y, |
| z, |
| elem, |
| serial: sdfIndex, |
| atom: elem, |
| resn: "UNL", |
| chain: "A", |
| resi: 1, |
| altLoc: " ", |
| b: charge, |
| bonds, |
| bondOrder, |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| private _parseV2000AtomStr( |
| atomStr: string, |
| atomParserIndex?: number |
| ): IAtom | undefined { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| if (atomStr.length < 34) { |
| return undefined; |
| } |
|
|
| |
| if (atomParserIndex === undefined) { |
| |
| |
| return undefined; |
| } |
|
|
| const x = parseFloat(atomStr.substring(0, 10).trim()); |
| const y = parseFloat(atomStr.substring(10, 20).trim()); |
| const z = parseFloat(atomStr.substring(20, 30).trim()); |
| const elem = atomStr.substring(31, 34).trim(); |
|
|
| let charge = 0; |
| if (atomStr.length >= 39) { |
| const chargeCode = parseInt(atomStr.substring(36, 39).trim(), 10); |
| switch (chargeCode) { |
| case 1: |
| charge = 3; |
| break; |
| case 2: |
| charge = 2; |
| break; |
| case 3: |
| charge = 1; |
| break; |
| |
| case 5: |
| charge = -1; |
| break; |
| case 6: |
| charge = -2; |
| break; |
| case 7: |
| charge = -3; |
| break; |
| default: |
| charge = 0; |
| break; |
| } |
| } |
|
|
| const bonds: number[] = []; |
| const bondOrder: number[] = []; |
|
|
| |
| this._parsedBonds.forEach((bond) => { |
| if (bond.atomIndex1 === atomParserIndex) { |
| bonds.push(bond.atomIndex2); |
| bondOrder.push(bond.bondType); |
| } else if (bond.atomIndex2 === atomParserIndex) { |
| bonds.push(bond.atomIndex1); |
| bondOrder.push(bond.bondType); |
| } |
| }); |
|
|
| const originalSdfAtomNumber = |
| this._atomOrderToSdfIndex.get(atomParserIndex); |
|
|
| return { |
| x, |
| y, |
| z, |
| elem, |
| serial: originalSdfAtomNumber, |
| atom: elem, |
| resn: "UNL", |
| chain: "A", |
| resi: 1, |
| altLoc: " ", |
| b: charge, |
| bonds, |
| bondOrder, |
| |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| getAtom(idx: number): IAtom { |
| const atomEntry = this._atoms[idx]; |
|
|
| |
| if (typeof atomEntry !== "string") { |
| return atomEntry as IAtom; |
| } |
|
|
| |
| const parsedAtom = this._parseAtomStr(atomEntry as string, idx); |
| if (parsedAtom === undefined) { |
| throw new Error( |
| `Failed to parse SDF atom line at index ${idx}: "${atomEntry}"` |
| ); |
| } |
|
|
| |
| this._atoms[idx] = parsedAtom; |
| return parsedAtom; |
| } |
| } |
|
|