Helper class for MiGA::TaxIndex.
Children of the taxon.
Datasets directly classified at the taxon (not at children).
Name of the taxon.
Rank of the taxon.
Initalize taxon at rank
with name
.
# File lib/miga/tax_index.rb, line 93 def initialize(rank, name) @rank = rank.to_sym @name = (name.nil? ? nil : name.miga_name) @children = [] @datasets = [] end
Add child at rank
with name
.
# File lib/miga/tax_index.rb, line 106 def add_child(rank, name) rank = rank.to_sym name = name.miga_name unless name.nil? child = children.find { |it| it.rank == rank and it.name == name } if child.nil? child = MiGA::TaxIndexTaxon.new(rank, name) @children << child end child end
Add dataset at the current taxon (not children).
# File lib/miga/tax_index.rb, line 119 def add_dataset(dataset) @datasets << dataset; end
Get all the datasets in the taxon (including children).
# File lib/miga/tax_index.rb, line 129 def all_datasets children.map(&:datasets).reduce(datasets, :+) end
Get the number of datasets in the taxon (including children).
# File lib/miga/tax_index.rb, line 123 def datasets_count children.map(&:datasets_count).reduce(datasets.size, :+) end
String representation of the taxon.
# File lib/miga/tax_index.rb, line 102 def tax_str; "#{rank}:#{name.nil? ? '?' : name}"; end
Hash representation of the taxon.
# File lib/miga/tax_index.rb, line 145 def to_hash { str: tax_str, datasets: datasets.map(&:name), children: children.map(&:to_hash) } end
JSON String of the taxon.
# File lib/miga/tax_index.rb, line 135 def to_json(*a) { str: tax_str, datasets: datasets.map(&:name), children: children }.to_json(a) end
Tabular String of the taxon.
# File lib/miga/tax_index.rb, line 155 def to_tab(unknown, indent = 0) o = '' if unknown or not datasets.empty? or not name.nil? o = "#{' ' * indent}#{tax_str}: #{datasets_count}\n" end indent += 2 datasets.each { |ds| o << "#{' ' * indent}# #{ds.name}\n" } children.each { |it| o << it.to_tab(unknown, indent) } o end