Sha256: 4ba51f12549acf642810d3c7dd0780b4948af8cc994adf100cfdae153879ba97

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module Bio::BGZF
  
  class Reader
    include Bio::BGZF

    attr_reader :f

    def initialize(f)
      @f = f
      @cur_block = nil
    end

    # Returns the reader's current virtual offset. Between
    # {#read_block} calls, the file position will always be at the
    # start of a block or at EOF, so the low 16 bits of the virtual
    # offset will always be zero.
    #
    # @return [Integer] virtual offset for current position
    def tell
      f.tell << 16
    end

    # Reads the BGZF block at the current position. Returns its
    # decompressed data.
    #
    # @return [String] decompressed block data
    def read_block
      decompress_block(f)
    end

    # Reads a portion of a BGZF block, starting from the given virtual
    # offset. If the offset is the start of a block (low 16 bits are
    # zero) the entire block's data will be returned. Otherwise, the
    # subset of the data starting at the given offset will be
    # returned.
    #
    # @param [Integer] vo virtual offset to start from
    # @return [String] decompressed block data
    def read_block_at(vo)
      block_offset = vo_block_offset(vo)
      data_offset = vo_data_offset(vo)
      f.seek(block_offset)
      block_data = decompress_block(f)
      if data_offset == 0
        return block_data
      else
        return block_data.slice(data_offset...block_data.size)
      end
    end

    # Iterates over the blocks in a BGZF file, yielding [block, vo] pairs where 
    def each_block
      if block_given?
        while true
          pos = tell
          b = read_block
          break unless b
          yield b, pos
        end
      else
        enum_for(:each_block)
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bio-bgzf-0.2.1 lib/bio-bgzf/reader.rb
bio-bgzf-0.2.0 lib/bio-bgzf/reader.rb