Class: Chem::MDL::SdfParser

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chem/db/sdf.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ SdfParser

Returns a new instance of SdfParser.


14
15
16
17
# File 'lib/chem/db/sdf.rb', line 14

def initialize file
  require 'chem/db/mdl'
  @input = open(file)
end

Class Method Details

.parse(file) ⇒ Object


56
57
58
# File 'lib/chem/db/sdf.rb', line 56

def self.parse file
  SdfParser.new(file)
end

Instance Method Details

#eachObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chem/db/sdf.rb', line 19

def each
  @input.rewind

  # for \r\n and \n
  first_entry = true
  from = 0
  @input.each("$$$$") do |entry|
    from = entry.index("\n") + 1 unless first_entry
    first_entry = false
    next if entry[from..-1].length < 3
    molio = StringIO.new(entry[from..-1])
    mol = MdlMolecule.parse_io(molio)
    mol.sdf_data = {}
    data_header = nil

    molio.each do |line|
      if line[0..0] == ">"
        if /<([^>]+)>/.match(line)
          data_header = $1
        elsif /(DT\d+)/.match(line)
          data_header = $1
        end
        mol.sdf_data[data_header] = []
      elsif /^$/.match(line)
        if mol.sdf_data[data_header].respond_to?(:join)
          mol.sdf_data[data_header] = mol.sdf_data[data_header].join("\n")
        end
        # end of data
      else
        mol.sdf_data[data_header] << line.chop
      end
    end
    yield mol
  end

end