Module: Chem::MDL::MdlMolParser

Included in:
KEGG::KeggCompound, MdlMolecule
Defined in:
lib/chem/db/mdl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



165
166
167
# File 'lib/chem/db/mdl.rb', line 165

def filename
  @filename
end

Instance Method Details

#entryObject Also known as: name



172
173
174
# File 'lib/chem/db/mdl.rb', line 172

def entry
  @title
end

#open(filename) ⇒ Object



166
167
168
169
170
# File 'lib/chem/db/mdl.rb', line 166

def open(filename)
  @filename = filename
  input = File.open(filename)
  parse(input)
end

#parse(input) ⇒ Object

Raises:

  • (MDLException)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/chem/db/mdl.rb', line 177

def parse input
  input.readline
  @title = input.readline
  raise MDLException if input.readline == nil
  line = input.readline
  n_atom = line[0..2].to_i
  n_bond = line[3..5].to_i

  if 0 > n_atom or 999 < n_atom or 0 > n_bond or 999 < n_bond
    raise "counts line format error"
  end

  n_atom.times do |n|
    mol = MDLAtom.new(input.readline)
    mol.number = n + 1
    @nodes.push(mol)
  end

  n_bond.times do |n|
    line = input.readline
    b = MDLBond.new line
    b_n = line[0..2].to_i
    e_n = line[3..5].to_i
    if (b_n > n_atom || b_n < 1 || e_n > n_atom || e_n < 1)
      p line
      raise "MDL bond line format error"
    end

    @edges.push([b, @nodes[b_n - 1], @nodes[e_n - 1]])
  end
  input.each do |line|
    break if /M  END/.match(line)
  end
  self
end