diff --git a/src/io/readers/newick.ts b/src/io/readers/newick.ts index 4c65f6d..41a4759 100644 --- a/src/io/readers/newick.ts +++ b/src/io/readers/newick.ts @@ -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; diff --git a/test/io/readers.spec.ts b/test/io/readers.spec.ts index 8f49a36..60695d5 100644 --- a/test/io/readers.spec.ts +++ b/test/io/readers.spec.ts @@ -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);