Class | Bio::Nexus::TreesBlock
In: | lib/bio/db/nexus.rb
Parent: | GenericBlock

## DESCRIPTION

Bio::Nexus::TreesBlock represents a trees nexus block.

# Example of Trees block:

Begin Trees;

     Tree best=(fish,(frog,(snake, mouse)));
     Tree other=(snake,(frog,( fish, mouse)));

End;

## USAGE

      require 'bio/db/nexus'

      # Create a new parser:
      nexus = Bio::Nexus.new( nexus_data_as_string )

      Get trees block(s):
      trees_block = nexus.get_trees_blocks[ 0 ]
      # Get first tree named "best" as String:
      string_fish = trees_block.get_tree_strings_by_name( "best" )[ 0 ]
      # Get first tree named "best" as Bio::Db::Newick object:
      tree_fish = trees_block.get_trees_by_name( "best" )[ 0 ]
      # Get first tree as Bio::Db::Newick object:
      tree_first = trees_block.get_tree( 0 )

### Methods

add_tree add_tree_name get_tree get_tree_names get_tree_strings get_tree_strings_by_name get_trees_by_name new to_nexus

### Constants

TREE | = | "Tree"

### Public Class methods

new( name )

[Source]

          # File lib/bio/db/nexus.rb, line 1458
    1458:       def initialize( name )
    1459:         super( name )
    1460:         @trees      = Array.new
    1461:         @tree_names = Array.new
    1462:       end

### Public Instance methods

add_tree( tree_as_string )

Adds a tree to this block.

Arguments:

- (required) tree_as_string: String

[Source]

          # File lib/bio/db/nexus.rb, line 1549
    1549:       def add_tree( tree_as_string )
    1550:         @trees.push( tree_as_string )
    1551:       end

add_tree_name( tree_name )

Adds a tree name to this block.

Arguments:

- (required) tree_name: String

[Source]

          # File lib/bio/db/nexus.rb, line 1541
    1541:       def add_tree_name( tree_name )
    1542:         @tree_names.push( tree_name )
    1543:       end

get_tree( i )

Returns tree i (same order as in nexus data) as newick parsed tree object.

Arguments:

- (required) i: Integer

Returns: | Bio::Newick

[Source]

          # File lib/bio/db/nexus.rb, line 1513
    1513:       def get_tree( i )
    1514:         newick = Bio::Newick.new( @trees[ i ] )
    1515:         tree = newick.tree
    1516:         tree
    1517:       end

get_tree_names()

Returns an array of tree names.

Returns: | Array

[Source]

          # File lib/bio/db/nexus.rb, line 1485
    1485:       def get_tree_names
    1486:         @tree_names
    1487:       end

get_tree_strings()

Returns an array of strings describing trees

Returns: | Array

[Source]

          # File lib/bio/db/nexus.rb, line 1478
    1478:       def get_tree_strings
    1479:         @trees
    1480:       end

get_tree_strings_by_name( name )

Returns an array of strings describing trees for which name matches the tree name.

Arguments:

- (required) name: String

Returns: | Array

[Source]

          # File lib/bio/db/nexus.rb, line 1495
    1495:       def get_tree_strings_by_name( name )
    1496:         found_trees = Array.new
    1497:         i = 0
    1498:         @tree_names.each do | n |
    1499:           if ( n == name )
    1500:             found_trees.push( @trees[ i ] )
    1501:           end
    1502:           i += 1
    1503:         end
    1504:         found_trees
    1505:       end

get_trees_by_name( name )

Returns an array of newick parsed tree objects for which name matches the tree name.

Arguments:

- (required) name: String

Returns: | Array of Bio::Newick

[Source]

          # File lib/bio/db/nexus.rb, line 1525
    1525:       def get_trees_by_name( name )
    1526:         found_trees = Array.new
    1527:         i = 0
    1528:         @tree_names.each do | n |
    1529:           if ( n == name )
    1530:             found_trees.push( get_tree( i ) )
    1531:           end
    1532:           i += 1
    1533:         end
    1534:         found_trees
    1535:       end

to_nexus()

Returns a String describing this block as nexus formatted data.

Returns: | String

[Source]

          # File lib/bio/db/nexus.rb, line 1467
    1467:       def to_nexus
    1468:         trees_ary = Array.new
    1469:         for i in 0 .. @trees.length - 1
    1470:           trees_ary.push( TREE + " " + @tree_names[ i ] + "=" + @trees[ i ] )
    1471:         end
    1472:         Nexus::Util::to_nexus_helper( TREES_BLOCK, trees_ary  )
    1473:       end

[Validate]