class Bio::MAF::ChunkReader

Reads MAF files in chunks. @api private

Attributes

chunk_size[RW]

Size, in bytes, of the chunks to read. Must be a power of 2. @return [Integer]

f[R]

{File} from which chunks are read. @return [File]

pos[RW]

Current position in the file. @return [Integer]

Public Class Methods

new(f, chunk_size) click to toggle source
# File lib/bio/maf/parser.rb, line 27
def initialize(f, chunk_size)
  @f = f
  self.chunk_size = chunk_size
  @pos = 0
end

Public Instance Methods

check_chunk_size(size) click to toggle source
# File lib/bio/maf/parser.rb, line 40
def check_chunk_size(size)
  if size < 1
    raise "Invalid chunk size: #{size}"
  end
  ## test whether it is a power of 2
  ## cf. http://bit.ly/JExNc4
  if size & (size - 1) != 0
    raise "Invalid chunk size (not a power of 2): #{size}}"
  end
end
chunk_size=(size) click to toggle source
# File lib/bio/maf/parser.rb, line 33
def chunk_size=(size)
  check_chunk_size(size)
  @chunk_size = size
  # power of 2 so don't worry about rounding
  # @chunk_shift = Math.log2(size).to_i
end
read_chunk() click to toggle source

Reads the next chunk of the file. @return [String] Next {#chunk_size} bytes of MAF data.

# File lib/bio/maf/parser.rb, line 53
def read_chunk
  chunk = f.read(@chunk_size)
  @pos += chunk.bytesize if chunk
  return chunk
end
read_chunk_at(offset, size_hint=@chunk_size) click to toggle source

Reads a chunk of the file.

Currently always reads size_hint bytes.

@param [Integer] offset file offset to read from. @param [Integer] size_hint desired size of chunk. @return [String] Chunk of MAF data.

# File lib/bio/maf/parser.rb, line 66
def read_chunk_at(offset, size_hint=@chunk_size)
  f.seek(offset)
  chunk = f.read(size_hint)
  @pos = offset + chunk.bytesize
  return chunk
end