Class: Bio::Nexus

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

Overview

DESCRIPTION

Bio::Nexus is a parser for nexus formatted data. It contains classes and constants enabling the representation and processing of nexus data.

USAGE

# Parsing a nexus formatted string str:
nexus = Bio::Nexus.new( nexus_str )

# Obtaining of the nexus blocks as array of GenericBlock or
# any of its subclasses (such as DistancesBlock):
blocks = nexus.get_blocks

# Getting a block by name:
my_blocks = nexus.get_blocks_by_name( "my_block" )

# Getting distance blocks:
distances_blocks = nexus.get_distances_blocks

# Getting trees blocks:
trees_blocks = nexus.get_trees_blocks

# Getting data blocks:
data_blocks = nexus.get_data_blocks

# Getting characters blocks: 
character_blocks = nexus.get_characters_blocks

# Getting taxa blocks: 
taxa_blocks = nexus.get_taxa_blocks

Defined Under Namespace

Classes: CharactersBlock, DataBlock, DistancesBlock, GenericBlock, NexusMatrix, NexusParseError, TaxaBlock, TreesBlock, Util

Constant Summary collapse

END_OF_LINE =
"\n"
INDENTENTION =
" "
DOUBLE_QUOTE =
"\""
SINGLE_QUOTE =
"'"
BEGIN_NEXUS =
"#NEXUS"
DELIMITER =
";"
BEGIN_BLOCK =
"Begin"
END_BLOCK =
"End" + DELIMITER
BEGIN_COMMENT =
"["
END_COMMENT =
"]"
TAXA =
"Taxa"
CHARACTERS =
"Characters"
DATA =
"Data"
DISTANCES =
"Distances"
TREES =
"Trees"
TAXA_BLOCK =
TAXA + DELIMITER
CHARACTERS_BLOCK =
CHARACTERS + DELIMITER
DATA_BLOCK =
DATA + DELIMITER
DISTANCES_BLOCK =
DISTANCES + DELIMITER
TREES_BLOCK =
TREES + DELIMITER
DIMENSIONS =
"Dimensions"
FORMAT =
"Format"
NTAX =
"NTax"
NCHAR =
"NChar"
DATATYPE =
"DataType"
TAXLABELS =
"TaxLabels"
MATRIX =
"Matrix"

Instance Method Summary collapse

Constructor Details

#initialize(nexus_str) ⇒ Nexus

Creates a new nexus parser for ‘nexus_str’.


Arguments:

  • (required) nexus_str: String - nexus formatted data



177
178
179
180
181
182
183
184
# File 'lib/bio/db/nexus.rb', line 177

def initialize( nexus_str )
  @blocks             = Array.new
  @current_cmd        = nil
  @current_subcmd     = nil
  @current_block_name = nil
  @current_block      = nil
  parse( nexus_str )
end

Instance Method Details

#get_blocksObject

Returns an Array of all blocks found in the String ‘nexus_str’ set via Bio::Nexus.new( nexus_str ).


Returns

Array of GenericBlocks or any of its subclasses



192
193
194
# File 'lib/bio/db/nexus.rb', line 192

def get_blocks
  @blocks
end

#get_blocks_by_name(name) ⇒ Object

A convenience methods which returns an array of all nexus blocks for which the name equals ‘name’ found in the String ‘nexus_str’ set via Bio::Nexus.new( nexus_str ).


Arguments:

  • (required) name: String

Returns

Array of GenericBlocks or any of its subclasses



204
205
206
207
208
209
210
211
212
# File 'lib/bio/db/nexus.rb', line 204

def get_blocks_by_name( name )
  found_blocks = Array.new
  @blocks.each do | block |
    if ( name == block.get_name )
      found_blocks.push( block )
    end
  end
  found_blocks
end

#get_characters_blocksObject

A convenience methods which returns an array of all characters blocks.


Returns

Array of CharactersBlocks



228
229
230
# File 'lib/bio/db/nexus.rb', line 228

def get_characters_blocks
  get_blocks_by_name( CHARACTERS_BLOCK.chomp( ";").downcase )
end

#get_data_blocksObject

A convenience methods which returns an array of all data blocks.


Returns

Array of DataBlocks



219
220
221
# File 'lib/bio/db/nexus.rb', line 219

def get_data_blocks
  get_blocks_by_name( DATA_BLOCK.chomp( ";").downcase )
end

#get_distances_blocksObject

A convenience methods which returns an array of all distances blocks.


Returns

Array of DistancesBlock



246
247
248
# File 'lib/bio/db/nexus.rb', line 246

def get_distances_blocks
  get_blocks_by_name( DISTANCES_BLOCK.chomp( ";").downcase )
end

#get_taxa_blocksObject

A convenience methods which returns an array of all taxa blocks.


Returns

Array of TaxaBlocks



255
256
257
# File 'lib/bio/db/nexus.rb', line 255

def get_taxa_blocks
  get_blocks_by_name( TAXA_BLOCK.chomp( ";").downcase )
end

#get_trees_blocksObject

A convenience methods which returns an array of all trees blocks.


Returns

Array of TreesBlocks



237
238
239
# File 'lib/bio/db/nexus.rb', line 237

def get_trees_blocks
  get_blocks_by_name( TREES_BLOCK.chomp( ";").downcase )
end

#to_sObject Also known as: to_str

Returns a String listing how many of each blocks it parsed.


Returns

String



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/bio/db/nexus.rb', line 263

def to_s
  str = String.new
  if get_blocks.length < 1
    str << "empty"
  else 
    str << "number of blocks: " << get_blocks.length.to_s
    if get_characters_blocks.length > 0
      str << " [characters blocks: " << get_characters_blocks.length.to_s << "] "
    end  
    if get_data_blocks.length > 0
      str << " [data blocks: " << get_data_blocks.length.to_s << "] "
    end
    if get_distances_blocks.length > 0
      str << " [distances blocks: " << get_distances_blocks.length.to_s << "] "
    end  
    if get_taxa_blocks.length > 0
      str << " [taxa blocks: " << get_taxa_blocks.length.to_s << "] "
    end    
    if get_trees_blocks.length > 0
      str << " [trees blocks: " << get_trees_blocks.length.to_s << "] "
    end        
  end
  str
end