Class: MiGA::TaxIndexTaxon

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

Overview

Helper class for MiGA::TaxIndex.

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

#initialize(rank, name) ⇒ TaxIndexTaxon

Initalize taxon at rank with name.



93
94
95
96
97
98
# 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

Instance Attribute Details

#childrenObject (readonly)

Children of the taxon.



87
88
89
# File 'lib/miga/tax_index.rb', line 87

def children
  @children
end

#datasetsObject (readonly)

Datasets directly classified at the taxon (not at children).



89
90
91
# File 'lib/miga/tax_index.rb', line 89

def datasets
  @datasets
end

#nameObject (readonly)

Name of the taxon.



85
86
87
# File 'lib/miga/tax_index.rb', line 85

def name
  @name
end

#rankObject (readonly)

Rank of the taxon.



83
84
85
# File 'lib/miga/tax_index.rb', line 83

def rank
  @rank
end

Instance Method Details

#add_child(rank, name) ⇒ Object

Add child at rank with name.



106
107
108
109
110
111
112
113
114
115
# 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(dataset) ⇒ Object

Add dataset at the current taxon (not children).



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

def add_dataset(dataset) @datasets << dataset; end

#all_datasetsObject

Get all the datasets in the taxon (including children).



129
130
131
# File 'lib/miga/tax_index.rb', line 129

def all_datasets
  children.map(&:datasets).reduce(datasets, :+)
end

#datasets_countObject

Get the number of datasets in the taxon (including children).



123
124
125
# File 'lib/miga/tax_index.rb', line 123

def datasets_count
  children.map(&:datasets_count).reduce(datasets.size, :+)
end

#tax_strObject

String representation of the taxon.



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

def tax_str; "#{rank}:#{name.nil? ? '?' : name}"; end

#to_hashObject

Hash representation of the taxon.



145
146
147
148
149
150
151
# File 'lib/miga/tax_index.rb', line 145

def to_hash
  {
    str: tax_str,
    datasets: datasets.map(&:name),
    children: children.map(&:to_hash)
  }
end

#to_json(*a) ⇒ Object

JSON String of the taxon.



135
136
137
138
139
140
141
# 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

#to_tab(unknown, indent = 0) ⇒ Object

Tabular String of the taxon.



155
156
157
158
159
160
161
162
163
164
# 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