Class: MiGA::Taxonomy
- Includes:
- Base
- Defined in:
- lib/miga/taxonomy.rb,
lib/miga/taxonomy/base.rb
Defined Under Namespace
Modules: Base
Constant Summary
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
- .LONG_RANKS ⇒ Object
-
.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
(also: #fetch)
Get
rank
value. -
#add_alternative(tax, replace = true) ⇒ Object
Add an alternative taxonomy.
-
#alternative(which = nil) ⇒ Object
Get the alternative taxonomies.
-
#delete_alternative ⇒ Object
Removes (and returns) all alternative taxonomies.
-
#highest(force_ranks = false) ⇒ Object
Get the most general rank as a two-entry Array (rank and value).
-
#in?(taxon) ⇒ Boolean
Evaluates if the loaded taxonomy includes
taxon
. -
#initialize(str, ranks = nil, alt = []) ⇒ Taxonomy
constructor
Create MiGA::Taxonomy from String or Array
str
. -
#lowest(force_ranks = false) ⇒ Object
Get the most specific rank as a two-entry Array (rank and value).
-
#namespace ⇒ Object
Namespace of the taxonomy (a String) or
nil
. -
#reset(str, ranks = nil) ⇒ Object
Reset ranks (including namespace) while leaving alternatives untouched.
-
#sorted_ranks(force_ranks = false, with_namespace = false) ⇒ 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(force_ranks = false) ⇒ Object
Generate cannonical String for the taxonomy.
Methods inherited from MiGA
CITATION, CITATION_ARRAY, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, #advance, debug?, debug_trace?, initialized?, #like_io?, #num_suffix, rc_path, #result_files_exist?, #say
Methods included from Common::Path
Methods included from Common::Format
#clean_fasta_file, #seqs_length, #tabulate
Methods included from Common::Net
#download_file_ftp, #known_hosts, #remote_connection
Methods included from Common::SystemCall
Constructor Details
#initialize(str, ranks = nil, alt = []) ⇒ 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. If alt
is passed, it must be an Array of String, Array, or Hash entries as defined above (except ranks
are not allowed).
23 24 25 26 |
# File 'lib/miga/taxonomy.rb', line 23 def initialize(str, ranks = nil, alt = []) reset(str, ranks) @alt = (alt || []).map { |i| Taxonomy.new(i) } end |
Instance Attribute Details
#ranks ⇒ Object (readonly)
Taxonomic hierarchy Hash.
13 14 15 |
# File 'lib/miga/taxonomy.rb', line 13 def ranks @ranks end |
Class Method Details
.json_create(o) ⇒ Object
Initialize from JSON-derived Hash o
23 24 25 |
# File 'lib/miga/taxonomy/base.rb', line 23 def json_create(o) new(o['str'], nil, o['alt']) end |
.KNOWN_RANKS ⇒ Object
27 28 29 |
# File 'lib/miga/taxonomy/base.rb', line 27 def KNOWN_RANKS() @@KNOWN_RANKS end |
.LONG_RANKS ⇒ Object
31 32 33 |
# File 'lib/miga/taxonomy/base.rb', line 31 def LONG_RANKS() @@LONG_RANKS end |
.normalize_rank(rank) ⇒ Object
Returns cannonical rank (Symbol) for the rank
String
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/miga/taxonomy/base.rb', line 8 def 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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/miga/taxonomy.rb', line 43 def <<(value) case value when Hash value.each do |r, n| next if n.nil? || n == '' @ranks[self.class.normalize_rank(r)] = n.tr('_', ' ') end when Array value.each { |v| self << v } when String self << Hash[*value.split(':', 2)] else raise 'Unsupported class: ' + value.class.name end end |
#[](rank) ⇒ Object Also known as: fetch
Get rank
value.
62 63 64 |
# File 'lib/miga/taxonomy.rb', line 62 def [](rank) @ranks[rank.to_sym] end |
#add_alternative(tax, replace = true) ⇒ Object
Add an alternative taxonomy. If the namespace matches an existing namespace, the alternative (or master) is replaced instead if replace
is true.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/miga/taxonomy.rb', line 91 def add_alternative(tax, replace = true) raise 'Unsupported taxonomy class.' unless tax.is_a? MiGA::Taxonomy alt_ns = alternative(tax.namespace) if !replace || tax.namespace.nil? || alt_ns.nil? @alt << tax else alt_ns.reset(tax.to_s) end end |
#alternative(which = nil) ⇒ Object
Get the alternative taxonomies.
-
If
which
is nil (default), returns all alternative taxonomies as Array (not including the master taxonomy). -
If
which
is Integer, returns the indexed taxonomy (starting with 0, the master taxonomy). -
Otherwise, returns the first taxonomy with namespace
which
(coerced as String), including the master taxonomy.
In the latter two cases it can be nil.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/miga/taxonomy.rb', line 77 def alternative(which = nil) case which when nil @alt when Integer ([self] + @alt)[which] else ([self] + @alt).find { |i| i.namespace.to_s == which.to_s } end end |
#delete_alternative ⇒ Object
Removes (and returns) all alternative taxonomies.
104 105 106 107 108 |
# File 'lib/miga/taxonomy.rb', line 104 def delete_alternative alt = @alt.dup @alt = [] alt end |
#highest(force_ranks = false) ⇒ Object
Get the most general rank as a two-entry Array (rank and value). If force_ranks
is true, it always returns the value for domain (d) even if undefined.
143 144 145 |
# File 'lib/miga/taxonomy.rb', line 143 def highest(force_ranks = false) sorted_ranks(force_ranks).first end |
#in?(taxon) ⇒ Boolean
Evaluates if the loaded taxonomy includes taxon
. It assumes that taxon
only has one informative rank. The evaluation is case-insensitive.
113 114 115 116 117 118 |
# File 'lib/miga/taxonomy.rb', line 113 def in?(taxon) r = taxon.ranks.keys.first return false if self[r].nil? self[r].casecmp(taxon[r]).zero? end |
#lowest(force_ranks = false) ⇒ Object
Get the most specific rank as a two-entry Array (rank and value). If force_ranks
is true, it always returns the value for dataset (ds) even if undefined.
151 152 153 |
# File 'lib/miga/taxonomy.rb', line 151 def lowest(force_ranks = false) sorted_ranks(force_ranks).last end |
#namespace ⇒ Object
Namespace of the taxonomy (a String) or nil
.
135 136 137 |
# File 'lib/miga/taxonomy.rb', line 135 def namespace self[:ns] end |
#reset(str, ranks = nil) ⇒ Object
Reset ranks (including namespace) while leaving alternatives untouched. See #initialize for str
and ranks
.
31 32 33 34 35 36 37 38 |
# File 'lib/miga/taxonomy.rb', line 31 def reset(str, ranks = nil) @ranks = {} if ranks.nil? initialize_by_str(str) else initialize_by_ranks(str, ranks) end end |
#sorted_ranks(force_ranks = false, with_namespace = false) ⇒ Object
Sorted list of ranks, as an Array of two-entry Arrays (rank and value). If force_ranks
is true, it returns all standard ranks even if undefined. If with_namespace
is true, it includes also the namespace.
124 125 126 127 128 129 130 131 |
# File 'lib/miga/taxonomy.rb', line 124 def sorted_ranks(force_ranks = false, with_namespace = false) @@KNOWN_RANKS.map do |r| next if (r == :ns && !with_namespace) || (ranks[r].nil? && !force_ranks) [r, ranks[r]] end.compact end |
#to_json(*a) ⇒ Object
Generate JSON-formated String representing the taxonomy.
165 166 167 168 169 |
# File 'lib/miga/taxonomy.rb', line 165 def to_json(*a) hsh = { JSON.create_id => self.class.name, 'str' => to_s } hsh['alt'] = alternative.map(&:to_s) unless alternative.empty? hsh.to_json(*a) end |
#to_s(force_ranks = false) ⇒ Object
Generate cannonical String for the taxonomy. If force_ranks
is true, it returns all the standard ranks even if undefined.
158 159 160 161 |
# File 'lib/miga/taxonomy.rb', line 158 def to_s(force_ranks = false) sorted_ranks(force_ranks, true) .map { |r| "#{r[0]}:#{(r[1] || '').gsub(/[\s:]/, '_')}" }.join(' ') end |