Skip to content

Commit

Permalink
Merge pull request #62 from clockor2/read-tree-bug-#58
Browse files Browse the repository at this point in the history
fix: fix multi tree read newick bug
  • Loading branch information
Wytamma authored Apr 12, 2024
2 parents ece3b15 + e8d04b2 commit f32f0ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/io/readers/newick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export function readNewick(str: string): Tree {
const stack: number[] = [];
const nodes: Node[] = [];

// check for multiple trees
if (str.includes('\n')) {
str = str.slice(0, str.indexOf('\n'));
console.warn(
'Multiple trees in Newick string. Only reading the first tree. Use readTreesFromNewick() to read all trees.'
);
}

for (let l = 0; l < str.length; ) {
while (l < str.length && (str.charAt(l) < '!' || str.charAt(l) > '~')) ++l;
if (l == str.length) break;
Expand Down
4 changes: 2 additions & 2 deletions test/io/readers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import { beastAnnotation, nhxAnnotation } from '../../src/io/writers/newick';

// test read local files
describe('read', () => {
test('readTreesFromNewick', () => {
test('readNewick', () => {

const inNewick = readFileSync('test/data/egTree.nwk', 'utf-8');

const tree = readTreesFromNewick(inNewick)[0];
const tree = readNewick(inNewick);

// test the number of leaves
expect(tree.leafList.length).toBe(274);
Expand Down

0 comments on commit f32f0ce

Please sign in to comment.