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.



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

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.



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

def index
  @index
end

#ioObject

Returns the value of attribute io.



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

def io
  @io
end

Class Method Details

.open(filename, &block) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.open(filename, &block)
  #p filename
  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)



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

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



61
62
63
64
65
# File 'lib/mspire/ident/peptide/db/io.rb', line 61

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def key?(key)
  @index[key]
end

#keysObject



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

def keys
  @index.keys
end

#sizeObject Also known as: length

number of entries



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

def size ; @index.size end

#valuesObject

all the protein lists



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

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