Skip to content

Commit

Permalink
Revert "Improve Newick parsing"
Browse files Browse the repository at this point in the history
Two recent issues (#71, #72) provide examples where the improved parsing
either didn't parse a valid newick tree or (much more worryingly)
returned an entirely incorrect tree structure, including nodes not
present in the newick. See those issues for details, including the tree
files. While this reversion will re-introduce bugs such as #66 and the
bug in
<https://discussion.nextstrain.org/t/displaying-trees-from-ncbi-pathogen-browser-in-auspice-us/1456/4>,
but they are lesser than the bugs introduced by #69.

This reverts commit cabba98, although
subsequent changes to package-lock.json mean it's not a clean revert.
  • Loading branch information
jameshadfield committed Dec 6, 2023
1 parent babee1b commit e42e75c
Show file tree
Hide file tree
Showing 3 changed files with 4,770 additions and 3,297 deletions.
88 changes: 49 additions & 39 deletions auspice_client_customisation/parseNewick.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,56 @@
import { parse as _parseNewick } from "newick-js";

const parseNewick = (nwk) => {
const {root, rootWeight, graph: [,edges]} = _parseNewick(nwk);
const edgesByParent = new Map();

for (const [parent, child, weight] of edges) {
if (!edgesByParent.has(parent))
edgesByParent.set(parent, new Set());
edgesByParent.get(parent).add({child, weight});
}

const constructTree = (parent, weight) => {
const tree = {
// Particulars of this object are tied to getTreeStruct() below.
name: parent.label ?? "",
node_attrs: {
div: Number.isFinite(weight) ? weight : 0,
}
};

const childEdges = edgesByParent.get(parent);

if (childEdges?.size) {
tree.children = [];
/**
* Newick format parser in JavaScript.
*
* Copyright (c) Jason Davies 2010.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/

for (const {child, weight} of childEdges) {
/* childEdges is reversed relative to the order given by the Newick input
* due to a side-effect of the parser's internals, so we unshift()
* instead of push() to restore the input order.
*/
tree.children.unshift(
constructTree(child, weight)
);
}
/* NOTE: parseNewick function slightly modified to produce an object better suited for Nextstrain. */

export const parseNewick = (nwk) => {
const ancestors = [];
let tree = {};
const tokens = nwk.split(/\s*(;|\(|\)|,|:)\s*/);
for (let i=0; i<tokens.length; i++) {
const token = tokens[i];
const subtree = {};
switch (token) {
case '(': // new child nodes up next
tree.children = [subtree];
ancestors.push(tree);
tree = subtree;
break;
case ',': // next node: another child of the last ancestor
ancestors[ancestors.length-1].children.push(subtree);
tree = subtree;
break;
case ')': // optional name next
tree = ancestors.pop();
break;
case ':': // optional length next
break;
default:
const x = tokens[i-1];
if (x === ')' || x === '(' || x === ',') {
tree.name = token;
} else if (x === ':') {
tree.node_attrs = {div: parseFloat(token)};
}
}

return tree;
};

return constructTree(root, rootWeight);
}
return tree;
};


const getTreeStruct = (nwk) => {
const tree = parseNewick(nwk);

Expand Down
Loading

0 comments on commit e42e75c

Please sign in to comment.