UNPKG

react-phylogeny-tree-gl

Version:

React integration of phylocanvas.gl. Component and hook for phylogenetic tree visualistion.

42 lines 1.36 kB
import { useCallback, useRef } from 'react'; export function setRootNLevelsUp(tree, nodeID, noLevels = 6, minLeafToRootLength = 5) { let node; let upLength = 0; if (nodeID !== null) { node = tree.findNodeById(nodeID); if (node !== undefined) { upLength += node.branchLength; const noLevelsUp = noLevels >= 1 ? noLevels : 1; for (let i = 0; i < noLevelsUp; i++) { if ('parent' in node) { upLength += node.parent.branchLength; node = node.parent; } else break; } while (upLength < minLeafToRootLength && 'parent' in node) { upLength += node.parent.branchLength; node = node.parent; } } } if (node) tree.setRoot(node.id); } export function useGetLatest(obj) { const ref = useRef(); ref.current = obj; return useCallback(() => ref.current, []); } export const EmptyArray = []; export function getNodeLeafOffspringsIDs(node) { if (node.isLeaf) return [node.id]; const leafIDs = node.children.reduce((acc, child) => { const ids = getNodeLeafOffspringsIDs(child); return [...acc, ...ids]; }, []); return leafIDs; } //# sourceMappingURL=utils.js.map