Class: Bio::GFF::GFF3::FileIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/db/gff/file/gfffileiterator.rb

Overview

GFF3::FileIterator takes a file and yields GFF3 records with their seek position included in the record.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileIterator

Returns a new instance of FileIterator.



40
41
42
# File 'lib/bio/db/gff/file/gfffileiterator.rb', line 40

def initialize filename
  @fh = File.open(filename)
end

Instance Attribute Details

#fasta_io_seekObject (readonly)

Returns the value of attribute fasta_io_seek.



38
39
40
# File 'lib/bio/db/gff/file/gfffileiterator.rb', line 38

def fasta_io_seek
  @fasta_io_seek
end

#fhObject

Returns the value of attribute fh.



37
38
39
# File 'lib/bio/db/gff/file/gfffileiterator.rb', line 37

def fh
  @fh
end

Instance Method Details

#each_recObject

Iterate over every record in the file, yielding the seekpos and line containing the record



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bio/db/gff/file/gfffileiterator.rb', line 46

def each_rec
  @fh.seek(0)
  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 !~ /^#/
      lastpos = @fh.tell
      yield fpos, line
      @fh.seek(lastpos) # reset filepos, just in case it changed
    end
    fpos = @fh.tell
  end
end

#each_sequenceObject

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bio/db/gff/file/gfffileiterator.rb', line 67

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