timetree

package module
v0.0.0-...-a50ec8e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2024 License: BSD-2-Clause Imports: 11 Imported by: 16

README

TimeTree

Package timetree provides a representation of a time calibrated phylogenetic tree.

Authorship and license

Copyright © 2022 J. Salvador Arias jsalarias@gmail.com. All rights reserved. Distributed under BSD2 licenses that can be found in the LICENSE file.

Documentation

Overview

Package timetree provides a representation of a time calibrated phylogenetic tree.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTreeNoName   = errors.New("tree without name")
	ErrTreeRepeated = errors.New("repeated tree name")
)

Tree collection errors

View Source
var (
	// Newick errors
	ErrNotNewick  = fmt.Errorf("not a newick tree file")
	ErrUnexpBrLen = fmt.Errorf("unexpected branch length")
)
View Source
var (
	// Tree adding errors
	ErrAddNoParent     = errors.New("parent ID not in tree")
	ErrAddRepeated     = errors.New("repeated taxon name")
	ErrAddInvalidBrLen = errors.New("invalid branch length")
	ErrAddNoSister     = errors.New("sister ID not in tree")
	ErrAddRootSister   = errors.New("sister ID is the root node")

	// Tree validation errors
	ErrValSingleChild = errors.New("node with a single descendant")
	ErrValUnnamedTerm = errors.New("unnamed terminal")

	// Age assignments
	ErrInvalidRootAge = errors.New("invalid root age")
	ErrOlderAge       = errors.New("age to old for node")
	ErrYoungerAge     = errors.New("age to young for node")
)

Functions

This section is empty.

Types

type Collection

type Collection struct {
	// contains filtered or unexported fields
}

A Collection is a collection of phylogenetic trees.

func NewCollection

func NewCollection() *Collection

NewCollection returns a new empty collection.

func Newick

func Newick(r io.Reader, name string, age int64) (*Collection, error)

Newick reads one or more trees in newick (parenthetical) format. Age set the age of the root node (in years), if age is 0, the age of the root node will be inferred from the largest branch length between any terminal and the root. Branch lengths will be interpreted as million years. Name sets the name of the first tree, any other tree name will be in the form <name>.<number> starting from 1.

func ReadTSV

func ReadTSV(r io.Reader) (*Collection, error)

ReadTSV reads a phylogenetic tree from a TSV file.

The TSV must contain the following fields:

-tree, for the name of the tree
-node, for the ID of the node
-parent, for of ID of the parent node
    (-1 is used for the root)
-age, the age of the node (in years)
-taxon, the taxonomic name of the node

Parent nodes should be defined, before any children node. Terminal nodes should have a unique taxonomic name.

Here is an example file:

# time calibrated phylogenetic tree
tree	node	parent	age	taxon
dinosaurs	0	-1	235000000
dinosaurs	1	0	230000000	Eoraptor lunensis
dinosaurs	2	0	170000000
dinosaurs	3	2	145000000	Ceratosaurus nasicornis
dinosaurs	4	2	71000000	Carnotaurus sastrei

func (*Collection) Add

func (c *Collection) Add(t *Tree) error

Add adds a tree to a tree collection. It will return an error if a the collection has a tree with the name of the added tree or the tree name is empty.

func (*Collection) Names

func (c *Collection) Names() []string

Names return the names of the trees in the collection.

func (*Collection) TSV

func (c *Collection) TSV(w io.Writer) error

TSV encodes a collection of phylogenetic trees into a TSV file.

func (*Collection) Tree

func (c *Collection) Tree(name string) *Tree

Tree returns a tree with a given name.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

A Tree is a time calibrated phylogenetic tree, a set of phylogenetic nodes with a single common ancestor.

func New

func New(name string, age int64) *Tree

New returns a new phylogenetic tree with a name and a root at the given age in years.

func (*Tree) Add

func (t *Tree) Add(id int, brLen int64, name string) (int, error)

Add adds a node as child of the indicated node ID, using the indicated branch length in years, and a taxon name for the node (that can be empty). It returns the ID of the added node or -1 and an error.

func (*Tree) AddSister

func (t *Tree) AddSister(id int, age, brLen int64, name string) (int, error)

AddSister adds a node as a sister group of the indicated node ID, using the indicated age of the added node and the branch length of the new branch, both in years, and the name of the added node (that can be empty). It returns the ID of the added node or -1 and an error.

func (*Tree) Age

func (t *Tree) Age(id int) int64

Age returns the age of the indicated node.

func (*Tree) Children

func (t *Tree) Children(id int) []int

Children returns an slice with the IDs of the children of a node.

func (*Tree) Delete

func (t *Tree) Delete(id int) error

Delete removes a node and all of its descendants from a tree.

func (*Tree) Depth

func (t *Tree) Depth(id int) int

Depth returns the number of nodes between the indicated node and the root of the tree.

func (*Tree) Format

func (t *Tree) Format()

Format sort the nodes of a tree, changing node IDs if necessary.

func (*Tree) IsRoot

func (t *Tree) IsRoot(id int) bool

IsRoot returns true if the indicated node is the root of the tree.

func (*Tree) IsTerm

func (t *Tree) IsTerm(id int) bool

IsTerm returns true if the indicated node is a terminal of the tree.

func (*Tree) Len

func (t *Tree) Len() int64

Len returns the total length (in years) of a tree.

func (*Tree) LenToRoot

func (t *Tree) LenToRoot(id int) int64

LenToRoot returns the length (in years) from a node to the root of the tree.

func (*Tree) MRCA

func (t *Tree) MRCA(names ...string) int

MRCA returns the most recent common ancestor of two or more terminals.

func (*Tree) Move

func (t *Tree) Move(age int64) error

Move sets the age of the root node (in years), and updates all node ages keeping the branch lengths. The age of the root must be at least equal to the distance to the most recent terminal.

func (*Tree) Name

func (t *Tree) Name() string

Name returns the name of the tree.

func (*Tree) Nodes

func (t *Tree) Nodes() []int

Nodes return an slice with IDs of the nodes of the tree.

func (*Tree) NumInternal

func (t *Tree) NumInternal() int

NumInternal returns the number of internal nodes (i.e., nodes with descendants).

func (*Tree) Parent

func (t *Tree) Parent(id int) int

Parent returns the ID of the parent of the indicated node. It will return -1 for the root or an invalid node.

func (*Tree) Root

func (t *Tree) Root() int

Root returns the ID of the root node which is 0.

func (*Tree) Set

func (t *Tree) Set(id int, age int64) error

Set sets the age of a node (in years).

func (*Tree) SetName

func (t *Tree) SetName(id int, name string) error

SetName sets the name of a node, removing any previous name of the node. If the node is not a terminal, the new name must be non-empty.

func (*Tree) SubTree

func (t *Tree) SubTree(id int, name string) *Tree

SubTree creates a new tree from a given node using the indicated name. If no name is given, it will use the node name, or a node identifier.

func (*Tree) TaxNode

func (t *Tree) TaxNode(name string) (int, bool)

TaxNode returns the ID of a node with a given taxon name. It returns false if the taxon does not exists.

func (*Tree) Taxa

func (t *Tree) Taxa() []string

Taxa returns all defined taxon names of the tree.

func (*Tree) Taxon

func (t *Tree) Taxon(id int) string

Taxon returns the taxon name of the node with the indicated ID.

func (*Tree) Terms

func (t *Tree) Terms() []string

Terms returns the name of all terminals of the tree.

func (*Tree) Validate

func (t *Tree) Validate() error

Validate will return an error if the tree is invalid. A tree is invalid if it has nodes with a single child, or terminal nodes are without a defined name.

Directories

Path Synopsis
cmd
timetree
TimeTree is a tool to manipulate time calibrated phylogenetic trees.
TimeTree is a tool to manipulate time calibrated phylogenetic trees.
timetree/add
Package add implements a command to add a new taxon to a tree.
Package add implements a command to add a new taxon to a tree.
timetree/draw
Package draw implements a command to output a phylogenetic tree from a TSV file into an SVG file.
Package draw implements a command to output a phylogenetic tree from a TSV file into an SVG file.
timetree/format
Package format implements a command to format the trees in a tree file.
Package format implements a command to format the trees in a tree file.
timetree/importcmd
Package importcmd implements a command to import phylogenetic trees from a newick file into tsv files.
Package importcmd implements a command to import phylogenetic trees from a newick file into tsv files.
timetree/list
Package list implements a command to print a list of trees in a tree file.
Package list implements a command to print a list of trees in a tree file.
timetree/newick
Package newick implements a command to output a phylogenetic tree from a TSV file into an equivalent Newick file.
Package newick implements a command to output a phylogenetic tree from a TSV file into an equivalent Newick file.
timetree/set
Package set implements a command to set node ages for a list of trees.
Package set implements a command to set node ages for a list of trees.
timetree/sim
Package sim implements a command to simulate a phylogenetic tree.
Package sim implements a command to simulate a phylogenetic tree.
timetree/sub
Package sub implements a command to produce a sub-tree from a phylogenetic tree in a tsv file.
Package sub implements a command to produce a sub-tree from a phylogenetic tree in a tsv file.
timetree/tax
Package tax implements a command to validate the terminal names for a list of trees.
Package tax implements a command to validate the terminal names for a list of trees.
timetree/terms
Package terms implements a command to print the list of terminals in a tree file.
Package terms implements a command to print the list of terminals in a tree file.
Package simulate creates random trees.
Package simulate creates random trees.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL