Class: Bio::Nexus::DataBlock

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

Overview

DESCRIPTION

Bio::Nexus::DataBlock represents a data nexus block. A data block is a Bio::Nexus::CharactersBlock with the added capability to store taxa names.

Example of Data block:

Begin Data;

Dimensions ntax=5 nchar=14;
Format Datatype=RNA gap=# MISSING=x MatchChar=^;
TaxLabels ciona cow [comment] ape 'purple urchin' "green lizard";
Matrix
 taxon_1 A- CCGTCGA-GTTA
 taxon_2 T- CCG-CGA-GATA
 taxon_3 A- C-GTCGA-GATA
 taxon_4 A- CCTCGA--GTTA
 taxon_5 T- CGGTCGT-CTTA;

End;

USAGE

require 'bio/db/nexus'

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

# Get first data block:   
data_block = nexus.get_data_blocks[ 0 ]
# Get first characters name:
seq_name = data_block.get_row_name( 0 )
# Get first characters row named "taxon_2" as Bio::Sequence sequence:
seq_tax_2 = data_block.get_sequences_by_name( "taxon_2" )[ 0 ]
# Get third characters row as Bio::Sequence sequence:
seq_2 = data_block.get_sequence( 2 )
# Get first characters row named "taxon_3" as String:   
string_tax_3 = data_block.get_characters_strings_by_name( "taxon_3" )
# Get name of first taxon:
taxon_0 = data_block.get_taxa[ 0 ]
# Get characters matrix as Bio::Nexus::NexusMatrix (names are in column 0)
characters_matrix = data_block.get_matrix

Constant Summary

Constants inherited from CharactersBlock

CharactersBlock::GAP, CharactersBlock::MATCHCHAR, CharactersBlock::MISSING

Instance Method Summary collapse

Methods inherited from CharactersBlock

#get_characters_string, #get_characters_strings_by_name, #get_datatype, #get_gap_character, #get_match_character, #get_matrix, #get_missing, #get_number_of_characters, #get_number_of_taxa, #get_row_name, #get_sequence, #get_sequences_by_name, #set_datatype, #set_gap_character, #set_match_character, #set_matrix, #set_missing, #set_number_of_characters, #set_number_of_taxa

Methods inherited from GenericBlock

#add_token, #get_name, #get_tokens, #to_s

Constructor Details

#initialize(name) ⇒ DataBlock

Creates a new DataBlock object named ‘name’.


Arguments:

  • (required) name: String



1232
1233
1234
1235
# File 'lib/bio/db/nexus.rb', line 1232

def initialize( name )
  super( name )
  @taxa = Array.new
end

Instance Method Details

#add_taxon(taxon) ⇒ Object

Adds a taxon name to this block.


Arguments:

  • (required) taxon: String



1288
1289
1290
# File 'lib/bio/db/nexus.rb', line 1288

def add_taxon( taxon )
  @taxa.push( taxon )
end

#get_taxaObject

Gets the taxa of this block.


Returns

Array



1280
1281
1282
# File 'lib/bio/db/nexus.rb', line 1280

def get_taxa
  @taxa
end

#to_nexusObject

Returns a String describing this block as nexus formatted data.


Returns

String



1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
# File 'lib/bio/db/nexus.rb', line 1240

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 << TAXLABELS << " " << Nexus::Util::array_to_string( get_taxa )
  line_3 << DELIMITER
  
  line_4 = String.new
  line_4 << MATRIX 
  Nexus::Util::to_nexus_helper( DATA_BLOCK, [ line_1, line_2, line_3, line_4 ] +
                                get_matrix.to_nexus_row_array  )
end