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



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

#inchiObject

Returns the value of attribute inchi.



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

def inchi
  @inchi
end

#uriObject

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

Parameters:

  • smiles (String)

    InChI string

Returns:



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.

Examples:

compound = Compound.from_name("Benzene")

Parameters:

  • name (String)

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

Returns:



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

Parameters:

  • smiles (String)

    SDF string

Returns:



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

Examples:

compound = Compound.from_smiles("c1ccccc1")

Parameters:

  • smiles (String)

    Smiles string

Returns:



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

Returns:

  • smiles as string



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

Parameters:

  • Array (Array)

    of feature names

  • Feature (String)

    dataset uri

Returns:

  • (Hash)

    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.

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



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

Examples:

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

Parameters:

  • smarts (String)

    Smarts string

Returns:

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

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



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

Parameters:

  • activating (Array)

    Array with activating Smarts strings

  • deactivating (Array)

    Array with deactivating Smarts strings

Returns:

  • (String)

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

Get gif image

Returns:

  • (image/gif)

    Image data



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

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

#to_image_uriString

Get URI of compound image

Returns:

  • (String)

    Compound image URI



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

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

#to_inchiString

Returns InChI string.

Returns:



86
87
88
# File 'lib/compound.rb', line 86

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:



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

Get png image

Examples:

image = compound.to_png

Returns:

  • (image/png)

    Image data



112
113
114
# File 'lib/compound.rb', line 112

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

#to_sdfString

Get sdf

Returns:



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

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

#to_smilesString

Returns Smiles string.

Returns:



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

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