Class: MiGA::Taxonomy

Inherits:
MiGA
  • Object
show all
Defined in:
lib/miga/taxonomy.rb

Overview

Taxonomic classifications in MiGA.

Constant Summary collapse

@@KNOWN_RANKS =
%w{ns d k p c o f g s ssp str ds}.map{|r| r.to_sym}
@@LONG_RANKS =
{root: "root", ns: "namespace", d: "domain", k: "kingdom",
p: "phylum", c: "class", o: "order", f: "family", g: "genus", s: "species",
ssp: "subspecies", str: "strain", ds: "dataset"}
@@RANK_SYNONYMS =

Synonms for cannonical ranks.

{
  "namespace"=>"ns",
  "domain"=>"d","superkingdom"=>"d",
  "kingdom"=>"k",
  "phylum"=>"p",
  "class"=>"c",
  "order"=>"o",
  "family"=>"f",
  "genus"=>"g",
  "species"=>"s","sp"=>"s",
  "subspecies"=>"ssp",
  "strain"=>"str","isolate"=>"str","culture"=>"str",
  "dataset"=>"ds","organism"=>"ds","genome"=>"ds","specimen"=>"ds"
}

Constants included from MiGA

CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiGA

CITATION, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, initialized?, #result_files_exist?, root_path, tabulate

Constructor Details

#initialize(str, ranks = nil) ⇒ Taxonomy

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.


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/miga/taxonomy.rb', line 65

def initialize(str, ranks=nil)
  @ranks = {}
  if ranks.nil?
    case str when Array, Hash
      self << str
    else
      "#{str} ".scan(/([A-Za-z]+):([^:]*)( )/){ |r,n,_| self << {r=>n} }
    end
  else
    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
      ranks.size==str.size
    (0 .. str.size).each{ |i| self << "#{ranks[i]}:#{str[i]}" }
  end
end

Instance Attribute Details

#ranksObject (readonly)

Taxonomic hierarchy Hash.


57
58
59
# File 'lib/miga/taxonomy.rb', line 57

def ranks
  @ranks
end

Class Method Details

.json_create(o) ⇒ Object

Initialize from JSON-derived Hash o.


40
# File 'lib/miga/taxonomy.rb', line 40

def self.json_create(o) new(o["str"]) ; end

.KNOWN_RANKSObject

Cannonical ranks.


11
# File 'lib/miga/taxonomy.rb', line 11

def self.KNOWN_RANKS() @@KNOWN_RANKS ; end

.LONG_RANKSObject

Long names of the cannonical ranks.


16
# File 'lib/miga/taxonomy.rb', line 16

def self.LONG_RANKS() @@LONG_RANKS ; end

.normalize_rank(rank) ⇒ Object

Returns cannonical rank (Symbol) for the rank String.


44
45
46
47
48
49
50
51
# File 'lib/miga/taxonomy.rb', line 44

def self.normalize_rank(rank)
  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.include? rank
  rank
end

Instance Method Details

#<<(value) ⇒ Object

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


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/miga/taxonomy.rb', line 86

def <<(value)
  if value.is_a? Array
    value.each{ |v| self << v }
  elsif value.is_a? String
    (rank, name) = value.split(/:/)
    self << { rank => name }
  elsif 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 ] = name_i.tr("_"," ")
    end
  else
    raise "Unsupported class: #{value.class.name}."
  end
end

#[](rank) ⇒ Object

Get rank value.


104
# File 'lib/miga/taxonomy.rb', line 104

def [](rank) @ranks[ rank.to_sym ] ; end

#highestObject

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


125
# File 'lib/miga/taxonomy.rb', line 125

def highest; sorted_ranks.first ; end

#is_in?(taxon) ⇒ Boolean

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

Returns:

  • (Boolean)

109
110
111
112
113
# File 'lib/miga/taxonomy.rb', line 109

def is_in? taxon
  r = taxon.ranks.keys.first
  return false if self[ r ].nil?
  self[ r ].downcase == taxon[ r ].downcase
end

#lowestObject

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


129
# File 'lib/miga/taxonomy.rb', line 129

def lowest; sorted_ranks.last ; end

#sorted_ranksObject

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


117
118
119
120
121
# File 'lib/miga/taxonomy.rb', line 117

def sorted_ranks
  @@KNOWN_RANKS.map do |r|
    ranks[r].nil? ? nil : [r, ranks[r]]
  end.compact
end

#to_json(*a) ⇒ Object

Generate JSON-formated String representing the taxonomy.


139
140
141
# File 'lib/miga/taxonomy.rb', line 139

def to_json(*a)
  { JSON.create_id => self.class.name, "str" => self.to_s }.to_json(*a)
end

#to_sObject

Generate cannonical String for the taxonomy.


133
134
135
# File 'lib/miga/taxonomy.rb', line 133

def to_s
  sorted_ranks.map{ |r| "#{r[0]}:#{r[1].gsub(/\s/,"_")}" }.join(" ")
end