Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in parsing PhyloXML files #459

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 24 additions & 4 deletions src/formats/phyloxml.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down