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, 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_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.
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.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/compound.rb', line 18 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 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 end |
Instance Attribute Details
#inchi ⇒ Object
Returns the value of attribute inchi.
11 12 13 |
# File 'lib/compound.rb', line 11 def inchi @inchi end |
#uri ⇒ Object
Returns the value of attribute uri.
11 12 13 |
# File 'lib/compound.rb', line 11 def uri @uri end |
Class Method Details
.from_inchi(inchi) ⇒ Compound
Create a compound from inchi string
54 55 56 57 58 59 |
# File 'lib/compound.rb', line 54 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.
76 77 78 79 80 81 82 |
# File 'lib/compound.rb', line 76 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
64 65 66 67 68 69 |
# File 'lib/compound.rb', line 64 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
44 45 46 47 48 49 |
# File 'lib/compound.rb', line 44 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
35 36 37 |
# File 'lib/compound.rb', line 35 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, subjectid = nil) ⇒ Hash
Lookup numerical values, returns hash with feature name as key and value as value
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/compound.rb', line 201 def lookup(feature_array,feature_dataset_uri,pc_type,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| if c_uri.split('/compound/').last == self.to_inchi entry = ds.data_entries[self.uri] break end } LOGGER.debug "#{entry.size} entries in feature ds for query." unless entry.nil? if entry.nil? uri, smiles_to_inchi = OpenTox::Algorithm.get_pc_descriptors({:compounds => [self.uri], :pc_type => pc_type}) uri = OpenTox::Algorithm.load_ds_csv(uri, smiles_to_inchi, subjectid) ds = OpenTox::Dataset.find(uri,subjectid) entry = ds.data_entries[self.uri] 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]} #LOGGER.debug "----- am #{entry.to_yaml}" entry end |
#match(smarts_array) ⇒ Array
Returns Array with matching Smarts strings.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/compound.rb', line 155 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
139 140 141 142 143 144 145 146 147 |
# File 'lib/compound.rb', line 139 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
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/compound.rb', line 175 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
237 238 239 240 241 |
# File 'lib/compound.rb', line 237 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_gif ⇒ image/gif
Get gif image
104 105 106 |
# File 'lib/compound.rb', line 104 def to_gif RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/image") end |
#to_image_uri ⇒ String
Get URI of compound image
118 119 120 |
# File 'lib/compound.rb', line 118 def to_image_uri File.join @uri, "image" end |
#to_inchi ⇒ String
Returns InChI string.
86 87 88 |
# File 'lib/compound.rb', line 86 def to_inchi @inchi end |
#to_names ⇒ String
Get all known compound names. Relies on an external service for name lookups.
126 127 128 129 130 131 132 |
# File 'lib/compound.rb', line 126 def to_names begin RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/names").split("\n") rescue "not available" end end |
#to_png ⇒ image/png
Get png image
112 113 114 |
# File 'lib/compound.rb', line 112 def to_png RestClientWrapper.get(File.join @uri, "image") end |