Class: Bio::NeXML::Cell

Inherits:
Object
  • Object
show all
Includes:
Mapper
Defined in:
lib/bio/db/nexml/matrix.rb

Overview

Cell is the smallest unit of a character state matrix or of a sequence. A cell maybe bound or unbound. If a cell points to a char and has a state, it is a bound cell. Bound cells correspond to the cell tag of NeXML. Value of a bound cell is the same as the ‘symbol’ of the state it points to. Value of a bound cell may be changed by assigning a different state to it. An unbound cell holds a raw value.

cell = Bio::NeXML::Cell.new( 'A' )
cell.bound?           #=> false
cell.value            #=> 'A'

# Assign a new value to an unbound cell.
cell.value = 'B'
cell.value            #=> 'B'

cell = Bio::NeXML::Cell.new( :char => char1, :state => stateA )
cell.bound?           #=> true
cell.value            #=> 'A'

# Can not assign a value to a bound cell directly.
cell.value = 'B'
cell.value            #=> 'A'

# Changing the state of a bound cell changes its value.
cell.state = stateB
cell.value            #=> 'B'

Direct Known Subclasses

ContinuousCell

Constant Summary collapse

@@writer =
Bio::NeXML::Writer.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mapper

#properties

Constructor Details

#initialize(char = nil, state = nil, options = {}) ⇒ Cell

Returns a new instance of Cell.



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/bio/db/nexml/matrix.rb', line 436

def initialize( char = nil, state = nil, options = {} )
  case char
  when Hash
    properties( char )
  when Char
    self.char = char
  else
    @value = char unless char.nil?
  end

  case state
  when State
    self.state = state
  when Hash
    properties( state )
  end

  properties( options ) unless options.nil?
end

Instance Attribute Details

#charObject

Returns the value of attribute char.



426
427
428
# File 'lib/bio/db/nexml/matrix.rb', line 426

def char
  @char
end

#labelObject

Returns the value of attribute label.



428
429
430
# File 'lib/bio/db/nexml/matrix.rb', line 428

def label
  @label
end

#stateObject

Returns the value of attribute state.



427
428
429
# File 'lib/bio/db/nexml/matrix.rb', line 427

def state
  @state
end

Instance Method Details

#bound?Boolean

Returns:

  • (Boolean)


466
467
468
# File 'lib/bio/db/nexml/matrix.rb', line 466

def bound?
  !!( char and state )
end

#to_strObject Also known as: to_s

Allow cells to be implicitly used as a String.



471
472
473
# File 'lib/bio/db/nexml/matrix.rb', line 471

def to_str
  value.to_s
end

#to_xmlObject



476
477
478
# File 'lib/bio/db/nexml/matrix.rb', line 476

def to_xml
  @@writer.create_node( "cell", @@writer.attributes( self, :state, :char ) )
end

#valueObject Also known as: symbol

Return the value of a cell.



457
458
459
# File 'lib/bio/db/nexml/matrix.rb', line 457

def value
  bound? ? state.symbol : @value
end

#value=(value) ⇒ Object



462
463
464
# File 'lib/bio/db/nexml/matrix.rb', line 462

def value=( value )
  bound? ? nil : @value = value
end