Class: Ms::Ident::Peptide::Db::IO

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ms/ident/peptide/db.rb

Overview

an object for on disk retrieval of db entries proteins are returned as an array. behaves much like a hash once it is opened.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ IO

Returns a new instance of IO.



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ms/ident/peptide/db.rb', line 199

def initialize(io)
  @io = io
  @index = {}
  re = /^(\w+)#{Regexp.escape(KEY_VALUE_DELIMITER)}/
    prev_io_pos = io.pos
  triplets = io.each_line.map do |line|
    key = re.match(line)[1]
    [key, prev_io_pos + key.bytesize+KEY_VALUE_DELIMITER.bytesize, prev_io_pos=io.pos]
  end
  triplets.each do |key, start, end_pos|
    @index[key] = [start, end_pos-start]
  end
end

Instance Attribute Details

#indexObject

Returns the value of attribute index.



197
198
199
# File 'lib/ms/ident/peptide/db.rb', line 197

def index
  @index
end

#ioObject

Returns the value of attribute io.



196
197
198
# File 'lib/ms/ident/peptide/db.rb', line 196

def io
  @io
end

Class Method Details

.open(filename, &block) ⇒ Object

Raises:

  • (ArgumentError)


189
190
191
192
193
194
# File 'lib/ms/ident/peptide/db.rb', line 189

def self.open(filename, &block)
  raise ArgumentError unless block
  File.open(filename) do |io|
    block.call(self.new(io))
  end
end

Instance Method Details

#[](key) ⇒ Object

returns an array of proteins for the given key (peptide aaseq)



214
215
216
217
218
219
220
221
# File 'lib/ms/ident/peptide/db.rb', line 214

def [](key)
  (start, length) = @index[key]
  return nil unless start
  @io.seek(start)
  string = @io.read(length)
  string.chomp!
  string.split("\t")
end

#each(&block) ⇒ Object

yields a pair of aaseq and protein array



237
238
239
240
241
# File 'lib/ms/ident/peptide/db.rb', line 237

def each(&block)
  @index.each do |key, start_length|
    block.call([key, self[key]])
  end
end

#keysObject



227
228
229
# File 'lib/ms/ident/peptide/db.rb', line 227

def keys
  @index.keys
end

#sizeObject Also known as: length

number of entries



224
# File 'lib/ms/ident/peptide/db.rb', line 224

def size ; @index.size end

#valuesObject

all the protein lists



232
233
234
# File 'lib/ms/ident/peptide/db.rb', line 232

def values
  keys.map {|key| self[key] }
end