phylogician-ts
Version:
Module to read, manipulate and write phylogenetic trees. Written in TypeScript
432 lines • 9.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class TreeNode {
/**
* Creates an instance of TreeNode.
* @param {string} name
* @param {(number|null)} branchLength
* @memberof TreeNode
*/
constructor(name, branchLength, id) {
this.name = name;
this.branchLength = branchLength;
this.children = [];
this.id = id;
this.parent = null;
this.reverseLadderized = false;
this.root = false;
this.viz = {
branch: {
color: 'black',
opacity: 1,
path: '',
width: 1,
},
label: {
color: 'black',
dim: {
h: 0,
w: 0,
},
font: {
family: '',
size: '16px',
style: 'normal',
},
show: true,
},
node: {
color: 'black',
shape: 'circle',
size: 3,
},
pos: {
a: 0,
r: 0,
x: 0,
y: 0,
},
};
this.displayNameChunks = [
Object.assign(Object.assign({}, this.viz.label), { value: this.name }),
];
this.displayNameUserChunks = [
{
value: this.name,
},
];
}
/**
* Sets parent to the Tree node.
*
* @param {Tree} parent
* @returns {TreeNode}
* @memberof Tree
*/
setParentNodeId(parent) {
this.parent = parent;
return this;
}
/**
* Returns parent of the node
*
* @returns {(number | null)}
* @memberof TreeNode
*/
getParentNodeId() {
return this.parent;
}
/**
* Returns true if the node is leaf
*
* @returns {boolean}
* @memberof TreeNode
*/
isLeaf() {
return this.children.length === 0 ? true : false;
}
/**
* Sets node to be root
*
* @memberof TreeNode
*/
setAsRoot() {
this.root = true;
return this;
}
/**
* Sets node to not be root
*
* @memberof TreeNode
*/
unsetAsRoot() {
this.root = false;
return this;
}
/**
*
*
* @returns {boolean}
* @memberof TreeNode
*/
isRoot() {
return this.root;
}
/**
* Returns the size of the node
*
* @returns {number}
* @memberof TreeNode
*/
getNodeSize() {
return this.viz.node.size;
}
/**
* Changes the node size
*
* @param {number} size
* @memberof TreeNode
*/
setNodeSize(size) {
this.viz.node.size = size;
return this;
}
/**
* Returns the color of the node
*
* @returns {string} node color
* @memberof TreeNode
*/
getNodeColor() {
return this.viz.node.color;
}
/**
* Changes the node color.
*
* @param {string} color
* @memberof TreeNode
*/
setNodeColor(color) {
this.viz.node.color = color;
return this;
}
/**
* Returns the shape of the node
*
* @returns {string} node shape
* @memberof TreeNode
*/
getNodeShape() {
return this.viz.node.shape;
}
/**
* Changes the node shape.
*
* @param {string} shape
* @memberof TreeNode
*/
setNodeShape(shape) {
this.viz.node.shape = shape;
return this;
}
/**
* Returns the position X of the node
*
* @returns {number} position X
* @memberof TreeNode
*/
getPosX() {
return this.viz.pos.x;
}
/**
* Changes the position X of the node.
*
* @param {number} x
* @memberof TreeNode
*/
setPosX(x) {
this.viz.pos.x = x;
return this;
}
/**
* Returns the position Y of the node
*
* @returns {number} position Y
* @memberof TreeNode
*/
getPosY() {
return this.viz.pos.y;
}
/**
* Changes the position Y of the node.
*
* @param {number} y
* @memberof TreeNode
*/
setPosY(y) {
this.viz.pos.y = y;
return this;
}
/**
* Returns the position "r" of the node (for Circular layout)
*
* @returns {number} position R
* @memberof TreeNode
*/
getPosR() {
return this.viz.pos.r;
}
/**
* Changes the position "r" of the node (for Circular layout).
*
* @param {number} r
* @memberof TreeNode
*/
setPosR(r) {
this.viz.pos.r = r;
return this;
}
/**
* Returns the position "a" of the node (for Circular layout)
*
* @returns {number} position Y
* @memberof TreeNode
*/
getPosA() {
return this.viz.pos.a;
}
/**
* Changes the position "a" of the node (for Circular layout).
*
* @param {number} a
* @memberof TreeNode
*/
setPosA(a) {
this.viz.pos.a = a;
return this;
}
/**
* Sets the branch path of the node.
*
* @param {string} path
* @memberof TreeNode
*/
setBranchPath(path) {
this.viz.branch.path = path || '';
return this;
}
/**
* Gets the branch path of the node.
*
* @returns {string}
* @memberof TreeNode
*/
getBranchPath() {
return this.viz.branch.path;
}
/**
* Sets the branch path of the node.
*
* @param {string} path
* @memberof TreeNode
*/
setBranchColor(color) {
this.viz.branch.color = color;
return this;
}
/**
* Gets the branch path of the node.
*
* @returns {string}
* @memberof TreeNode
*/
getBranchColor() {
return this.viz.branch.color;
}
/**
* Returns the width of the label of the node
*
* @returns {number}
* @memberof TreeNode
*/
getLabelWidth() {
return this.viz.label.dim.w;
}
/**
* Sets the width of the label of the node
*
* @param {number} width
* @memberof TreeNode
*/
setLabelWidth(width) {
this.viz.label.dim.w = width;
return this;
}
/**
* Returns the height of the label of the node
*
* @returns {number}
* @memberof TreeNode
*/
getLabelHeight() {
return this.viz.label.dim.h;
}
/**
* Sets the height of the label of the node
*
* @param {number} height
* @memberof TreeNode
*/
setLabelHeight(height) {
this.viz.label.dim.h = height;
return this;
}
/**
* Returns the color of the label of the node
*
* @returns {string}
* @memberof TreeNode
*/
getDefaultLabelColor() {
return this.viz.label.color;
}
/**
* Sets the color of the label of the node
*
* @param {string} height
* @memberof TreeNode
*/
setDefaultLabelColor(height) {
this.viz.label.color = height;
this.setDisplayNameChunks(this.displayNameUserChunks);
return this;
}
/**
* Sets the font size of the label of the node.
*
* @param {string} fontSize
* @memberof TreeNode
*/
setDefaultFontSize(fontSize) {
this.viz.label.font.size = fontSize;
return this;
}
/**
* Gets the font size of the label of the node.
*
* @returns {string}
* @memberof TreeNode
*/
getDefaultFontSize() {
return this.viz.label.font.size;
}
/**
* Sets if label should be displayed or hidden
*
* @param {boolean} showLabel
* @memberof TreeNode
*/
setShowLabel(showLabel) {
this.viz.label.show = showLabel;
return this;
}
/**
* Gets if display is hidden or not.
*
* @returns {string}
* @memberof TreeNode
*/
getShowLabel() {
return this.viz.label.show;
}
/**
* Sets name to be displayed in tree in chuncks with different styles
*
* @param {string} displayName
* @returns {this}
* @memberof TreeNode
*/
setDisplayNameChunks(displayNameList) {
this.displayNameUserChunks = displayNameList;
this.displayNameChunks = displayNameList.map(displayName => {
return Object.assign(Object.assign({}, this.viz.label), displayName);
});
return this;
}
/**
* Gets the display name chunks.
*
* @returns {string}
* @memberof TreeNode
*/
getDisplayNameChunks() {
return this.displayNameChunks;
}
/**
* Sets name to be displayed in tree (default same as .name with default parameters)
*
* @param {string} displayName
* @returns {this}
* @memberof TreeNode
*/
setDisplayName(displayName) {
const simpleNameChunk = [
{
value: displayName,
},
];
this.setDisplayNameChunks(simpleNameChunk);
return this;
}
/**
* Gets the name to be displayed in tree.
*
* @returns {string}
* @memberof TreeNode
*/
getDisplayName() {
return this.displayNameChunks.map(d => d.value).join(' ');
}
}
exports.TreeNode = TreeNode;
//# sourceMappingURL=TreeNode.js.map