Class: Bio::Nexus::TreesBlock

Inherits:
GenericBlock show all
Defined in:
lib/bio/db/nexus.rb

Overview

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 )

Constant Summary

TREE =
"Tree"

Instance Method Summary collapse

Methods inherited from GenericBlock

#add_token, #get_name, #get_tokens, #to_s

Constructor Details

#initialize(name) ⇒ TreesBlock

Returns a new instance of TreesBlock



1459
1460
1461
1462
1463
# File 'lib/bio/db/nexus.rb', line 1459

def initialize( name )
  super( name ) 
  @trees      = Array.new
  @tree_names = Array.new
end

Instance Method Details

#add_tree(tree_as_string) ⇒ Object

Adds a tree to this block.


Arguments:

  • (required) tree_as_string: String



1550
1551
1552
# File 'lib/bio/db/nexus.rb', line 1550

def add_tree( tree_as_string )
  @trees.push( tree_as_string )
end

#add_tree_name(tree_name) ⇒ Object

Adds a tree name to this block.


Arguments:

  • (required) tree_name: String



1542
1543
1544
# File 'lib/bio/db/nexus.rb', line 1542

def add_tree_name( tree_name )
  @tree_names.push( tree_name )
end

#get_tree(i) ⇒ Object

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


Arguments:

  • (required) i: Integer

Returns

Bio::Newick



1514
1515
1516
1517
1518
# File 'lib/bio/db/nexus.rb', line 1514

def get_tree( i )
  newick = Bio::Newick.new( @trees[ i ] )
  tree = newick.tree
  tree
end

#get_tree_namesObject

Returns an array of tree names.


Returns

Array



1486
1487
1488
# File 'lib/bio/db/nexus.rb', line 1486

def get_tree_names
  @tree_names 
end

#get_tree_stringsObject

Returns an array of strings describing trees


Returns

Array



1479
1480
1481
# File 'lib/bio/db/nexus.rb', line 1479

def get_tree_strings
  @trees  
end

#get_tree_strings_by_name(name) ⇒ Object

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


Arguments:

  • (required) name: String

Returns

Array



1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
# File 'lib/bio/db/nexus.rb', line 1496

def get_tree_strings_by_name( name )
  found_trees = Array.new
  i = 0
  @tree_names.each do | n |
    if ( n == name )
      found_trees.push( @trees[ i ] )
    end
    i += 1
  end
  found_trees
end

#get_trees_by_name(name) ⇒ Object

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


Arguments:

  • (required) name: String

Returns

Array of Bio::Newick



1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
# File 'lib/bio/db/nexus.rb', line 1526

def get_trees_by_name( name )
  found_trees = Array.new
  i = 0
  @tree_names.each do | n |
    if ( n == name )
      found_trees.push( get_tree( i ) )
    end
    i += 1
  end
  found_trees
end

#to_nexusObject

Returns a String describing this block as nexus formatted data.


Returns

String



1468
1469
1470
1471
1472
1473
1474
# File 'lib/bio/db/nexus.rb', line 1468

def to_nexus
  trees_ary = Array.new
  for i in 0 .. @trees.length - 1
    trees_ary.push( TREE + " " + @tree_names[ i ] + "=" + @trees[ i ] )
  end
  Nexus::Util::to_nexus_helper( TREES_BLOCK, trees_ary  )
end