diff --git a/package.json b/package.json index 7a6364f..85d2e54 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "commander": "^6.2.1", "csv-stringify": "^5.3.6", "d3": "6", + "fast-xml-parser": "^4.3.6", "jquery": "3", "lodash": "^4.17.11", "moment": "^2.29.4", diff --git a/src/formats/phyloxml.js b/src/formats/phyloxml.js index 8849614..df7a733 100644 --- a/src/formats/phyloxml.js +++ b/src/formats/phyloxml.js @@ -1,3 +1,5 @@ +import { XMLParser } from 'fast-xml-parser'; + // Changes XML to JSON // Modified version from here: http://davidwalsh.name/convert-xml-json function xmlToJson(xml) { @@ -46,15 +48,18 @@ var phyloxml_parser = function(xml, options) { function parsePhyloxml(node, index) { if (node.clade) { + if (!Array.isArray(node.clade)) { + node.clade = [node.clade]; + } node.clade.forEach(parsePhyloxml); node.children = node.clade; delete node.clade; } - node.annotation = 1; - node.attribute = "0.01"; + node.annotation = 1; + node.attribute = "0.01"; if (node.branch_length) { - node.attribute = node.branch_length; + node.attribute = node.branch_length; } if (node.taxonomy) { node.name = node.taxonomy.scientific_name; @@ -66,8 +71,23 @@ var phyloxml_parser = function(xml, options) { var tree_json; + if (typeof xml === "string") { + if (DOMParser) { + const parser = new DOMParser(); + xml = parser.parseFromString(xml, "text/xml"); + } else { + const parser = new XMLParser(); + xml = parser.parse(xml); + } + } + xml = xmlToJson(xml); - tree_json = xml.phyloxml.phylogeny.clade; + var phylogeny = xml.phyloxml.phylogeny; + if (Array.isArray(phylogeny)) { + phylogeny = phylogeny[0]; + console.warn('PhyloXML files with multiple phylogenies are not currently supported. Only the first phylogeny will be loaded.') + } + tree_json = phylogeny.clade; tree_json.name = "root"; parsePhyloxml(tree_json, 0);