| |
| |
| |
| |
| |
| |
|
|
| import { IFileInfo } from "@/FileSystem/Types"; |
| import { IAtom } from "@/UI/Navigation/TreeView/TreeInterfaces"; |
| import { GLModel } from "@/UI/Panels/Viewer/GLModelType"; |
|
|
| |
| interface IBounds { |
| minX: number; |
| minY: number; |
| minZ: number; |
| maxX: number; |
| maxY: number; |
| maxZ: number; |
| } |
|
|
| |
| |
| |
| export abstract class EasyParserParent { |
| |
| |
| |
| |
| |
| |
| constructor(src: IFileInfo | GLModel | IAtom[] | undefined) { |
| if (src !== undefined) { |
| this._load(src); |
| } |
| } |
| protected _atoms: (string | IAtom)[] = []; |
|
|
| |
| |
| |
| |
| |
| abstract _load(src: IFileInfo | GLModel | IAtom[]): void; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| abstract _parseAtomStr( |
| atomStr: string, |
| atomParserIndex?: number |
| ): IAtom | undefined; |
| |
| |
| |
| |
| |
| |
| getAtom(idx: number): IAtom { |
| const atom = this._atoms[idx]; |
|
|
| |
| if (typeof atom !== "string") { |
| return atom as IAtom; |
| } |
| const parsedAtom = this._parseAtomStr(atom as string, idx); |
| if (parsedAtom === undefined) { |
| throw new Error("Failed to parse atom."); |
| } |
| this._atoms[idx] = parsedAtom; |
| return parsedAtom; |
| } |
|
|
| |
| |
| |
| |
| |
| get length(): number { |
| return this._atoms.length; |
| } |
|
|
| |
| |
| |
| |
| |
| get atoms(): IAtom[] { |
| return this._atoms.map((atom, idx) => { |
| return this.getAtom(idx); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| selectedAtoms(sel: { [key: string]: string[] }, extract = false): IAtom[] { |
| |
| |
|
|
| |
| |
|
|
| |
| let atoms: [number, IAtom][] = []; |
| for (let i = 0; i < this.length; i++) { |
| atoms.push([i, this.getAtom(i)]); |
| } |
|
|
| const keys = Object.keys(sel); |
|
|
| |
| if (keys.length === 0) { |
| if (extract) { |
| this._atoms = []; |
| } |
| return atoms.map(([idx, atom]) => atom); |
| } |
|
|
| let matchingAtoms: [number, IAtom][] = []; |
|
|
| for (const key of keys) { |
| const val = sel[key]; |
|
|
| let filterFunc: (atom: IAtom) => boolean = (atom: IAtom) => true; |
|
|
| switch (key) { |
| case "resn": |
| filterFunc = (atom) => val.includes(atom.resn); |
| break; |
| case "chain": |
| filterFunc = (atom) => val.includes(atom.chain); |
| break; |
| case "elem": |
| filterFunc = (atom) => { |
| if (atom.elem === undefined) { |
| return false; |
| } |
| return val.includes(atom.elem); |
| }; |
| break; |
| default: |
| |
| debugger; |
| } |
|
|
| matchingAtoms = matchingAtoms.concat( |
| atoms.filter(([idx, atom]) => filterFunc(atom)) |
| ); |
|
|
| |
| atoms = atoms.filter(([idx, atom]) => !filterFunc(atom)); |
| } |
|
|
| if (extract) { |
| |
| matchingAtoms.sort(([idx1, atom1], [idx2, atom2]) => idx1 - idx2); |
| this._atoms = atoms.map(([idx, atom]) => atom); |
| } |
|
|
| |
| matchingAtoms.sort(([idx1, atom1], [idx2, atom2]) => idx1 - idx2); |
| return matchingAtoms.map(([idx, atom]) => atom); |
| } |
|
|
| |
| |
| |
| |
| |
| appendAtoms(atoms: IAtom | IAtom[]): void { |
| if (!Array.isArray(atoms)) { |
| atoms = [atoms]; |
| } |
| this._atoms = this._atoms.concat(atoms); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| getBounds(stride = 1): IBounds | null { |
| if (stride < 1) { |
| throw new Error("Stride must be >= 1"); |
| } |
|
|
| let minX = Infinity; |
| let minY = Infinity; |
| let minZ = Infinity; |
| let maxX = -Infinity; |
| let maxY = -Infinity; |
| let maxZ = -Infinity; |
| let foundCoords = false; |
|
|
| for (let i = 0; i < this.length; i += stride) { |
| const atom = this.getAtom(i); |
| if ( |
| atom.x !== undefined && |
| atom.y !== undefined && |
| atom.z !== undefined |
| ) { |
| foundCoords = true; |
| minX = Math.min(minX, atom.x); |
| minY = Math.min(minY, atom.y); |
| minZ = Math.min(minZ, atom.z); |
| maxX = Math.max(maxX, atom.x); |
| maxY = Math.max(maxY, atom.y); |
| maxZ = Math.max(maxZ, atom.z); |
| } |
| } |
|
|
| if (!foundCoords) { |
| return null; |
| } |
|
|
| return { minX, minY, minZ, maxX, maxY, maxZ }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public isFlat(): boolean { |
| if (this.length === 0) { |
| return false; |
| } |
| const atoms = this.atoms; |
| if (atoms.length === 0) { |
| return false; |
| } |
| |
| |
| const xCoords = atoms.map((a) => a.x); |
| const yCoords = atoms.map((a) => a.y); |
| const zCoords = atoms.map((a) => a.z); |
| |
| |
| const allXZero = xCoords.every((c) => c === 0); |
| const allYZero = yCoords.every((c) => c === 0); |
| const allZZero = zCoords.every((c) => c === 0); |
| return allXZero || allYZero || allZZero; |
| } |
| |
| |
| |
| |
| |
| |
| |
| private _buildSpatialGrid(stride: number, cellSize: number): Map<string, number[]> { |
| const grid = new Map<string, number[]>(); |
| for (let i = 0; i < this.length; i += stride) { |
| const atom = this.getAtom(i); |
| if ( |
| atom.x === undefined || |
| atom.y === undefined || |
| atom.z === undefined |
| ) { |
| continue; |
| } |
| const cx = Math.floor(atom.x / cellSize); |
| const cy = Math.floor(atom.y / cellSize); |
| const cz = Math.floor(atom.z / cellSize); |
| const key = `${cx},${cy},${cz}`; |
| if (!grid.has(key)) { |
| grid.set(key, []); |
| } |
| grid.get(key)!.push(i); |
| } |
| return grid; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| isWithinDistance( |
| otherParser: EasyParserParent, |
| distance: number, |
| selfStride = 1, |
| otherStride = 1 |
| ): boolean { |
| |
| if (selfStride < 1) { |
| throw new Error("selfStride must be >= 1"); |
| } |
| if (otherStride < 1) { |
| throw new Error("otherStride must be >= 1"); |
| } |
|
|
| const distanceSqThreshold = distance * distance; |
|
|
| |
| const bounds1 = this.getBounds(selfStride); |
| const bounds2 = otherParser.getBounds(otherStride); |
|
|
| |
| if (!bounds1 || !bounds2) { |
| return false; |
| } |
|
|
| |
| if ( |
| bounds1.maxX < bounds2.minX - distance || |
| bounds1.minX > bounds2.maxX + distance || |
| bounds1.maxY < bounds2.minY - distance || |
| bounds1.minY > bounds2.maxY + distance || |
| bounds1.maxZ < bounds2.minZ - distance || |
| bounds1.minZ > bounds2.maxZ + distance |
| ) { |
| return false; |
| } |
| |
| |
| |
| |
| |
| |
| const count1 = Math.ceil(this.length / selfStride); |
| const count2 = Math.ceil(otherParser.length / otherStride); |
| if (count1 * count2 > 5000) { |
| |
| let gridParser: EasyParserParent; |
| let queryParser: EasyParserParent; |
| let gridStride: number; |
| let queryStride: number; |
| if (count1 >= count2) { |
| |
| gridParser = this; |
| gridStride = selfStride; |
| queryParser = otherParser; |
| queryStride = otherStride; |
| } else { |
| gridParser = otherParser; |
| gridStride = otherStride; |
| |
| queryParser = this; |
| queryStride = selfStride; |
| } |
| const grid = gridParser._buildSpatialGrid(gridStride, distance); |
| for (let i = 0; i < queryParser.length; i += queryStride) { |
| const qAtom = queryParser.getAtom(i); |
| if (qAtom.x === undefined || qAtom.y === undefined || qAtom.z === undefined) continue; |
| const cx = Math.floor(qAtom.x / distance); |
| const cy = Math.floor(qAtom.y / distance); |
| const cz = Math.floor(qAtom.z / distance); |
| |
| for (let dx = -1; dx <= 1; dx++) { |
| for (let dy = -1; dy <= 1; dy++) { |
| for (let dz = -1; dz <= 1; dz++) { |
| const key = `${cx + dx},${cy + dy},${cz + dz}`; |
| const binIndices = grid.get(key); |
| if (!binIndices) continue; |
| |
| for (const idx of binIndices) { |
| const gAtom = gridParser.getAtom(idx); |
| const distSq = |
| (qAtom.x - gAtom.x!) ** 2 + |
| (qAtom.y - gAtom.y!) ** 2 + |
| (qAtom.z - gAtom.z!) ** 2; |
| if (distSq <= distanceSqThreshold) { |
| return true; |
| } |
| } |
| } |
| } |
| } |
| } |
| return false; |
| } |
| |
| for (let i = 0; i < this.length; i += selfStride) { |
| const atom1 = this.getAtom(i); |
| |
| if ( |
| atom1.x === undefined || |
| atom1.y === undefined || |
| atom1.z === undefined |
| ) { |
| continue; |
| } |
| |
| const x1 = atom1.x; |
| const y1 = atom1.y; |
| const z1 = atom1.z; |
|
|
| for (let j = 0; j < otherParser.length; j += otherStride) { |
| const atom2 = otherParser.getAtom(j); |
| |
| if ( |
| atom2.x === undefined || |
| atom2.y === undefined || |
| atom2.z === undefined |
| ) { |
| continue; |
| } |
| const x2 = atom2.x; |
|
|
| |
| const dx = x1 - x2; |
| const dxSq = dx * dx; |
| if (dxSq > distanceSqThreshold) { |
| continue; |
| } |
|
|
| const y2 = atom2.y; |
| const dy = y1 - y2; |
| const dySq = dy * dy; |
| if (dxSq + dySq > distanceSqThreshold) { |
| continue; |
| } |
|
|
| const z2 = atom2.z; |
| const dz = z1 - z2; |
| const dzSq = dz * dz; |
| const distanceSq = dxSq + dySq + dzSq; |
|
|
| |
| if (distanceSq <= distanceSqThreshold) { |
| return true; |
| } |
| } |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public getUniqueResidues(): { names: Set<string>; ids: Set<number> } { |
| const residueNames = new Set<string>(); |
| const residueIds = new Set<number>(); |
| for (let i = 0; i < this.length; i++) { |
| const atom = this.getAtom(i); |
| if (atom.resn) { |
| residueNames.add(atom.resn); |
| } |
| if (atom.resi !== undefined) { |
| |
| residueIds.add(atom.resi); |
| } |
| } |
| return { names: residueNames, ids: residueIds }; |
| } |
|
|
| |
| |
| |
| |
| |
| public hasHydrogens(): boolean { |
| |
| return this.atoms.some((atom) => atom.elem?.toUpperCase() === "H"); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|