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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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import { messagesApi } from "@/Api/Messages";
import { PopupVariant } from "@/UI/MessageAlerts/Popups/InterfacesAndEnums";
import { FileInfo } from "../FileInfo";
import { IFileInfo } from "../Types";
import {
    IFormatInfo,
    getFormatInfoGivenType,
} from "../LoadSaveMolModels/Types/MolFormats";
import { OpenBabelQueue } from "./OpenBabelQueue";
import {
    IUserArgOption,
    IUserArgSelect,
} from "@/UI/Forms/FormFull/FormFullInterfaces";
import { getSetting } from "@/Plugins/Core/Settings/LoadSaveSettings";
import { isTest } from "@/Core/GlobalVars";

export enum WhichMolsGen3D {
    All,
    None,
    OnlyIfLacks3D,
}

export enum Gen3DLevel {
    None = "none",
    Fastest = "fastest",
    Fast = "fast",
    Medium = "medium",
    Better = "better",
    Best = "best",

    // Default used to be medium for speed, but I was surprised to learn that
    // even ATP (basic molecule) had bad geometry unless best. SMILES I used for
    // testing:
    // O[C@@H]1[C@@H](CO[P@@](=O)(O[P@](=O)(OP(=O)(O)O)O)O)O[C@H]([C@@H]1O)n1cnc2c1ncnc2N

    // Default = "medium",
    Default = "best",
}

export interface IGen3DOptions {
    whichMols: WhichMolsGen3D;
    level?: Gen3DLevel;
}

/**
 * Gets the user argument select for specifying how to generate 3D coordinates.
 *
 * @param {string}  label              The label for the argument.
 * @param {string}  description        The description for the argument.
 * @param {boolean} includeNoneOption  Whether to include the "none" option.
 * @param {string}  defaultVal         The default value for the argument.
 * @returns {IUserArgSelect}  The user argument.
 */
export function getGen3DUserArg(
    label: string,
    description: string,
    includeNoneOption = false,
    defaultVal = Gen3DLevel.Default
): IUserArgSelect {
    const options = [
        {
            description:
                "fastest: no forcefield optimization or conformer search",
            val: "fastest",
        },
        {
            description:
                "fast: quick forcefield optimization, no conformer search",
            val: "fast",
        },
        {
            description:
                "medium: quick forcefield optimization and fast conformer search",
            val: "medium",
        },
        {
            description:
                "better: medium forcefield optimization and fast conformer search",
            val: "better",
        },
        {
            description:
                "best: max forcefield optimization and thorough conformer search",
            val: "best",
        },
    ] as IUserArgOption[];

    // let defaultVal = "medium";

    if (includeNoneOption) {
        options.unshift({
            description:
                "recommended: max forcefield optimization and thorough conformer search",
            val: "best",
        });
        options.unshift({
            description: "none: do not generate 3D coordinates",
            val: "none",
        });
        defaultVal = Gen3DLevel.None;
    }

    return {
        label: label,
        description: description,
        id: "gen3D",
        val: defaultVal,
        options: options,
    } as IUserArgSelect;
}

/**
 * Runs OpenBabel.
 *
 * @param {string}       appId                   The app ID.
 * @param {string[][]}  argsLists                The arguments to pass to
 *                                               OpenBabel. Each set of
 *                                               arguments is a string[], so
 *                                               passing multiple argument sets
 *                                               (e.g., one for each input
 *                                               file), requires string[][].
 * @param {FileInfo[] | IFileInfo[]} inputFiles  The input files to pass to
 *                                               OpenBabel.
 * @param {boolean}      [surpressMsgs=false]    Whether to surpress messages.
 * @returns {Promise<any>}  A promise that resolves to the output of the
 *     program. Void if there is an error?
 */
async function runOpenBabel(
    appId: string,
    argsLists: string[][],
    inputFiles: FileInfo[] | IFileInfo[],
    surpressMsgs = false
): Promise<any> {
    // Quick validation to make sure argsLists is in right format.
    if (argsLists.length > 0 && !Array.isArray(argsLists[0])) {
        throw new Error("argsLists must be an array of arrays.");
    }

    // Associate an index with each inputFile so you can reorder them after
    // finishing.
    inputFiles.forEach((f, i) => {
        f.auxData = i;
    });

    inputFiles = inputFiles.map((f) =>
        (f as FileInfo).serialize ? (f as FileInfo).serialize() : f
    );

    // Construct payloads by "zipping" the inputFiles and argsLists together.
    const payloads: any[] = [];
    for (let i = 0; i < inputFiles.length; i++) {
        payloads.push({
            args: argsLists[i],
            inputFile: inputFiles[i],
            surpressMsgs,
        });
    }

    const maxProcs = (await getSetting("maxProcs")) as number;

    return await new OpenBabelQueue(appId, payloads, maxProcs).done;
}

/**
 * Consider whether a warning is needed about lack of 3D coordinates. If needed,
 * show the warning.
 *
 * @param {IFormatInfo[]} formatInfos  The format infos.
 * @returns {any}  A timer that can be used to clear the warning if needed.
 */
function considerThreeDNeededWarning(
    formatInfos: (IFormatInfo | undefined)[]
): any {
    const warningNeeded = formatInfos.some(
        (f) => f !== undefined && f.lacks3D === true
    );

    let msgTimer = undefined;
    if (warningNeeded && !isTest) {
        msgTimer = setTimeout(() => {
            // Warn user

            messagesApi.popupMessage(
                "Converting Compounds to 3D",
                "<p>One or more input molecules does not include 3D coordinates. Currently calculating coordinates, which could take a while. Molecule(s) will appear in the Viewer when ready.</p><p>Tip: You can select the method for generating coordinates via <i>File → Open...</i> to speed up the calculation or improve the quality of the generated structures.</p>",
                PopupVariant.Warning,
                undefined,
                false,
                {}
            );
        }, 2000);
    }

    return msgTimer;
}

/**
 * Gets the formats that OpenBabel can read and write. NOTE: this breaks
 * openbabel! It's not clear why. So just use it to see the formats (for
 * debugging), then uncomment.
 *
 * @returns {Promise<IFormatInfo[]>}  A promise that resolves to the formats.
 */
// export async function getObabelFormats(): Promise<IFormatInfo[]> {
//     const fakeFile = new FileInfo({
//         name: "fakeFile",
//         contents: "",
//     } as IFileInfo);
//     const obabelFormats = await runOpenBabel(
//         "getFormats",
//         [["-L", "formats"]],
//         // [["--version"]],
//         [fakeFile]
//     );

//     console.log(obabelFormats[0].stdOutOrErr);

//     return obabelFormats;
// }

/**
 * Separates a file into multiple files, one for each molecule.
 *
 * @param  {FileInfo[]}    srcFileInfos  The source file info.
 * @param  {IFormatInfo[]} formatInfos   The format infos.
 * @returns {Promise<FileInfo[]>}  A promise that resolves to the separated
 *    files.
 */
async function separateFiles(
    srcFileInfos: FileInfo[],
    formatInfos: (IFormatInfo | undefined)[]
): Promise<FileInfo[]> {
    // Note that the approach here is to divide the file into multiple files
    // (since one input SDF can have multiple molecules), and then to separate
    // the individual models into grousp to run on separate webworkers. You
    // incur some overhead by shuttling the split file back to main thread only
    // to be redistributed to multiple web workers, but it's the best approach
    // in terms of speed.

    const separateFileCmds = srcFileInfos.map((srcFileInfo, i) => [
        srcFileInfo.name,
        "-m",
        "-O",
        `tmp${i}.${srcFileInfo.getFormatInfo()?.primaryExt}`,
    ]);

    const fileContentsFromInputs = await runOpenBabel(
        "convertPrep",
        separateFileCmds,
        srcFileInfos
    );

    // Note that a given input molecule can yield multiple outputs if it
    // contained many molecules (e.g., multi-molecule SDF file)
    const individualMolFiles = fileContentsFromInputs.map(
        (f: any) => f.outputFiles
    );

    // Convert them to a FileInfo
    let fileInfoIdx = -1;
    return individualMolFiles
        .map((fileContent: string[], i: any) => {
            return fileContent.map((f: string) => {
                fileInfoIdx++;
                const ext = srcFileInfos[i].getFormatInfo()?.primaryExt;
                return {
                    name: `tmp${fileInfoIdx}.${ext}`,
                    contents: f,
                    auxData: formatInfos[i],
                } as IFileInfo;
            });
        })
        .flat();
}

/**
 * Converts molecules to another format using OpenBabel.
 *
 * @param  {FileInfo[]}    fileInfos             The information about the file
 *                                               to convert.
 * @param  {string}        targetFormat          The target extension.
 * @param  {IGen3DOptions} [gen3D]               Whether to assign 3D
 *                                               coordinates.
 * @param  {number | null} [pH]                  The pH to use for protonation.
 *                                               If null, removes hydrogen
 *                                               atoms.
 * @param  {boolean}       [desalt=false]        Whether to desalt the
 *                                               molecules.
 * @param  {boolean}       [surpressMsgs=false]  Whether to surpress messages.
 * @returns {Promise<string[]>}  A promise that resolves to the converted
 *    molecules.
 */
async function convertToNewFormat(
    fileInfos: FileInfo[],
    targetFormat: string,
    gen3D?: IGen3DOptions,
    pH?: number | null,
    desalt = false,
    surpressMsgs = false
): Promise<string[]> {
    const cmdsList = fileInfos.map((fileInfo: FileInfo) => {
        const cmds = [fileInfo.name, "-m"]; // Note always dividing into multiple files.

        if (desalt) {
            cmds.push(...["-r"]);
        }

        // If not specified, set to only if lacks 3d.
        let whichMols =
            gen3D?.whichMols === undefined
                ? WhichMolsGen3D.OnlyIfLacks3D
                : gen3D.whichMols;

        if (gen3D?.level === Gen3DLevel.None) {
            // A second way to say no 3D generation.
            whichMols = WhichMolsGen3D.None;
        }

        // If not specified, set to default
        const level =
            gen3D?.level === undefined ? Gen3DLevel.Default : gen3D.level;

        switch (whichMols) {
            case WhichMolsGen3D.All:
                cmds.push(...["--gen3D", level]);
                break;
            case WhichMolsGen3D.OnlyIfLacks3D:
                if (fileInfo.auxData && fileInfo.auxData.lacks3D === true) {
                    cmds.push(...["--gen3D", level]);
                }
                break;
            default:
                break;
        }

        // if (
        //     gen3D === true ||
        //     (fileInfo.auxData !== undefined &&
        //         fileInfo.auxData.lacks3D === true)
        // ) {
        //     cmds.push(...["--gen3D", "best"]);
        // }

        if (pH !== undefined && pH !== null) {
            cmds.push(...["-p", pH.toString()]);
        } else if (pH === null) {
            // Removes hydrogens. Good for 2D visualizations.
            cmds.push(...["-d"]);
        }

        // Are there additional arguments to pass to OpenBabel?
        const formatInfo = getFormatInfoGivenType(targetFormat);

        const extToUse = formatInfo?.obabelFormatName ?? targetFormat;

        cmds.push(...["-O", "tmpout." + extToUse]);

        if (formatInfo?.extraObabelArgs !== undefined) {
            cmds.push(...formatInfo.extraObabelArgs);
        }

        // console.log(cmds);

        return cmds;
    });

    // tmpPass = [cmdsList, fileInfos.map(f => JSON.parse(JSON.stringify(f)))];
    const convertedFileContents = await runOpenBabel(
        "convert",
        cmdsList,
        fileInfos,
        surpressMsgs
    );

    // The output files are located in the .outputFiles properties.
    // Flatten them into one array.
    return convertedFileContents.map((c: any) => c.outputFiles).flat();
}

/**
 * Converts a molecule to another format using OpenBabel.
 *
 * @param  {FileInfo[]}  srcFileInfos          The information about the file to
 *                                             convert.
 * @param  {string}      targetFormat          The target extension.
 * @param  {boolean}     [gen3D]               Whether to assign 3D coordinates.
 * @param  {number}      [pH]                  The pH to use for protonation. If
 *                                             null, removes hydrogens (-d).
 * @param  {boolean}     [desalt=false]        Whether to desalt the molecules.
 * @param  {boolean}     [surpressMsgs=false]  Whether to surpress messages.
 * @returns {Promise<string>}  A promise that resolves to the converted
 *     molecule.
 */
export async function convertFileInfosOpenBabel(
    srcFileInfos: FileInfo[], // Can be multiple-model SDF file, for example.
    targetFormat: string,
    gen3D?: IGen3DOptions,
    pH?: number | null,
    desalt = false,
    surpressMsgs = false
    // debug?: boolean
): Promise<string[]> {
    // Get info about the file
    const formatInfos = srcFileInfos.map((f) => f.getFormatInfo());

    const msgTimer = considerThreeDNeededWarning(formatInfos);

    // If the format is CIF or MCIF, do not separate files. This is because
    // the intermediate CIF writing by Open Babel can be lossy (stripping
    // polymer entity info), causing the subsequent PDB conversion to fail
    // to identify chains correctly.
    const skipSeparation = formatInfos.some(
        (f) => f?.primaryExt === "cif" || f?.primaryExt === "mcif"
    );

    let fileInfos: FileInfo[];
    if (skipSeparation) {
        fileInfos = srcFileInfos;
        // Initialize auxData if missing, as separateFiles usually does this
        fileInfos.forEach((f, i) => {
            if (!f.auxData) f.auxData = formatInfos[i];
        });
    } else {
        fileInfos = await separateFiles(srcFileInfos, formatInfos);
    }

    const outputFiles = await convertToNewFormat(
        fileInfos,
        targetFormat,
        gen3D,
        pH,
        desalt,
        surpressMsgs
    );

    // TODO: Report what's in the .stdOutAndErr property? Not sure needed.

    if (msgTimer !== undefined) {
        clearTimeout(msgTimer);
        // messagesApi.closePopupMessage();
    }

    return outputFiles;

    // .catch((err: any) => {
    //     throw err;
    // });

    // return runOpenBabel(cmds, [srcFileInfo])
    //     .then((convertedFileContents: any) => {
    //         messagesApi.closePopupMessage();
    //         return convertedFileContents.outputFiles;
    //     })
    //     .catch((err: any) => {
    //         throw err;
    //     });
}