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
Creates new NexusMatrix.
# File lib/bio/db/nexus.rb, line 1585 def initialize() @rows = Hash.new @max_row = -1 @max_col = -1 end
Public Instance Methods
Returns the maximal columns number.
- Returns
-
Integer
# File lib/bio/db/nexus.rb, line 1641 def get_max_col return @max_col end
Returns the maximal row number.
- Returns
-
Integer
# File lib/bio/db/nexus.rb, line 1648 def get_max_row return @max_row end
Convenience method which return the value of column 0 and row 'row' which is usually the name.
Arguments:
-
(required) row: Integer
- Returns
# File lib/bio/db/nexus.rb, line 1667 def get_name( row ) get_value( row, 0 ).to_s end
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
# File lib/bio/db/nexus.rb, line 1680 def get_row_string( row, spacer = "" ) row_str = String.new if is_empty? return row_str end for col in 1 .. get_max_col row_str << get_value( row, col ) << spacer end row_str end
Returns all rows as Array of Strings separated by 'spacer' for which column 0 is 'name'.
Arguments:
- Returns
-
Array
# File lib/bio/db/nexus.rb, line 1698 def get_row_strings_by_name( name, spacer = "" ) row_strs = Array.new if is_empty? return row_strs end for row in 0 .. get_max_row if ( get_value( row, 0 ) == name ) row_strs.push( get_row_string( row, spacer ) ) end end row_strs end
Returns the value at row 'row' and column 'col'.
Arguments:
-
(required) row: Integer
-
(required) col: Integer
- Returns
# File lib/bio/db/nexus.rb, line 1623 def get_value( row, col ) if ( ( row > get_max_row() ) || ( row < 0 ) ) raise( NexusMatrixError, "value for row (" + row.to_s + ") is out of range [max row: " + get_max_row().to_s + "]" ) elsif ( ( col > get_max_col() ) || ( row < 0 ) ) raise( NexusMatrixError, "value for column (" + col.to_s + ") is out of range [max column: " + get_max_col().to_s + "]" ) end r = @rows[ row ] if ( ( r == nil ) || ( r.length < 1 ) ) return nil end r[ col ] end
Returns true of matrix is empty.
- Returns
-
true or false
# File lib/bio/db/nexus.rb, line 1656 def is_empty? return get_max_col < 0 || get_max_row < 0 end
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, line 1597 def set_value( row, col, value ) if ( ( row < 0 ) || ( col < 0 ) ) raise( NexusTableError, "attempt to use negative values for row or column" ) end if ( row > get_max_row() ) set_max_row( row ) end if ( col > get_max_col() ) set_max_col( col ) end row_map = nil if ( @rows.has_key?( row ) ) row_map = @rows[ row ] else row_map = Hash.new @rows[ row ] = row_map end row_map[ col ] = value end
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, line 1733 def to_nexus_row_array( spacer = "", append_delimiter = true ) ary = Array.new if is_empty? return ary end max_length = 10 for row in 0 .. get_max_row l = get_value( row, 0 ).length if ( l > max_length ) max_length = l end end for row in 0 .. get_max_row row_str = String.new ary.push( row_str ) name = get_value( row, 0 ) name = name.ljust( max_length + 1 ) row_str << name << " " << get_row_string( row, spacer ) if ( spacer != nil && spacer.length > 0 ) row_str.chomp!( spacer ) end if ( append_delimiter && row == get_max_row ) row_str << DELIMITER end end ary end
Returns matrix as String, returns “empty” if empty.
- Returns
# File lib/bio/db/nexus.rb, line 1714 def to_s if is_empty? return "empty" end str = String.new row_array = to_nexus_row_array( spacer = " ", false ) row_array.each do | row | str << row << END_OF_LINE end str end
Private Instance Methods
Returns row data as Array.
Arguments:
-
(required) row: Integer
- Returns
-
Array
# File lib/bio/db/nexus.rb, line 1769 def get_row( row ) return @rows[ row ] end
Sets maximal column number.
Arguments:
-
(required) max_col: Integer
# File lib/bio/db/nexus.rb, line 1777 def set_max_col( max_col ) @max_col = max_col end
Sets maximal row number.
Arguments:
-
(required) max_row: Integer
# File lib/bio/db/nexus.rb, line 1785 def set_max_row( max_row ) @max_row = max_row end