summaryrefslogtreecommitdiff
path: root/compound.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2013-01-18 13:45:04 +0100
committerChristoph Helma <helma@in-silico.ch>2013-01-18 13:45:04 +0100
commit29fcbe3990952d61565eafd45d3f00ed9d4f297c (patch)
tree3e8406cabd4a63c9b45880e466d18582910187ec /compound.rb
parenta4c8201828614b40e01f37aed31727cd60754620 (diff)
local inchi conversion, url_for removed, gem dependencies in gemspec
Diffstat (limited to 'compound.rb')
-rw-r--r--compound.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/compound.rb b/compound.rb
new file mode 100644
index 0000000..cb194ff
--- /dev/null
+++ b/compound.rb
@@ -0,0 +1,58 @@
+require "openbabel"
+module OpenTox
+
+ # Perform OpenBabel conversions locally in order to prevent net overhead
+ class Compound
+
+ attr_writer :smiles, :inchi
+
+ # Create a compound from smiles string
+ # @example
+ # compound = OpenTox::Compound.from_smiles("c1ccccc1")
+ # @param [String] smiles Smiles string
+ # @return [OpenTox::Compound] Compound
+ def self.from_smiles service_uri, smiles, subjectid=nil
+ inchi = obconversion(smiles,'smi','inchi')
+ compound = Compound.new(File.join service_uri, inchi)
+ compound.inchi = inchi
+ compound.smiles = smiles
+ compound
+ end
+
+ # Create a compound from inchi string
+ # @param [String] smiles InChI string
+ # @return [OpenTox::Compound] Compound
+ def self.from_inchi service_uri, inchi, subjectid=nil
+ compound = Compound.new(File.join service_uri, inchi)
+ compound.inchi = inchi
+ compound
+ end
+
+ # Create a compound from sdf string
+ # @param [String] smiles SDF string
+ # @return [OpenTox::Compound] Compound
+ def self.from_sdf service_uri, sdf, subjectid=nil
+ inchi = obconversion(sdf,'sdf','inchi')
+ compound = Compound.new(File.join service_uri, inchi)
+ compound.inchi = inchi
+ compound
+ end
+
+ private
+
+ # Convert identifier from OpenBabel input_format to OpenBabel output_format
+ def self.obconversion(identifier,input_format,output_format)
+ obconversion = OpenBabel::OBConversion.new
+ obmol = OpenBabel::OBMol.new
+ obconversion.set_in_and_out_formats input_format, output_format
+ obconversion.read_string obmol, identifier
+ case output_format
+ when /smi|can|inchi/
+ obconversion.write_string(obmol).gsub(/\s/,'').chomp
+ else
+ obconversion.write_string(obmol)
+ end
+ end
+ end
+
+end