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



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

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



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

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



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

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

#get_datatypeObject

Gets the “datatype” property.


Returns

String



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

def get_datatype
  @data_type
end

#get_gap_characterObject

Gets the “gap character” property.


Returns

String



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

def get_gap_character
  @gap_character
end

#get_match_characterObject

Gets the “match character” property.


Returns

String



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

def get_match_character 
  @match_character 
end

#get_matrixObject

Gets the matrix.


Returns

Bio::Nexus::NexusMatrix



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

def get_matrix
  @matrix 
end

#get_missingObject

Gets the “missing” property.


Returns

String



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

def get_missing
  @missing
end

#get_number_of_charactersObject

Gets the “number of characters” property.


Returns

Integer



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

def get_number_of_characters
  @number_of_characters
end

#get_number_of_taxaObject

Gets the “number of taxa” property.


Returns

Integer



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

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



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

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



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

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



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

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



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

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



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

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



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

def set_match_character( match_character )
  @match_character = match_character
end

#set_matrix(matrix) ⇒ Object

Sets the matrix.


Arguments:

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



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

def set_matrix( matrix )
  @matrix = matrix
end

#set_missing(missing) ⇒ Object

Sets the “missing” property.


Arguments:

  • (required) missing: String



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

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



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

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



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

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



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
998
# File 'lib/bio/db/nexus.rb', line 967

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