| |
| |
| import { messagesApi } from "@/Api/Messages"; |
| import type { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList"; |
| import { parseUsing3DMolJs } from "./_ParseUsing3DMolJs"; |
| import { parseUsingOpenBabel } from "./_ParseUsingOpenBabel"; |
| import { parseUsingMolModa } from "./_ParseUsingMolModa"; |
| import { molFormatInformation, MolLoader } from "../Types/MolFormats"; |
| import { getFileNameParts } from "@/FileSystem/FilenameManipulation"; |
| import { addDefaultLoadMolParams, ILoadMolParams } from "./Types"; |
| import { stopAllWaitSpinners } from "@/UI/MessageAlerts/WaitSpinner"; |
| import { isAnyPopupOpen } from "@/UI/MessageAlerts/Popups/OpenPopupList"; |
| import { getSetting } from "@/Plugins/Core/Settings/LoadSaveSettings"; |
| import { PopupVariant } from "@/UI/MessageAlerts/Popups/InterfacesAndEnums"; |
| |
|
|
| |
| |
|
|
| |
| const _allAcceptableFileTypes = Object.values(molFormatInformation).reduce( |
| (acc, val) => acc.concat(val.exts.map((x) => x.toUpperCase())), |
| [] as string[] |
| ); |
| _allAcceptableFileTypes.sort(); |
|
|
| |
| export const fileTypesAccepts = |
| _allAcceptableFileTypes.map((f) => `.${f.toLowerCase()}`).join(",") + |
| ",.zip"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| function _fixTitle(title: string, defaultTitle: string): string { |
| if ([undefined, ""].indexOf(title) !== -1) { |
| return defaultTitle; |
| } |
| title = title.replace("*****", defaultTitle); |
|
|
| |
| if (title.startsWith(":")) { |
| title = title.slice(1); |
| } |
|
|
| return title; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function parseAndLoadMoleculeFile( |
| params: ILoadMolParams |
| ): Promise<void | TreeNodeList> { |
| params = addDefaultLoadMolParams(params); |
| const spinnerId = messagesApi.startWaitSpinner(); |
|
|
| const formatInfo = params.fileInfo.getFormatInfo(); |
| if (formatInfo === undefined) { |
| const errorMessage = `Could not determine file format for "${params.fileInfo.name}".`; |
| messagesApi.popupError(errorMessage); |
| return Promise.reject(new Error(errorMessage)); |
| } |
|
|
| |
| if (formatInfo.neverDesalt === true) { |
| console.warn( |
| `File format ${formatInfo.description} does not support desalting.` |
| ); |
| params.desalt = false; |
| } |
|
|
| |
| if (formatInfo.textPreProcessor) { |
| params.fileInfo.contents = formatInfo.textPreProcessor( |
| params.fileInfo.contents |
| ); |
| } |
|
|
| |
| |
| let promise: Promise<TreeNodeList>; |
|
|
| let { loader } = formatInfo; |
|
|
| |
| |
| |
| if (formatInfo.primaryExt === "mol2" && !params.desalt) { |
| loader = MolLoader.Mol3D; |
| } |
|
|
| switch (loader) { |
| case MolLoader.Mol3D: { |
| promise = parseUsing3DMolJs(params.fileInfo, formatInfo); |
| break; |
| } |
| case MolLoader.OpenBabel: { |
| promise = parseUsingOpenBabel( |
| params.fileInfo, |
| formatInfo, |
| params.desalt, |
| params.gen3D, |
| params.surpressMsgs |
| ); |
| break; |
| } |
| case MolLoader.MolModaFormat: { |
| return parseUsingMolModa(params.fileInfo).then((payload: any) => { |
| messagesApi.stopWaitSpinner(spinnerId); |
| return payload; |
| }); |
| } |
| |
| |
| |
| } |
|
|
| |
| |
| return promise |
| .then(async (treeNodeList: TreeNodeList) => { |
| |
| |
| |
| |
|
|
| if (treeNodeList.length === 0) { |
| if (isAnyPopupOpen()) { |
| stopAllWaitSpinners(); |
| return treeNodeList; |
| } |
| let msg = |
| "<p>File contained no valid molecules. Are you certain it's correctly formatted?</p>"; |
|
|
| stopAllWaitSpinners(); |
|
|
| |
| if (params.fileInfo.contents.trim() !== "") { |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| msg += `<p>File contents:</p><code><textarea disabled class="form-control" rows="3">${params.fileInfo.contents}</textarea>`; |
| } |
| messagesApi.popupError(msg); |
| return treeNodeList; |
| } |
|
|
| |
| |
| const topLevelName = getFileNameParts( |
| params.fileInfo.name |
| ).basename; |
| const mergedTreeNodeList = treeNodeList.merge(topLevelName); |
|
|
| |
| |
| |
| |
| mergedTreeNodeList.flattened.forEach((t) => { |
| t.title = _fixTitle(t.title, params.defaultTitle as string); |
| }); |
|
|
| |
| const terminalNodes = mergedTreeNodeList.terminals; |
|
|
| |
| |
| for (let i = 0; i < terminalNodes.length; i++) { |
| const node = terminalNodes.get(i); |
| |
| if (node.title.indexOf("undefined") >= 0) { |
| const { basename } = getFileNameParts(params.fileInfo.name); |
| node.title = basename + ":" + (i + 1).toString(); |
| } |
| } |
|
|
| |
| if (params.hideOnLoad) { |
| mergedTreeNodeList.flattened.forEach((n) => { |
| n.visible = false; |
| }); |
| } |
|
|
| if (params.addToTree) { |
| |
| |
| mergedTreeNodeList.addToMainTree( |
| params.tag, |
| true, |
| true, |
| !params.hideOnLoad |
| ); |
|
|
| |
| if (!params.hideOnLoad) { |
| const initialCompoundsVisible = await getSetting( |
| "initialCompoundsVisible" |
| ); |
| if (terminalNodes.length > initialCompoundsVisible) { |
| messagesApi.popupMessage( |
| "Some Molecules not Visible", |
| `The ${params.fileInfo.name} file contained ${terminalNodes.length} molecules. Only ${initialCompoundsVisible} are initially shown for performance's sake. Use the Navigator to toggle the visibility of the remaining molecules.`, |
| PopupVariant.Info, |
| undefined, |
| false, |
| {} |
| ); |
| } |
| } |
| } |
|
|
| messagesApi.stopWaitSpinner(spinnerId); |
| return mergedTreeNodeList; |
| }) |
| .catch((err) => { |
| messagesApi.stopWaitSpinner(spinnerId); |
| throw err; |
| }); |
| } |
|
|