Class: OpenTox::Compound
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.
Attributes included from OpenTox
Class Method Summary collapse
-
.from_inchi(inchi) ⇒ Compound
Create a compound from inchi string.
-
.from_name(name) ⇒ Compound
Create a compound from name.
-
.from_sdf(sdf) ⇒ Compound
Create a compound from sdf string.
-
.from_smiles(smiles) ⇒ Compound
Create a compound from smiles string.
-
.smiles(uri) ⇒ Object
request smiles from compound service via accept header.
Instance Method Summary collapse
-
#initialize(uri = nil) ⇒ Compound
constructor
Compound.
-
#lookup(feature_array, feature_dataset_uri, pc_type, lib, subjectid = nil) ⇒ Hash
Lookup numerical values, returns hash with feature name as key and value as value.
-
#match(smarts_array) ⇒ Array
Array with matching Smarts strings.
- #match?(smarts) ⇒ Boolean
-
#match_hits(smarts_array) ⇒ Hash
Match_hits an array of smarts strings, returns hash with matching smarts as key and number of non-unique hits as value.
-
#matching_smarts_image_uri(activating, deactivating) ⇒ String
Get URI of compound image with highlighted fragments.
-
#to_ambit_names_hash ⇒ Hash
Get all known compound names sorted by classification.
-
#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_names_hash ⇒ Hash
Get all known compound names sorted by classification.
-
#to_png ⇒ image/png
Get png image.
-
#to_sdf ⇒ String
Get sdf.
-
#to_smiles ⇒ String
Smiles string.
Methods included from OpenTox
#add_metadata, all, #delete, #load_metadata, sign_in, text_to_html, #to_rdfxml
Constructor Details
#initialize(uri = nil) ⇒ Compound
Returns Compound.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/compound.rb', line 19 def initialize(uri=nil) @uri = uri if (@uri =~ URI::regexp) || @uri.nil? 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 if @uri and @inchi.to_s.size==0 LOGGER.warn "REMOVE ABMIT HACK: no inchi for compound "+@uri.to_s+", load via smiles" @inchi = Compound.smiles2inchi(Compound.smiles(@uri)) end else raise "Not able to create compound with uri: #{@uri}" end end |
Instance Attribute Details
#inchi ⇒ Object
Returns the value of attribute inchi.
12 13 14 |
# File 'lib/compound.rb', line 12 def inchi @inchi end |
#uri ⇒ Object
Returns the value of attribute uri.
12 13 14 |
# File 'lib/compound.rb', line 12 def uri @uri end |
Class Method Details
.from_inchi(inchi) ⇒ Compound
Create a compound from inchi string
59 60 61 62 63 64 |
# File 'lib/compound.rb', line 59 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) ⇒ Compound
Create a compound from name. Relies on an external service for name lookups.
81 82 83 84 85 86 87 |
# File 'lib/compound.rb', line 81 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) ⇒ Compound
Create a compound from sdf string
69 70 71 72 73 74 |
# File 'lib/compound.rb', line 69 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) ⇒ Compound
Create a compound from smiles string
49 50 51 52 53 54 |
# File 'lib/compound.rb', line 49 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 |
.smiles(uri) ⇒ Object
request smiles from compound service via accept header
40 41 42 |
# File 'lib/compound.rb', line 40 def self.smiles(uri) RestClientWrapper.get(uri, :accept => 'chemical/x-daylight-smiles').to_s.chomp end |
Instance Method Details
#lookup(feature_array, feature_dataset_uri, pc_type, lib, subjectid = nil) ⇒ Hash
Lookup numerical values, returns hash with feature name as key and value as value
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/compound.rb', line 249 def lookup(feature_array,feature_dataset_uri,pc_type,lib,subjectid=nil) ds = OpenTox::Dataset.find(feature_dataset_uri,subjectid) #entry = ds.data_entries[self.uri] entry = nil ds.data_entries.each { |c_uri, values| compound = OpenTox::Compound.new(c_uri) if compound.to_inchi == self.to_inchi # Compare compounds by InChI entry = ds.data_entries[c_uri] break end } LOGGER.debug "#{entry.size} entries in feature ds for query." unless entry.nil? if entry.nil? temp_ds = OpenTox::Dataset.create(CONFIG[:services]["opentox-dataset"],subjectid); temp_ds.add_compound(self.uri); temp_uri = temp_ds.save(subjectid) uri = RestClientWrapper.post(File.join(CONFIG[:services]["opentox-algorithm"], "/pc/AllDescriptors"), {:dataset_uri => temp_uri, :pc_type => pc_type, :lib => lib, :subjectid => subjectid}) ds = OpenTox::Dataset.find(uri, subjectid) entry = ds.data_entries[self.uri] ds.delete(subjectid) temp_ds.delete(subjectid) end features = entry.keys features.each { |feature| new_feature = File.join(feature_dataset_uri, "feature", feature.split("/").last) entry[new_feature] = entry[feature].flatten.first.to_f # see algorithm/lazar.rb:182, to_f because feature type detection doesn't work w 1 entry entry.delete(feature) unless feature == new_feature # e.g. when loading from ambit } #res = feature_array.collect {|v| entry[v]} entry end |
#match(smarts_array) ⇒ Array
Returns Array with matching Smarts strings.
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/compound.rb', line 201 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
185 186 187 188 189 190 191 192 193 |
# File 'lib/compound.rb', line 185 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 |
#match_hits(smarts_array) ⇒ Hash
Match_hits an array of smarts strings, returns hash with matching smarts as key and number of non-unique hits as value
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/compound.rb', line 221 def match_hits(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_hits = {} #LOGGER.debug "dv ----------- obmol #{Compound.new(@inchi).to_smiles}" smarts_array.collect do |smarts| #LOGGER.debug "dv ----------- all smarts #{smarts}" smarts_pattern.init(smarts) if smarts_pattern.match(obmol) hits = smarts_pattern.get_map_list smarts_hits[smarts] = hits.size end end #LOGGER.debug "dv ----------- smarts => hits #{smarts_hits}" return smarts_hits #smarts_array.collect { |s| s if match?(s)}.compact end |
#matching_smarts_image_uri(activating, deactivating) ⇒ String
Get URI of compound image with highlighted fragments
285 286 287 288 289 |
# File 'lib/compound.rb', line 285 def matching_smarts_image_uri(activating, deactivating) activating_smarts = "\"#{activating.collect{|smarts| CGI.escape(smarts)}.join("\"/\"")}\"" deactivating_smarts = "\"#{deactivating.collect{|smarts| CGI.escape(smarts)}.join("\"/\"")}\"" File.join @uri, "smarts/activating", activating_smarts, "deactivating", deactivating_smarts end |
#to_ambit_names_hash ⇒ Hash
Get all known compound names sorted by classification. Relies on an external service for name lookups.
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/compound.rb', line 167 def to_ambit_names_hash begin ds = OpenTox::Dataset.new ds.save ds.load_rdfxml(RestClientWrapper.get("http://apps.ideaconsult.net:8080/ambit2/query/compound/search/names?type=smiles&property=&search=#{@inchi}")) ds.save ds.uri rescue "not available" end end |
#to_gif ⇒ image/gif
Get gif image
109 110 111 |
# File 'lib/compound.rb', line 109 def to_gif RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/image") end |
#to_image_uri ⇒ String
Get URI of compound image
123 124 125 |
# File 'lib/compound.rb', line 123 def to_image_uri File.join @uri, "image" end |
#to_inchi ⇒ String
Returns InChI string.
91 92 93 |
# File 'lib/compound.rb', line 91 def to_inchi @inchi end |
#to_names ⇒ String
Get all known compound names. Relies on an external service for name lookups.
131 132 133 134 135 136 137 |
# File 'lib/compound.rb', line 131 def to_names begin RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/names").split("\n") rescue "not available" end end |
#to_names_hash ⇒ Hash
Get all known compound names sorted by classification. Relies on an external service for name lookups.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/compound.rb', line 144 def to_names_hash begin xml = RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/names/xml") xmldoc = REXML::Document.new(xml) data = {} xmldoc.root.elements[1].elements.each{|e| if data.has_key?(e.attribute("classification").value) == false data[e.attribute("classification").value] = [e.text] else data[e.attribute("classification").value].push(e.text) end } data rescue "not available" end end |
#to_png ⇒ image/png
Get png image
117 118 119 |
# File 'lib/compound.rb', line 117 def to_png RestClientWrapper.get(File.join @uri, "image") end |