Class: Chem::MDL::SdfParser
- Inherits:
-
Object
- Object
- Chem::MDL::SdfParser
- Includes:
- Enumerable
- Defined in:
- lib/chem/db/sdf.rb
Class Method Summary collapse
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(file) ⇒ SdfParser
constructor
A new instance of SdfParser.
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
Instance Method Details
#each ⇒ Object
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 |