From f83ddbb8c823d8b65974e4b449b212f813dc96b0 Mon Sep 17 00:00:00 2001 From: Prajwal Kulkarni Date: Mon, 29 Apr 2024 13:04:07 +0530 Subject: [PATCH] refactor: GRO parser --- src/parsers/GRO.ts | 53 ++++++++++++++++++++++++---------------------- src/specs.ts | 3 +++ 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/parsers/GRO.ts b/src/parsers/GRO.ts index 8960b96c..cedfb0e3 100644 --- a/src/parsers/GRO.ts +++ b/src/parsers/GRO.ts @@ -1,33 +1,36 @@ +import { AtomSpec } from "specs"; import { ParserOptionsSpec } from "./ParserOptionsSpec"; import { assignPDBBonds } from "./utils/assignPDBBonds"; import { atomNameToElem } from "./utils/atomNameToElem"; /** * Parse a gro file from str and create atoms - * - * @param {string} - * str - * @param {ParserOptionsSpec} - * options* + * + * @param {string} str + * @param {ParserOptionsSpec} options * @category Parsers -*/ + * @returns {Array} - Returns a 2D array of type AtomSpec + */ -export function GRO(str: string, options: ParserOptionsSpec) { - var allatoms: any[][] & Record = []; - var lines = str.split(/\r?\n|\r/); +export function GRO(str: string, options: ParserOptionsSpec) { + const allatoms: AtomSpec[][] & { box?: string[] } = []; + const lines = str.split(/\r?\n|\r/); while (lines.length > 0) { - if (lines.length < 3) break; - var atomCount = parseInt(lines[1]); - if (isNaN(atomCount) || atomCount <= 0) break; - if (lines.length < atomCount + 3) break; - var atoms: any[] = []; + const atomCount = parseInt(lines[1]); + const breakCondition = + lines.length < 3 || + isNaN(atomCount) || + atomCount <= 0 || + lines.length < atomCount + 3; + if (breakCondition) break; + const atoms: AtomSpec[] = []; allatoms.push(atoms); - var offset = 2; - var start = atoms.length; - var end = start + atomCount; - for (var i = start; i < end; i++) { - var line = lines[offset++]; - var atom: Record = {}; + let offset = 2; + const start = atoms.length; + const end = start + atomCount; + for (let i = start; i < end; i++) { + const line = lines[offset++]; + const atom: AtomSpec = {}; atom.serial = i; atom.atom = line.slice(10, 15).trim(); atom.elem = atomNameToElem(atom.atom, true); @@ -50,11 +53,11 @@ export function GRO(str: string, options: ParserOptionsSpec) { if (lines.length <= offset + 3) { //single line left, assume it is the box - var last = lines[offset++]; - var box = last.trim().split(/\s+/); - if (box.length == 3) { - for (var b = 0; b < 3; b++) { - (box[b] as any) = parseFloat(box[b]) * 10.0; + const last = lines[offset++]; + const box = last.trim().split(/\s+/); + if (box.length === 3) { + for (let b = 0; b < 3; b++) { + box[b] = (parseFloat(box[b]) * 10.0).toString(); } allatoms.box = box; } diff --git a/src/specs.ts b/src/specs.ts index 31219184..d17611bc 100644 --- a/src/specs.ts +++ b/src/specs.ts @@ -81,6 +81,9 @@ export interface AtomSpec { uMat?: Record; symmetries?: Vector3[]; sym?: any; + dx?: number; + dy?: number; + dz?: number; } /**