Class: OpenTox::Compound
- Inherits:
-
Object
- Object
- OpenTox::Compound
- Defined in:
- lib/compound.rb
Overview
Ruby wrapper for OpenTox Compound Webservices (opentox.org/dev/apis/api-1.2/structure).
Instance Attribute Summary collapse
-
#inchi ⇒ Object
Returns the value of attribute inchi.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
-
.from_inchi(inchi) ⇒ OpenTox::Compound
Create a compound from inchi string.
-
.from_name(name) ⇒ OpenTox::Compound
Create a compound from name.
-
.from_sdf(sdf) ⇒ OpenTox::Compound
Create a compound from sdf string.
-
.from_smiles(smiles) ⇒ OpenTox::Compound
Create a compound from smiles string.
Instance Method Summary collapse
-
#initialize(uri = nil) ⇒ OpenTox::Compound
constructor
Compound.
-
#match(smarts_array) ⇒ Array
Array with matching Smarts strings.
- #match?(smarts) ⇒ Boolean
-
#matching_smarts_image_uri(activating, deactivating) ⇒ String
Get URI of compound image with highlighted fragments.
-
#to_gif ⇒ image/gif
Get gif image.
-
#to_image_uri ⇒ String
Get URI of compound image.
-
#to_inchi ⇒ String
InChI string.
-
#to_names ⇒ String
Get all known compound names.
-
#to_png ⇒ image/png
Get png image.
-
#to_sdf ⇒ String
Get sdf.
-
#to_smiles ⇒ String
Smiles string.
Constructor Details
#initialize(uri = nil) ⇒ OpenTox::Compound
Returns Compound.
16 17 18 19 20 21 22 23 24 |
# File 'lib/compound.rb', line 16 def initialize(uri=nil) @uri = uri case @uri when /InChI/ # shortcut for IST services @inchi = @uri.sub(/^.*InChI/, 'InChI') else @inchi = RestClientWrapper.get(@uri, :accept => 'chemical/x-inchi').to_s.chomp if @uri end end |
Instance Attribute Details
#inchi ⇒ Object
Returns the value of attribute inchi.
9 10 11 |
# File 'lib/compound.rb', line 9 def inchi @inchi end |
#uri ⇒ Object
Returns the value of attribute uri.
9 10 11 |
# File 'lib/compound.rb', line 9 def uri @uri end |
Class Method Details
.from_inchi(inchi) ⇒ OpenTox::Compound
Create a compound from inchi string
41 42 43 44 45 46 |
# File 'lib/compound.rb', line 41 def self.from_inchi(inchi) c = Compound.new c.inchi = inchi c.uri = File.join(CONFIG[:services]["opentox-compound"],URI.escape(c.inchi)) c end |
.from_name(name) ⇒ OpenTox::Compound
Create a compound from name. Relies on an external service for name lookups.
63 64 65 66 67 68 69 |
# File 'lib/compound.rb', line 63 def self.from_name(name) c = Compound.new # paranoid URI encoding to keep SMILES charges and brackets c.inchi = RestClientWrapper.get("#{@@cactus_uri}#{URI.encode(name, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}/stdinchi").to_s.chomp c.uri = File.join(CONFIG[:services]["opentox-compound"],URI.escape(c.inchi)) c end |
.from_sdf(sdf) ⇒ OpenTox::Compound
Create a compound from sdf string
51 52 53 54 55 56 |
# File 'lib/compound.rb', line 51 def self.from_sdf(sdf) c = Compound.new c.inchi = Compound.sdf2inchi(sdf) c.uri = File.join(CONFIG[:services]["opentox-compound"],URI.escape(c.inchi)) c end |
.from_smiles(smiles) ⇒ OpenTox::Compound
Create a compound from smiles string
31 32 33 34 35 36 |
# File 'lib/compound.rb', line 31 def self.from_smiles(smiles) c = Compound.new c.inchi = Compound.smiles2inchi(smiles) c.uri = File.join(CONFIG[:services]["opentox-compound"],URI.escape(c.inchi)) c end |
Instance Method Details
#match(smarts_array) ⇒ Array
Returns Array with matching Smarts strings.
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/compound.rb', line 142 def match(smarts_array) # avoid recreation of OpenBabel objects obconversion = OpenBabel::OBConversion.new obmol = OpenBabel::OBMol.new obconversion.set_in_format('inchi') obconversion.read_string(obmol,@inchi) smarts_pattern = OpenBabel::OBSmartsPattern.new smarts_array.collect do |smarts| smarts_pattern.init(smarts) smarts if smarts_pattern.match(obmol) end.compact #smarts_array.collect { |s| s if match?(s)}.compact end |
#match?(smarts) ⇒ Boolean
126 127 128 129 130 131 132 133 134 |
# File 'lib/compound.rb', line 126 def match?(smarts) obconversion = OpenBabel::OBConversion.new obmol = OpenBabel::OBMol.new obconversion.set_in_format('inchi') obconversion.read_string(obmol,@inchi) smarts_pattern = OpenBabel::OBSmartsPattern.new smarts_pattern.init(smarts) smarts_pattern.match(obmol) end |
#matching_smarts_image_uri(activating, deactivating) ⇒ String
Get URI of compound image with highlighted fragments
161 162 163 164 165 |
# File 'lib/compound.rb', line 161 def matching_smarts_image_uri(activating, deactivating) activating_smarts = URI.encode "\"#{activating.join("\"/\"")}\"" deactivating_smarts = URI.encode "\"#{deactivating.join("\"/\"")}\"" File.join @uri, "smarts/activating", URI.encode(activating_smarts),"deactivating", URI.encode(deactivating_smarts) end |
#to_gif ⇒ image/gif
Get gif image
91 92 93 |
# File 'lib/compound.rb', line 91 def to_gif RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/image") end |
#to_image_uri ⇒ String
Get URI of compound image
105 106 107 |
# File 'lib/compound.rb', line 105 def to_image_uri File.join @uri, "image" end |
#to_inchi ⇒ String
Returns InChI string.
73 74 75 |
# File 'lib/compound.rb', line 73 def to_inchi @inchi end |
#to_names ⇒ String
Get all known compound names. Relies on an external service for name lookups.
113 114 115 116 117 118 119 |
# File 'lib/compound.rb', line 113 def to_names begin RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/names").split("\n") rescue "not available" end end |
#to_png ⇒ image/png
Get png image
99 100 101 |
# File 'lib/compound.rb', line 99 def to_png RestClientWrapper.get(File.join @uri, "image") end |