| Class | Bio::Nexus::NexusMatrix |
| In: |
lib/bio/db/nexus.rb
|
| Parent: | Object |
Bio::Nexus::NexusMatrix represents a characters or distance matrix, where the names are stored in column zero.
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", "+" )
Creates new NexusMatrix.
# 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
Returns the maximal columns number.
| Returns: | Integer |
# File lib/bio/db/nexus.rb, line 1641
1641: def get_max_col
1642: return @max_col
1643: end
Returns the maximal row number.
| Returns: | Integer |
# File lib/bio/db/nexus.rb, line 1648
1648: def get_max_row
1649: return @max_row
1650: 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:
| Returns: | String |
# 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
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
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
Returns the value at row ‘row’ and column ‘col’.
Arguments:
| Returns: | Object |
# 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
Returns true of matrix is empty.
| Returns: | true or false |
# File lib/bio/db/nexus.rb, line 1656
1656: def is_empty?
1657: return get_max_col < 0 || get_max_row < 0
1658: end
Sets the value at row ‘row’ and column ‘col’ to ‘value’.
Arguments:
# 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
Helper method to produce nexus formatted data.
Arguments:
| Returns: | Array |
# 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
Returns matrix as String, returns "empty" if empty.
| Returns: | String |
# 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