diff options
| author | Terry Truong <terry06890@gmail.com> | 2022-03-28 12:10:27 +1100 |
|---|---|---|
| committer | Terry Truong <terry06890@gmail.com> | 2022-03-28 12:10:27 +1100 |
| commit | e39f5ada10723dc1f5c29f32543051f90df03041 (patch) | |
| tree | 04b7a75789a92da305c3d4493d2e1b4d41a260dd /src/tol.ts | |
| parent | 34e34b9620d88b5d76b87a4bbcd473c2fc6002a9 (diff) | |
Move TolNode code to tol.ts
Diffstat (limited to 'src/tol.ts')
| -rw-r--r-- | src/tol.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/tol.ts b/src/tol.ts new file mode 100644 index 0000000..0f05ba9 --- /dev/null +++ b/src/tol.ts @@ -0,0 +1,49 @@ +/* + * Contains classes used for representing tree-of-life data. + */ + +// Represents a tree-of-life node/tree +export class TolNode { + name: string; + children: TolNode[]; + parent: TolNode | null; + constructor(name: string, children: TolNode[] = [], parent = null){ + this.name = name; + this.children = children; + this.parent = parent; + } +} +// Represents a tree-of-life node obtained from tol.json +export class TolNodeRaw { + name: string; + children?: TolNodeRaw[]; + constructor(name: string, children: TolNodeRaw[] = []){ + this.name = name; + this.children = children; + } +} +// Converts a TolNodeRaw tree to a TolNode tree +export function tolFromRaw(node: TolNodeRaw): TolNode { + function helper(node: TolNodeRaw, parent: TolNode | null){ + let tolNode = new TolNode(node.name); + if (node.children == null){ + tolNode.children = []; + } else { + tolNode.children = Array(node.children.length); + node.children.forEach((child, idx) => {tolNode.children[idx] = helper(child, tolNode)}); + } + tolNode.parent = parent; + return tolNode; + } + return helper(node, null); +} +// Returns a mapping from TolNode names to TolNodes in a given tree +export function getTolMap(tolTree: TolNode): Map<string, TolNode> { + function helper(node: TolNode, map: Map<string, TolNode>){ + map.set(node.name, node); + node.children.forEach(child => helper(child, map)); + } + let map = new Map(); + helper(tolTree, map); + return map; +} |
