Class: Ms::Ident::Peptide::Db::IO
- Inherits:
-
Object
- Object
- Ms::Ident::Peptide::Db::IO
- 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
-
#index ⇒ Object
Returns the value of attribute index.
-
#io ⇒ Object
Returns the value of attribute io.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
returns an array of proteins for the given key (peptide aaseq).
-
#each(&block) ⇒ Object
yields a pair of aaseq and protein array.
-
#initialize(io) ⇒ IO
constructor
A new instance of IO.
- #keys ⇒ Object
-
#size ⇒ Object
(also: #length)
number of entries.
-
#values ⇒ Object
all the protein lists.
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
#index ⇒ Object
Returns the value of attribute index.
197 198 199 |
# File 'lib/ms/ident/peptide/db.rb', line 197 def index @index end |
#io ⇒ Object
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
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 |
#keys ⇒ Object
227 228 229 |
# File 'lib/ms/ident/peptide/db.rb', line 227 def keys @index.keys end |
#size ⇒ Object Also known as: length
number of entries
224 |
# File 'lib/ms/ident/peptide/db.rb', line 224 def size ; @index.size end |
#values ⇒ Object
all the protein lists
232 233 234 |
# File 'lib/ms/ident/peptide/db.rb', line 232 def values keys.map {|key| self[key] } end |