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, args = {}) ⇒ SeqFile

Returns a new instance of SeqFile.

Parameters:

  • fname (String)

    the name of the fastA or fastQ file to parse

  • type (Symbol)

    whether the file is :fasta or :fastq

  • check_fasta_seq (Bool)

    keyword arg for whether to check for ‘>’ in the sequence of fastA files.

Raises:



19
20
21
22
23
24
25
# File 'lib/parse_fasta/seq_file.rb', line 19

def initialize fname, args = {}
  type = check_file fname

  @check_fasta_seq = args.fetch :check_fasta_seq, true
  @fname = fname
  @type = type
end

Instance Attribute Details

#typeSymbol

Returns the type of the SeqFile (:fasta or :fastq).

Returns:

  • (Symbol)

    the type of the SeqFile (:fasta or :fastq)



7
8
9
# File 'lib/parse_fasta/seq_file.rb', line 7

def type
  @type
end

Class Method Details

.open(fname, args = {}) ⇒ SeqFile

An alias for SeqFile.new

Returns:



30
31
32
# File 'lib/parse_fasta/seq_file.rb', line 30

def self.open fname, args = {}
  self.new fname, args
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:

  • (ParseFasta::Error::SequenceFormatError)

    if a fastA file contains a record with a ‘>’ character in the header, and the SeqFile object was not initialized with check_fasta_seq: false



60
61
62
63
64
65
66
67
68
# File 'lib/parse_fasta/seq_file.rb', line 60

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