class MiGA::Taxonomy

Taxonomic classifications in MiGA.



Taxonomic hierarchy Hash.

Public Class Methods

KNOWN_RANKS() click to toggle source

Cannonical ranks.

# File lib/miga/taxonomy.rb, line 11
def self.KNOWN_RANKS() @@KNOWN_RANKS ; end
LONG_RANKS() click to toggle source

Long names of the cannonical ranks.

# File lib/miga/taxonomy.rb, line 17
def self.LONG_RANKS() @@LONG_RANKS ; end
json_create(o) click to toggle source

Initialize from JSON-derived Hash o.

# File lib/miga/taxonomy.rb, line 41
def self.json_create(o) new(o['str']) ; end
new(str, ranks = nil) click to toggle source

Create MiGA::Taxonomy from String or Array str. The string is a series of space-delimited entries, the array is a vector of entries. Each entry can be either a rank:value pair (if ranks is nil), or just values in the same order as ther ranks in ranks. Alternatively, str as a Hash with rank => value pairs is also supported.

# File lib/miga/taxonomy.rb, line 67
def initialize(str, ranks = nil)
  @ranks = {}
  if ranks.nil?
    case str when Array, Hash
      self << str
      "#{str} ".scan(/([A-Za-z]+):([^:]*)( )/){ |r,n,_| self << {r=>n} }
    ranks = ranks.split(/\s+/) unless ranks.is_a? Array
    str = str.split(/\s/) unless str.is_a? Array
    raise "Unequal number of ranks (#{ranks.size}) " +
      "and names (#{str.size}):#{ranks} => #{str}" unless
    (0 .. str.size).each{ |i| self << "#{ranks[i]}:#{str[i]}" }
normalize_rank(rank) click to toggle source

Returns cannonical rank (Symbol) for the rank String.

# File lib/miga/taxonomy.rb, line 45
def self.normalize_rank(rank)
  return rank.to_sym if @@_KNOWN_RANKS_H[rank.to_sym]
  rank = rank.to_s.downcase
  return nil if rank == 'no rank'
  rank = @@RANK_SYNONYMS[rank] unless @@RANK_SYNONYMS[rank].nil?
  rank = rank.to_sym
  return nil unless @@_KNOWN_RANKS_H[rank]

Public Instance Methods

<<(value) click to toggle source

Add value to the hierarchy, that can be an Array, a String, or a Hash, as described in initialize.

# File lib/miga/taxonomy.rb, line 88
def <<(value)
  if value.is_a? Hash
    value.each_pair do |rank_i, name_i|
      next if name_i.nil? or name_i == ""
      @ranks[ Taxonomy.normalize_rank rank_i ] ="_"," ")
  elsif value.is_a? Array
    value.each{ |v| self << v }
  elsif value.is_a? String
    (rank, name) = value.split(/:/)
    self << { rank => name }
    raise "Unsupported class: #{}."
[](rank) click to toggle source

Get rank value.

# File lib/miga/taxonomy.rb, line 106
def [](rank) @ranks[ rank.to_sym ] ; end
highest() click to toggle source

Get the most general rank as a two-entry Array (rank and value).

# File lib/miga/taxonomy.rb, line 127
def highest; sorted_ranks.first ; end
is_in?(taxon) click to toggle source

Evaluates if the loaded taxonomy includes taxon. It assumes that taxon only has one informative rank. The evaluation is case-insensitive.

# File lib/miga/taxonomy.rb, line 111
def is_in? taxon
  r = taxon.ranks.keys.first
  return false if self[ r ].nil?
  self[ r ].downcase == taxon[ r ].downcase
lowest() click to toggle source

Get the most specific rank as a two-entry Array (rank and value).

# File lib/miga/taxonomy.rb, line 131
def lowest; sorted_ranks.last ; end
sorted_ranks() click to toggle source

Sorted list of ranks, as an Array of two-entry Arrays (rank and value).

# File lib/miga/taxonomy.rb, line 119
def sorted_ranks do |r|
    ranks[r].nil? ? nil : [r, ranks[r]]
to_json(*a) click to toggle source

Generate JSON-formated String representing the taxonomy.

# File lib/miga/taxonomy.rb, line 141
def to_json(*a)
  { JSON.create_id =>, "str" => self.to_s }.to_json(*a)
to_s() click to toggle source

Generate cannonical String for the taxonomy.

# File lib/miga/taxonomy.rb, line 135
def to_s{ |r| "#{r[0]}:#{r[1].gsub(/[\s:]/,"_")}" }.join(" ")