class Bio::BGZF::Writer

Attributes

buf[R]
f[R]
last_write_pos[R]

Return the virtual offset of the last {#write} call. This is a hook for e.g. building an index on the fly.

level[R]

Public Class Methods

new(f, level=2) { |self| ... } click to toggle source
# File lib/bio-bgzf/writer.rb, line 14
def initialize(f, level=2)
  @f = f
  @level = level
  @buf = ''
  @buf_write_pos = 0
  if block_given?
    begin
      yield self
    ensure
      self.close
    end
  end
end

Public Instance Methods

_cur_write_pos() click to toggle source

@api private

# File lib/bio-bgzf/writer.rb, line 43
def _cur_write_pos
  @buf_write_pos + buf.bytesize
end
_each_slice(s) { |slice(offset, MAX_BYTES)| ... } click to toggle source
# File lib/bio-bgzf/writer.rb, line 63
def _each_slice(s)
  n = 0
  size = s.bytesize
  while true
    offset = n * MAX_BYTES
    break if offset >= size
    yield s.slice(offset, MAX_BYTES)
    n += 1
  end
end
close() click to toggle source
# File lib/bio-bgzf/writer.rb, line 74
def close
  write_buf
  f.close
end
tell() click to toggle source
# File lib/bio-bgzf/writer.rb, line 28
def tell
  f.tell << 16
end
write(s) click to toggle source
# File lib/bio-bgzf/writer.rb, line 47
def write(s)
  if s.bytesize > MAX_BYTES
    write_buf
    @last_write_pos = _cur_write_pos
    _each_slice(s) do |slice|
      write(slice)
    end
  else
    if (s.bytesize + buf.bytesize) > MAX_BYTES
      write_buf
    end
    @last_write_pos = _cur_write_pos
    buf << s
  end
end
write_buf() click to toggle source
# File lib/bio-bgzf/writer.rb, line 32
def write_buf
  if buf.size > 0
    raise "Buffer too large: #{buf.bytesize}" if buf.bytesize > MAX_BYTES
    block = pack(buf, level)
    f.write(block)
    @buf = ''
    @buf_write_pos = tell
  end
end