| 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 = "best", |
| } |
|
|
| export interface IGen3DOptions { |
| whichMols: WhichMolsGen3D; |
| level?: Gen3DLevel; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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[]; |
|
|
| |
|
|
| 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; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async function runOpenBabel( |
| appId: string, |
| argsLists: string[][], |
| inputFiles: FileInfo[] | IFileInfo[], |
| surpressMsgs = false |
| ): Promise<any> { |
| |
| if (argsLists.length > 0 && !Array.isArray(argsLists[0])) { |
| throw new Error("argsLists must be an array of arrays."); |
| } |
|
|
| |
| |
| inputFiles.forEach((f, i) => { |
| f.auxData = i; |
| }); |
|
|
| inputFiles = inputFiles.map((f) => |
| (f as FileInfo).serialize ? (f as FileInfo).serialize() : f |
| ); |
|
|
| |
| 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; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function considerThreeDNeededWarning( |
| formatInfos: (IFormatInfo | undefined)[] |
| ): any { |
| const warningNeeded = formatInfos.some( |
| (f) => f !== undefined && f.lacks3D === true |
| ); |
|
|
| let msgTimer = undefined; |
| if (warningNeeded && !isTest) { |
| msgTimer = setTimeout(() => { |
| |
|
|
| 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; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| async function separateFiles( |
| srcFileInfos: FileInfo[], |
| formatInfos: (IFormatInfo | undefined)[] |
| ): Promise<FileInfo[]> { |
| |
| |
| |
| |
| |
| |
|
|
| const separateFileCmds = srcFileInfos.map((srcFileInfo, i) => [ |
| srcFileInfo.name, |
| "-m", |
| "-O", |
| `tmp${i}.${srcFileInfo.getFormatInfo()?.primaryExt}`, |
| ]); |
|
|
| const fileContentsFromInputs = await runOpenBabel( |
| "convertPrep", |
| separateFileCmds, |
| srcFileInfos |
| ); |
|
|
| |
| |
| const individualMolFiles = fileContentsFromInputs.map( |
| (f: any) => f.outputFiles |
| ); |
|
|
| |
| 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(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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"]; |
|
|
| if (desalt) { |
| cmds.push(...["-r"]); |
| } |
|
|
| |
| let whichMols = |
| gen3D?.whichMols === undefined |
| ? WhichMolsGen3D.OnlyIfLacks3D |
| : gen3D.whichMols; |
|
|
| if (gen3D?.level === Gen3DLevel.None) { |
| |
| whichMols = WhichMolsGen3D.None; |
| } |
|
|
| |
| 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 (pH !== undefined && pH !== null) { |
| cmds.push(...["-p", pH.toString()]); |
| } else if (pH === null) { |
| |
| cmds.push(...["-d"]); |
| } |
|
|
| |
| const formatInfo = getFormatInfoGivenType(targetFormat); |
|
|
| const extToUse = formatInfo?.obabelFormatName ?? targetFormat; |
|
|
| cmds.push(...["-O", "tmpout." + extToUse]); |
|
|
| if (formatInfo?.extraObabelArgs !== undefined) { |
| cmds.push(...formatInfo.extraObabelArgs); |
| } |
|
|
| |
|
|
| return cmds; |
| }); |
|
|
| |
| const convertedFileContents = await runOpenBabel( |
| "convert", |
| cmdsList, |
| fileInfos, |
| surpressMsgs |
| ); |
|
|
| |
| |
| return convertedFileContents.map((c: any) => c.outputFiles).flat(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function convertFileInfosOpenBabel( |
| srcFileInfos: FileInfo[], |
| targetFormat: string, |
| gen3D?: IGen3DOptions, |
| pH?: number | null, |
| desalt = false, |
| surpressMsgs = false |
| |
| ): Promise<string[]> { |
| |
| const formatInfos = srcFileInfos.map((f) => f.getFormatInfo()); |
|
|
| const msgTimer = considerThreeDNeededWarning(formatInfos); |
|
|
| |
| |
| |
| |
| const skipSeparation = formatInfos.some( |
| (f) => f?.primaryExt === "cif" || f?.primaryExt === "mcif" |
| ); |
|
|
| let fileInfos: FileInfo[]; |
| if (skipSeparation) { |
| fileInfos = srcFileInfos; |
| |
| 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 |
| ); |
|
|
| |
|
|
| if (msgTimer !== undefined) { |
| clearTimeout(msgTimer); |
| |
| } |
|
|
| return outputFiles; |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|