1
2
3
4
5
6
7
8
9
10
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
require 'rest_client'
require 'crack/xml'
module OpenTox
class OpenTox
attr_reader :uri
# Escape all nonword characters
def uri_escape(string)
URI.escape(string, /[^\w]/)
end
end
class Compound < OpenTox
# Initialize with <tt>:uri => uri</tt>, <tt>:smiles => smiles</tt> or <tt>:name => name</tt> (name can be also an InChI/InChiKey, CAS number, etc)
def initialize(params)
if params[:uri]
@uri = params[:uri].to_s
elsif params[:smiles]
@uri = RestClient.post ENV['OPENTOX_COMPOUNDS'] ,:smiles => uri_escape(params[:smiles])
elsif params[:name]
@uri = RestClient.post ENV['OPENTOX_COMPOUNDS'] ,:name => uri_escape(params[:name])
end
end
# Get the (canonical) smiles
def smiles
RestClient.get @uri
end
# Matchs a smarts string
def match?(smarts)
if RestClient.get(@uri + '/match/' + uri_escape(smarts)) == 'true'
true
else
false
end
end
# Match an array of smarts features, returns matching features
def match(smarts_features)
smarts_features.collect{ |smarts| smarts if self.match?(smarts.name) }.compact
end
end
class Feature < OpenTox
# Initialize with <tt>:uri => uri</tt>, or <tt>:name => name, :values => hash_of_property_names_and_values</tt>
def initialize(params)
if params[:uri]
@uri = params[:uri].to_s
else
@uri = ENV['OPENTOX_FEATURES'] + uri_escape(params[:name])
params[:values].each do |k,v|
@uri += '/' + k.to_s + '/' + v.to_s
end
end
end
# Get the value of a property
def value(property)
RestClient.get @uri + '/' + property
end
# Get the name of the feature
def name
RestClient.get @uri + '/name'
end
end
class Dataset < OpenTox
# Initialize with <tt>:uri => uri</tt> or <tt>:name => name</tt> (creates a new dataset)
def initialize(params)
if params[:uri]
@uri = params[:uri].to_s
elsif params[:name]
@uri = RestClient.post ENV['OPENTOX_DATASETS'], :name => params[:name]
RestClient.delete @uri + '/associations'
end
end
# Get the dataset name
def name
RestClient.get @uri + '/name'
end
# Get all compounds from a dataset
def compounds
RestClient.get(@uri + '/compounds').split("\n").collect{ |c| Compound.new(:uri => c) }
end
# Get all compounds and features from a dataset, returns a hash with compound_uris as keys and arrays of features as values
def all_compounds_and_features
compounds = {}
Crack::XML.parse(RestClient.get @uri + '/compounds/features')['dataset']['compound'].each do |c|
features = c['feature_uri'].collect{ |f| Feature.new :uri => f }
compounds[c['uri']] = features
end
compounds
end
# Get all features from a dataset
def all_features
RestClient.get(@uri + '/features').split("\n").collect{|f| Feature.new(:uri => f)}
end
# Get all features for a compound
def features(compound)
RestClient.get(@uri + '/compound/' + uri_escape(compound.uri) + '/features').split("\n").collect{|f| Feature.new(:uri => f) }
end
# Add a compound and a feature to a dataset
def add(compound,feature)
RestClient.put @uri, :compound_uri => compound.uri, :feature_uri => feature.uri
end
end
class Fminer < OpenTox
# Create a new dataset with BBRC features
def initialize(training_dataset)
@dataset_uri = RestClient.post ENV['OPENTOX_FMINER'], :dataset_uri => training_dataset.uri
end
def dataset
Dataset.new(:uri => @dataset_uri)
end
end
end
|