Class: Bio::Nexus::CharactersBlock

Inherits:
GenericBlock show all
Defined in:
lib/bio/db/nexus.rb

Overview

DESCRIPTION

Bio::Nexus::CharactersBlock represents a characters nexus block.

Example of Characters block:

Begin Characters;

Dimensions NChar=20
           NTax=4;
Format DataType=DNA
Missing=x
Gap=- MatchChar=.;
Matrix
 fish  ACATA GAGGG TACCT CTAAG
 frog  ACTTA GAGGC TACCT CTAGC
 snake ACTCA CTGGG TACCT TTGCG
 mouse ACTCA GACGG TACCT TTGCG;

End;

USAGE

require 'bio/db/nexus'

# Create a new parser:
nexus = Bio::Nexus.new( nexus_data_as_string )

# Get first characters block (same methods as Nexus::DataBlock except
# it lacks get_taxa method):   
characters_block = nexus.get_characters_blocks[ 0 ]

Direct Known Subclasses

DataBlock

Constant Summary collapse

MISSING =
"Missing"
GAP =
"Gap"
MATCHCHAR =
"MatchChar"

Instance Method Summary collapse

Methods inherited from GenericBlock

#add_token, #get_name, #get_tokens, #to_s

Constructor Details

#initialize(name) ⇒ CharactersBlock

Creates a new CharactersBlock object named ‘name’.


Arguments:

  • (required) name: String



951
952
953
954
955
956
957
958
959
960
# File 'lib/bio/db/nexus.rb', line 951

def initialize( name )
  super( name )
  @number_of_taxa = 0
  @number_of_characters = 0
  @data_type = String.new
  @gap_character = String.new
  @missing = String.new
  @match_character = String.new
  @matrix = NexusMatrix.new
end

Instance Method Details

#get_characters_string(row) ⇒ Object

Returns character data as String for matrix row ‘row’.


Arguments:

  • (required) row: Integer

Returns

String



1106
1107
1108
# File 'lib/bio/db/nexus.rb', line 1106

def get_characters_string( row )
  get_matrix.get_row_string( row, "" )
end

#get_characters_strings_by_name(name) ⇒ Object

Returns character data as String Array for matrix rows named ‘name’.


Arguments:

  • (required) name: String

Returns

Array of Strings



1095
1096
1097
# File 'lib/bio/db/nexus.rb', line 1095

def get_characters_strings_by_name( name )
  get_matrix.get_row_strings_by_name( name, "" )
end

#get_datatypeObject

Gets the “datatype” property.


Returns

String



1018
1019
1020
# File 'lib/bio/db/nexus.rb', line 1018

def get_datatype
  @data_type
end

#get_gap_characterObject

Gets the “gap character” property.


Returns

String



1025
1026
1027
# File 'lib/bio/db/nexus.rb', line 1025

def get_gap_character
  @gap_character
end

#get_match_characterObject

Gets the “match character” property.


Returns

String



1039
1040
1041
# File 'lib/bio/db/nexus.rb', line 1039

def get_match_character 
  @match_character 
end

#get_matrixObject

Gets the matrix.


Returns

Bio::Nexus::NexusMatrix



1046
1047
1048
# File 'lib/bio/db/nexus.rb', line 1046

def get_matrix
  @matrix 
end

#get_missingObject

Gets the “missing” property.


Returns

String



1032
1033
1034
# File 'lib/bio/db/nexus.rb', line 1032

def get_missing
  @missing
end

#get_number_of_charactersObject

Gets the “number of characters” property.


Returns

Integer



1011
1012
1013
# File 'lib/bio/db/nexus.rb', line 1011

def get_number_of_characters
  @number_of_characters
end

#get_number_of_taxaObject

Gets the “number of taxa” property.


Returns

Integer



1003
1004
1005
# File 'lib/bio/db/nexus.rb', line 1003

def get_number_of_taxa
  @number_of_taxa
end

#get_row_name(row) ⇒ Object

Returns the String in the matrix at row ‘row’ and column 0, which usually is interpreted as a sequence name (if the matrix contains molecular sequence characters).


Arguments:

  • (required) row: Integer

Returns

String



1084
1085
1086
# File 'lib/bio/db/nexus.rb', line 1084

def get_row_name( row )
  get_matrix.get_name( row )
end

#get_sequence(row) ⇒ Object

Returns the characters in the matrix at row ‘row’ as Bio::Sequence object. Column 0 of the matrix is set as the definition of the Bio::Sequence object.


Arguments:

  • (required) row: Integer

Returns

Bio::Sequence



1072
1073
1074
# File 'lib/bio/db/nexus.rb', line 1072

def get_sequence( row )
  create_sequence( get_characters_string( row ), get_row_name( row )  )
end

#get_sequences_by_name(name) ⇒ Object

Returns character data as Bio::Sequence object Array for matrix rows named ‘name’.


Arguments:

  • (required) name: String

Returns

Bio::Sequence



1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/bio/db/nexus.rb', line 1056

def get_sequences_by_name( name )
  seq_strs = get_characters_strings_by_name( name )
  seqs = Array.new
  seq_strs.each do | seq_str |
    seqs.push( create_sequence( seq_str, name ) )
  end
  seqs
end

#set_datatype(data_type) ⇒ Object

Sets the “data type” property.


Arguments:

  • (required) data_type: String



1130
1131
1132
# File 'lib/bio/db/nexus.rb', line 1130

def set_datatype( data_type )
  @data_type = data_type
end

#set_gap_character(gap_character) ⇒ Object

Sets the “gap character” property.


Arguments:

  • (required) gap_character: String



1138
1139
1140
# File 'lib/bio/db/nexus.rb', line 1138

def set_gap_character( gap_character )
  @gap_character = gap_character
end

#set_match_character(match_character) ⇒ Object

Sets the “match character” property.


Arguments:

  • (required) match_character: String



1154
1155
1156
# File 'lib/bio/db/nexus.rb', line 1154

def set_match_character( match_character )
  @match_character = match_character
end

#set_matrix(matrix) ⇒ Object

Sets the matrix.


Arguments:

  • (required) matrix: Bio::Nexus::NexusMatrix



1162
1163
1164
# File 'lib/bio/db/nexus.rb', line 1162

def set_matrix( matrix )
  @matrix = matrix
end

#set_missing(missing) ⇒ Object

Sets the “missing” property.


Arguments:

  • (required) missing: String



1146
1147
1148
# File 'lib/bio/db/nexus.rb', line 1146

def set_missing( missing )
  @missing = missing
end

#set_number_of_characters(number_of_characters) ⇒ Object

Sets the “number of characters” property.


Arguments:

  • (required) number_of_characters: Integer



1122
1123
1124
# File 'lib/bio/db/nexus.rb', line 1122

def set_number_of_characters( number_of_characters )
  @number_of_characters = number_of_characters
end

#set_number_of_taxa(number_of_taxa) ⇒ Object

Sets the “number of taxa” property.


Arguments:

  • (required) number_of_taxa: Integer



1114
1115
1116
# File 'lib/bio/db/nexus.rb', line 1114

def set_number_of_taxa( number_of_taxa )
  @number_of_taxa = number_of_taxa
end

#to_nexusObject

Returns a String describing this block as nexus formatted data.


Returns

String



966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
# File 'lib/bio/db/nexus.rb', line 966

def to_nexus
  line_1 = String.new
  line_1 << DIMENSIONS  
  if ( Nexus::Util::larger_than_zero( get_number_of_taxa ) )
    line_1 << " " <<  NTAX << "=" << get_number_of_taxa
  end
  if ( Nexus::Util::larger_than_zero( get_number_of_characters ) )
    line_1 << " " <<  NCHAR << "=" << get_number_of_characters
  end
  line_1 << DELIMITER
  
  line_2 = String.new
  line_2 << FORMAT  
  if ( Nexus::Util::longer_than_zero( get_datatype ) )
    line_2 << " " <<  DATATYPE << "=" << get_datatype
  end
  if ( Nexus::Util::longer_than_zero( get_missing ) )
    line_2 << " " <<  MISSING << "=" << get_missing
  end
  if ( Nexus::Util::longer_than_zero( get_gap_character ) )
    line_2 << " " <<  GAP << "=" << get_gap_character
  end
  if ( Nexus::Util::longer_than_zero( get_match_character ) )
    line_2 << " " <<  MATCHCHAR << "=" << get_match_character
  end
  line_2 << DELIMITER
  
  line_3 = String.new
  line_3 << MATRIX 
  Nexus::Util::to_nexus_helper( CHARACTERS_BLOCK, [ line_1, line_2, line_3 ] +
                                get_matrix.to_nexus_row_array  )
end