summaryrefslogtreecommitdiff
path: root/bin/sdf2csv.rb
blob: ba31fad938b1478ad4b007ab274da4cdfdac9db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env ruby
require_relative "../lib/compound"

read_result = false
sdf = ""
feature_name = ""
compound = nil
features = {}
table = [["ID","SMILES"]]

File.readlines(ARGV[0]).each do |line|
  if line.match %r{\$\$\$\$}
    sdf << line
    id = sdf.split("\n").first.chomp
    compound = Compound.from_sdf sdf
    row = [id,compound.smiles]
    features.each do |f,v|
      table[0] << f unless table[0].include? f
      row[table[0].index(f)] = v
    end
    table << row
    sdf = ""
    features = {}
  elsif line.match(/^>\s+</)
    feature_name = line.match(/^>\s+<(.*)>/)[1]
    read_result = true
  else
    if read_result
      value = line.chomp
      features[feature_name] = value
      read_result = false
    else
      sdf << line
    end
  end
end

table.each{|row| puts row.join(",")}