Class: Dna

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

Overview

Dna

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle) ⇒ Dna

Returns a new instance of Dna.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dna.rb', line 11

def initialize(handle)
  @handle = handle
  @format = detect_format
  @iterator =
    case @format
    when :fasta
      FastaParser.new @handle
    when :fastq
      FastqParser.new @handle
    when :qseq
      QSEQParser.new @handle
    else
      raise "#{@format} not supported."
    end
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

Instance Method Details

#detect_formatObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dna.rb', line 27

def detect_format
  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



45
46
47
# File 'lib/dna.rb', line 45

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