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-js

v1.2.1

Published

TypeScript/JavaScript methods for parsing and writing in Newick tree format.

Downloads

42

Readme

newick-js

A Node.js module that parses strings into graphs according to the Newick tree format, and writes graphs as Newick tree strings.

Requirements

This module minimally requires ECMAScript 2015 (ES6), since it uses Set objects.

Installation

npm

npm install newick-js --save

yarn

yarn add newick-js

Usage

JavaScript and TypeScript

Importing the Functions

To import the functions in JavaScript:

const { parse, write } = require("newick-js")

To import the functions in TypeScript:

import { parse, write } from "newick-js"

To import separately (minimizing file size for tree-shaking):

import { parse } from "newick-js/dist/src/parse"
import { write } from "newick-js/dist/src/write"

Interface

(The type declarations are written in TypeScript.)

Vertices

A vertex is a plain object with an optional label field.

interface Vertex {
    label?: string
}
Arcs

An arc is an array which includes, in order:

  1. a head vertex (the parent),
  2. a tail vertex (the child), and
  3. a weight (a number, possibly NaN).
type Arc = [Vertex, Vertex, number]
Graphs

A graph is an array which includes, in order:

  1. a set of vertices, and
  2. a set of arcs.
type Graph = [Set<Vertex>, Set<Arc>]
Functions

The parse() function takes a string and yields a parse result, including a graph, a root vertex, and a weight for the root vertex (possibly NaN).

declare function parse(s: string): ParseResult
interface ParseResult {
    graph: Graph
    root: Vertex
    rootWeight: number
}

The write() method takes a graph and yields a Newick tree string.

declare function write(graph: Graph, rootWeight?: number): string

Example Usage

(JavaScript or TypeScript, after importing the functions)

const result = parse("(Pongo:15.76,(Gorilla:9.06,(Pan:6.65,Homo:6.65):2.41)Homininae:6.70)Hominidae:4.43;")
console.log(`Root: ${result.root.label}`)
console.log(`Root Weight: ${result.rootWeight}`)
const vertices = result.graph[0]
console.log(`Number of Vertices: ${vertices.size}`)
const arcs = result.graph[1]
console.log(`Number of Arcs: ${arcs.size}`)
console.log(write(result.graph, result.rootWeight))

Output:

> Root: Hominidae
> Root Weight: 4.43
> Number of Vertices: 7
> Number of Arcs: 6
> ((Homo:6.65,Pan:6.65):2.41,Gorilla:9.06)Homininae:6.7,(Pongo:15.76)Hominidae;

See test/test.js for further examples.

AMD

define(function (require, exports, module) {
    var newick = require("newick-js")
})

Testing

yarn test