File size: 2,640 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
// Entry point for all the save-molecule functions except molmoda, which should
// be accessed directly.

import { compileByMolecule } from "./CompileByMolecule";
import { saveTxtFiles, getConvertedTxtsWithNaming } from "./SaveMolModelsUtils";
import {
    IMolsToConsider,
    ICompiledNodes,
    ICmpdNonCmpdFileInfos,
} from "./Types";
import { FileInfo } from "@/FileSystem/FileInfo";
import { TreeNodeType } from "@/UI/Navigation/TreeView/TreeInterfaces";

/**
 * Compiles (organizes) all the molecules.
 *
 * @param  {IMolsToConsider} molsToConsider   The molecules to compile.
 * @param  {boolean}   separateComponents     Whether to separate components.
 * @returns {ICompiledNodes}  The compiled nodes.
 */
export function compileMolModels(
    molsToConsider: IMolsToConsider,
 separateComponents: boolean
): ICompiledNodes {
 return compileByMolecule(molsToConsider, separateComponents);
}

/**
 * Converts compiled models to file infos.
 *
 * @param  {ICompiledNodes} compiledNodes   The compiled models.
 * @param  {{ [key in TreeNodeType]?: string }}   formats  Map of formats for each component type.
 * @param  {string} fallbackFormat Format to use if specific type not in map (or for mixed).
 * @returns {Promise<ICmpdNonCmpdFileInfos>}  The file infos.
 */
export function convertCompiledMolModelsToIFileInfos(
    compiledNodes: ICompiledNodes,
 formats: { [key in TreeNodeType]?: string },
 fallbackFormat: string
): Promise<ICmpdNonCmpdFileInfos> {
 
 const allPromises: Promise<FileInfo[]>[] = [];

 compiledNodes.byType.forEach((nodeGroups, type) => {
  const format = formats[type] || fallbackFormat;
  
  nodeGroups.forEach(group => {
      // Always merge the group into one file.
      allPromises.push(getConvertedTxtsWithNaming(group, format, true, type));
  });
 });

 return Promise.all(allPromises).then((results) => {
  const flat = results.reduce((acc, val) => acc.concat(val), []);
  // Populate legacy fields for compatibility if needed, though flat list is usually sufficient for saving
  const compoundFileInfos = flat.filter(f => f.name.includes("Compound")); 
  const nonCompoundFileInfos = flat.filter(f => !f.name.includes("Compound"));

  return { 
      allFileInfos: flat,
      compoundFileInfos,
      nonCompoundFileInfos
  };
 });
}

/**
 * Saves the compiled models.
 *
 * @param  {string}    filename       The filename to save to.
 * @param  {ICmpdNonCmpdFileInfos} infos  The file infos.
 * @returns {Promise<any>}  The promise.
 */
export function saveMolFiles(
    filename: string,
 infos: ICmpdNonCmpdFileInfos
): Promise<any> {
 return saveTxtFiles(infos.allFileInfos, filename);
}