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

Attributes included from Common::Net

#remote_connection_uri

Instance Method Summary collapse

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

#root_path, #script_path

Methods included from Common::Format

#clean_fasta_file, #seqs_length, #tabulate

Methods included from Common::Net

#download_file_ftp, #http_request, #known_hosts, #main_server, #net_method, #normalize_encoding, #remote_connection

Methods included from Common::SystemCall

#run_cmd, #run_cmd_opts

Constructor Details

#initializeTaxIndex

Initialize an empty MiGA::TaxIndex



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

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

Instance Attribute Details

#datasetsObject (readonly)

Datasets in the index.



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

def datasets
  @datasets
end

#rootObject (readonly)

Taxonomy root.



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

def root
  @root
end

Instance Method Details

#<<(dataset) ⇒ Object

Index dataset, a MiGA::Dataset object.



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

def <<(dataset)
  return nil if dataset.[:tax].nil?

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

    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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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_jsonObject

Generate JSON String for the index.



62
63
64
65
66
67
# 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) ⇒ Object

Generate tabular String for the index.



71
72
73
# File 'lib/miga/tax_index.rb', line 71

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