File size: 2,678 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
// 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.

import { IFileInfo } from "@/FileSystem/Types";
import { EasyParserParent } from "./EasyParserParent";
import { IAtom } from "@/UI/Navigation/TreeView/TreeInterfaces";

/**
 * A parser for MOL2 files.
 */
export class EasyParserMol2 extends EasyParserParent {
    /**
     * Load the source.
     *
     * @param {IFileInfo} src  The source to parse.
     */
    _load(src: IFileInfo): void {
        if (src.contents.indexOf("@<TRIPOS>ATOM") === -1) {
            throw new Error("MOL2 file does not contain @<TRIPOS>ATOM section. Incorrect format?");
        }
        const prts = src.contents.split("@<TRIPOS>ATOM");
        let atoms = prts[1].split("@<TRIPOS>")[0];

        // While first char is newline, remove it
        while (atoms[0] === "\n") {
            atoms = atoms.slice(1);
        }

        // Trim the right side.
        atoms = atoms.trimRight();

        this._atoms = atoms.split("\n").map((atom: string) => atom.trim());
    }

    /**
     * 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 {
        // Atom looks like this:
        // "     31  C4        39.2670   22.5690   13.2440 C.ar  501  ATP501      0.1692"

        // Split by spaces.
        const [serialOrig, atomName, xOrig, yOrig, zOrig, elemOrig, resiOrig, resn, bOrig] = atomStr.split(/\s+/);
        const serial = parseInt(serialOrig);
        const x = parseFloat(xOrig);
        const y = parseFloat(yOrig);
        const z = parseFloat(zOrig);
        const elem = elemOrig.split(".")[0]
        const resi = parseInt(resiOrig);
        const b = parseFloat(bOrig);
        
        // In this context, b is the charge.

        // Some not specified
        const chain = "A";
        const altLoc = " ";

        return {
            resn,
            chain,
            resi,
            x,
            y,
            z,
            bondOrder: [],
            bonds: [],
            elem,
            serial,
            altLoc,
            b,
            atom: atomName,
        };
    }
}