Class: Mspire::Ident::Peptide::Db::IO

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#index_by, #uniq_by

Constructor Details

#initialize(io) ⇒ IO

Returns a new instance of IO.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mspire/ident/peptide/db/io.rb', line 18

def initialize(io)
  @io = io
  @index = {}
  re = /^(\w+)#{Regexp.escape(Mspire::Ident::Peptide::Db::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+Mspire::Ident::Peptide::Db::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.



16
17
18
# File 'lib/mspire/ident/peptide/db/io.rb', line 16

def index
  @index
end

#ioObject

Returns the value of attribute io.



15
16
17
# File 'lib/mspire/ident/peptide/db/io.rb', line 15

def io
  @io
end

Class Method Details

.open(filename, &block) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/mspire/ident/peptide/db/io.rb', line 8

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)



33
34
35
36
37
38
39
40
# File 'lib/mspire/ident/peptide/db/io.rb', line 33

def [](key)
  (start, length) = @index[key]
  return nil unless start
  @io.seek(start)
  string = @io.read(length)
  string.chomp!
  string.split(Mspire::Ident::Peptide::Db::PROTEIN_DELIMITER)
end

#each(&block) ⇒ Object

yields a pair of aaseq and protein array



56
57
58
59
60
# File 'lib/mspire/ident/peptide/db/io.rb', line 56

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

#keysObject



46
47
48
# File 'lib/mspire/ident/peptide/db/io.rb', line 46

def keys
  @index.keys
end

#sizeObject Also known as: length

number of entries



43
# File 'lib/mspire/ident/peptide/db/io.rb', line 43

def size ; @index.size end

#valuesObject

all the protein lists



51
52
53
# File 'lib/mspire/ident/peptide/db/io.rb', line 51

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