class MiGA::TaxIndex

Indexing methods based on taxonomy.

Attributes

datasets[R]

Datasets in the index.

root[R]

Taxonomy root.

Public Class Methods

new() click to toggle source

Initialize an empty MiGA::TaxIndex

# File lib/miga/tax_index.rb, line 19
def initialize
  @root = MiGA::TaxIndexTaxon.new(:root, 'biota')
  @datasets = []
end

Public Instance Methods

<<(dataset) click to toggle source

Index dataset, a MiGA::Dataset object.

# File lib/miga/tax_index.rb, line 26
def <<(dataset)
  return nil if dataset.metadata[:tax].nil?

  taxon = @root
  MiGA::Taxonomy.KNOWN_RANKS.each do |rank|
    next if rank == :ns

    taxon = taxon.add_child(rank, dataset.metadata[:tax][rank])
  end
  taxon.add_dataset dataset
  @datasets << dataset
end
taxa_by_rank(rank) click to toggle source

Finds all the taxa in the collection at the rank taxonomic rank

# File lib/miga/tax_index.rb, line 41
def taxa_by_rank(rank)
  rank = MiGA::Taxonomy.normalize_rank(rank)
  taxa = [@root]
  select = []
  loop do
    new_taxa = []
    taxa.map(&:children).flatten.each do |ch|
      if ch.rank == rank
        select << ch
      elsif !ch.children.empty?
        new_taxa << ch
      end
    end
    taxa = new_taxa
    break if taxa.empty?
  end
  select
end
to_json() click to toggle source

Generate JSON String for the index.

# File lib/miga/tax_index.rb, line 62
def to_json
  MiGA::Json.generate(
    root: root.to_hash,
    datasets: datasets.map(&:name)
  )
end
to_tab(unknown = false) click to toggle source

Generate tabular String for the index.

# File lib/miga/tax_index.rb, line 71
def to_tab(unknown = false)
  root.to_tab(unknown)
end