| import { IAtom } from "@/UI/Navigation/TreeView/TreeInterfaces"; |
| import { TreeNode } from "@/TreeNodes/TreeNode/TreeNode"; |
| import { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList"; |
| import { GLModel } from "@/UI/Panels/Viewer/GLModelType"; |
| import { |
| standardProteinResidues, |
| solventSel, |
| } from "../Types/ComponentSelections"; |
| import { makeEasyParser } from "../ParseMolModels/EasyParser"; |
| import { twoLetterElems } from "../NameVars"; |
| import { IFileInfo } from "@/FileSystem/Types"; |
|
|
| const chainOptions = [ |
| "A", |
| "B", |
| "C", |
| "D", |
| "E", |
| "F", |
| "G", |
| "H", |
| "I", |
| "J", |
| "K", |
| "L", |
| "M", |
| "N", |
| "O", |
| "P", |
| "Q", |
| "R", |
| "S", |
| "T", |
| "U", |
| "V", |
| "W", |
| "X", |
| "Y", |
| "Z", |
| ]; |
|
|
| |
| |
| |
| |
| |
| |
| |
| function _rjust(str: string, width: number): string { |
| while (str.length < width) { |
| str = " " + str; |
| } |
| str = str.substring(str.length - width); |
| return str; |
| } |
| |
| |
| |
| |
| |
| |
| |
| function _ljust(str: string, width: number): string { |
| while (str.length < width) { |
| str += " "; |
| } |
| str = str.substring(0, width); |
| return str; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function _alignAtomName(atomName: string, element?: string): string { |
| |
| |
|
|
| if (atomName === "") { |
| return ""; |
| } |
|
|
| |
| |
| if (atomName.length >= 4) { |
| return atomName.substring(0, 4); |
| } |
|
|
| if (atomName.length === 1) { |
| return ` ${atomName} `; |
| } |
|
|
| if (element) { |
| |
| if (element.length === 2) { |
| return _ljust(atomName, 4); |
| } |
|
|
| |
| if (element.length === 1) { |
| return " " + _ljust(atomName, 3); |
| } |
| } else if ( |
| twoLetterElems.indexOf(atomName.substring(0, 2).toUpperCase()) !== -1 |
| ) { |
| |
| return _ljust(atomName, 4); |
| } |
|
|
| return _rjust(atomName, 4); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function _createPDBLine(isProt: boolean, atom: IAtom): string { |
| |
| atom.serial = atom.serial === undefined ? 0 : atom.serial; |
| atom.atom = atom.atom === undefined ? "C" : atom.atom; |
| atom.elem = atom.elem === undefined ? "C" : atom.elem; |
| atom.altLoc = atom.altLoc === undefined ? " " : atom.altLoc; |
| atom.resn = atom.resn === undefined ? "MOL" : atom.resn; |
| atom.chain = atom.chain === undefined ? "A" : atom.chain; |
| atom.resi = atom.resi === undefined ? 1 : atom.resi; |
| atom.x = atom.x === undefined ? 0 : atom.x; |
| atom.y = atom.y === undefined ? 0 : atom.y; |
| atom.z = atom.z === undefined ? 0 : atom.z; |
| atom.b = atom.b === undefined ? 0 : atom.b; |
|
|
| let pdbLine = _ljust(isProt ? "ATOM" : "HETATM", 6); |
| pdbLine += _rjust((atom.serial as number).toString(), 5); |
| pdbLine += " "; |
| pdbLine += _alignAtomName(atom.atom as string, atom.elem); |
| pdbLine += atom.altLoc; |
| pdbLine += _rjust(atom.resn, 3); |
| pdbLine += _rjust(atom.chain, 2); |
| pdbLine += _rjust(atom.resi.toString(), 4); |
| pdbLine += " "; |
| pdbLine += _rjust((atom.x as number).toFixed(3), 11); |
| pdbLine += _rjust((atom.y as number).toFixed(3), 8); |
| pdbLine += _rjust((atom.z as number).toFixed(3), 8); |
| pdbLine += _rjust("1.00", 6); |
| pdbLine += _rjust((atom.b ? (atom.b as number) : 0).toFixed(2), 6); |
| pdbLine += _rjust(" ", 10); |
| pdbLine += _rjust(atom.elem?.toUpperCase() as string, 2); |
| return pdbLine; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function _getAtomsOfModel(mol: GLModel | IAtom[] | IFileInfo): IAtom[] { |
| return makeEasyParser(mol).atoms; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function _mergeMols(mols: GLModel[] | IAtom[][] | IFileInfo[]): IAtom[] { |
| let curIdx = 0; |
| let curSerial = 1; |
| const chainsAvailable: Set<string> = new Set(chainOptions); |
| const allAtoms: IAtom[] = []; |
|
|
| for (const mol of mols) { |
| |
| const atoms = _getAtomsOfModel(mol); |
|
|
| |
| let curChain: string; |
| if (atoms[0] && atoms[0].chain && chainsAvailable.has(atoms[0].chain)) { |
| |
| curChain = atoms[0].chain; |
| } else { |
| |
| |
| curChain = chainsAvailable.values().next().value; |
| } |
| chainsAvailable.delete(curChain); |
|
|
| const firstIndex = curIdx; |
|
|
| for (const atom of atoms) { |
| const atomCopy: IAtom = { ...atom }; |
| atomCopy.chain = curChain; |
| atomCopy.serial = curSerial; |
| atomCopy.index = curIdx; |
| |
| atomCopy.bonds = atomCopy.bonds?.map((idx) => idx + firstIndex); |
| curSerial += 1; |
| curIdx += 1; |
| allAtoms.push(atomCopy); |
| } |
| } |
|
|
| return allAtoms; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function _convertTreeNodeListToPDB( |
| treeNodeList: TreeNodeList, |
| merge = false |
| ): string[] { |
| let mols = treeNodeList.filters |
| .keepModels() |
| .map((treeNode: TreeNode) => treeNode.model as GLModel | IAtom[] | IFileInfo[]); |
| |
| |
|
|
| if (mols.length === 0) { |
| return [""]; |
| } |
|
|
| const pdbTxts: string[] = []; |
| const conectTxts: string[] = []; |
|
|
| if (merge) { |
| |
| |
| mols = [_mergeMols(mols)]; |
| } |
|
|
| |
|
|
| for (const mol of mols) { |
| const atoms: IAtom[] = _getAtomsOfModel(mol); |
| const atomsToConect: IAtom[] = []; |
| const pdbLines: string[] = []; |
|
|
| for (const atom of atoms) { |
| |
|
|
| |
| |
|
|
| const isProt = standardProteinResidues.indexOf(atom.resn) !== -1; |
| const pdbLine = _createPDBLine(isProt, atom); |
|
|
| if ( |
| !isProt && |
| (atom.bonds as number[]).length > 0 && |
| solventSel.resn.indexOf(atom.resn) === -1 |
| ) { |
| atomsToConect.push(atom); |
| } |
|
|
| |
|
|
| pdbLines.push(pdbLine); |
| } |
| pdbTxts.push(pdbLines.join("\n")); |
|
|
| const conects: string[] = []; |
| for (const atom of atomsToConect) { |
| let conect = "CONECT"; |
| conect += _rjust((atom.serial as number).toString(), 5); |
| for (const bondedAtomIdx of atom.bonds as number[]) { |
| if (atoms[bondedAtomIdx] === undefined) { |
| continue; |
| } |
|
|
| const bondedAtomSerial = atoms[bondedAtomIdx].serial as number; |
| conect += _rjust(bondedAtomSerial.toString(), 5); |
| } |
| conects.push(conect); |
| } |
| conectTxts.push(conects.join("\n")); |
| } |
|
|
| if (!merge) { |
| |
| return pdbTxts.map((pdb, i) => { |
| return pdb + "\n" + conectTxts[i]; |
| }); |
| } |
|
|
| |
| return [pdbTxts.join("\nTER\n") + "\n" + conectTxts.join("\n")]; |
| } |
|
|