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}
@@_KNOWN_RANKS_H =
Hash[ @@KNOWN_RANKS.map{ |i| [i,true]
@@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, clean_fasta_file, initialized?, #result_files_exist?, root_path, script_path, seqs_length, 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.



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

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.



59
60
61
# File 'lib/miga/taxonomy.rb', line 59

def ranks
  @ranks
end

Class Method Details

.json_create(o) ⇒ Object

Initialize from JSON-derived Hash o.



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

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.



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

def self.LONG_RANKS() @@LONG_RANKS ; end

.normalize_rank(rank) ⇒ Object

Returns cannonical rank (Symbol) for the rank String.



45
46
47
48
49
50
51
52
53
# 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]
  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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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 ] = name_i.tr("_"," ")
    end
  elsif value.is_a? Array
    value.each{ |v| self << v }
  elsif value.is_a? String
    (rank, name) = value.split(/:/)
    self << { rank => name }
  else
    raise "Unsupported class: #{value.class.name}."
  end
end

#[](rank) ⇒ Object

Get rank value.



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

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

#highestObject

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



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

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)


111
112
113
114
115
# 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
end

#lowestObject

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



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

def lowest; sorted_ranks.last ; end

#sorted_ranksObject

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



119
120
121
122
123
# File 'lib/miga/taxonomy.rb', line 119

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.



141
142
143
# File 'lib/miga/taxonomy.rb', line 141

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.



135
136
137
# File 'lib/miga/taxonomy.rb', line 135

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