Class: OpenTox::Compound

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = nil) ⇒ OpenTox::Compound

Returns Compound.

Examples:

compound = OpenTox::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



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

#inchiObject

Returns the value of attribute inchi.



9
10
11
# File 'lib/compound.rb', line 9

def inchi
  @inchi
end

#uriObject

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

Parameters:

  • smiles (String)

    InChI string

Returns:



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.

Examples:

compound = OpenTox::Compound.from_name("Benzene")

Parameters:

  • name (String)

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

Returns:



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

Parameters:

  • smiles (String)

    SDF string

Returns:



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

Examples:

compound = OpenTox::Compound.from_smiles("c1ccccc1")

Parameters:

  • smiles (String)

    Smiles string

Returns:



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.

Examples:

compound = OpenTox::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



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

Examples:

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

Parameters:

  • smarts (String)

    Smarts string

Returns:

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

Parameters:

  • activating (Array)

    Array with activating Smarts strings

  • deactivating (Array)

    Array with deactivating Smarts strings

Returns:

  • (String)

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

Get gif image

Returns:

  • (image/gif)

    Image data



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

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

#to_image_uriString

Get URI of compound image

Returns:

  • (String)

    Compound image URI



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

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

#to_inchiString

Returns InChI string.

Returns:



73
74
75
# File 'lib/compound.rb', line 73

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:



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

Get png image

Examples:

image = compound.to_png

Returns:

  • (image/png)

    Image data



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

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

#to_sdfString

Get sdf

Returns:



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

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

#to_smilesString

Returns Smiles string.

Returns:



79
80
81
# File 'lib/compound.rb', line 79

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