Class: ParseFasta::SeqFile

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname) ⇒ SeqFile

Returns a new instance of SeqFile.

Raises:



34
35
36
37
38
39
# File 'lib/parse_fasta/seq_file.rb', line 34

def initialize fname
  type = check_file fname

  @fname = fname
  @type = type
end

Instance Attribute Details

#typeSymbol



25
26
27
# File 'lib/parse_fasta/seq_file.rb', line 25

def type
  @type
end

Class Method Details

.open(fname) ⇒ SeqFile

An alias for SeqFile.new



44
45
46
# File 'lib/parse_fasta/seq_file.rb', line 44

def self.open fname
  self.new fname
end

Instance Method Details

#each_record {|record| ... } ⇒ Object

Analagous to IO#each_line, SeqFile#each_record is used to go through a fastA or fastQ file record by record. It will accept gzipped files as well.

If the input is a fastA file, then the record that is yielded will have the desc and qual instance variables be nil. If it is a fastQ record then those instance variables will not be nil.

Examples:

Parsing a fastA file

ParseFasta::SeqFile.open("seqs.fa").each_record do |rec|
  puts [rec.header, rec.seq].join "\t"

  rec.desc.nil? #=> true
  rec.qual.nil? #=> true
end

Parsing a gzipped fastQ file

ParseFasta::SeqFile.open("seqs.fq.gz").each_record do |rec|
  puts [rec.header, rec.seq, rec.desc, rec.qual].join "\t"
end

Yield Parameters:

Raises:



73
74
75
76
77
78
79
80
81
# File 'lib/parse_fasta/seq_file.rb', line 73

def each_record &b
  line_parser = "parse_#{@type}_lines"

  if gzipped? @fname
    each_record_gzipped line_parser, &b
  else
    each_record_non_gzipped line_parser, &b
  end
end