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 collapse

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.



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

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



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

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



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

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



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

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



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

def get_tree_names
  @tree_names 
end

#get_tree_stringsObject

Returns an array of strings describing trees


Returns

Array



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

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



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

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



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

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



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

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