Class | Bio::Nexus::NexusMatrix
In: | lib/bio/db/nexus.rb
Parent: | Object

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

### Methods

get_max_col get_max_row get_name get_row_string get_row_strings_by_name get_value is_empty? new set_value to_nexus_row_array to_s to_str

### Classes and Modules

Class Bio::Nexus::NexusMatrix::NexusMatrixError

### Public Class methods

new()

Creates new NexusMatrix.

[Source]

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

### Public Instance methods

get_max_col()

Returns the maximal columns number.

Returns: | Integer

[Source]

          # File lib/bio/db/nexus.rb, line 1641
    1641:       def get_max_col
    1642:         return @max_col
    1643:       end

get_max_row()

Returns the maximal row number.

Returns: | Integer

[Source]

          # File lib/bio/db/nexus.rb, line 1648
    1648:       def get_max_row
    1649:         return @max_row
    1650:       end

get_name( row )

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

Arguments:

- (required) row: Integer

Returns: | String

[Source]

          # File lib/bio/db/nexus.rb, line 1667
    1667:       def get_name( row )
    1668:         get_value( row, 0 ).to_s
    1669:       end

get_row_string( row, spacer = "" )

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

[Source]

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

get_row_strings_by_name( name, spacer = "" )

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

[Source]

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

get_value( row, col )

Returns the value at row ‘row’ and column ‘col’.

Arguments:

- (required) row: Integer
- (required) col: Integer

Returns: | Object

[Source]

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

is_empty?()

Returns true of matrix is empty.

Returns: | true or false

[Source]

          # File lib/bio/db/nexus.rb, line 1656
    1656:       def is_empty?
    1657:         return get_max_col < 0 || get_max_row < 0
    1658:       end

set_value( row, col, value )

Sets the value at row ‘row’ and column ‘col’ to ‘value’.

Arguments:

- (required) row: Integer
- (required) col: Integer
- (required) value: Object

[Source]

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

to_nexus_row_array( spacer = "", append_delimiter = true )

Helper method to produce nexus formatted data.

Arguments:

- (optional) spacer: String
- (optional) append_delimiter: true or false

Returns: | Array

[Source]

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

to_s()

Returns matrix as String, returns "empty" if empty.

Returns: | String

[Source]

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

to_str()

Alias for to_s

[Validate]