Class: Bio::NeXML::State

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

Overview

State defines a possible observation with its ‘symbol’ attribute. A state may be ambiguous. An ambiguous state must define an ambiguity mapping which may be ‘polymorphic’, resolved in an ‘and’ context, or uncertain, resolved in a ‘or’ context.

state = Bio::NeXML::State.new( 'state1', :label => 'A label' )
state.id            #=> 'state1'
state.label         #=> 'A label'
state.ambiguous?    #=> true
state.ambiguity     #=> :polymorphic

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mapper

#properties

Constructor Details

#initialize(id, symbol = nil, options = {}, &block) ⇒ State

Returns a new instance of State.



40
41
42
43
44
45
# File 'lib/bio/db/nexml/matrix.rb', line 40

def initialize( id, symbol = nil, options = {}, &block )
  @id = id
  symbol.is_a?( Hash ) ? options = symbol : self.symbol = symbol
  properties( options ) unless options.empty?
  block.arity < 1 ? instance_eval( &block ) : block.call( self ) if block_given?
end

Instance Attribute Details

#ambiguityObject

Polymorphic or uncertain.



24
25
26
# File 'lib/bio/db/nexml/matrix.rb', line 24

def ambiguity
  @ambiguity
end

#idObject

A file level unique identifier.



18
19
20
# File 'lib/bio/db/nexml/matrix.rb', line 18

def id
  @id
end

#labelObject

A human readable description of the state.



27
28
29
# File 'lib/bio/db/nexml/matrix.rb', line 27

def label
  @label
end

#symbolObject

Observation for this state.



21
22
23
# File 'lib/bio/db/nexml/matrix.rb', line 21

def symbol
  @symbol
end

Class Method Details

.polymorphic(id, symbol = nil, options = {}, &block) ⇒ Object



106
107
108
109
110
# File 'lib/bio/db/nexml/matrix.rb', line 106

def polymorphic( id, symbol = nil, options = {}, &block )
  state = new( id, symbol, options, &block )
  state.ambiguity = :polymorphic
  state
end

.uncertain(id, symbol = nil, options = {}, &block) ⇒ Object



112
113
114
115
116
# File 'lib/bio/db/nexml/matrix.rb', line 112

def uncertain( id, symbol = nil, options = {}, &block )
  state = new( id, symbol, options, &block )
  state.ambiguity = :uncertain
  state
end

Instance Method Details

#add_member(member) ⇒ Object

Takes a Bio::NeXML::State object and adds it to the ambiguity mapping of the state. Returns # self.



53
# File 'lib/bio/db/nexml/matrix.rb', line 53

def add_member( member ); end

#ambiguous?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/bio/db/nexml/matrix.rb', line 55

def ambiguous?
  !!ambiguity
end

#countObject Also known as: length



71
72
73
# File 'lib/bio/db/nexml/matrix.rb', line 71

def count
  number_of_members
end

#each(&block) ⇒ Object

Iterate over each member in self passing it to the block given. If no block is provided, it returns an Enumerator.



78
79
80
# File 'lib/bio/db/nexml/matrix.rb', line 78

def each( &block )
  @members.each( &block )
end

#include?(member) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/bio/db/nexml/matrix.rb', line 67

def include?( member )
  has_member?( member )
end

#polymorphic?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/bio/db/nexml/matrix.rb', line 59

def polymorphic?
  ambiguity == :polymorphic
end

#to_strObject Also known as: to_s



82
83
84
# File 'lib/bio/db/nexml/matrix.rb', line 82

def to_str
  symbol.to_s
end

#to_xmlObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bio/db/nexml/matrix.rb', line 87

def to_xml
  tagname = nil
  if ambiguity == :polymorphic
    tagname = "polymorphic_state_set"
  elsif ambiguity == :uncertain
    tagname = "uncertain_state_set"
  else
    tagname = "state"
  end
  node = @@writer.create_node( tagname, @@writer.attributes( self, :id, :label, :symbol ) )
  if count > 0
    self.each_member do |member|
      node << @@writer.create_node( "member", :state => member.id )
    end
  end
  node
end

#uncertain?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/bio/db/nexml/matrix.rb', line 63

def uncertain?
  ambiguity == :uncertain
end