class Bio::Nexus::NexusMatrix

DESCRIPTION

Bio::Nexus::NexusMatrix represents a characters or distance matrix, where the names are stored in column zero.

USAGE

require 'bio/db/nexus'

# Create a new parser:
nexus = Bio::Nexus.new( nexus_data_as_string )
# Get distances block(s):
distances_block = nexus.get_distances_blocks[ 0 ]
# Get matrix as Bio::Nexus::NexusMatrix object:
matrix = distances_blocks.get_matrix
# Get value (column 0 are names):
val = matrix.get_value( 1, 5 )
# Return first row as String (all columns except column 0),
# values are separated by "_":
row_str_0 = matrix.get_row_string( 0, "_" )
# Return all rows named "ciona" as String (all columns except column 0),
# values are separated by "+":
ciona_rows = matrix.get_row_strings_by_name( "ciona", "+" )

Public Class Methods

new() click to toggle source

Creates new NexusMatrix.

     # File lib/bio/db/nexus.rb
1586 def initialize()
1587   @rows = Hash.new
1588   @max_row = -1
1589   @max_col = -1
1590 end

Public Instance Methods

get_max_col() click to toggle source

Returns the maximal columns number.


Returns

Integer

     # File lib/bio/db/nexus.rb
1642 def get_max_col 
1643   return @max_col
1644 end
get_max_row() click to toggle source

Returns the maximal row number.


Returns

Integer

     # File lib/bio/db/nexus.rb
1649 def get_max_row 
1650   return @max_row
1651 end
get_name( row ) click to toggle source

Convenience method which return the value of column 0 and row 'row' which is usually the name.


Arguments:

  • (required) row: Integer

Returns

String

     # File lib/bio/db/nexus.rb
1668 def get_name( row )
1669   get_value( row, 0 ).to_s
1670 end
get_row_string( row, spacer = "" ) click to toggle source

Returns the values of columns 1 to maximal column length in row 'row' concatenated as string. Individual values can be separated by 'spacer'.


Arguments:

  • (required) row: Integer

  • (optional) spacer: String

Returns

String

     # File lib/bio/db/nexus.rb
1681 def get_row_string( row, spacer = "" )
1682   row_str = String.new
1683   if is_empty?
1684     return row_str
1685   end
1686   for col in 1 .. get_max_col
1687     row_str << get_value( row, col ) << spacer
1688   end
1689   row_str
1690 end
get_row_strings_by_name( name, spacer = "" ) click to toggle source

Returns all rows as Array of Strings separated by 'spacer' for which column 0 is 'name'.


Arguments:

  • (required) name: String

  • (optional) spacer: String

Returns

Array

     # File lib/bio/db/nexus.rb
1699 def get_row_strings_by_name( name, spacer = "" )
1700   row_strs = Array.new
1701   if is_empty?
1702     return row_strs
1703   end
1704   for row in 0 .. get_max_row
1705     if ( get_value( row, 0 ) == name )
1706       row_strs.push( get_row_string( row, spacer ) )  
1707     end
1708   end
1709   row_strs
1710 end
get_value( row, col ) click to toggle source

Returns the value at row 'row' and column 'col'.


Arguments:

  • (required) row: Integer

  • (required) col: Integer

Returns

Object

     # File lib/bio/db/nexus.rb
1624 def get_value( row, col )
1625   if ( ( row > get_max_row() ) || ( row < 0 ) ) 
1626     raise( NexusMatrixError, "value for row (" + row.to_s +
1627       ") is out of range [max row: " + get_max_row().to_s + "]" )
1628   elsif ( ( col > get_max_col() ) || ( row < 0 ) )
1629     raise( NexusMatrixError, "value for column (" + col.to_s +
1630      ") is out of range [max column: " + get_max_col().to_s + "]" )
1631   end
1632   r = @rows[ row ]
1633   if ( ( r == nil ) || ( r.length < 1 ) ) 
1634     return nil
1635   end
1636   r[ col ]
1637 end
is_empty?() click to toggle source

Returns true of matrix is empty.


Returns

true or false

     # File lib/bio/db/nexus.rb
1657 def is_empty?
1658   return get_max_col < 0 || get_max_row < 0
1659 end
set_value( row, col, value ) click to toggle source

Sets the value at row 'row' and column 'col' to 'value'.


Arguments:

  • (required) row: Integer

  • (required) col: Integer

  • (required) value: Object

     # File lib/bio/db/nexus.rb
1598 def set_value( row, col, value ) 
1599   if ( ( row < 0 ) || ( col < 0 ) )
1600       raise( NexusTableError, "attempt to use negative values for row or column" )
1601   end
1602   if ( row > get_max_row() ) 
1603     set_max_row( row )
1604   end
1605   if ( col > get_max_col() ) 
1606     set_max_col( col )
1607   end
1608   row_map = nil
1609   if ( @rows.has_key?( row ) ) 
1610     row_map = @rows[ row ]
1611   else 
1612     row_map = Hash.new
1613     @rows[ row ] = row_map
1614   end
1615   row_map[ col ] = value
1616 end
to_nexus_row_array( spacer = "", append_delimiter = true ) click to toggle source

Helper method to produce nexus formatted data.


Arguments:

  • (optional) spacer: String

  • (optional) append_delimiter: true or false

Returns

Array

     # File lib/bio/db/nexus.rb
1734 def to_nexus_row_array( spacer = "", append_delimiter = true )
1735   ary = Array.new
1736   if is_empty?
1737     return ary
1738   end
1739   max_length = 10
1740   for row in 0 .. get_max_row
1741     l = get_value( row, 0 ).length
1742     if ( l > max_length )
1743       max_length = l
1744     end
1745   end  
1746   for row in 0 .. get_max_row
1747     row_str = String.new
1748     ary.push( row_str )
1749     name = get_value( row, 0 )
1750     name = name.ljust( max_length + 1 )
1751     row_str << name << " " << get_row_string( row, spacer )
1752     if ( spacer != nil && spacer.length > 0 )
1753       row_str.chomp!( spacer )
1754     end
1755     if ( append_delimiter && row == get_max_row )
1756       row_str << DELIMITER 
1757     end
1758   end
1759   ary
1760 end
to_s() click to toggle source

Returns matrix as String, returns “empty” if empty.


Returns

String

     # File lib/bio/db/nexus.rb
1715 def to_s
1716   if is_empty?
1717     return "empty"  
1718   end
1719   str = String.new
1720   row_array = to_nexus_row_array( " ", false )
1721   row_array.each do | row |
1722     str << row << END_OF_LINE
1723   end
1724   str
1725 end
Also aliased as: to_str
to_str()
Alias for: to_s