Class: FastaFile

Inherits:
File
  • Object
show all
Defined in:
lib/parse_fasta/fasta_file.rb

Overview

Provides simple interface for parsing fasta format files.

Instance Method Summary collapse

Instance Method Details

#each_record(separate_lines = nil) {|header, sequence| ... } ⇒ Object

Analagous to File#each_line, #each_record is used to go through a fasta file record by record.

Examples:

Parsing a fasta file (default behavior)

FastaFile.open('reads.fna', 'r').each_record do |header, sequence|
  puts [header, sequence.gc].join("\t")
end

Parsing a fasta file (with truthy value param)

FastaFile.open('reads.fna','r').each_record(1) do |header, sequence|
  # header => 'sequence_1'
  # sequence => ['AACTG', 'AGTCGT', ... ]
end

Parameters:

  • separate_lines (Object) (defaults to: nil)

    If truthy, separate lines of record into an array, but if falsy, yield a Sequence object for the sequence instead.

Yields:

  • The header and sequence for each record in the fasta file to the block

Yield Parameters:

  • header (String)

    The header of the fasta record without the leading ‘>’

  • sequence (Sequence, Array<String>)

    The sequence of the fasta record. If separate_lines is falsy (the default behavior), will be Sequence, but if truthy will be Array<String>.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/parse_fasta/fasta_file.rb', line 50

def each_record(separate_lines=nil)
  if separate_lines
    self.each("\n>") do |line|
      header, sequence = parse_line_separately(line)
      yield(header.strip, sequence)
    end
  else
    self.each("\n>") do |line|
      header, sequence = parse_line(line)
      yield(header.strip, Sequence.new(sequence))
    end
  end
end