| import { IMolsToConsider } from "@/FileSystem/LoadSaveMolModels/SaveMolModels/Types"; |
| import { getMoleculesFromStore } from "@/Store/StoreExternalAccess"; |
| import { TreeNode } from "../../../TreeNodes/TreeNode/TreeNode"; |
| import { SelectedType, TreeNodeType } from "./TreeInterfaces"; |
| import { TreeNodeList } from "../../../TreeNodes/TreeNodeList/TreeNodeList"; |
| import { treeNodeDeepClone } from "../../../TreeNodes/Deserializers"; |
| import { makeEasyParser } from "@/FileSystem/LoadSaveMolModels/ParseMolModels/EasyParser"; |
| import { EasyParserParent } from "@/FileSystem/LoadSaveMolModels/ParseMolModels/EasyParser/EasyParserParent"; |
| import * as api from "@/Api"; |
| import { YesNo } from "@/UI/MessageAlerts/Popups/InterfacesAndEnums"; |
| import { getSetting } from "@/Plugins/Core/Settings/LoadSaveSettings"; |
|
|
| |
| |
| |
| |
| |
| |
| export function selectNodesBasedOnCondition( |
| treeNodeList: TreeNodeList, |
| conditionFunc: (node: TreeNode) => boolean |
| ) { |
| const allNodes = treeNodeList.flattened; |
| allNodes.forEach((n) => { |
| if (conditionFunc(n)) { |
| n.selected = SelectedType.True; |
| } else { |
| n.selected = SelectedType.False; |
| } |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function getTerminalNodesToConsider( |
| molsToConsider: IMolsToConsider, |
| terminalNodes?: TreeNodeList |
| ): TreeNodeList { |
| if (terminalNodes === undefined) { |
| terminalNodes = getMoleculesFromStore(); |
| } |
|
|
| |
| terminalNodes = terminalNodes.filters.onlyTerminal; |
|
|
| let molsToKeep = new TreeNodeList(); |
|
|
| if (molsToConsider.visible) { |
| molsToKeep.extend(terminalNodes.filters.keepVisible()); |
| } |
|
|
| if (molsToConsider.selected) { |
| molsToKeep.extend(terminalNodes.filters.keepSelected()); |
| } |
|
|
| if (molsToConsider.hiddenAndUnselected) { |
| molsToKeep.extend( |
| |
| terminalNodes.filter( |
| (m: TreeNode) => !m.visible && m.selected === SelectedType.False |
| ) |
| ); |
| } |
|
|
| |
| molsToKeep = molsToKeep.filters.onlyUnique; |
|
|
| return molsToKeep; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function cloneMolsWithAncestry( |
| treeNodeList: TreeNodeList, |
| includeAncestors = true |
| ): Promise<TreeNodeList> { |
| const allMols = getMoleculesFromStore(); |
| const promises: Promise<TreeNode>[] = []; |
|
|
| treeNodeList.forEach((nodeToActOn: TreeNode) => { |
| |
| let nodeGenealogy: TreeNodeList; |
| if (includeAncestors) { |
| |
| nodeGenealogy = nodeToActOn.getAncestry(allMols); |
|
|
| |
| for (let i = 0; i < nodeGenealogy.length; i++) { |
| |
| const clonedNode = nodeGenealogy.get(i).shallowCopy(); |
| nodeGenealogy.set(i, clonedNode); |
| } |
|
|
| |
| |
| |
| for (let i = 0; i < nodeGenealogy.length - 1; i++) { |
| |
| |
| nodeGenealogy.get(i).clearChildren(); |
| } |
|
|
| |
| for (let i = 0; i < nodeGenealogy.length - 1; i++) { |
| nodeGenealogy.get(i).nodes?.push(nodeGenealogy.get(i + 1)); |
|
|
| |
| nodeGenealogy.get(i + 1).parentId = nodeGenealogy.get(i).id; |
| } |
| } else { |
| |
| nodeGenealogy = new TreeNodeList([nodeToActOn.shallowCopy()]); |
| } |
|
|
| |
| |
| const clonedTree = treeNodeDeepClone(nodeGenealogy.get(0), true) |
| .then((topNode: TreeNode): TreeNode => { |
| |
| |
| const allNodesFlattened = new TreeNodeList([topNode]); |
| const children = topNode.nodes; |
| if (children) { |
| allNodesFlattened.extend(children.flattened); |
| } |
|
|
| |
| allNodesFlattened.forEach((node: TreeNode) => { |
| node.selected = SelectedType.False; |
| node.viewerDirty = true; |
| node.focused = false; |
| }); |
|
|
| return topNode; |
| }) |
| .catch((err: Error) => { |
| throw err; |
| }); |
|
|
| promises.push(clonedTree); |
| }); |
|
|
| return Promise.all(promises).then((treeNodes: TreeNode[]) => { |
| return new TreeNodeList(treeNodes); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function mergeTreeNodes( |
| nodeList: TreeNodeList, |
| newName = "mergedMol" |
| ): Promise<TreeNode> { |
| |
| return Promise.all( |
| nodeList.map((node: TreeNode) => { |
| return treeNodeDeepClone(node, true); |
| }) |
| ) |
| .then((nodeList: TreeNode[]) => { |
| const merged = nodeList[0]; |
| for (let i = 1; i < nodeList.length; i++) { |
| merged.mergeInto(nodeList[i]); |
| } |
| merged.title = newName; |
| return merged; |
| }) |
| .catch((err: Error) => { |
| throw err; |
| }); |
|
|
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
|
|
| |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function getUniqueResiduesFromVisibleMolecules(): { |
| names: string[]; |
| ids: number[]; |
| } { |
| const allMolecules: TreeNodeList = getMoleculesFromStore(); |
| const visibleTerminalNodes: TreeNodeList = |
| allMolecules.filters.onlyTerminal.filters.keepVisible(); |
| const allResidueNames = new Set<string>(); |
| const allResidueIds = new Set<number>(); |
| visibleTerminalNodes.forEach((node: TreeNode) => { |
| if (node.model) { |
| const parser: EasyParserParent = makeEasyParser(node.model); |
| const { names: nodeResNames, ids: nodeResIds } = |
| parser.getUniqueResidues(); |
| nodeResNames.forEach((name) => allResidueNames.add(name)); |
| nodeResIds.forEach((id) => allResidueIds.add(id)); |
| } |
| }); |
| return { |
| names: Array.from(allResidueNames).sort(), |
| ids: Array.from(allResidueIds).sort((a, b) => a - b), |
| }; |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function organizeNodesIntoHierarchy( |
| treeNodes: TreeNode[], |
| rootTitle: string, |
| divideCompoundsByChain = true |
| ): TreeNode { |
| |
| const categories: { [key: string]: any } = {}; |
| for (const treeNode of treeNodes) { |
| if (treeNode.type) { |
| let categoryName = |
| treeNode.type.charAt(0).toUpperCase() + treeNode.type.slice(1); |
| if ( |
| [ |
| TreeNodeType.Compound, |
| TreeNodeType.Metal, |
| TreeNodeType.Lipid, |
| TreeNodeType.Ions, |
| ].includes(treeNode.type) |
| ) { |
| categoryName += "s"; |
| } |
| if (!categories[categoryName]) { |
| categories[categoryName] = []; |
| } |
| categories[categoryName].push(treeNode); |
| } |
| } |
| |
| if (categories["Compounds"]) { |
| if (divideCompoundsByChain) { |
| const compounds = categories["Compounds"]; |
| const newCompounds: { [key: string]: TreeNode[] } = {}; |
| |
| const availableChainsOrig = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""); |
| for (const treeNode of compounds) { |
| const chain = getChain(treeNode, availableChainsOrig); |
| if (!newCompounds[chain]) { |
| newCompounds[chain] = []; |
| } |
| newCompounds[chain].push(treeNode); |
| } |
| categories["Compounds"] = newCompounds; |
| } else { |
| categories["Compounds"] = { |
| A: categories["Compounds"], |
| }; |
| } |
| } |
| |
| const rootNode = new TreeNode({ |
| title: rootTitle, |
| treeExpanded: false, |
| visible: true, |
| selected: SelectedType.False, |
| focused: false, |
| viewerDirty: true, |
| nodes: new TreeNodeList([]), |
| }); |
| const categoryOrder = [ |
| "Protein", |
| "Nucleic", |
| "Compounds", |
| "Metals", |
| "Lipids", |
| "Ions", |
| "Solvent", |
| ]; |
| const titleToTypeMap: Map<string, TreeNodeType> = new Map([ |
| ["Protein", TreeNodeType.Protein], |
| ["Nucleic", TreeNodeType.Nucleic], |
| ["Compounds", TreeNodeType.Compound], |
| ["Metals", TreeNodeType.Metal], |
| ["Lipids", TreeNodeType.Lipid], |
| ["Ions", TreeNodeType.Ions], |
| ["Solvent", TreeNodeType.Solvent], |
| ]); |
| for (const title of categoryOrder) { |
| const type = titleToTypeMap.get(title); |
| if (!type || !categories[title] || categories[title].length === 0) { |
| continue; |
| } |
| const categoryNode = new TreeNode({ |
| title: title, |
| treeExpanded: false, |
| visible: true, |
| selected: SelectedType.False, |
| focused: false, |
| viewerDirty: true, |
| type: type, |
| nodes: new TreeNodeList([]), |
| }); |
| rootNode.nodes?.push(categoryNode); |
| if (title === "Compounds") { |
| for (const chain of Object.keys(categories[title])) { |
| const chainNode = new TreeNode({ |
| title: chain as string, |
| treeExpanded: false, |
| visible: true, |
| selected: SelectedType.False, |
| focused: false, |
| viewerDirty: true, |
| type: type, |
| nodes: new TreeNodeList(categories[title][chain]), |
| }); |
| categoryNode.nodes?.push(chainNode); |
| } |
| } else { |
| |
| const availableChainsOrig = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""); |
| for (const treeNode of categories[title]) { |
| const chain = getChain(treeNode, availableChainsOrig); |
| treeNode.title = chain as string; |
| categoryNode.nodes?.push(treeNode); |
| } |
| } |
| } |
| return rootNode; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function getChain(treeNode: TreeNode, availableChains: string[]): string { |
| let chain: string | undefined = undefined; |
| if (!treeNode.model) { |
| |
| chain = availableChains.shift(); |
| } else { |
| const firstAtom = makeEasyParser(treeNode.model).getAtom(0); |
| if (!firstAtom) { |
| |
| chain = availableChains.shift(); |
| } else { |
| const firstAtomChain = firstAtom.chain; |
| if (firstAtomChain === "" || firstAtomChain === undefined) { |
| |
| chain = availableChains.shift(); |
| } else { |
| |
| chain = firstAtomChain; |
| } |
| } |
| } |
| return chain || "A"; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function loadHierarchicallyFromTreeNodes( |
| treeNodes: TreeNode[], |
| rootNodeTitle: string, |
| divideCompoundsByChain = true |
| ): TreeNode { |
| |
| const allTreeNodes: TreeNode[] = []; |
| for (const treeNode of treeNodes) { |
| if (treeNode.nodes) { |
| const terminalNodes = treeNode.nodes.terminals; |
| const nodes = terminalNodes._nodes; |
| if (nodes.length === 1) { |
| nodes[0].title = treeNode.title; |
| } else { |
| for (let i = 0; i < nodes.length; i++) { |
| nodes[i].title = `${treeNode.title}:${i + 1}`; |
| } |
| } |
| allTreeNodes.push(...nodes); |
| } else { |
| allTreeNodes.push(treeNode); |
| } |
| } |
| return organizeNodesIntoHierarchy( |
| allTreeNodes, |
| rootNodeTitle, |
| divideCompoundsByChain |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function toggleVisibilityWithConfirmation( |
| nodesToToggle: TreeNodeList |
| ): Promise<void> { |
| if (nodesToToggle.length === 0) { |
| return; |
| } |
|
|
| |
| const anyVisible = nodesToToggle.some((n) => n.visible); |
| const newVisibleState = !anyVisible; |
|
|
| const performToggle = () => { |
| nodesToToggle.forEach((nodeToToggle: TreeNode) => { |
| nodeToToggle.visible = newVisibleState; |
| nodeToToggle.viewerDirty = true; |
| }); |
| }; |
|
|
| if (newVisibleState) { |
| |
| const allAffectedTerminals = new TreeNodeList(); |
| nodesToToggle.forEach((node) => { |
| if (node.model) { |
| |
| allAffectedTerminals.push(node); |
| } else if (node.nodes) { |
| |
| allAffectedTerminals.extend(node.nodes.terminals); |
| } |
| }); |
|
|
| const uniqueTerminals = allAffectedTerminals.filters.onlyUnique; |
| const countToMakeVisible = uniqueTerminals.filter( |
| (n) => !n.visible |
| ).length; |
| const visibilityThreshold = await getSetting("initialCompoundsVisible"); |
| if (countToMakeVisible > visibilityThreshold) { |
| const resp = await api.messages.popupYesNo( |
| `You are about to make ${countToMakeVisible} molecules visible at once. This may impact performance. Do you want to continue?`, |
| "Performance Warning", |
| "Yes, Continue", |
| "Cancel" |
| ); |
| if (resp === YesNo.Yes) { |
| performToggle(); |
| } |
| |
| } else { |
| performToggle(); |
| } |
| } else { |
| |
| performToggle(); |
| } |
| } |
|
|