Class: Bio::FlatFile::Splitter::Default

Inherits:
Template show all
Defined in:
lib/bio/io/flatfile.rb

Overview

Default splitter. It sees following constants in the given class.

DELIMITER

(String) delimiter indicates the end of a entry.

FLATFILE_HEADER

(String) start of a entry, located on head of a line.

DELIMITER_OVERRUN

(Integer) excess read size included in DELIMITER.

Instance Attribute Summary collapse

Attributes inherited from Template

#entry, #entry_ended_pos, #entry_pos_flag, #entry_start_pos

Instance Method Summary collapse

Constructor Details

#initialize(klass, bstream) ⇒ Default

Creates a new splitter.

klass

database class

bstream

input stream. It must be a BufferedInputStream object.



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/bio/io/flatfile.rb', line 284

def initialize(klass, bstream)
  @stream = bstream
  @delimiter = klass::DELIMITER rescue nil
  @header = klass::FLATFILE_HEADER rescue nil
  # for specific classes' benefit
  unless header
    if klass == Bio::GenBank or klass == Bio::GenPept
      @header = 'LOCUS '
    end
  end
  @delimiter_overrun = klass::DELIMITER_OVERRUN rescue nil
  @entry_pos_flag = nil
end

Instance Attribute Details

#delimiterObject

(String) delimiter indicates the end of a entry.



299
300
301
# File 'lib/bio/io/flatfile.rb', line 299

def delimiter
  @delimiter
end

#delimiter_overrunObject

(Integer) excess read data size included in delimiter.



305
306
307
# File 'lib/bio/io/flatfile.rb', line 305

def delimiter_overrun
  @delimiter_overrun
end

#headerObject

(String) start of a entry, located on head of a line.



302
303
304
# File 'lib/bio/io/flatfile.rb', line 302

def header
  @header
end

Instance Method Details

#get_entryObject

gets a entry



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/bio/io/flatfile.rb', line 335

def get_entry
  p0 = @entry_pos_flag ? @stream.pos : nil
  e  = @stream.gets(@delimiter)
  if e and @delimiter_overrun then
    if e[-@delimiter.size, @delimiter.size ] == @delimiter then
      overrun = e[-@delimiter_overrun, @delimiter_overrun]
      e[-@delimiter_overrun, @delimiter_overrun] = ''
      @stream.ungets(overrun)
    end
  end
  p1 = @entry_pos_flag ? @stream.pos : nil
  @entry_start_pos = p0
  @entry = e
  @entry_ended_pos = p1
  @entry
end

#skip_leaderObject

Skips leader of the entry.

If @header is not nil, it reads till the contents of @header comes at the head of a line. If correct FLATFILE_HEADER is found, returns true. Otherwise, returns nil.



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/bio/io/flatfile.rb', line 313

def skip_leader
  if @header then
    data = ''
    while s = @stream.gets(@header)
      data << s
      if data.split(/[\r\n]+/)[-1] == @header then
        @stream.ungets(@header)
        return true
      end
    end
    # @header was not found. For safety,
    # pushes back data with removing white spaces in the head.
    data.sub(/\A\s+/, '')
    @stream.ungets(data)
    return nil
  else
    @stream.skip_spaces
    return nil
  end
end