| import { randomID } from "@/Core/Utils/MiscUtils"; |
| import { sendResponseToMainThread } from "@/Core/WebWorkers/WorkerHelper"; |
| import { FileInfo } from "../FileInfo"; |
|
|
| |
|
|
| |
|
|
| |
|
|
| |
| |
| |
|
|
| import * as Webobabel from "../../../public/js/obabel-wasm/obabel.js"; |
|
|
| let oBabelModReady: any = undefined; |
| let stdOutOrErr = ""; |
| let stdErr = ""; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function runBabel(args: string[], inputFiles: FileInfo[]): Promise<any> { |
| |
| if (oBabelModReady === undefined) { |
| |
| const fsHelperFuncs = { |
| |
| |
| |
| |
| |
| |
| |
| |
| _bindModule(Module: any) { |
| |
| |
| |
| const This = this as any; |
| for (const key in This) { |
| This[key] = This[key].bind(Module); |
| } |
| }, |
|
|
| |
| |
| |
| |
| |
| mkdir(path: string) { |
| (this as any).FS.mkdir(path, true, true); |
| }, |
|
|
| |
| |
| |
| |
| |
| |
| writeFile: function (path: string, text: string) { |
| (this as any).FS.writeFile(path, text, { |
| encoding: "utf-8", |
| mode: 0o777, |
| }); |
| }, |
|
|
| |
| |
| |
| |
| |
| |
| readFile(path: string): string { |
| return new TextDecoder("utf-8").decode( |
| (this as any).FS.readFile(path) |
| ); |
| }, |
|
|
| |
| |
| |
| |
| |
| |
| |
| readDir(path: string): string[] { |
| return (this as any).FS.readdir(path); |
| }, |
|
|
| |
| |
| |
| |
| |
| |
| |
| chmod(path: string, mode: string) { |
| (this as any).FS.chmod(path, mode); |
| }, |
|
|
| |
| |
| |
| |
| |
| unlink(path: string) { |
| (this as any).FS.unlink(path); |
| }, |
|
|
| |
| |
| |
| |
| |
| rmdir(path: string) { |
| |
| (this as any).FS.rmdir(path); |
| }, |
| }; |
|
|
| const Module = { |
| |
| |
| files: fsHelperFuncs, |
| |
| logReadFiles: true, |
| noInitialRun: true, |
| |
| locateFile: (path: string) => { |
| return "obabel-wasm/" + path; |
| }, |
| print: (text: string) => { |
| stdOutOrErr += text + "\n"; |
| }, |
| printErr: (text: string) => { |
| stdOutOrErr += text + "\n"; |
| stdErr += text + "\n"; |
| }, |
| preRun: [ |
| function (This: any) { |
| This.ENV.BABEL_DATADIR = "/data"; |
| }, |
| ], |
| }; |
|
|
| Module.files._bindModule(Module); |
| oBabelModReady = Webobabel(Module); |
| } |
|
|
| return oBabelModReady |
| .then((mod: any) => { |
| |
| const tmpDir = "/tmp_" + randomID() + "/"; |
| mod.files.mkdir(tmpDir); |
| mod.files.chmod(tmpDir, 0o777); |
|
|
| |
| |
| const inputFileNameSet = new Set( |
| inputFiles.map((file) => file.name) |
| ); |
| if (inputFileNameSet.size !== inputFiles.length) { |
| console.log( |
| "Uniq names:", |
| inputFileNameSet, |
| ". Size != ", |
| inputFiles.length |
| ); |
| throw new Error("Input file names must be unique."); |
| } |
|
|
| |
| for (const file of inputFiles) { |
| mod.files.writeFile(tmpDir + file.name, file.contents); |
| } |
|
|
| |
| |
| let inputFileActuallyUsed: FileInfo | undefined = undefined; |
| let outputFileActuallyUsed: string | undefined = undefined; |
|
|
| |
| |
| for (let i = 0; i < args.length; i++) { |
| if (args[i] === "-O") { |
| |
| outputFileActuallyUsed = args[i + 1]; |
| args[i + 1] = tmpDir + args[i + 1]; |
| } else if (inputFileNameSet.has(args[i])) { |
| |
| |
| inputFileActuallyUsed = inputFiles.find( |
| (file) => file.name === args[i] |
| ); |
| args[i] = tmpDir + args[i]; |
| } |
| } |
|
|
| |
| |
| |
| let inputFileBaseWithoutNumbers = (<FileInfo>( |
| inputFileActuallyUsed |
| )) as FileInfo | string; |
| if (inputFileBaseWithoutNumbers !== undefined) { |
| inputFileBaseWithoutNumbers = ( |
| inputFileBaseWithoutNumbers as FileInfo |
| ).name |
| .replace(/\d/g, "") |
| .split(".")[0]; |
| } |
|
|
| const outputFileBaseWithoutNumbers = outputFileActuallyUsed |
| ?.replace(/\d/g, "") |
| .split(".")[0]; |
|
|
| if ( |
| inputFileBaseWithoutNumbers === outputFileBaseWithoutNumbers && |
| <FileInfo>inputFileActuallyUsed !== undefined |
| ) { |
| throw new Error( |
| "Input and output file names must be sufficiently different: " + |
| (<FileInfo>inputFileActuallyUsed).name + |
| " vs. " + |
| outputFileActuallyUsed |
| ); |
| } |
|
|
| const filesBeforeRun = mod.files.readDir(tmpDir); |
|
|
| try { |
| |
| |
| |
| mod.callMain(args); |
| } catch (err) { |
| console.log("ERR", err); |
| console.log(args); |
| console.log(inputFiles[0].contents); |
| stdOutOrErr += err; |
| stdErr += err; |
| } |
|
|
| const filesAfterRun = mod.files.readDir(tmpDir); |
|
|
| |
| |
| let newFiles = filesAfterRun.filter( |
| (fileName: string) => !filesBeforeRun.includes(fileName) |
| ); |
|
|
| if ( |
| newFiles.length === 0 && |
| args.indexOf("-L") === -1 && |
| args.indexOf("--version") === -1 |
| ) { |
| |
| |
| console.warn("No new files were created."); |
| newFiles = [inputFileActuallyUsed?.name]; |
| } |
|
|
| const contents: string[] = newFiles.map((fileName: string) => { |
| if (fileName === inputFileActuallyUsed?.name) { |
| return "{ERROR}"; |
| } |
| fileName = tmpDir + fileName; |
| return mod.files.readFile(fileName); |
| }); |
|
|
| |
| const filesToDelete = [ |
| ...new Set([...newFiles, ...inputFileNameSet]), |
| ]; |
| for (const fileName of filesToDelete) { |
| mod.files.unlink(tmpDir + fileName); |
| } |
| mod.files.rmdir(tmpDir); |
|
|
| return [inputFileActuallyUsed?.auxData, contents]; |
| }) |
| .then((outputFilesData: [any, string[]]) => { |
| return { |
| orderIdxs: outputFilesData[0], |
| outputFiles: outputFilesData[1], |
| stdOutOrErr, |
| stdErr, |
| }; |
| }) |
| .catch((err: Error) => { |
| throw err; |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function easyParsePDBIfPossible( |
| args: string[], |
| inputFiles: any |
| ): boolean | Promise<any> { |
| const testArgs = args.filter((arg) => arg !== "-m" && arg !== "-O"); |
| if (testArgs.length !== 2) { |
| return false; |
| } |
|
|
| |
|
|
| |
| const fileExt = [".pdb", ".PDB"]; |
| if ( |
| !fileExt.includes(testArgs[0].slice(-4)) || |
| testArgs[0].slice(-4) !== testArgs[1].slice(-4) |
| ) { |
| return false; |
| } |
|
|
| |
| const fileInfoToUse = inputFiles.find((f: any) => f.name === testArgs[0]); |
| if (!fileInfoToUse) { |
| return false; |
| } |
|
|
| |
| |
| const pdbTxtToParse = fileInfoToUse.contents.trim(); |
| const lines = pdbTxtToParse.split("\n"); |
|
|
| |
| const lastLine = lines[lines.length - 1].trim(); |
| if (lastLine !== "END" && lastLine !== "ENDMDL") { |
| lines.push("END"); |
| } |
|
|
| const splitLines: string[][] = []; |
| let currentSplitLine: string[] = []; |
| for (const line of lines) { |
| if (line === "END" || line === "ENDMDL") { |
| splitLines.push(currentSplitLine); |
| currentSplitLine = []; |
| } else { |
| currentSplitLine.push(line); |
| } |
| } |
|
|
| return Promise.resolve({ |
| orderIdxs: fileInfoToUse.auxData, |
| outputFiles: splitLines.map((lines) => lines.join("\n")), |
| }); |
| } |
|
|
| let currentlyRunning = false; |
|
|
| self.onmessage = (params: MessageEvent) => { |
| if (currentlyRunning) { |
| throw new Error("Already running"); |
| } |
|
|
| currentlyRunning = true; |
| const argsSets = params.data.map((d: any) => d.args); |
| const inputFiles = params.data.map((d: any) => d.inputFile); |
| |
|
|
| const promises = argsSets.map((args: string[]) => { |
| |
| |
| |
| |
|
|
| |
|
|
| |
|
|
| |
| |
| |
| const pdbToPdbPromise = easyParsePDBIfPossible(args, inputFiles); |
| if (pdbToPdbPromise !== false) { |
| return pdbToPdbPromise; |
| } |
|
|
| return runBabel(args, inputFiles); |
| }); |
|
|
| Promise.all(promises) |
| .then((results: any) => { |
| sendResponseToMainThread(results); |
| currentlyRunning = false; |
| return; |
| }) |
| .catch((err: Error) => { |
| throw err; |
| }); |
| }; |
|
|