summaryrefslogtreecommitdiff
path: root/lib/model.rb
blob: cd35f09e84a0a73ff5010f0f7291e1d1649b1002 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
module OpenTox
	module Model
   
		class Lazar
			include Owl

			attr_accessor :dataset, :predictions
			
			# Create a new prediction model from a dataset
			def initialize(yaml)
				super()
				id = File.basename(yaml,'.yaml')
				# TODO Untyped Individual: http://localhost:4003/lazar/{id} ????
				@lazar = YAML.load_file yaml
				self.uri = File.join(@@config[:services]["opentox-model"],'lazar',id)
				self.title = "lazar model for #{@lazar[:endpoint]}"
				self.source = "http://github.com/helma/opentox-model"
				self.parameters = {
					"Dataset URI" => { :scope => "mandatory", :value => "dataset_uri=#{@lazar[:activity_dataset]}" },
					"Feature URI for dependent variable" => { :scope => "mandatory", :value => "feature_uri=#{@lazar[:endpoint]}" },
					"Feature generation URI" => { :scope => "mandatory", :value => "feature_generation_uri=" } #TODO write to yaml
				}
				self.algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
				self.trainingDataset = @lazar[:activity_dataset]
				self.dependentVariables = @lazar[:endpoint]
				self.independentVariables = "http://localhost:4002/fminer#BBRC_representative" # TODO read this from dataset
				self.predictedVariables = @lazar[:endpoint] #+ " lazar prediction"
				@dataset = OpenTox::Dataset.new
				@predictions = {}
			end

			def self.find(uri)
=begin
				begin
					YAML.load(RestClient.get uri)
					Lazar.new uri
				rescue
					halt 404, "Model #{uri} not found."
				end
=end
			end

			def self.find_all
				RestClient.get(@@config[:services]["opentox-model"]).split("\n")
			end
			
			# Predict a compound
			def predict(compound)
				RestClient.post(@uri, :compound_uri => compound.uri)
			end

			def database_activity?(compound_uri)
				# find database activities
				db_activities = @lazar[:activities][compound_uri]
				if db_activities
					c = @dataset.find_or_create_compound(compound_uri)
					f = @dataset.find_or_create_feature(@lazar[:endpoint])
					v = db_activities.join(',')
					@dataset.add c,f,v
					@predictions[compound_uri] = { @lazar[:endpoint] => {:measured_activities => db_activities}}
					true
				else
					false
				end
			end

			def classify(compound_uri)

				compound = OpenTox::Compound.new(:uri => compound_uri)
				compound_matches = compound.match @lazar[:features]

				conf = 0.0
				neighbors = []
				classification = nil

				@lazar[:fingerprints].each do |uri,matches|

					sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(compound_matches,matches,@lazar[:p_values])
					if sim > 0.3
						neighbors << uri
						@lazar[:activities][uri].each do |act|
							case act.to_s
							when 'true'
								conf += OpenTox::Utils.gauss(sim)
							when 'false'
								conf -= OpenTox::Utils.gauss(sim)
							end
						end
					end
			  end
      
				conf = conf/neighbors.size
				if conf > 0.0
					classification = true
				elsif conf < 0.0
					classification = false
				end
			  
				compound = @dataset.find_or_create_compound(compound_uri)
				feature = @dataset.find_or_create_feature(@lazar[:endpoint])

        if (classification != nil)
  				tuple = @dataset.create_tuple(feature,{ 'lazar#classification' => classification, 'lazar#confidence' => conf})
  				@dataset.add_tuple compound,tuple
  				@predictions[compound_uri] = { @lazar[:endpoint] => { :lazar_prediction => {
  						:classification => classification,
  						:confidence => conf,
  						:neighbors => neighbors,
  						:features => compound_matches
  					} } }
  			end
			end

			def self.base_uri
				@@config[:services]["opentox-model"]
			end

			def self.create(data)
				RestClient.post(@@config[:services]["opentox-model"], data, :content_type => "application/x-yaml").to_s
			end

			def endpoint
				YAML.load(RestClient.get uri)[:endpoint]
			end

			def algorithm=(algorithm)
				me = @model.subject(RDF['type'],OT[self.owl_class])
				@model.add me, OT['algorithm'], Redland::Uri.new(algorithm) # untyped individual comes from this line, why??
				@model.add Redland::Uri.new(algorithm), RDF['type'], OT['Algorithm']
			end

			def trainingDataset=(trainingDataset)
				me = @model.subject(RDF['type'],OT[self.owl_class])
				@model.add me, OT['trainingDataset'], Redland::Uri.new(trainingDataset) # untyped individual comes from this line, why??
				@model.add Redland::Uri.new(trainingDataset), RDF['type'], OT['Dataset']
			end

			def dependentVariables=(dependentVariables)
				me = @model.subject(RDF['type'],OT[self.owl_class])
				@model.add me, OT['dependentVariables'], Redland::Uri.new(dependentVariables) # untyped individual comes from this line, why??
				@model.add Redland::Uri.new(dependentVariables), RDF['type'], OT['Feature']
			end

			def independentVariables=(independentVariables)
				me = @model.subject(RDF['type'],OT[self.owl_class])
				@model.add me, OT['independentVariables'], Redland::Uri.new(independentVariables) # untyped individual comes from this line, why??
				@model.add Redland::Uri.new(independentVariables), RDF['type'], OT['Feature']
			end

			def predictedVariables=(predictedVariables)
				me = @model.subject(RDF['type'],OT[self.owl_class])
				@model.add me, OT['predictedVariables'], Redland::Uri.new(predictedVariables) # untyped individual comes from this line, why??
				@model.add Redland::Uri.new(predictedVariables), RDF['type'], OT['Feature']
			end
		end
	end
end