UNPKG

orthoxml-parser

Version:

library to parse ortoxml and extract hog tree as json

188 lines (139 loc) 6.22 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Example to parse an orthoxml</title> <script src="../dist/orthoxml-parser.js"></script> <script src="https://d3js.org/d3.v7.min.js"></script> </head> <body> <div id="test"> </div> <script> var example_file = './data/HOG_C0348246.orthoxml'; fetch(example_file) .then(response => response.text()) .then((d) => { console.log(example_file) var data = OrthoxmlParser.parse(d); console.log(data); var width = 700 margin = ({top: 10, right: 120, bottom: 10, left: 40}) dy = width / 6 dx = 15 tree = d3.tree().nodeSize([dx, dy]) //diagonal = d3.linkHorizontal().x(d => d.y).y(d => d.x) function diagonal(d) { return "M" + d.source.y + "," + d.source.x + "V" + d.target.x + "H" + d.target.y; } const root = d3.hierarchy(data); root.x0 = dy / 2; root.y0 = 0; root.descendants().forEach((d, i) => { d.id = i; d._children = d.children; }); const svg = d3.select("body").append("svg") .attr("viewBox", [-margin.left, -margin.top, width, dx]) .style("font", "10px sans-serif") .style("user-select", "none"); const gLink = svg.append("g") .attr("fill", "none") .attr("stroke", "#555") .attr("stroke-opacity", 0.4) .attr("stroke-width", 1.5); const gNode = svg.append("g") .attr("cursor", "pointer") .attr("pointer-events", "all"); function update(event, source) { const duration = event?.altKey ? 2500 : 250; const nodes = root.descendants().reverse(); const links = root.links(); // Compute the new tree layout. tree(root); let left = root; let right = root; root.eachBefore(node => { if (node.x < left.x) left = node; if (node.x > right.x) right = node; }); const height = right.x - left.x + margin.top + margin.bottom; const transition = svg.transition() .duration(duration) .attr("viewBox", [-margin.left, left.x - margin.top, width, height]) .tween("resize", window.ResizeObserver ? null : () => () => svg.dispatch("toggle")); // Update the nodes… const node = gNode.selectAll("g") .data(nodes, d => d.id); // Enter any new nodes at the parent's previous position. const nodeEnter = node.enter().append("g") .attr("transform", d => `translate(${source.y0},${source.x0})`) .attr("fill-opacity", 0) .attr("stroke-opacity", 0) .on("click", (event, d) => { d.children = d.children ? null : d._children; update(event, d); }); nodeEnter.append("circle") .attr("r", 2.5) .attr("fill", d => d.data.paralog ? 'red' : d._children ? "#555" : "#999") .attr("stroke-width", 10); nodeEnter.append("text") .attr("dy", "0.31em") .attr("x", d => d._children ? -6 : 6) .attr("text-anchor", d => d._children ? "end" : "start") .text(d => String(d.data.id) +'_' + d.data.species) .clone(true).lower() .attr("stroke-linejoin", "round") .attr("stroke-width", 3) .attr("stroke", "white"); // Transition nodes to their new position. const nodeUpdate = node.merge(nodeEnter).transition(transition) .attr("transform", d => `translate(${d.y},${d.x})`) .attr("fill-opacity", 1) .attr("stroke-opacity", 1); // Transition exiting nodes to the parent's new position. const nodeExit = node.exit().transition(transition).remove() .attr("transform", d => `translate(${source.y},${source.x})`) .attr("fill-opacity", 0) .attr("stroke-opacity", 0); // Update the links… const link = gLink.selectAll("path") .data(links, d => d.target.id); // Enter any new links at the parent's previous position. const linkEnter = link.enter().append("path") .attr("d", d => { const o = {x: source.x0, y: source.y0}; return diagonal({source: o, target: o}); }); // Transition links to their new position. link.merge(linkEnter).transition(transition) .attr("d", diagonal); // Transition exiting nodes to the parent's new position. link.exit().transition(transition).remove() .attr("d", d => { const o = {x: source.x, y: source.y}; return diagonal({source: o, target: o}); }); // Stash the old positions for transition. root.eachBefore(d => { d.x0 = d.x; d.y0 = d.y; }); } update(null, root); svg.node(); }) /* var example_file = './data/HOG_C0348246_augmented.orthoxml'; fetch(example_file) .then(response => response.text()) .then((data) => { console.log(example_file) var hog_tree = OrthoxmlParser.parse(data, true); console.log(hog_tree); }) */ </script> </body> </html>