File size: 4,528 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
import { runWorker } from "@/Core/WebWorkers/RunWorker";
import { QueueParent } from "@/Queue/QueueParent";
import { IJobInfo } from "@/Queue/QueueTypes";
import { messagesApi } from "@/Api/Messages";

/**
 * A convert-with-openbabel queue.
 */
export class OpenBabelQueue extends QueueParent {
    /**
     * Run a batch of jobs.
     *
     * @param {IJobInfo[]} inputBatch  The input batch.
     * @param {number}     procs       The number of processors to use.
     * @returns {Promise<IJobInfo[]>}  The output batch.
     */
    public runJobBatch(
        inputBatch: IJobInfo[],
        procs: number
    ): Promise<IJobInfo[]> {
        const inputs = inputBatch.map((jobInfo) => jobInfo.input);

        // Remove any tree node (to avoid serialization issues).
        for (let i = 0; i < inputs.length; i++) {
            if (inputs[i].inputFile.treeNode !== undefined) {
                inputs[i].inputFile.treeNode = undefined;
            }
        }
        return runWorker(
            new Worker(new URL("./OpenBabel.worker.ts", import.meta.url)),
            inputs,
            true // auto terminate the worker.
        )
            .then((outputBatch: any[]) => {
                const errorMessages: string[] = [];
                for (let i = 0; i < outputBatch.length; i++) {
                    const output = outputBatch[i];
                    const input = inputs[i];
                    if (
                        output.stdErr &&
                        output.stdErr.toLowerCase().includes("error")
                    ) {
                        if (input.surpressMsgs) {
                            continue;
                        }
                        const stdErr = output.stdErr;
                        let errorMessage: string;
                        if (
                            stdErr.includes("Cannot enlarge memory") ||
                            stdErr.includes("(OOM)")
                        ) {
                            errorMessage = `The molecule '${input.inputFile.name}' is too large to process due to memory limitations.`;
                        } else {
                            let details = stdErr.replace("Open Babel ", "");
                            details = details.replace(/^=+$/gm, "");
                            details = details.replace(/^\*{1,10}/gm, "");
                            details = details.trim();
                            details = details.replace(/([^.!?])$/gm, "$1.");
                            details = details.replace(/\s{2,}/g, " ");
                            details = details.trim();
                            details = details.replace(/\n/g, " ");
                            errorMessage = `Could not process '${input.inputFile.name}'. The structure may be too large or the format incorrect. Technical details: ${details}`;
                        }
                        errorMessages.push(errorMessage);
                    }
                }
                if (errorMessages.length > 0) {
                    let finalMessage = "";
                    if (errorMessages.length === 1) {
                        finalMessage = errorMessages[0];
                    } else {
                        finalMessage =
                            "<p>Multiple errors occurred during processing:</p><ul><li>" +
                            errorMessages.join("</li><li>") +
                            "</li></ul>";
                    }
                    messagesApi.popupError(finalMessage);
                }
                // If any of the files have {ERROR} in them, let user know and
                // remove from outputBatch.
                for (let i = 0; i < outputBatch.length; i++) {
                    if (outputBatch[i].outputFiles) {
                        outputBatch[i].outputFiles = outputBatch[
                            i
                        ].outputFiles.filter(
                            (file: string | undefined) =>
                                file !== "{ERROR}" && file !== undefined
                        );
                    } else {
                        outputBatch[i].outputFiles = [];
                    }
                }
                // Update .output value of each jobInfo.
                for (let i = 0; i < inputBatch.length; i++) {
                    inputBatch[i].output = outputBatch[i];
                }

                return inputBatch;
            })
            .catch((err) => {
                throw err;
            });
    }
}