Class: OpenTox::Compound

Inherits:
Object
  • Object
show all
Includes:
OpenTox
Defined in:
lib/compound.rb

Overview

Ruby wrapper for OpenTox Compound Webservices (opentox.org/dev/apis/api-1.2/structure).

Instance Attribute Summary collapse

Attributes included from OpenTox

#metadata

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

compound = Compound.new("http://webservices.in-silico.ch/compound/InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H"")

Parameters:

  • uri (optional, String) (defaults to: nil)

    Compound URI



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

#inchiObject

Returns the value of attribute inchi.



12
13
14
# File 'lib/compound.rb', line 12

def inchi
  @inchi
end

#uriObject

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

Parameters:

  • smiles (String)

    InChI string

Returns:



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.

Examples:

compound = Compound.from_name("Benzene")

Parameters:

  • name (String)

    name can be also an InChI/InChiKey, CAS number, etc

Returns:



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

Parameters:

  • smiles (String)

    SDF string

Returns:



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

Examples:

compound = Compound.from_smiles("c1ccccc1")

Parameters:

  • smiles (String)

    Smiles string

Returns:



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

Returns:

  • smiles as string



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

Parameters:

  • Array (Array)

    of feature names

  • Feature (String)

    dataset uri

  • Comma (String)

    separated pc types

  • Comma (String)

    separated lib

Returns:

  • (Hash)

    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.

Examples:

compound = Compound.from_name("Benzene")
compound.match(['cc','cN']) # returns ['cc']

Parameters:

  • smarts_array (Array)

    Array with Smarts strings

Returns:

  • (Array)

    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

Examples:

compound = Compound.from_name("Benzene")
compound.match?("cN") # returns false

Parameters:

  • smarts (String)

    Smarts string

Returns:

  • (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

Examples:

compound = Compound.from_name("Benzene")
compound.match(['cc','cN']) # returns ['cc']

Parameters:

  • smarts_array (Array)

    Array with Smarts strings

Returns:

  • (Hash)

    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

Parameters:

  • activating (Array)

    Array with activating Smarts strings

  • deactivating (Array)

    Array with deactivating Smarts strings

Returns:

  • (String)

    URI for 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_hashHash

Get all known compound names sorted by classification. Relies on an external service for name lookups.

Examples:

names = compound.to_names_hash

Returns:

  • (Hash)

    Classification => Name Array



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_gifimage/gif

Get gif image

Returns:

  • (image/gif)

    Image data



109
110
111
# File 'lib/compound.rb', line 109

def to_gif
	RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/image")
end

#to_image_uriString

Get URI of compound image

Returns:

  • (String)

    Compound image URI



123
124
125
# File 'lib/compound.rb', line 123

def to_image_uri
    File.join @uri, "image"
end

#to_inchiString

Returns InChI string.

Returns:



91
92
93
# File 'lib/compound.rb', line 91

def to_inchi
    @inchi
end

#to_namesString

Get all known compound names. Relies on an external service for name lookups.

Examples:

names = compound.to_names

Returns:



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_hashHash

Get all known compound names sorted by classification. Relies on an external service for name lookups.

Examples:

names = compound.to_names_hash

Returns:

  • (Hash)

    Classification => Name Array



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_pngimage/png

Get png image

Examples:

image = compound.to_png

Returns:

  • (image/png)

    Image data



117
118
119
# File 'lib/compound.rb', line 117

def to_png
    RestClientWrapper.get(File.join @uri, "image")
end

#to_sdfString

Get sdf

Returns:



103
104
105
# File 'lib/compound.rb', line 103

def to_sdf
	Compound.obconversion(@inchi,'inchi','sdf')
end

#to_smilesString

Returns Smiles string.

Returns:



97
98
99
# File 'lib/compound.rb', line 97

def to_smiles
	Compound.obconversion(@inchi,'inchi','can')
end