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, line 1586
def initialize()
  @rows = Hash.new
  @max_row = -1
  @max_col = -1
end

Public Instance Methods

get_max_col() click to toggle source

Returns the maximal columns number.


Returns

Integer

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

Returns the maximal row number.


Returns

Integer

# File lib/bio/db/nexus.rb, line 1649
def get_max_row 
  return @max_row
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, line 1668
def get_name( row )
  get_value( row, 0 ).to_s
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, line 1681
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
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:

Returns

Array

# File lib/bio/db/nexus.rb, line 1699
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
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, line 1624
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
is_empty?() click to toggle source

Returns true of matrix is empty.


Returns

true or false

# File lib/bio/db/nexus.rb, line 1657
def is_empty?
  return get_max_col < 0 || get_max_row < 0
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, line 1598
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
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, line 1734
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
to_s() click to toggle source

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


Returns

String

# File lib/bio/db/nexus.rb, line 1715
def to_s
  if is_empty?
    return "empty"  
  end
  str = String.new
  row_array = to_nexus_row_array( " ", false )
  row_array.each do | row |
    str << row << END_OF_LINE
  end
  str
end
Also aliased as: to_str
to_str()
Alias for: to_s