File size: 5,519 Bytes
71174bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import { getMoleculesFromStore } from "@/Store/StoreExternalAccess";
import type { TreeNodeList } from "../TreeNodeList/TreeNodeList";
import type { TreeNode } from "./TreeNode";
/**
* TreeNodeDescriptions class
*/
export class TreeNodeDescriptions {
private parentTreeNode: TreeNode;
/**
* Constructor.
*
* @param {TreeNode} parentTreeNode The parent TreeNode.
*/
constructor(parentTreeNode: TreeNode) {
this.parentTreeNode = parentTreeNode;
}
/**
* Gets a description of a molecule. Useful when you want to refer to a
* molecule in text (not the heirarchical tree). If slugified, could be used
* as a filename. TODO: Not currently used. Use pathName instead.
*
* @param {TreeNode} mol The molecule to describe.
* @param {TreeNodeList} treeNodeList The list of all molecules.
* @param {boolean} [noCategory=false] Whether to include the
* category component of the
* description ("protein",
* "compound", "metal", etc.).
* @returns {string} The description.
*/
// public getMolDescription(
// mol: TreeNode,
// treeNodeList: TreeNodeList,
// noCategory = false
// ): string {
// // If it has no parent, just return it's title.
// let curMol: TreeNode | null = mol;
// const titles = [getFileNameParts(curMol.title as string).basename];
// while (curMol.parentId) {
// curMol = treeNodeList.filters.onlyId(curMol.parentId);
// if (curMol) {
// // Add to top of list
// titles.unshift(
// getFileNameParts(curMol.title as string).basename
// );
// continue;
// }
// break;
// }
// if (noCategory && (titles.length > 2 || titles[1] === "Protein")) {
// // Remove one in position 1 ("protein", "compound", "metal", etc.)
// titles.splice(1, 1);
// }
// return titles.join(":").split("(")[0].trim();
// }
/**
* Gets the name of the molecule in path-like format.
*
* @param {string} [separator=">"] The separator to use.
* @param {number} [maxLength=20] Abbreviate to no longer than
* this length. If 0 or less, don't
* abbreviate.
* @param {TreeNodeList} [molsToConsider] The molecules to consider when
* determining the path. If not
* specified, uses all molecules
* (default).
* @returns {string} The name of the molecule in path-like format.
*/
public pathName(
separator = ">",
maxLength = 20,
molsToConsider?: TreeNodeList
): string {
// If molecules not provided, get them from the store (all molecules).
if (molsToConsider === undefined) {
molsToConsider = getMoleculesFromStore();
}
const placeholder = "...";
const ancestors = this.parentTreeNode.getAncestry(molsToConsider);
let titles = ancestors.map((x) => x.title);
// Simplify words some
let newTitle = "";
if (maxLength > 0) {
titles = titles.map((x) => {
if (x === undefined) {
return "";
}
return x
.replace("Protein", "Prot")
.replace("Compound", "Cmpd")
.replace("Solvent", "Solv")
.replace("protein", "prot")
.replace("compound", "cmpd")
.replace("protonated", "prot")
.replace("docking", "dock")
.replace("solvent", "solv");
});
newTitle = titles.join(separator);
while (titles.length > 3) {
if (newTitle.length < maxLength) {
break;
}
// remove any existing elements of value ""
titles = titles.filter((x) => x !== placeholder);
// Set middle element to ""
let middleIdx = Math.floor(titles.length / 2);
if (middleIdx === titles.length - 1) {
middleIdx--;
}
if (middleIdx === 0) {
middleIdx++;
}
titles[middleIdx] = placeholder;
newTitle = titles.join(separator);
}
if (newTitle.length > maxLength) {
// remove all lowercase vowels after first 5 characters
const firstPart = newTitle.slice(0, 5);
const secondPart = newTitle.slice(5);
newTitle = firstPart + secondPart.replace(/[aeiou]/g, "");
}
// If it's still too long, just give the molecule title.
if (newTitle.length > maxLength && this.parentTreeNode.title) {
newTitle = this.parentTreeNode.title;
}
} else {
// Not abbreviating
newTitle = titles.join(separator);
}
return newTitle;
}
}
|