Class: MiGA::Taxonomy
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}
- @@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
-
#ranks ⇒ Object
readonly
Taxonomic hierarchy Hash.
Class Method Summary collapse
-
.json_create(o) ⇒ Object
Initialize from JSON-derived Hash
o
. -
.KNOWN_RANKS ⇒ Object
Cannonical ranks.
-
.normalize_rank(rank) ⇒ Object
Returns cannonical rank (Symbol) for the
rank
String.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Add
value
to the hierarchy, that can be an Array, a String, or a Hash, as described in #initialize. -
#[](rank) ⇒ Object
Get
rank
value. -
#highest ⇒ Object
Get the most general rank as a two-entry Array (rank and value).
-
#initialize(str, ranks = nil) ⇒ Taxonomy
constructor
Create MiGA::Taxonomy from String or Array
str
. -
#is_in?(taxon) ⇒ Boolean
Evaluates if the loaded taxonomy includes
taxon
. -
#lowest ⇒ Object
Get the most specific rank as a two-entry Array (rank and value).
-
#sorted_ranks ⇒ Object
Sorted list of ranks, as an Array of two-entry Arrays (rank and value).
-
#to_json(*a) ⇒ Object
Generate JSON-formated String representing the taxonomy.
-
#to_s ⇒ Object
Generate cannonical String for the taxonomy.
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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/miga/taxonomy.rb', line 58 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
#ranks ⇒ Object (readonly)
Taxonomic hierarchy Hash.
50 51 52 |
# File 'lib/miga/taxonomy.rb', line 50 def ranks @ranks end |
Class Method Details
.json_create(o) ⇒ Object
Initialize from JSON-derived Hash o
.
33 |
# File 'lib/miga/taxonomy.rb', line 33 def self.json_create(o) new(o["str"]) ; end |
.KNOWN_RANKS ⇒ Object
Cannonical ranks.
11 |
# File 'lib/miga/taxonomy.rb', line 11 def self.KNOWN_RANKS() @@KNOWN_RANKS ; end |
.normalize_rank(rank) ⇒ Object
Returns cannonical rank (Symbol) for the rank
String.
37 38 39 40 41 42 43 44 |
# File 'lib/miga/taxonomy.rb', line 37 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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/miga/taxonomy.rb', line 79 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.
97 |
# File 'lib/miga/taxonomy.rb', line 97 def [](rank) @ranks[ rank.to_sym ] ; end |
#highest ⇒ Object
Get the most general rank as a two-entry Array (rank and value).
118 |
# File 'lib/miga/taxonomy.rb', line 118 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.
102 103 104 105 106 |
# File 'lib/miga/taxonomy.rb', line 102 def is_in? taxon r = taxon.ranks.keys.first return false if self[ r ].nil? self[ r ].downcase == taxon[ r ].downcase end |
#lowest ⇒ Object
Get the most specific rank as a two-entry Array (rank and value).
122 |
# File 'lib/miga/taxonomy.rb', line 122 def lowest; sorted_ranks.last ; end |
#sorted_ranks ⇒ Object
Sorted list of ranks, as an Array of two-entry Arrays (rank and value).
110 111 112 113 114 |
# File 'lib/miga/taxonomy.rb', line 110 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.
132 133 134 |
# File 'lib/miga/taxonomy.rb', line 132 def to_json(*a) { JSON.create_id => self.class.name, "str" => self.to_s }.to_json(*a) end |
#to_s ⇒ Object
Generate cannonical String for the taxonomy.
126 127 128 |
# File 'lib/miga/taxonomy.rb', line 126 def to_s sorted_ranks.map{ |r| "#{r[0]}:#{r[1].gsub(/\s/,"_")}" }.join(" ") end |