File size: 3,068 Bytes
71174bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | // Originally, I repurposed the 3dmol.js parser for when plugins need to access
// information about atoms. But I came to realize that this is overkill. I'm now
// going to create a minimal parser for PDB and MOL2 files instead, since these
// are the formats that molmoda uses internally for protein and compound files,
// respectively. It doens't need to have a lot of functionality. It just needs
// to be light on memory.
// resn,resi,chain
import { IAtom } from "@/UI/Navigation/TreeView/TreeInterfaces";
import { EasyParserParent } from "./EasyParserParent";
import { IFileInfo } from "@/FileSystem/Types";
import { twoLetterElems } from "../../NameVars";
/**
* A parser for PDB files.
*/
export class EasyParserPDB extends EasyParserParent {
/**
* Load the source.
*
* @param {IFileInfo} src The source to parse.
*/
_load(src: IFileInfo): void {
const atomLines = src.contents.split("\n");
// Keep only lines that start with ATOM or HETATM
this._atoms = atomLines.filter((line: string) => {
return line.startsWith("ATOM") || line.startsWith("HETATM");
});
}
/**
* Parse an atom.
*
* @param {string} atomStr The string to parse.
* @param {number} [atomParserIndex] Optional: The 0-based index of this
* atom in the parser's internal list.
* @returns {IAtom} The parsed atom.
*/
_parseAtomStr(atomStr: string, atomParserIndex?: number): IAtom {
// You must parse it.
const atomName = atomStr.slice(12, 16).trim();
const serial = parseInt(atomStr.slice(6, 11).trim());
const altLoc = atomStr.slice(16, 17);
const resn = atomStr.slice(17, 20).trim();
const chain = atomStr.slice(21, 22);
const resi = parseInt(atomStr.slice(22, 26).trim());
const x = parseFloat(atomStr.slice(30, 38).trim());
const y = parseFloat(atomStr.slice(38, 46).trim());
const z = parseFloat(atomStr.slice(46, 54).trim());
const b = parseFloat(atomStr.slice(60, 66).trim());
const hetflag = atomStr.slice(0, 6).trim() === "HETATM";
let elem = atomStr.slice(76, 78).trim();
if (elem === "") {
// Get first two letters of atom name
elem = atomStr.slice(12, 14).trim();
// Make upper case
elem = elem.toUpperCase();
// If it's not in twoLetterElems, get first letter.
if (twoLetterElems.indexOf(elem) === -1) {
elem = elem[0];
}
}
if (elem.length > 1) {
// Otherwise, make all but first letter lowercase.
elem = elem[0] + elem.slice(1).toLowerCase();
}
return {
resn,
chain,
resi,
x,
y,
z,
bondOrder: [],
bonds: [],
elem,
serial,
altLoc,
b,
atom: atomName,
hetflag,
};
}
}
|