summaryrefslogtreecommitdiff
path: root/lib/loael-mazzatorta.rb
blob: 890dbed970b9d5284067b0f13a20f6bc0c870508 (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
require 'csv'
require 'tempfile'

class String
  def float?
    Float(self) rescue false
  end
end
class LoaelMazzatorta

  def self.predict smiles
    smiles = [smiles] unless smiles.is_a? Array
    predictions = []
    input = Tempfile.new("/tmp/smi")
    begin
      input.write smiles.join("\n")
      input.close
      loaels = `cd #{File.join(File.dirname(__FILE__),"..","java")}; java -cp LoaelMazzatorta.class:CustomDescriptors.class:insilicoCore.jar:cdk-1.4.9-pruned.jar:. LoaelMazzatorta #{input.path}`.split "\n"
      loaels.each_with_index do |loael,i|
        if loael.float?
          predictions << {"value" => loael.to_f, "SMILES" => smiles[i]}
        else
          predictions << {"warnings" => [loael], "SMILES" => smiles[i]}
        end
      end
    ensure
      input.unlink
    end
    predictions.size == 1 ? predictions[0] : predictions
  end

end