File size: 10,686 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { MoleculeInput, IMoleculeInputParams } from "./MoleculeInput";
import { getMoleculesFromStore } from "@/Store/StoreExternalAccess";
import { TreeNode } from "@/TreeNodes/TreeNode/TreeNode";
import { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList";
import {
    IAtom,
    SelectedType,
    TreeNodeType,
} from "@/UI/Navigation/TreeView/TreeInterfaces";
import { convertFileInfosOpenBabel } from "@/FileSystem/OpenBabel/OpenBabel";
import { FileInfo } from "@/FileSystem/FileInfo";

// Mock dependencies
jest.mock("@/Store/StoreExternalAccess", () => ({
    getMoleculesFromStore: jest.fn(),
}));

jest.mock("@/FileSystem/OpenBabel/OpenBabel", () => ({
    convertFileInfosOpenBabel: jest.fn(),
}));

// Mock the module that uses `import.meta.url` to prevent compilation errors in Jest.
// This also breaks a circular dependency by delaying the instantiation of TreeNodeList.
jest.mock(
    "@/FileSystem/LoadSaveMolModels/ParseMolModels/ParseMolModelsUtils",
    () => ({
        parseMolecularModelFromTexts: jest.fn().mockImplementation(() => {
            // Dynamically require TreeNodeList inside the mock implementation
            // to break the circular dependency at module load time.
            const {
                TreeNodeList,
            } = require("@/TreeNodes/TreeNodeList/TreeNodeList");
            return Promise.resolve(new TreeNodeList([]));
        }),
    })
);

describe("MoleculeInput component filtering", () => {
    beforeEach(() => {
        // Clear mocks before each test
        (getMoleculesFromStore as jest.Mock).mockClear();
        (convertFileInfosOpenBabel as jest.Mock).mockClear();

        // Mock the implementation for OpenBabel to just pass through the PDB content
        // This simulates a format conversion from PDB to PDB.
        (convertFileInfosOpenBabel as jest.Mock).mockImplementation(
            async (fileInfos: FileInfo[], targetFormat: string) => {
                // This test only deals with pdb and pdbqt which are text based.
                if (
                    targetFormat === "pdb" ||
                    targetFormat === "pdbqt" ||
                    targetFormat === "pdbqtlig"
                ) {
                    return fileInfos.map((fi) => fi.contents);
                }
                return [];
            }
        );

        // --- Create a mock TreeNodeList representing a parsed PDB file ---
        // This structure mimics a protein with associated metal and solvent.
        const proteinAtom: IAtom = {
            serial: 1,
            atom: "CA",
            resn: "GLY",
            chain: "A",
            resi: 1,
            elem: "C",
            x: 1,
            y: 1,
            z: 1,
            bonds: [],
            bondOrder: [],
        };
        const metalAtom: IAtom = {
            serial: 2,
            atom: "ZN",
            resn: "ZN",
            chain: "A",
            resi: 201,
            elem: "ZN",
            hetflag: true,
            x: 2,
            y: 2,
            z: 2,
            bonds: [],
            bondOrder: [],
        };
        const solventAtom: IAtom = {
            serial: 3,
            atom: "O",
            resn: "HOH",
            chain: "A",
            resi: 301,
            elem: "O",
            hetflag: true,
            x: 3,
            y: 3,
            z: 3,
            bonds: [],
            bondOrder: [],
        };
        const nucleicAtom: IAtom = {
            serial: 4,
            atom: "P",
            resn: "A",
            chain: "B",
            resi: 1,
            elem: "P",
            x: 4,
            y: 4,
            z: 4,
            bonds: [],
            bondOrder: [],
        };

        // Terminal nodes that hold the actual molecular data
        const proteinTerminalNode = new TreeNode({
            title: "Protein Chain A",
            type: TreeNodeType.Protein,
            model: [proteinAtom],
            visible: true,
            selected: SelectedType.False,
            treeExpanded: false,
            focused: false,
            viewerDirty: false,
            src: "test.pdb",
        });
        const metalTerminalNode = new TreeNode({
            title: "ZN",
            type: TreeNodeType.Metal,
            model: [metalAtom],
            visible: true,
            selected: SelectedType.False,
            treeExpanded: false,
            focused: false,
            viewerDirty: false,
            src: "test.pdb",
        });
        const solventTerminalNode = new TreeNode({
            title: "HOH",
            type: TreeNodeType.Solvent,
            model: [solventAtom],
            visible: true,
            selected: SelectedType.False,
            treeExpanded: false,
            focused: false,
            viewerDirty: false,
            src: "test.pdb",
        });
        const nucleicTerminalNode = new TreeNode({
            title: "NA Chain B",
            type: TreeNodeType.Nucleic,
            model: [nucleicAtom],
            visible: true,
            selected: SelectedType.False,
            treeExpanded: false,
            focused: false,
            viewerDirty: false,
            src: "test.pdb",
        });

        // The root node containing the hierarchy
        const rootNode = new TreeNode({
            title: "MyMolecule",
            visible: true,
            selected: SelectedType.False,
            treeExpanded: true,
            focused: false,
            viewerDirty: false,
            nodes: new TreeNodeList([
                proteinTerminalNode,
                metalTerminalNode,
                solventTerminalNode,
                nucleicTerminalNode,
            ]),
            src: "test.pdb",
        });

        proteinTerminalNode.parentId = rootNode.id;
        metalTerminalNode.parentId = rootNode.id;
        solventTerminalNode.parentId = rootNode.id;
        nucleicTerminalNode.parentId = rootNode.id;

        const mockMolecules = new TreeNodeList([rootNode]);
        (getMoleculesFromStore as jest.Mock).mockReturnValue(mockMolecules);
    });

    it("should include protein, metal, solvent, and nucleic acid by default", async () => {
        const params: IMoleculeInputParams = {
            considerProteins: true,
            considerCompounds: false, // Ensure we are only testing protein-related components
            proteinFormat: "pdb",
            // Defaults are includeMetalsAsProtein: true, includeSolventAsProtein: true
        };
        const moleculeInput = new MoleculeInput(params);
        const result =
            (await moleculeInput.getProtAndCompoundPairs()) as FileInfo[];

        expect(result).toHaveLength(1);
        const pdbContent = result[0].contents;
        expect(pdbContent).toMatch(/ATOM\s+.*\s+CA\s+GLY\s+/); // Protein
        expect(pdbContent).toMatch(/HETATM\s+.*\s+ZN\s+ZN\s+/); // Metal
        expect(pdbContent).toMatch(/HETATM\s+.*\s+O\s+HOH\s+/); // Solvent
        expect(pdbContent).toMatch(/HETATM\s+.*\s+P\s+A\s+/); // Nucleic Acid
    });

    it("should exclude metals when includeMetalsAsProtein is false", async () => {
        const params: IMoleculeInputParams = {
            considerProteins: true,
            considerCompounds: false,
            proteinFormat: "pdb",
            includeMetalsAsProtein: false,
            includeSolventAsProtein: true,
        };
        const moleculeInput = new MoleculeInput(params);
        const result =
            (await moleculeInput.getProtAndCompoundPairs()) as FileInfo[];
        expect(result).toHaveLength(1);
        const pdbContent = result[0].contents;
        expect(pdbContent).toMatch(/ATOM\s+.*\s+CA\s+GLY\s+/); // Protein
        expect(pdbContent).not.toMatch(/HETATM\s+.*\s+ZN\s+ZN\s+/); // Metal excluded
        expect(pdbContent).toMatch(/HETATM\s+.*\s+O\s+HOH\s+/); // Solvent included
        expect(pdbContent).toMatch(/HETATM\s+.*\s+P\s+A\s+/); // Nucleic Acid included
    });

    it("should exclude solvent when includeSolventAsProtein is false", async () => {
        const params: IMoleculeInputParams = {
            considerProteins: true,
            considerCompounds: false,
            proteinFormat: "pdb",
            includeMetalsAsProtein: true,
            includeSolventAsProtein: false,
        };
        const moleculeInput = new MoleculeInput(params);
        const result =
            (await moleculeInput.getProtAndCompoundPairs()) as FileInfo[];
        expect(result).toHaveLength(1);
        const pdbContent = result[0].contents;
        expect(pdbContent).toMatch(/ATOM\s+.*\s+CA\s+GLY\s+/); // Protein
        expect(pdbContent).toMatch(/HETATM\s+.*\s+ZN\s+ZN\s+/); // Metal included
        expect(pdbContent).not.toMatch(/HETATM\s+.*\s+O\s+HOH\s+/); // Solvent excluded
        expect(pdbContent).toMatch(/HETATM\s+.*\s+P\s+A\s+/); // Nucleic Acid included
    });

    it("should exclude nucleic acids when includeNucleicAsProtein is false", async () => {
        const params: IMoleculeInputParams = {
            considerProteins: true,
            considerCompounds: false,
            proteinFormat: "pdb",
            includeNucleicAsProtein: false,
        };
        const moleculeInput = new MoleculeInput(params);
        const result =
            (await moleculeInput.getProtAndCompoundPairs()) as FileInfo[];
        expect(result).toHaveLength(1);
        const pdbContent = result[0].contents;
        expect(pdbContent).toMatch(/ATOM\s+.*\s+CA\s+GLY\s+/); // Protein
        expect(pdbContent).toMatch(/HETATM\s+.*\s+ZN\s+ZN\s+/); // Metal included
        expect(pdbContent).toMatch(/HETATM\s+.*\s+O\s+HOH\s+/); // Solvent included
        expect(pdbContent).not.toMatch(/HETATM\s+.*\s+P\s+A\s+/); // Nucleic Acid excluded
    });

    it("should exclude both metal and solvent when both flags are false", async () => {
        const params: IMoleculeInputParams = {
            considerProteins: true,
            considerCompounds: false,
            proteinFormat: "pdb",
            includeMetalsAsProtein: false,
            includeSolventAsProtein: false,
        };
        const moleculeInput = new MoleculeInput(params);
        const result =
            (await moleculeInput.getProtAndCompoundPairs()) as FileInfo[];
        expect(result).toHaveLength(1);
        const pdbContent = result[0].contents;
        expect(pdbContent).toMatch(/ATOM\s+.*\s+CA\s+GLY\s+/); // Protein
        expect(pdbContent).not.toMatch(/HETATM\s+.*\s+ZN\s+ZN\s+/); // Metal excluded
        expect(pdbContent).not.toMatch(/HETATM\s+.*\s+O\s+HOH\s+/); // Solvent excluded
        expect(pdbContent).toMatch(/HETATM\s+.*\s+P\s+A\s+/); // Nucleic Acid included
    });
});