Class: OpenTox::Compound

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

Overview

< OpenTox

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Compound

Initialize with :uri => uri, :smiles => smiles or :name => name (name can be also an InChI/InChiKey, CAS number, etc)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/compound.rb', line 11

def initialize(params)
	if params[:smiles]
		@inchi = smiles2inchi(params[:smiles])
		@uri = File.join(@@config[:services]["opentox-compound"],URI.escape(@inchi))
	elsif params[:inchi]
		@inchi = params[:inchi]
		@uri = File.join(@@config[:services]["opentox-compound"],URI.escape(@inchi))
	elsif params[:sdf]
		@inchi = sdf2inchi(params[:sdf])
		@uri = File.join(@@config[:services]["opentox-compound"],URI.escape(@inchi))
	elsif params[:name]
		# paranoid URI encoding to keep SMILES charges and brackets
		@inchi = RestClient.get("#{@@cactus_uri}#{URI.encode(params[:name], Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}/stdinchi").body.chomp
		# this was too hard for me to debug and leads to additional errors (ch)
		#@inchi = RestClientWrapper.get("#{@@cactus_uri}#{URI.encode(params[:name], Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}/stdinchi").chomp
		@uri = File.join(@@config[:services]["opentox-compound"],URI.escape(@inchi))
	elsif params[:uri]
		@uri = params[:uri]
		case params[:uri]
		when /ambit/ # Ambit does not deliver InChIs reliably
			smiles = RestClientWrapper.get @uri, :accept => 'chemical/x-daylight-smiles'
			@inchi = obconversion(smiles,'smi','inchi')
		when /InChI/ # shortcut for IST services
			@inchi = params[:uri].sub(/^.*InChI/, 'InChI')
		else
			@inchi = RestClientWrapper.get @uri, :accept => 'chemical/x-inchi'
		end
	end
end

Instance Attribute Details

#inchiObject (readonly)

Returns the value of attribute inchi.



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

def inchi
  @inchi
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#display_smarts_uri(activating, deactivating, highlight = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/compound.rb', line 66

def display_smarts_uri(activating, deactivating, highlight = nil)
  LOGGER.debug activating.to_yaml unless activating.nil?
  activating_smarts = URI.encode "\"#{activating.join("\"/\"")}\""
  deactivating_smarts = URI.encode "\"#{deactivating.join("\"/\"")}\""
  if highlight.nil?
    File.join @@config[:services]["opentox-compound"], "smiles", URI.encode(smiles), "smarts/activating", URI.encode(activating_smarts),"deactivating", URI.encode(deactivating_smarts)
  else
    File.join @@config[:services]["opentox-compound"], "smiles", URI.encode(smiles), "smarts/activating", URI.encode(activating_smarts),"deactivating", URI.encode(deactivating_smarts), "highlight", URI.encode(highlight)
  end
end

#gifObject



50
51
52
# File 'lib/compound.rb', line 50

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

#image_uriObject



77
78
79
# File 'lib/compound.rb', line 77

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

#match(smarts_array) ⇒ Object

Match an array of smarts features, returns matching features



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

def match(smarts_array)
	smarts_array.collect{|s| s if match?(s)}.compact
end

#match?(smarts) ⇒ Boolean

Matchs a smarts string

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
# File 'lib/compound.rb', line 82

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_all(smarts_array) ⇒ Object

AM Match an array of smarts features, returns (0)1 for (non)matching features at each pos



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

def match_all(smarts_array)
	smarts_array.collect{|s| match?(s) ? 1 : 0 }
end

#namesObject



58
59
60
61
62
63
64
# File 'lib/compound.rb', line 58

def names
    begin
      RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/names")
    rescue
      "not available"
    end
end

#obconversion(identifier, input_format, output_format) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/compound.rb', line 115

def obconversion(identifier,input_format,output_format)
	obconversion = OpenBabel::OBConversion.new
	obmol = OpenBabel::OBMol.new
	obconversion.set_in_and_out_formats input_format, output_format
	obconversion.read_string obmol, identifier
	case output_format
	when /smi|can|inchi/
		obconversion.write_string(obmol).gsub(/\s/,'').chomp
	else
		obconversion.write_string(obmol)
	end
end

#pngObject



54
55
56
# File 'lib/compound.rb', line 54

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

#sdfObject



46
47
48
# File 'lib/compound.rb', line 46

def sdf
	obconversion(@inchi,'inchi','sdf')
end

#sdf2inchi(sdf) ⇒ Object



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

def sdf2inchi(sdf)
	obconversion(sdf,'sdf','inchi')
end

#smilesObject

Get the (canonical) smiles



42
43
44
# File 'lib/compound.rb', line 42

def smiles
	obconversion(@inchi,'inchi','can')
end

#smiles2cansmi(smiles) ⇒ Object



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

def smiles2cansmi(smiles)
	obconversion(smiles,'smi','can')
end

#smiles2inchi(smiles) ⇒ Object



107
108
109
# File 'lib/compound.rb', line 107

def smiles2inchi(smiles)
	obconversion(smiles,'smi','inchi')
end