Class: FastaParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dna/parsers/fasta.rb

Instance Method Summary collapse

Constructor Details

#initialize(handle) ⇒ FastaParser

Returns a new instance of FastaParser.



3
4
5
# File 'lib/dna/parsers/fasta.rb', line 3

def initialize(handle)
  @handle = handle
end

Instance Method Details

#each {|Fasta.new(:name => header, :sequence => sequence)| ... } ⇒ Object

Yields:

  • (Fasta.new(:name => header, :sequence => sequence))


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dna/parsers/fasta.rb', line 7

def each
  sequence, header = nil, nil
  @handle.each do |line|

    line.strip!
    next if line.strip.empty? # skip blank lines

    if line[0].chr == '>'
      yield Fasta.new(:name => header, :sequence => sequence) if sequence
      sequence = ''
      header = line[1..-1]
    else
      sequence << line.tr(' ','')
    end
  end
  yield Fasta.new(:name => header, :sequence => sequence)
end