Class: Bio::MAF::ParseContext

Inherits:
Object
  • Object
show all
Includes:
MAFParsing
Defined in:
lib/bio/maf/parser.rb

Overview

A MAF parsing context, used for random-access parsing.

Constant Summary

Constants included from MAFParsing

MAFParsing::BLOCK_START, MAFParsing::BLOCK_START_OR_EOS, MAFParsing::COMMENT, MAFParsing::E, MAFParsing::EOL_OR_EOF, MAFParsing::I, MAFParsing::Q, MAFParsing::S, MAFParsing::STRAND_SYM

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MAFParsing

#_parse_block, #gather_leading_fragment, #parse_block_data, #parse_empty_line, #parse_error, #parse_maf_vars, #parse_seq_line, #parse_trailing_fragment, #seq_filter_ok?, #trailing_nl?

Constructor Details

#initialize(fd, chunk_size, parser) ⇒ ParseContext

Returns a new instance of ParseContext.



384
385
386
387
388
389
390
391
# File 'lib/bio/maf/parser.rb', line 384

def initialize(fd, chunk_size, parser)
  @f = fd
  @parser = parser
  @opts = parser.opts
  reader = opts[:chunk_reader] || ChunkReader
  @cr = reader.new(@f, chunk_size)
  @last_block_pos = -1
end

Instance Attribute Details

#at_endObject



382
383
384
# File 'lib/bio/maf/parser.rb', line 382

def at_end
  @at_end
end

#chunk_startObject



382
383
384
# File 'lib/bio/maf/parser.rb', line 382

def chunk_start
  @chunk_start
end

#crObject



381
382
383
# File 'lib/bio/maf/parser.rb', line 381

def cr
  @cr
end

#fObject



381
382
383
# File 'lib/bio/maf/parser.rb', line 381

def f
  @f
end

#last_block_posObject



382
383
384
# File 'lib/bio/maf/parser.rb', line 382

def last_block_pos
  @last_block_pos
end

#optsObject



381
382
383
# File 'lib/bio/maf/parser.rb', line 381

def opts
  @opts
end

#parserObject



381
382
383
# File 'lib/bio/maf/parser.rb', line 381

def parser
  @parser
end

#sObject



381
382
383
# File 'lib/bio/maf/parser.rb', line 381

def s
  @s
end

Instance Method Details

#append_chunks_to(len) ⇒ Object



446
447
448
449
450
451
# File 'lib/bio/maf/parser.rb', line 446

def append_chunks_to(len)
  # XXX: need to rethink this for BGZF; prefetching ChunkReader
  while s.string.size < len
    s.string << cr.read_chunk()
  end
end

#fetch_blocks(offset, len, block_offsets) ⇒ Array<Block>

Fetch and parse blocks at given offset and len

Parameters:

  • offset (Integer)

    Offset to start parsing at.

  • len (Integer)

    Number of bytes to read.

  • block_offsets (Array)

    Offsets of blocks to parse.

Returns:



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/bio/maf/parser.rb', line 414

def fetch_blocks(offset, len, block_offsets)
  if block_given?
    start_chunk_read_if_needed(offset, len)
    # read chunks until we have the entire merged set of
    # blocks ready to parse
    # to avoid fragment joining
    append_chunks_to(len)
    # parse the blocks
    block_offsets.each do |expected_offset|
      block = _parse_block
      parse_error("expected a block at offset #{expected_offset} but could not parse one!") unless block
      parse_error("got block with offset #{block.offset}, expected #{expected_offset}!") unless block.offset == expected_offset
      yield block
    end
  else
    enum_for(:fetch_blocks, offset, len, block_offsets)
  end
end

#parse_emptyObject



397
398
399
# File 'lib/bio/maf/parser.rb', line 397

def parse_empty
  parser.parse_empty
end

#parse_extendedObject



401
402
403
# File 'lib/bio/maf/parser.rb', line 401

def parse_extended
  parser.parse_extended
end

#sequence_filterObject



393
394
395
# File 'lib/bio/maf/parser.rb', line 393

def sequence_filter
  parser.sequence_filter
end

#set_last_block_pos!Object



405
406
407
# File 'lib/bio/maf/parser.rb', line 405

def set_last_block_pos!
  @last_block_pos = s.string.rindex(BLOCK_START)
end

#start_chunk_read_if_needed(offset, len) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/bio/maf/parser.rb', line 433

def start_chunk_read_if_needed(offset, len)
  if chunk_start \
    && (chunk_start <= offset) \
    && (offset < (chunk_start + s.string.size))
    ## the selected offset is in the current chunk
    s.pos = offset - chunk_start
  else
    chunk = cr.read_chunk_at(offset, len)
    @chunk_start = offset
    @s = StringScanner.new(chunk)
  end
end