Class: Bio::Nexus::NexusMatrix

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

Overview

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", "+" )

Defined Under Namespace

Classes: NexusMatrixError

Instance Method Summary collapse

Constructor Details

#initializeNexusMatrix

Creates new NexusMatrix.


1586
1587
1588
1589
1590
# File 'lib/bio/db/nexus.rb', line 1586

def initialize()
  @rows = Hash.new
  @max_row = -1
  @max_col = -1
end

Instance Method Details

#get_max_colObject

Returns the maximal columns number.


Returns

Integer


1642
1643
1644
# File 'lib/bio/db/nexus.rb', line 1642

def get_max_col 
  return @max_col
end

#get_max_rowObject

Returns the maximal row number.


Returns

Integer


1649
1650
1651
# File 'lib/bio/db/nexus.rb', line 1649

def get_max_row 
  return @max_row
end

#get_name(row) ⇒ Object

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


Arguments:

  • (required) row: Integer

Returns

String


1668
1669
1670
# File 'lib/bio/db/nexus.rb', line 1668

def get_name( row )
  get_value( row, 0 ).to_s
end

#get_row_string(row, spacer = "") ⇒ Object

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


1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
# 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 = "") ⇒ Object

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


1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
# 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) ⇒ Object

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


Arguments:

  • (required) row: Integer

  • (required) col: Integer

Returns

Object


1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
# 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?Boolean

Returns true of matrix is empty.


Returns

true or false


1657
1658
1659
# 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) ⇒ Object

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


Arguments:

  • (required) row: Integer

  • (required) col: Integer

  • (required) value: Object


1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
# 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) ⇒ Object

Helper method to produce nexus formatted data.


Arguments:

  • (optional) spacer: String

  • (optional) append_delimiter: true or false

Returns

Array


1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
# 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_sObject Also known as: to_str

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


Returns

String


1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
# 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