Class: Dna

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dna/dna.rb,
lib/dna/version.rb

Overview

Dna

Defined Under Namespace

Classes: Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle, args = {}) ⇒ Dna

Returns a new instance of Dna.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/dna/dna.rb', line 8

def format
  @format
end

Instance Method Details

#detect_formatObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



54
55
56
# File 'lib/dna/dna.rb', line 54

def each &block
  @iterator.each(&block)
end