File size: 4,291 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
import { getMoleculesFromStore } from "@/Store/StoreExternalAccess";
import { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList";
import { getTerminalNodesToConsider } from "@/UI/Navigation/TreeView/TreeUtils";
import { IMolsToConsider, ICompiledNodes } from "./Types";
import { TreeNodeType } from "@/UI/Navigation/TreeView/TreeInterfaces";
import { TreeNode } from "@/TreeNodes/TreeNode/TreeNode";

/**
 * Runs the job when the user wants to save in a non-molmoda format, by
 * molecule.
 *
 * @param {IMolsToConsider} molsToConsider   The molecules to save.
 * @param {boolean}   separateComponents     Whether to separate components into different files.
 * @returns {ICompiledNodes}  The compiled nodes organized by type and grouping.
 */
export function compileByMolecule(
    molsToConsider: IMolsToConsider,
    separateComponents: boolean
): ICompiledNodes {
    // Not using molmoda format. Create ZIP file with protein and small
    // molecules.
    const byType = new Map<TreeNodeType, TreeNodeList[]>();
 const nodeGroups: TreeNodeList[] = [];
 const compoundsNodes = new TreeNodeList();

    getMoleculesFromStore().forEach((topLevelNode) => {
        // Get all descendants for this molecule
        const allNodes = new TreeNodeList([topLevelNode]);
        if (topLevelNode.nodes) {
            allNodes.extend(topLevelNode.nodes);
        }

        // Filter based on user selection (visible, selected, etc.)
        let terminalNodes = getTerminalNodesToConsider(
            molsToConsider,
            allNodes
        );

        // Remove undefineds, regions
        terminalNodes = terminalNodes.filters.removeUndefined();
        terminalNodes = terminalNodes.filters.keepRegions(false);

        if (terminalNodes.length === 0) return;

        if (separateComponents) {
            // Group by type
            const nodesByType = new Map<TreeNodeType, TreeNodeList>();
   // Helper lists for legacy output
   const nonCompoundNodesForThisMol = new TreeNodeList();

            terminalNodes.forEach((node) => {
                const type = node.type || TreeNodeType.Other;
                if (!nodesByType.has(type)) {
                    nodesByType.set(type, new TreeNodeList());
                }
                nodesByType.get(type)?.push(node);

    // Legacy population
    if (type === TreeNodeType.Compound) {
     compoundsNodes.push(node);
    } else {
     nonCompoundNodesForThisMol.push(node);
    }
            });

   // Process each type for byType map
            nodesByType.forEach((nodes, type) => {
                if (!byType.has(type)) {
                    byType.set(type, []);
                }

                // Compounds are always saved individually (one file per compound)
                if (type === TreeNodeType.Compound) {
                    nodes.forEach((node) => {
                        byType.get(type)?.push(new TreeNodeList([node]));
                    });
                } else {
                    // Other components (Protein, Nucleic, Metal, etc.) are grouped per top-level molecule.
                    // So all chains of Protein A go into one file.
                    byType.get(type)?.push(nodes);
                }
            });

   // Legacy: Add non-compound nodes group
   if (nonCompoundNodesForThisMol.length > 0) {
    nodeGroups.push(nonCompoundNodesForThisMol);
   }

        } else {
   // Lump everything for this molecule into one group under 'Other' (or mix)
            if (!byType.has(TreeNodeType.Other)) {
                byType.set(TreeNodeType.Other, []);
            }
            byType.get(TreeNodeType.Other)?.push(terminalNodes);

   // For legacy compatibility, if we aren't separating, we treat everything as a "non-compound" group usually,
   // or we separate compounds if they exist?
   // Original behavior for !keepCompoundsSeparate was grouping everything into nodeGroups.
   // But usually keepCompoundsSeparate passed to compileByMolecule was true in the calling code of compileMolModels.
   // In compileMolModels (original), if !keepCompoundsSeparate, it grouped everything.
   // Here, if separateComponents is false, we put everything in nodeGroups.
   nodeGroups.push(terminalNodes);
        }
    });

 return { byType, nodeGroups, compoundsNodes };
}