UNPKG

phylotree

Version:

A JavaScript library for developing applications and interactive visualizations involving [phylogenetic trees](https://en.wikipedia.org/wiki/Phylogenetic_tree), written as an extension of the [D3](http://d3js.org) [hierarchy layout](https://github.com/d3/

47 lines (40 loc) 1.39 kB
import * as _ from "underscore"; import { XMLParser } from "fast-xml-parser"; var nexml_parser = function(xml_string, options) { const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "", parseAttributeValue: true }); const xml = parser.parse(xml_string); var trees = xml["nex:nexml"].trees.tree.map(function(nexml_tree) { var node_list = nexml_tree.node.map(d => d), node_hash = node_list.reduce(function(a, b) { b.edges = []; b.name = b.id; a[b.id] = b; return a; }, {}), roots = node_list.filter(d => d.root), root_id = roots > 0 ? roots[0].id : node_list[0].id; node_hash[root_id].name = "root"; nexml_tree.edge.map(d => d).forEach(function(edge) { node_hash[edge.source].edges.push(edge); }); function parseNexml(node, index) { if (node.edges) { var targets = _.pluck(node.edges, "target"); node.children = _.values(_.pick(node_hash, targets)); node.children.forEach(function(child, i) { child.attribute = node.edges[i].length || ""; }); node.children.forEach(parseNexml); node.annotation = ""; } } parseNexml(node_hash[root_id]); return node_hash[root_id]; }); return trees; }; export default nexml_parser;