Class: HashTable

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HashTableAux, Printer
Defined in:
lib/hash_table.rb

Direct Known Subclasses

GO::Annotation, GTF, Mutation::Collection, SDRF, SegFile

Defined Under Namespace

Classes: HashLine

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashTableAux

#load_file

Methods included from Printer

#print, #write

Constructor Details

#initialize(obj = nil, opts = {}) ⇒ HashTable

Returns a new instance of HashTable.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/hash_table.rb', line 200

def initialize(obj=nil,opts={})
  fix_opts(opts)
  create_header
  create_index
  @lines = []
  @preamble = []
  @sleeve = {}
  @comment = @opts[:comment] || self.class.comment

  if obj && obj.is_a?(String) && File.exists?(obj)
    parse_file obj
  elsif obj && obj.is_a?(Array)
    # it's a stack of lines. Go with it.
    @lines = obj
  end
end

Class Attribute Details

.commentObject (readonly)

Returns the value of attribute comment.



95
96
97
# File 'lib/hash_table.rb', line 95

def comment
  @comment
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



116
117
118
# File 'lib/hash_table.rb', line 116

def header
  @header
end

#typesObject

Returns the value of attribute types.



116
117
118
# File 'lib/hash_table.rb', line 116

def types
  @types
end

Class Method Details

.header_offObject



110
111
112
# File 'lib/hash_table.rb', line 110

def header_off
  @use_header = nil
end

.header_onObject



107
108
109
# File 'lib/hash_table.rb', line 107

def header_on
  @use_header = true
end

.line_class(klass) ⇒ Object



100
101
102
# File 'lib/hash_table.rb', line 100

def line_class klass
  @line_type = const_get klass.to_s.camel_case
end

.line_typeObject



96
97
98
# File 'lib/hash_table.rb', line 96

def line_type
  @line_type || HashLine
end

.use_header?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/hash_table.rb', line 104

def use_header?
  @use_header
end

Instance Method Details

#<<(hash) ⇒ Object



217
218
219
# File 'lib/hash_table.rb', line 217

def << hash
  add_line hash
end

#[](ind) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/hash_table.rb', line 117

def [](ind)
  if ind.is_a? Range
    wrap @lines[ind]
  else
    @lines[ind]
  end
end

#concat(other_table) ⇒ Object

Raises:

  • (TypeError)


221
222
223
224
225
226
227
# File 'lib/hash_table.rb', line 221

def concat other_table
  raise TypeError unless other_table.is_a? self.class
  other_table.each do |line|
    add_line line
  end
  self
end

#eachObject



194
195
196
197
198
# File 'lib/hash_table.rb', line 194

def each
  @lines.each do |l|
    yield l
  end
end

#formatted_headerObject



169
170
171
172
173
# File 'lib/hash_table.rb', line 169

def formatted_header
  @header.map do |h|
    @sleeve[h] || h
  end.join("\t")
end

#idx(key, value = nil) ⇒ Object



125
126
127
# File 'lib/hash_table.rb', line 125

def idx key, value=nil
  @wrapped_index[ [key, value] ] ||= get_wrapped_table key, value
end

#idx_keys(key) ⇒ Object



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

def idx_keys(key)
  @bare_index[key].keys
end

#inspectObject



190
191
192
# File 'lib/hash_table.rb', line 190

def inspect
  "#<#{self.class.name}:#{object_id} @lines=#{@lines.count}>"
end

#output(f) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/hash_table.rb', line 179

def output f
  f.puts preamble
  f.puts formatted_header if use_header?
  @lines.each do |l|
    l = yield l if block_given?
    next if !l || l.invalid?
    f.puts l.to_s
  end
  true
end

#preambleObject



175
176
177
# File 'lib/hash_table.rb', line 175

def preamble
  @preamble
end

#sample(*args) ⇒ Object



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

def sample *args
  samp = @lines.sample *args
  if samp.is_a? Array
    wrap samp
  else
    samp
  end
end

#select!(&block) ⇒ Object



155
156
157
158
# File 'lib/hash_table.rb', line 155

def select! &block
  @lines.select! &block
  self
end

#sort_by!(&block) ⇒ Object



160
161
162
163
# File 'lib/hash_table.rb', line 160

def sort_by! &block
  @lines.sort_by! &block
  self
end

#sum(col) ⇒ Object



133
134
135
136
137
# File 'lib/hash_table.rb', line 133

def sum(col)
  inject(0) do |sum,line|
    sum += line[col].to_f
  end
end

#update_index(key) ⇒ Object



233
234
235
236
237
238
# File 'lib/hash_table.rb', line 233

def update_index key
  create_index_for key
  @lines.each do |line|
    index_line_to_key line, key
  end
end

#use_header?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/hash_table.rb', line 165

def use_header?
  self.class.use_header?
end

#wrap(lines) ⇒ Object



229
230
231
# File 'lib/hash_table.rb', line 229

def wrap lines
  self.class.new lines, @opts.merge( :header => @header.clone, :types => @types.clone )
end