Class: Bio::BGZF::Writer

Inherits:
Object
  • Object
show all
Includes:
Bio::BGZF
Defined in:
lib/bio-bgzf/writer.rb

Constant Summary

Constants included from Bio::BGZF

CM, FLG, ID1, ID2, MAX_BYTES, MTIME, OS, SI1, SI2, SLEN, XFL, XLEN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bio::BGZF

decompress_block, pack, read_bgzf_block, unpack, vo_block_offset, vo_data_offset

Constructor Details

#initialize(f, level = 2) ⇒ Writer

Returns a new instance of Writer.



14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#bufObject (readonly)

Returns the value of attribute buf.



8
9
10
# File 'lib/bio-bgzf/writer.rb', line 8

def buf
  @buf
end

#fObject (readonly)

Returns the value of attribute f.



8
9
10
# File 'lib/bio-bgzf/writer.rb', line 8

def f
  @f
end

#last_write_posObject (readonly)

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



12
13
14
# File 'lib/bio-bgzf/writer.rb', line 12

def last_write_pos
  @last_write_pos
end

#levelObject (readonly)

Returns the value of attribute level.



8
9
10
# File 'lib/bio-bgzf/writer.rb', line 8

def level
  @level
end

Instance Method Details

#_cur_write_posObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
# File 'lib/bio-bgzf/writer.rb', line 43

def _cur_write_pos
  @buf_write_pos + buf.bytesize
end

#_each_slice(s) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# 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

#closeObject



74
75
76
77
# File 'lib/bio-bgzf/writer.rb', line 74

def close
  write_buf
  f.close
end

#tellObject



28
29
30
# File 'lib/bio-bgzf/writer.rb', line 28

def tell
  f.tell << 16
end

#write(s) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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_bufObject



32
33
34
35
36
37
38
39
40
# 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