Class: Bio::GFF::GFF3::FileIterator
- Inherits:
-
Object
- Object
- Bio::GFF::GFF3::FileIterator
- Defined in:
- lib/bio/db/gff/gfffileiterator.rb
Overview
GFF3::FileIterator takes a file and yields GFF3 records with their seek position included in the record.
Instance Attribute Summary collapse
-
#fasta_io_seek ⇒ Object
readonly
Returns the value of attribute fasta_io_seek.
-
#fh ⇒ Object
Returns the value of attribute fh.
Instance Method Summary collapse
-
#each_rec ⇒ Object
Iterate over every record in the file, yielding the record ID and (File)Record, which includes the io_seek position in the file.
-
#each_sequence ⇒ Object
Iterate over all contained FASTA sequences, yielding the ID and sequence as a FASTA record.
-
#initialize(filename) ⇒ FileIterator
constructor
A new instance of FileIterator.
Constructor Details
#initialize(filename) ⇒ FileIterator
Returns a new instance of FileIterator.
28 29 30 |
# File 'lib/bio/db/gff/gfffileiterator.rb', line 28 def initialize filename @fh = File.open(filename) end |
Instance Attribute Details
#fasta_io_seek ⇒ Object (readonly)
Returns the value of attribute fasta_io_seek.
26 27 28 |
# File 'lib/bio/db/gff/gfffileiterator.rb', line 26 def fasta_io_seek @fasta_io_seek end |
#fh ⇒ Object
Returns the value of attribute fh.
25 26 27 |
# File 'lib/bio/db/gff/gfffileiterator.rb', line 25 def fh @fh end |
Instance Method Details
#each_rec ⇒ Object
Iterate over every record in the file, yielding the record ID and (File)Record, which includes the io_seek position in the file
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bio/db/gff/gfffileiterator.rb', line 34 def each_rec() fpos = 0 @fh.each_line do | line | line = line.strip if line == "##FASTA" @fasta_io_seek = fpos break end if line.size != 0 and line !~ /^#/ rec = FileRecord.new(fpos, line) lastpos = @fh.tell id = rec.id yield id, rec @fh.seek(lastpos) # reset filepos, just in case it changed end fpos = @fh.tell end end |
#each_sequence ⇒ Object
Iterate over all contained FASTA sequences, yielding the ID and sequence as a FASTA record. Normally call each_rec first and you can test for existing FASTA records if fasta_io_seek != nil
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bio/db/gff/gfffileiterator.rb', line 56 def each_sequence if @fasta_io_seek == nil # Find the FASTA location first @fh.each_line do | line | break if line.strip == "##FASTA" end else @fh.seek(@fasta_io_seek) end fasta = Bio::GFF::FastaReader.new(@fh) fasta.each do | id, fastarec | yield id, fastarec end end |