class Dna

Dna

Attributes

format[R]

Public Class Methods

new(handle, args = {}) click to toggle source
# File lib/dna/dna.rb, line 10
def initialize(handle, args = {})
  @handle = handle
  @format = args[:format] || detect_format
  @iterator =
    case @format
    when :fasta
      FastaParser.new @handle
    when :fastq
      FastqParser.new @handle
    when :qseq
      QSEQParser.new @handle
    else
      raise IOError, "format '#{@format}' not supported. (or file is empty)"
    end
end

Public Instance Methods

detect_format() click to toggle source
# File lib/dna/dna.rb, line 26
def detect_format

  # is gzipped?
  unless @handle.class == Array # for tests mostly...
    begin
      @handle = Zlib::GzipReader.new(@handle)
    rescue
      @handle.rewind
    end
  end

  first_line = @handle.first
  @handle.rewind if @handle.class == File

  return :unknown if first_line == nil

  # detect qseq by counting number of tabs.
  if first_line.split("\t").length == 11
    return :qseq
  elsif first_line[0].chr == '>'
    return :fasta
  elsif first_line[0].chr == '@'
    return :fastq
  else
    raise Exception, "cannot detect format of input"
  end
end
each(&block) click to toggle source
# File lib/dna/dna.rb, line 54
def each &block
  @iterator.each(&block)
end