Class: Bio::NeXML::Characters

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

Overview

A character state matrix. This class is analogous to the characters element of NeXML.

Direct Known Subclasses

Continuous, Dna, Protein, Restriction, Rna, Standard

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mapper

#properties

Constructor Details

#initialize(id, options = {}) ⇒ Characters

Returns a new instance of Characters.



821
822
823
824
825
826
827
# File 'lib/bio/db/nexml/matrix.rb', line 821

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

Instance Attribute Details

#formatObject

A characters block holds a single format definition



808
809
810
# File 'lib/bio/db/nexml/matrix.rb', line 808

def format
  @format
end

#idObject

An id should be uniquely scoped in an NeXML file. It need not be unique globally. It is a compulsory attribute.



805
806
807
# File 'lib/bio/db/nexml/matrix.rb', line 805

def id
  @id
end

#labelObject

A human readable description. Its usage is optional.



814
815
816
# File 'lib/bio/db/nexml/matrix.rb', line 814

def label
  @label
end

#matrixObject

A characters block holds a single matrix definition



811
812
813
# File 'lib/bio/db/nexml/matrix.rb', line 811

def matrix
  @matrix
end

Instance Method Details

#add_format(format) ⇒ Object



829
830
831
# File 'lib/bio/db/nexml/matrix.rb', line 829

def add_format( format )
  @format = format
end

#add_matrix(matrix) ⇒ Object



833
834
835
# File 'lib/bio/db/nexml/matrix.rb', line 833

def add_matrix( matrix )
  @matrix = matrix
end

#create_format(options = {}) ⇒ Object



855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'lib/bio/db/nexml/matrix.rb', line 855

def create_format( options = {} )
  format = Format.new( options )
  states = format.create_states
  lookup_table = self.lookup
  state_for_symbol = {}
  lookup_table.keys.each do |key|
    if lookup_table[key].length == 1
      state = states.create_state( :symbol => key )
      state_for_symbol[key] = state
    end
  end
  lookup_table.keys.each do |key|
    if lookup_table[key].length != 1
      state = states.create_state( :symbol => key, :ambiguity => :uncertain )
      lookup_table[key].each do |symbol|
        state.add_member( state_for_symbol[symbol] )
      end
    end
  end        
  add_format format
  format
end

#create_matrix(options = {}) ⇒ Object



844
845
846
847
848
849
850
851
852
853
# File 'lib/bio/db/nexml/matrix.rb', line 844

def create_matrix( options = {} )
  matrix = nil
  if self.class.name =~ /Seqs$/
    matrix = SeqMatrix.new( options )
  else
    matrix = CellMatrix.new( options )
  end        
  add_matrix matrix
  matrix
end

#create_raw(string, row = nil) ⇒ Object



878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# File 'lib/bio/db/nexml/matrix.rb', line 878

def create_raw( string, row = nil )
  matrix = self.matrix
  format = self.format
  if row == nil
    row = matrix.create_row
  end        
  if row.kind_of? SeqRow
    sequence = Sequence.new
    sequence.value = join_sequence split_sequence string          
    row.add_sequence( sequence )
  end
  if row.kind_of? CellRow
    split_seq = split_sequence string
    pos = 0
    states = format.states.first
    split_seq.each do |symbol|
      char = format.chars[pos]
      if char == nil
        char = format.create_char( states )
      end
      state = states.get_state_by_symbol( symbol )
      if state == nil
        state = states.create_state( symbol )
      end
      cell = Cell.new char, state
      row.add_cell cell
      pos += 1
    end
  end
  row
end

#join_sequence(array) ⇒ Object



914
915
916
# File 'lib/bio/db/nexml/matrix.rb', line 914

def join_sequence( array )
  array.join
end

#split_sequence(string) ⇒ Object



910
911
912
# File 'lib/bio/db/nexml/matrix.rb', line 910

def split_sequence( string )
  string.split(//)
end

#to_xmlObject



837
838
839
840
841
842
# File 'lib/bio/db/nexml/matrix.rb', line 837

def to_xml
  node = @@writer.create_node( "characters", @@writer.attributes( self, :id, :"xsi:type", :otus, :label ) )
  node << self.format.to_xml
  node << self.matrix.to_xml
  node
end