| import { IAtom, TreeNodeType } from "@/UI/Navigation/TreeView/TreeInterfaces"; |
| import * as api from "@/Api"; |
| import { getFormatInfoGivenType, IFormatInfo } from "../Types/MolFormats"; |
| import { getFileNameParts } from "@/FileSystem/FilenameManipulation"; |
| import { FileInfo } from "@/FileSystem/FileInfo"; |
| import { TreeNode } from "@/TreeNodes/TreeNode/TreeNode"; |
| import { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList"; |
| import { makeEasyParser } from "../ParseMolModels/EasyParser"; |
| import { slugify, capitalize } from "@/Core/Utils/StringUtils"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function separateCompoundNonCompoundTerminalNodes( |
| treeNodeList: TreeNodeList |
| ): { [key: string]: TreeNodeList } { |
| let terminalNodes = treeNodeList.filters.onlyTerminal; |
|
|
| |
| terminalNodes = terminalNodes.filters.onlyUnique; |
|
|
| const compoundNodes = terminalNodes.filters.keepType(TreeNodeType.Compound); |
| |
| const nonCompoundNodes = terminalNodes.filter( |
| (node: TreeNode) => node.type !== TreeNodeType.Compound |
| ); |
| return { compoundNodes, nonCompoundNodes }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function getConvertedTxtsWithNaming( |
| nodes: TreeNodeList, |
| targetExt: string, |
| merge: boolean, |
| type?: TreeNodeType |
| ): Promise<FileInfo[]> { |
| |
| const ext = getPrimaryExt(targetExt); |
| nodes = nodes.filters.keepRegions(false); |
|
|
| |
| if (nodes.length === 0) { |
| return Promise.resolve([]); |
| } |
|
|
|
|
| return nodes |
| .toFileInfos(ext, merge) |
| .then((molFileInfos: FileInfo[]) => { |
| return molFileInfos.map((molFileInfo, idx) => { |
| |
| const molEntry = nodes.get(idx); |
| molFileInfo.name = getFilename(molEntry, ext, type); |
| molFileInfo.treeNode = molEntry; |
| return molFileInfo; |
| }); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function getConvertedTxts( |
| nodes: TreeNodeList, |
| targetExt: string, |
| merge: boolean, |
| filename?: string |
| ): Promise<FileInfo[]> { |
| const ext = getPrimaryExt(targetExt); |
| nodes = nodes.filters.keepRegions(false); |
| if (nodes.length === 0) return Promise.resolve([]); |
| return nodes.toFileInfos(ext, merge).then((molFileInfos) => { |
| return molFileInfos.map((molFileInfo, idx) => { |
| molFileInfo.name = filename ? `${filename}.${ext}` : getFilename(nodes.get(idx), ext); |
| molFileInfo.treeNode = nodes.get(idx); |
| return molFileInfo; |
| }); |
| }); |
| } |
|
|
| export function getPrimaryExt(format: string): string { |
| const formatInfo = getFormatInfoGivenType(format) as IFormatInfo; |
| return formatInfo ? formatInfo.primaryExt : format; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function getFilename(treeNode: TreeNode, ext: string, type?: TreeNodeType): string { |
| let txtPrts = [getFileNameParts(treeNode.src as string).basename]; |
|
|
| |
| if (treeNode.type === TreeNodeType.Compound) { |
| const easyMol = makeEasyParser(treeNode.model); |
| const firstAtom: IAtom = easyMol.getAtom(0); |
| const resn = firstAtom.resn ? firstAtom.resn.trim() : ""; |
| const resi = firstAtom.resi ? firstAtom.resi.toString().trim() : ""; |
| if (resn) txtPrts.push(resn); |
| if (resi) txtPrts.push(resi); |
| } |
|
|
| const easyMol = makeEasyParser(treeNode.model); |
| const firstAtom: IAtom = easyMol.getAtom(0); |
| const chain = firstAtom.chain ? firstAtom.chain.trim() : ""; |
| if (chain && chain !== " ") txtPrts.push(chain); |
|
|
| |
| if (type && type !== TreeNodeType.Other && type !== TreeNodeType.Compound) { |
| txtPrts.push(capitalize(type)); |
| } else if (type === TreeNodeType.Other) { |
| |
| |
| } |
|
|
| |
| txtPrts = txtPrts.filter((x) => x); |
|
|
| return slugify(txtPrts.join("-"), false) + "." + ext; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function saveTxtFiles( |
| files: FileInfo[], |
| compressedName: string |
| ): Promise<any> { |
| if (files.length === 1) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
|
|
| const userExt = getFileNameParts(compressedName).ext; |
| const fileExt = getFileNameParts(files[0].name).ext; |
|
|
| if (userExt && userExt.toLowerCase() === fileExt.toLowerCase()) { |
| files[0].name = compressedName; |
| } else if (!userExt) { |
| |
| files[0].name = compressedName + "." + fileExt; |
| } |
|
|
| return api.fs.saveTxt(files[0]); |
| } |
| if (!compressedName.toLowerCase().endsWith(".zip")) { |
| compressedName += ".zip"; |
| } |
|
|
| return api.fs.saveZipWithTxtFiles(compressedName, files); |
| } |
|
|