class Bio::MAF::FASTARangeReader

Constants

GT

Attributes

f[R]
pos[R]

Public Class Methods

new(fspec) click to toggle source
# File lib/bio/maf/tiler.rb, line 217
def initialize(fspec)
  if fspec.respond_to? :seek
    @f = fspec
  else
    reader_class = if fspec =~ /.gz$/
                     Zlib::GzipReader
                   else
                     File
                   end
    @f = reader_class.open(fspec)
  end
  position_at_start
end

Public Instance Methods

position_at_start() click to toggle source
# File lib/bio/maf/tiler.rb, line 233
def position_at_start
  first = f.readline
  raise "expected FASTA comment" unless first =~ /^>/
  @pos = 0
end
read_interval(z_start, z_end) click to toggle source
# File lib/bio/maf/tiler.rb, line 239
def read_interval(z_start, z_end)
  if z_start < pos
    position_at_start
  end
  data = ''
  region_size = z_end - z_start
  in_region = false
  f.each_line do |line_raw|
    if line_raw.getbyte(0) == GT
      raise "unexpected description line: #{line_raw.inspect}"
    end
    line = line_raw.strip
    end_pos = pos + line.size
    if (! in_region) && pos <= z_start && z_start < end_pos
      offset = z_start - pos
      end_offset = [(offset + region_size), line.size].min
      data << line.slice(offset...end_offset)
      in_region = true
    elsif in_region
      need = region_size - data.size
      raise "should not happen: region #{region_size}, data #{data.size}, need #{need}" if need < 0
      if need > line.size
        data << line
      else
        # last line
        data << line.slice(0, need)
        break
      end
    end
    @pos = end_pos
  end
  return data
end