Class: Bio::FlatFileIndex::Indexer::Parser::TemplateParser

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/io/flatfile/indexer.rb

Constant Summary collapse

NAMESTYLE =
NameSpaces.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTemplateParser

Returns a new instance of TemplateParser.



79
80
81
82
83
# File 'lib/bio/io/flatfile/indexer.rb', line 79

def initialize
  @namestyle = self.class::NAMESTYLE
  @secondary = NameSpaces.new
  @errorlog = []
end

Instance Attribute Details

#dbclassObject

Returns the value of attribute dbclass.



84
85
86
# File 'lib/bio/io/flatfile/indexer.rb', line 84

def dbclass
  @dbclass
end

#errorlogObject (readonly)

Returns the value of attribute errorlog.



85
86
87
# File 'lib/bio/io/flatfile/indexer.rb', line 85

def errorlog
  @errorlog
end

#fileidObject (readonly)

Returns the value of attribute fileid.



120
121
122
# File 'lib/bio/io/flatfile/indexer.rb', line 120

def fileid
  @fileid
end

#formatObject

Returns the value of attribute format.



84
85
86
# File 'lib/bio/io/flatfile/indexer.rb', line 84

def format
  @format
end

#primaryObject (readonly)

Returns the value of attribute primary.



84
85
86
# File 'lib/bio/io/flatfile/indexer.rb', line 84

def primary
  @primary
end

#secondaryObject (readonly)

Returns the value of attribute secondary.



84
85
86
# File 'lib/bio/io/flatfile/indexer.rb', line 84

def secondary
  @secondary
end

Instance Method Details

#add_secondary_namespaces(*names) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bio/io/flatfile/indexer.rb', line 98

def add_secondary_namespaces(*names)
  DEBUG.print "add_secondary_namespaces: #{names.inspect}\n"
  names.each do |x|
    unless x.is_a?(NameSpace) then
      y = @namestyle[x]
      raise 'unknown secondary namespace' unless y
      @secondary << y
    end
  end
  true
end

#close_flatfileObject



164
165
166
167
# File 'lib/bio/io/flatfile/indexer.rb', line 164

def close_flatfile
  DEBUG.print "close flatfile #{@flatfilename.inspect}\n"
  @flatfile.close
end

#eachObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bio/io/flatfile/indexer.rb', line 122

def each
  @flatfile.each do |x|
    @entry = x
    pos = @flatfile.entry_start_pos
    len = @flatfile.entry_ended_pos - @flatfile.entry_start_pos
    begin
      yield pos, len
    rescue RuntimeError, NameError => evar
      DEBUG.print "Caught error: #{evar.inspect}\n"
      DEBUG.print "in #{@flatfilename.inspect} position #{pos}\n"
      DEBUG.print "===begin===\n"
      DEBUG.print @flatfile.entry_raw.to_s.chomp
      DEBUG.print "\n===end===\n"
      @errorlog << [ evar, @flatfilename, pos ]
      if @fatal then
        DEBUG.print "Fatal error occurred, stop creating index...\n"
        raise evar
      else
        DEBUG.print "This entry shall be incorrectly indexed.\n"
      end
    end #rescue
  end
end

#open_flatfile(fileid, file) ⇒ Object

administration of a single flatfile



111
112
113
114
115
116
117
118
119
# File 'lib/bio/io/flatfile/indexer.rb', line 111

def open_flatfile(fileid, file)
  @fileid = fileid
  @flatfilename = file
  DEBUG.print "fileid=#{fileid} file=#{@flatfilename.inspect}\n"
  @flatfile = Bio::FlatFile.open(@dbclass, file, 'rb')
  @flatfile.raw = nil
  @flatfile.entry_pos_flag = true
  @entry = nil
end

#parse_primaryObject



146
147
148
149
150
151
152
153
# File 'lib/bio/io/flatfile/indexer.rb', line 146

def parse_primary
  r = self.primary.proc.call(@entry)
  unless r.is_a?(String) and r.length > 0
    #@fatal = true
    raise 'primary id must be a non-void string (skipped this entry)'
  end
  r
end

#parse_secondaryObject



155
156
157
158
159
160
161
162
# File 'lib/bio/io/flatfile/indexer.rb', line 155

def parse_secondary
  self.secondary.each do |x|
    p = x.proc.call(@entry)
    p.each do |y|
      yield x.name, y if y.length > 0
    end
  end
end

#set_primary_namespace(name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/bio/io/flatfile/indexer.rb', line 87

def set_primary_namespace(name)
  DEBUG.print "set_primary_namespace: #{name.inspect}\n"
  if name.is_a?(NameSpace) then
    @primary = name
  else
    @primary = @namestyle[name] 
  end
  raise 'unknown primary namespace' unless @primary
  @primary
end