Class: MiGA::TaxIndex

Inherits:
MiGA
  • Object
show all
Defined in:
lib/miga/tax_index.rb

Overview

Indexing methods based on taxonomy.

Constant Summary

Constants included from MiGA

CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute 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, initialized?, #result_files_exist?, root_path, tabulate

Constructor Details

#initializeTaxIndex

Initialize an empty MiGA::TaxIndex


20
21
22
23
# File 'lib/miga/tax_index.rb', line 20

def initialize
  @root = MiGA::TaxIndexTaxon.new :root, "biota"
  @datasets = []
end

Instance Attribute Details

#datasetsObject (readonly)

Datasets in the index.


14
15
16
# File 'lib/miga/tax_index.rb', line 14

def datasets
  @datasets
end

#rootObject (readonly)

Taxonomy root.


16
17
18
# File 'lib/miga/tax_index.rb', line 16

def root
  @root
end

Instance Method Details

#<<(dataset) ⇒ Object

Index dataset, a MiGA::Dataset object.


27
28
29
30
31
32
33
34
35
# File 'lib/miga/tax_index.rb', line 27

def <<(dataset)
  return nil if dataset.[:tax].nil?
  taxon = @root
  MiGA::Taxonomy.KNOWN_RANKS.each do |rank|
    taxon = taxon.add_child(rank, dataset.[:tax][rank])
  end
  taxon.add_dataset dataset
  @datasets << dataset
end

#taxa_by_rank(rank) ⇒ Object

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


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/miga/tax_index.rb', line 39

def taxa_by_rank(rank)
  rank = MiGA::Taxonomy.normalize_rank(rank)
  taxa = [@root]
  select = []
  loop do
    new_taxa = []
    taxa.map{ |tx| tx.children }.flatten.each do |ch|
      if ch.rank == rank
        select << ch
      elsif not ch.children.empty?
        new_taxa << ch
      end
    end
    break if new_taxa.empty?
  end
  select
end

#to_jsonObject

Generate JSON String for the index.


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

def to_json
  JSON.pretty_generate({ root:root.to_hash,
    datasets:datasets.map{ |d| d.name } })
end

#to_tab(unknown = false) ⇒ Object

Generate tabular String for the index.


66
# File 'lib/miga/tax_index.rb', line 66

def to_tab(unknown=false) ; root.to_tab(unknown) ; end