Class: Bio::FlatFile::Splitter::Default

Inherits:
Template show all
Defined in:
lib/bio/io/flatfile/splitter.rb

Overview

Default splitter. It sees following constants in the given class.

DELIMITER

(String) delimiter indicates the end of a entry.

FLATFILE_HEADER

(String) start of a entry, located on head of a line.

DELIMITER_OVERRUN

(Integer) excess read size included in DELIMITER.

Direct Known Subclasses

Blast::Report::BlastXmlSplitter

Instance Attribute Summary collapse

Attributes inherited from Template

#entry, #entry_ended_pos, #entry_pos_flag, #entry_start_pos, #parsed_entry

Instance Method Summary collapse

Methods inherited from Template

#get_parsed_entry, #rewind

Constructor Details

#initialize(klass, bstream) ⇒ Default

Creates a new splitter.

klass

database class

bstream

input stream. It must be a BufferedInputStream object.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bio/io/flatfile/splitter.rb', line 128

def initialize(klass, bstream)
  super(klass, bstream)

  @delimiter = klass::DELIMITER rescue nil
  @header = klass::FLATFILE_HEADER rescue nil
  # for specific classes' benefit
  unless header
    if (defined?(Bio::GenBank) and klass == Bio::GenBank) or
        (defined?(Bio::GenPept) and klass == Bio::GenPept)
      @header = 'LOCUS '
    end
  end
  @delimiter_overrun = klass::DELIMITER_OVERRUN rescue nil
end

Instance Attribute Details

#delimiterObject

(String) delimiter indicates the end of a entry.



144
145
146
# File 'lib/bio/io/flatfile/splitter.rb', line 144

def delimiter
  @delimiter
end

#delimiter_overrunObject

(Integer) excess read data size included in delimiter.



150
151
152
# File 'lib/bio/io/flatfile/splitter.rb', line 150

def delimiter_overrun
  @delimiter_overrun
end

#headerObject

(String) start of a entry, located on head of a line.



147
148
149
# File 'lib/bio/io/flatfile/splitter.rb', line 147

def header
  @header
end

Instance Method Details

#get_entryObject

gets a entry



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/bio/io/flatfile/splitter.rb', line 180

def get_entry
  p0 = stream_pos()
  e  = stream.gets(@delimiter)
  if e and @delimiter_overrun then
    if e[-@delimiter.size, @delimiter.size ] == @delimiter then
      overrun = e[-@delimiter_overrun, @delimiter_overrun]
      e[-@delimiter_overrun, @delimiter_overrun] = ''
      stream.ungets(overrun)
    end
  end
  p1 = stream_pos()
  self.entry_start_pos = p0
  self.entry = e
  self.entry_ended_pos = p1
  return entry
end

#skip_leaderObject

Skips leader of the entry.

If @header is not nil, it reads till the contents of @header comes at the head of a line. If correct FLATFILE_HEADER is found, returns true. Otherwise, returns nil.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/bio/io/flatfile/splitter.rb', line 158

def skip_leader
  if @header then
    data = ''
    while s = stream.gets(@header)
      data << s
      if data.split(/[\r\n]+/)[-1] == @header then
        stream.ungets(@header)
        return true
      end
    end
    # @header was not found. For safety,
    # pushes back data with removing white spaces in the head.
    data.sub(/\A\s+/, '')
    stream.ungets(data)
    return nil
  else
    stream.skip_spaces
    return nil
  end
end