npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

newick-tree-parser

v1.0.1

Published

A Newick format and tree structure parser.

Downloads

3

Readme

Newick Tree Parser

JavaScript library for parsing the Newick Tree format.

Usage:

import { Newick } from 'newick-tree-parser'
const newick = new Newick('some data')

Documentation:

NewickTools.dfs

Returns nodes data

import { NewickTools } from 'newick-tree-parser'
NewickTools.dfs(tree)

tree - string or parsed object

var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var data = NewickTools.dfs(tree);

Returns object

{
  A: 0.1,
  B: 0.2,
  C: 0.3,
  D: 0.4,
  E: 0.5
}

NewickTools.dfs(tree, nodeCallback)
  • tree - string or parsed object
  • nodeCallback - function (node)
var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var data = NewickTools.dfs(tree, function (e) {
  e.length *= 10;
  return e;
});

Returns object

{
  A: 1,
  B: 2,
  C: 3,
  D: 4,
  E: 5
}

NewickTools.equals

Check if two trees are equal

import { NewickTools } from 'newick-tree-parser'

NewickTools.equals(tree1, tree2)

tree1, tree2 - Newick instanse Returns boolean true or false

var tree1 = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var tree2 = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');

NewickTools.equals(tree1, tree2); // true;

tree2 = NewickTools.parse('(A:0.11,B:0.22,(C:0.33,D:0.44)E:0.55)F');

NewickTools.equals(tree1, tree2); // false

NewickTools.getRoot

Returns a root of the tree

import { NewickTools } from 'newick-tree-parser'

NewickTools.getRoot(s)

s - string on parsed object

var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var root = NewickTools.getRoot(tree);\

Returns string (root name)

'F'

NewickTools.map

Iterates tree's nodes and applies callback for each node

Alias for NewickTools.dfs(tree, callback)

import { NewickTools } from 'newick-tree-parser'

NewickTools.map(tree, callback)

tree - string or parsed object callback - function (node)

var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var mappedTree = NewickTools.map(tree, function (e) {
  e *= 10;
  e.name += ' mapped';
  return e;
});

Returns object

{
    name: "F mapped",
    branchset: [
    {
        name: "A mapped",
        length: 1
    },
    {
        name: "B mapped",
        length: 2
    },
    {
        name: "E mapped",
        length: 5,
        branchset: [
        {
            name: "C mapped",
            length: 3
        },
        {
            name: "D mapped",
            length: 4
        }]
    }]
}

NewickTools.normalize

Returns normalized tree in [0; 1]

import { NewickTools } from 'newick-tree-parser'

NewickTools.normalize(tree)

tree - string or parsed object

var tree = NewickTools.parse('(A:5,B:20)F;');
var normalizedTree = NewickTools.normalize(tree)

Returns object

{
  name:"F",
  branchset: [
    {name:"A", length: 0.2},
    {name:"B", length: 0.8}
  ]
}

NewickTools.parse

Parse Newick string into tree-object

import { NewickTools } from 'newick-tree-parser'

NewickTools.parse(s)

s - string

var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');

Returns object

{
    name: "F",
    branchset: [
    {
        name: "A",
        length: 0.1
    },
    {
        name: "B",
        length: 0.2
    },
    {
        name: "E",
        length: 0.5,
        branchset: [
        {
            name: "C",
            length: 0.3
        },
        {
            name: "D",
            length: 0.4
        }]
    }]
}

NewickTools.serialize

Serializes tree

import { NewickTools } from 'newick-tree-parser'

NewickTools.serialize(tree)

tree - string or parsed object

var tree = NewickTools.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var serializedTree = NewickTools.serialize(tree)

Returns string

'(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F;'

private.cast

Casts tree or string to tree-object

private

cast(s)

s - string or parsed object

var tree = '(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F';
tree = cast(tree);

Returns object

{
    name: "F",
    branchset: [
    {
        name: "A",
        length: 0.1
    },
    {
        name: "B",
        length: 0.2
    },
    {
        name: "E",
        length: 0.5,
        branchset: [
        {
            name: "C",
            length: 0.3
        },
        {
            name: "D",
            length: 0.4
        }]
    }]
}

private.getRoot

Returns a root of the tree

private

getRoot(s)

s - string or parsed object

var tree = Newick.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
tree = getRoot(tree);

Returns string (root name)

'F'

private.serialize

Serializes tree

private

serialize(node)

node - tree node

var tree = Newick.parse('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var serializedTree = Newick.serialize(tree)

Returns string

'(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F'

public.dfs

Returns nodes data

public method

var data = newick.dfs();
var tree = new Newick('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var data = newick.dfs();

Returns object

{
  A: 0.1,
  B: 0.2,
  C: 0.3,
  D: 0.4,
  E: 0.5
}

var data = newick.dfs(nodeCallback);

nodeCallback - function (node)

var tree = new Newick('(A:0.1,B:0.2,(C:0.3,D:0.4)E:0.5)F');
var data = newick.dfs(function (e) {
  e.length *= 10;
  return e;
});

Returns object

{
  A: 1,
  B: 2,
  C: 3,
  D: 4,
  E: 5
}