Documentation, examples, tutorials and more

<<

NAME

Smash::Utils::Tree - Object encapsulating a tree structure and associated functions.

SYNOPSIS

        use Smash::Utils::Tree;

        # make tree

        my $tree = new Smash::Utils::Tree(NAME => "NCBI", TYPE => "phylogenetic");

        # add root

        my $root = new Smash::Utils::Tree::Node(ID => 1, \
                                                NAME => "root", \
                                                RANK => "no rank", \
                                                PARENT => -10000);
        $tree->add_node($root);
        $tree->set_root($root);

        # add a child to the root

        my $bacteria = new Smash::Utils::Tree::Node(ID => 2, \
                                                NAME => "Bacteria", \
                                                RANK => "superkingdom");
        $tree->add_node($bacteria);
        $root->add_child($bacteria);

        my $bac_chlor = new Smash::Utils::Tree::Node(ID => 3, \
                                                NAME => "Bacteroidetes/Chlorobi", \
                                                RANK => "superphylum");
        $tree->add_node($bac_chlor);
        $bacteria->add_child($bac_chlor);

        my $bacteroidetes = new Smash::Utils::Tree::Node(ID => 4, \
                                                NAME => "Bacteroidetes", \
                                                RANK => "phylum");
        $tree->add_node($bacteroidetes);
        $bac_chlor->add_child($bacteroidetes);

        # get information for a node

        my $node = $tree->nodes->{3};
        $node->id;             # 3
        $node->rank;           # superphylum
        $node->name;           # Bacteroidetes/Chlorobi
        $node->newick_name;    # Bacteroidetes_Chlorobi

DESCRIPTION

Smash:Utils::MatrixIO provides functions to read and write matrices stored as hashes.

FUNCTIONS

Properties

name

returns the name of the tree.

type

returns the type of the tree (commonly used are "phylogenetic" and "functional").

root

returns the id of the root of the tree.

rootlink

returns the link to the root of the tree.

nodes

returns the hash containing all nodes, indexed by the id's.

get_id_for_ranked_name($rank, $name)

returns the id of the node for a given name at the given rank. E.g.,

        $tree->get_id_for_ranked_name("genus", "Bacteroides"); # returns 816 in NCBI tree

Tree manipulation functions

add_node($node)

adds the node to this tree.

remove_node($node)

removes the node from this tree.

count_leaves

Returns the number of leaf nodes.

delete_data

Deletes data at every node.

propagate_data_to_root($node_id, $value)

Propagates $value at DATA all the way upto root from the given node.

prune_tree

Prunes the tree so that nodes without values for the DATA field will be removed. Needs DATA to be set in the nodes to remain, otherwise the whole tree will disappear and it cannot be undone. For example, if you have a list of node-ids that you want to keep, then set DATA to 1 in each of these nodes, propagate it all the way up to ROOT, then prune the tree.

prune_tree_recursive

Internal recursive routine called by prune_tree.

remove_straight_links

Removes internal nodes with just one child. For example,

            |
        ____|____
        |       |
        |       |
        A       |
        |       |
        |       |
        B       |
        |       |
        |       |
        C       D
In this case, C is connected to A directly and becomes

            |
        ____|____
        |       |
        |       |
        |       |
        |       |
        |       |
        |       |
        |       |
        |       |
        C       D
propagate_data_average

Propagate values set in the DATA field bottom-up by setting the value at a node as the average of its immediate children.

propagate_data_average_recursive

Internal recursive routine called by propagate_data_average.

print_newick

Prints the tree in Newick format. This prints the whole tree. If you want a subtree to be printed, prune the tree beforehand using the DATA field. For example, if you have a list of node-ids that you want to keep, then set DATA to 1 in each of these nodes, propagate it all the way up to ROOT, then prune the tree.

print_newick_recursive

Internal recursive routine called by print_newick.

print_tree

Prints the tree in XML format.

print_tree_recursive

Internal recursive routine called by print_tree.

print_data

Prints the tree in XML format, only for nodes with DATA being set. Similar to print_tree.

print_data_recursive

Internal recursive routine called by print_data.

dump_tree

Dumps two files: names.dmp and nodes.dmp in NCBI taxonomy dump format corresponding to the tree.

Phylogenetic tree-specific functions

get_full_lineage()

returns list containing the ids of all the ancestral nodes in order (bottom-to-top).

get_full_lineage_string_for_id()

returns the taxonomic lineage as string in the following format:

        <lineage superkingdom="Bacteria" phylum="Bacteroidetes">
get_common_ancestor()

returns the lowest common ancestor (LCA) for a given list of node ids (calls one of the two versions specified below).

get_common_ancestor_for_two()

returns the lowest common ancestor (LCA) for a given list of exactly two node ids.

get_common_ancestor_generic()

returns the lowest common ancestor (LCA) for a given list of node ids.

NAME

Smash::Utils::Tree::Node - Object encapsulating the node of a tree structure.

DESCRIPTION

FUNCTIONS

Properties of the object and methods.

Properties

id

returns taxonomy id.

name

returns the name of the organism at the node.

newick_name

returns the name that is newick compatible.

rank

returns the rank of the organism at the node.

parent

returns the id of the parent node.

parentlink

returns the parent node object.

nchildren

returns the number of children.

children

returns the hash containing all children nodes.

data

returns the data stored at the given node.

Tree manipulation functions

add_child($child)

add the given node to the children of this node.

remove_child($child)

removes the given from the children of this node.

copy_node()

returns a new Smash::Utils::Tree::Node object that is an exact copy of this node.

<<