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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
require 'csv'
require 'tempfile'
module OpenTox
module Algorithm
@ambit_descriptor_algorithm_uri = "http://apps.ideaconsult.net:8080/ambit2/algorithm/org.openscience.cdk.qsar.descriptors.molecular."
@ambit_ds_service_uri = "http://apps.ideaconsult.net:8080/ambit2/dataset/"
@ambit_mopac_model_uri = "http://apps.ideaconsult.net:8080/ambit2/model/69632"
@keysfile = File.join(ENV['HOME'], ".opentox", "config", "pc_descriptors.yaml")
include OpenTox
# Calculate physico-chemical descriptors.
# @param[Hash] required: :dataset_uri, :pc_type, :rjb, :task, :add_uri, optional: :descriptor, :lib, :subjectid
# @return[String] dataset uri
def self.pc_descriptors(params)
ds = OpenTox::Dataset.find(params[:dataset_uri],params[:subjectid])
compounds = ds.compounds.collect
task_weights = {"joelib"=> 20, "openbabel"=> 1, "cdk"=> 50 }
task_weights.keys.each { |step| task_weights.delete(step) if (params[:lib] && (!params[:lib].split(",").include?(step)))}
task_weights["load"] = 10
task_sum = Float task_weights.values.sum
task_weights.keys.each { |step| task_weights[step] /= task_sum }
task_weights.keys.each { |step| task_weights[step] = (task_weights[step]*100).floor }
jl_master=nil
cdk_master=nil
ob_master=nil
# # # openbabel (via ruby bindings)
if !params[:lib] || params[:lib].split(",").include?("openbabel")
ob_master, ob_ids = get_ob_descriptors( { :compounds => compounds, :pc_type => params[:pc_type], :descriptor => params[:descriptor] } )
params[:task].progress(params[:task].metadata[OT.percentageCompleted] + task_weights["openbabel"]) if params[:task]
end
# # # joelib (via rjb)
if !params[:lib] || params[:lib].split(",").include?("joelib")
jl_master, jl_ids = get_jl_descriptors( { :compounds => compounds, :rjb => params[:rjb], :pc_type => params[:pc_type], :descriptor => params[:descriptor] } )
params[:task].progress(params[:task].metadata[OT.percentageCompleted] + task_weights["joelib"]) if params[:task]
end
# # # cdk (via REST)
if !params[:lib] || params[:lib].split(",").include?("cdk")
ambit_result_uri, smiles_to_inchi, cdk_ids = get_cdk_descriptors( { :compounds => compounds, :pc_type => params[:pc_type], :task => params[:task], :step => task_weights["cdk"], :descriptor => params[:descriptor] } )
#LOGGER.debug "Ambit result uri for #{params.inspect}: '#{ambit_result_uri.to_yaml}'"
cdk_master, cdk_ids, ambit_ids = load_ds_csv(ambit_result_uri, smiles_to_inchi, cdk_ids )
params[:task].progress(params[:task].metadata[OT.percentageCompleted] + task_weights["load"]) if params[:task]
end
# # # fuse CSVs ("master" structures)
if jl_master && cdk_master
nr_cols = (jl_master[0].size)-1
LOGGER.debug "Merging #{nr_cols} new columns"
cdk_master.each {|row| nr_cols.times { row.push(nil) } }
jl_master.each do |row|
temp = cdk_master.assoc(row[0]) # Finds the appropriate line in master
((-1*nr_cols)..-1).collect.each { |idx|
temp[idx] = row[nr_cols+idx+1] if temp # Updates columns if line is found
}
end
master = cdk_master
else # either jl_master or cdk_master nil
master = jl_master || cdk_master
end
if ob_master && master
nr_cols = (ob_master[0].size)-1
LOGGER.debug "Merging #{nr_cols} new columns"
master.each {|row| nr_cols.times { row.push(nil) } } # Adds empty columns to all rows
ob_master.each do |row|
temp = master.assoc(row[0]) # Finds the appropriate line in master
((-1*nr_cols)..-1).collect.each { |idx|
temp[idx] = row[nr_cols+idx+1] if temp # Updates columns if line is found
}
end
else # either ob_master or master nil
master = ob_master || master
end
if master
ds = OpenTox::Dataset.find(
OpenTox::RestClientWrapper.post(
File.join(CONFIG[:services]["opentox-dataset"]), master.collect { |row| row.join(",") }.join("\n"), {:content_type => "text/csv", :subjectid => params[:subjectid]}
),params[:subjectid]
)
# # # add feature metadata
pc_descriptors = YAML::load_file(@keysfile)
ambit_ids && ambit_ids.each_with_index { |id,idx|
raise "Feature not found" if ! ds.features[File.join(ds.uri, "feature", id.to_s)]
ds.add_feature_metadata(File.join(ds.uri, "feature", id.to_s),{DC.description => "#{pc_descriptors[cdk_ids[idx]][:name]} [#{pc_descriptors[cdk_ids[idx]][:pc_type]}, #{pc_descriptors[cdk_ids[idx]][:lib]}]"})
ds.add_feature_metadata(File.join(ds.uri, "feature", id.to_s),{DC.creator => @ambit_descriptor_algorithm_uri + cdk_ids[idx]})
ds.add_feature_metadata(File.join(ds.uri, "feature", id.to_s),{OT.hasSource => params[:dataset_uri]})
}
ob_ids && ob_ids.each { |id|
raise "Feature not found" if ! ds.features[File.join(ds.uri, "feature", id.to_s)]
ds.add_feature_metadata(File.join(ds.uri, "feature", id.to_s),{DC.description => "#{pc_descriptors[id][:name]} [#{pc_descriptors[id][:pc_type]}, #{pc_descriptors[id][:lib]}]"})
creator_uri = ds.uri.gsub(/\/dataset\/.*/, "/algorithm/pc")
creator_uri += "/#{id}" if params[:add_uri]
ds.add_feature_metadata(File.join(ds.uri, "feature", id.to_s),{DC.creator => creator_uri})
ds.add_feature_metadata(File.join(ds.uri, "feature", id.to_s),{OT.hasSource => params[:dataset_uri]})
}
jl_ids && jl_ids.each { |id|
raise "Feature not found" if ! ds.features[File.join(ds.uri, "feature", id.to_s)]
ds.add_feature_metadata(File.join(ds.uri, "feature", id.to_s),{DC.description => "#{pc_descriptors[id][:name]} [#{pc_descriptors[id][:pc_type]}, #{pc_descriptors[id][:lib]}]"})
creator_uri = ds.uri.gsub(/\/dataset\/.*/, "/algorithm/pc")
creator_uri += "/#{id}" if params[:add_uri]
ds.add_feature_metadata(File.join(ds.uri, "feature", id.to_s),{DC.creator => creator_uri})
ds.add_feature_metadata(File.join(ds.uri, "feature", id.to_s),{OT.hasSource => params[:dataset_uri]})
}
ds.save(params[:subjectid])
else
raise OpenTox::BadRequestError.new "No descriptors matching your criteria found."
end
end
# Calculate OpenBabel physico-chemical descriptors.
# @param[Hash] required: :compounds, :pc_type, :task, optional: :descriptor
# @return[Array] CSV, array of field ids, array of field descriptions
def self.get_ob_descriptors(params)
master = nil
begin
csvfile = Tempfile.open(['ob_descriptors-','.csv'])
pc_descriptors = YAML::load_file(@keysfile)
ids = pc_descriptors.collect{ |id, info|
id if info[:lib] == "openbabel" && params[:pc_type].split(",").include?(info[:pc_type]) && (!params[:descriptor] || id == params[:descriptor])
}.compact
if ids.length > 0
csvfile.puts((["SMILES"] + ids).join(","))
# remember inchis
inchis = params[:compounds].collect { |c_uri|
URI.encode_www_form_component(OpenTox::Compound.new(c_uri).to_inchi)
}
# Process compounds
obmol = OpenBabel::OBMol.new
obconversion = OpenBabel::OBConversion.new
obconversion.set_in_and_out_formats 'inchi', 'can'
inchis.each_with_index { |inchi, c_idx|
row = [inchis[c_idx]]
obconversion.read_string(obmol, URI.decode_www_form_component(inchi))
ids.each { |name|
if obmol.respond_to?(name.underscore)
val = eval("obmol.#{name.underscore}") if obmol.respond_to?(name.underscore)
else
if name != "nF" && name != "spinMult" && name != "nHal" && name != "logP"
val = OpenBabel::OBDescriptor.find_type(name.underscore).predict(obmol)
elsif name == "nF"
val = OpenBabel::OBDescriptor.find_type("nf").predict(obmol)
elsif name == "spinMult" || name == "nHal" || name == "logP"
val = OpenBabel::OBDescriptor.find_type(name).predict(obmol)
end
end
if OpenTox::Algorithm.numeric?(val)
val = Float(val)
val = nil if val.nan?
val = nil if (val && val.infinite?)
end
row << val
}
LOGGER.debug "Compound #{c_idx+1} (#{inchis.size}), #{row.size} entries"
csvfile.puts(row.join(","))
csvfile.flush
}
master = CSV::parse(File.open(csvfile.path, "rb").read)
end
rescue Exception => e
LOGGER.debug "#{e.class}: #{e.message}"
LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
ensure
csvfile.close!
end
[ master, ids ]
end
# Calculate Joelib2 physico-chemical descriptors.
# @param[Hash] required: :compounds, :pc_type, :task, optional: :descriptor
# @return[Array] CSV, array of field ids, array of field descriptions
def self.get_jl_descriptors(params)
master = nil
s = params[:rjb]; raise "No Java environment" unless s
# Load keys, enter CSV headers
begin
csvfile = Tempfile.open(['jl_descriptors-','.csv'])
pc_descriptors = YAML::load_file(@keysfile)
ids = pc_descriptors.collect{ |id, info|
id if info[:lib] == "joelib" && params[:pc_type].split(",").include?(info[:pc_type]) && (!params[:descriptor] || id == params[:descriptor])
}.compact
if ids.length > 0
csvfile.puts((["SMILES"] + ids).join(","))
# remember inchis
inchis = params[:compounds].collect { |c_uri|
cmpd = OpenTox::Compound.new(c_uri)
URI.encode_www_form_component(cmpd.to_inchi)
}
# Process compounds
params[:compounds].each_with_index { |c_uri, c_idx|
cmpd = OpenTox::Compound.new(c_uri)
inchi = cmpd.to_inchi
sdf_data = cmpd.to_sdf
infile = Tempfile.open(['jl_descriptors-in-','.sdf'])
outfile_path = infile.path.gsub(/jl_descriptors-in/,"jl_descriptors-out")
begin
infile.puts sdf_data
infile.flush
s.new(infile.path, outfile_path) # runs joelib
row = [inchis[c_idx]]
ids.each_with_index do |k,i| # Fill row
re = Regexp.new(k)
open(outfile_path) do |f|
f.each do |line|
if @prev == k
entry = line.chomp
val = nil
if OpenTox::Algorithm.numeric?(entry)
val = Float(entry)
val = nil if val.nan?
val = nil if (val && val.infinite?)
end
row << val
break
end
@prev = line.gsub(/^.*types./,"").gsub(/count./,"").gsub(/>/,"").chomp if line =~ re
end
end
end
LOGGER.debug "Compound #{c_idx+1} (#{inchis.size}), #{row.size} entries"
csvfile.puts(row.join(","))
csvfile.flush
rescue Exception => e
LOGGER.debug "#{e.class}: #{e.message}"
LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
ensure
File.delete(infile.path.gsub(/\.sdf/,".numeric.sdf"))
File.delete(outfile_path)
infile.close!
end
}
master = CSV::parse(File.open(csvfile.path, "rb").read)
end
rescue Exception => e
LOGGER.debug "#{e.class}: #{e.message}"
LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
ensure
[ csvfile].each { |f| f.close! }
end
[ master, ids ]
end
# Calculate CDK physico-chemical descriptors via Ambit -- DO NOT OVERLOAD Ambit.
# @param[Hash] required: :compounds, :pc_type, :task, :step optional: :descriptor
# @return[Array] array of Ambit result uri, piecewise (1st: base, 2nd: SMILES, 3rd+: features, hash smiles to inchi, array of field descriptions
def self.get_cdk_descriptors(params)
ambit_result_uri = [] # 1st pos: base uri, then features
smiles_to_inchi = {}
task_weights = {"electronic"=> 4, "topological"=> 19, "constitutional"=> 12, "geometrical"=> 3, "hybrid"=> 2, "cpsa"=> 1 }
task_weights.keys.each { |pc_type| task_weights.delete(pc_type) if (params[:pc_type] && (!params[:pc_type].split(",").include?(pc_type)))}
task_sum = Float task_weights.values.sum
task_weights.keys.each { |pc_type| task_weights[pc_type] /= task_sum }
task_weights.keys.each { |pc_type| task_weights[pc_type] *= params[:step] }
# extract wanted descriptors from config file and parameters
pc_descriptors = YAML::load_file(@keysfile)
ids = pc_descriptors.collect { |id, info|
"#{info[:pc_type]}:::#{id}" if info[:lib] == "cdk" && params[:pc_type].split(",").include?(info[:pc_type]) && (!params[:descriptor] || id == params[:descriptor])
}.compact
if ids.size > 0
ids.sort!
ids.collect! { |id| id.split(":::").last }
# create dataset at Ambit
begin
params[:compounds].each do |n|
cmpd = OpenTox::Compound.new(n)
smiles_string = cmpd.to_smiles
smiles_to_inchi[smiles_string] = URI.encode_www_form_component(cmpd.to_inchi)
end
smi_file = Tempfile.open(['pc_ambit', '.csv']) ; smi_file.puts( "SMILES\n" + smiles_to_inchi.keys.join("\n") ) ; smi_file.flush
ambit_ds_uri = OpenTox::RestClientWrapper.post(@ambit_ds_service_uri, {:file => File.new(smi_file.path)}, {:content_type => "multipart/form-data", :accept => "text/uri-list"} )
ambit_result_uri = [ ambit_ds_uri + "?" ] # 1st pos: base uri, then features
rescue Exception => e
LOGGER.debug "#{e.class}: #{e.message}"
LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
ensure
smi_file.close! if smi_file
end
# get SMILES feature URI
ambit_smiles_uri = OpenTox::RestClientWrapper.get(
ambit_ds_uri + "/features",
{:accept=> "text/uri-list"}
).chomp
ambit_result_uri << ("feature_uris[]=" + URI.encode_www_form_component(ambit_smiles_uri) + "&")
# always calculate 3D (http://goo.gl/Tk81j), then get results
OpenTox::RestClientWrapper.post(
@ambit_mopac_model_uri,
{:dataset_uri => ambit_ds_uri},
{:accept => "text/uri-list"}
)
current_cat = ""
ids.each_with_index do |id, i|
old_cat = current_cat; current_cat = pc_descriptors[id][:pc_type]
params[:task].progress(params[:task].metadata[OT.percentageCompleted] + task_weights[old_cat]) if params[:task] && old_cat != current_cat && old_cat != ""
algorithm = Algorithm::Generic.new(@ambit_descriptor_algorithm_uri+id)
result_uri = algorithm.run({:dataset_uri => ambit_ds_uri})
ambit_result_uri << result_uri.split("?")[1] + "&"
LOGGER.debug "Ambit (#{ids.size}): #{i+1}"
end
params[:task].progress(params[:task].metadata[OT.percentageCompleted] + task_weights[current_cat]) if params[:task]
#LOGGER.debug "Ambit result: #{ambit_result_uri.join('')}"
end
[ ambit_result_uri, smiles_to_inchi, ids ]
end
# Load dataset via CSV
# @param[Array] Ambit result uri, piecewise (1st: base, 2nd: SMILES, 3rd+: features
# @param[Hash] keys: SMILES, values: InChIs
# @param[Array] field descriptions, one for each feature
# @return[Array] CSV, array of field ids, array of field descriptions
def self.load_ds_csv(ambit_result_uri, smiles_to_inchi, single_ids, subjectid=nil)
master=nil
ids=[]
ambit_ids=[]
if ambit_result_uri.size > 0
(1...ambit_result_uri.size).collect { |idx|
curr_uri = ambit_result_uri[0] + ambit_result_uri[idx]
#LOGGER.debug "Requesting #{curr_uri}"
csv_data = CSV.parse( OpenTox::RestClientWrapper.get(curr_uri, {:accept => "text/csv", :subjectid => subjectid}) )
if csv_data[0] && csv_data[0].size>1
if master.nil? # This is the smiles entry
(1...csv_data.size).each{ |idx| csv_data[idx][1] = smiles_to_inchi[csv_data[idx][1]] }
master = csv_data
next
else
index_uri = csv_data[0].index("SMILES")
csv_data.map {|i| i.delete_at(index_uri)} if index_uri #Removes additional SMILES information
nr_cols = (csv_data[0].size)-1
LOGGER.debug "Merging #{nr_cols} new columns"
ids += Array.new(nr_cols, single_ids[idx-2])
master.each {|row| nr_cols.times { row.push(nil) } } # Adds empty columns to all rows
csv_data.each do |row|
temp = master.assoc(row[0]) # Finds the appropriate line in master
((-1*nr_cols)..-1).collect.each { |idx|
temp[idx] = row[nr_cols+idx+1] if temp # Updates columns if line is found
}
end
end
end
}
index_uri = master[0].index("Compound")
master.map {|i| i.delete_at(index_uri)}
master[0].each {|cell| cell.chomp!(" ")}
master[0][0] = "Compound" #"SMILES"
index_smi = master[0].index("SMILES")
master.map {|i| i.delete_at(index_smi)} if index_smi
master[0][0] = "SMILES"
ambit_ids=master[0].collect {|header| header.to_s.gsub(/[\/.\\\(\)\{\}\[\]]/,"_")}
ambit_ids.shift
end
#LOGGER.debug "-------- AM: Writing to dumpfile"
#File.open("/tmp/test.csv", 'w') {|f| f.write( master.collect {|r| r.join(",")}.join("\n") ) }
[ master, ids, ambit_ids ]
end
# Gauss kernel
# @return [Float]
def self.gauss(x, sigma = 0.3)
d = 1.0 - x.to_f
Math.exp(-(d*d)/(2*sigma*sigma))
end
# For symbolic features
# @param [Array] Array to test, must indicate non-occurrence with 0.
# @return [Boolean] Whether the feature is singular or non-occurring or present everywhere.
def self.isnull_or_singular?(array)
nr_zeroes = array.count(0)
return (nr_zeroes == array.size) || # remove non-occurring feature
(nr_zeroes == array.size-1) || # remove singular feature
(nr_zeroes == 0) # also remove feature present everywhere
end
# Numeric value test
# @param[Object] value
# @return [Boolean] Whether value is a number
def self.numeric?(value)
true if Float(value) rescue false
end
# For symbolic features
# @param [Array] Array to test, must indicate non-occurrence with 0.
# @return [Boolean] Whether the feature has variance zero.
def self.zero_variance?(array)
return array.uniq.size == 1
end
# Sum of an array for Arrays.
# @param [Array] Array with values
# @return [Integer] Sum of size of values
def self.sum_size(array)
sum=0
array.each { |e| sum += e.size }
return sum
end
# Minimum Frequency
# @param [Integer] per-mil value
# return [Integer] min-frequency
def self.min_frequency(training_dataset,per_mil)
minfreq = per_mil * training_dataset.compounds.size.to_f / 1000.0 # AM sugg. 8-10 per mil for BBRC, 50 per mil for LAST
minfreq = 2 unless minfreq > 2
Integer (minfreq)
end
# Effect calculation for classification. It is assumed that the elements of the arrays match each other pairwise
# @param [Array] Array of occurrences per class (in the form of Enumerables).
# @param [Array] Array of database instance counts per class.
def self.effect(occurrences, db_instances)
max=0
max_value=0
nr_o = self.sum_size(occurrences)
nr_db = db_instances.to_scale.sum
occurrences.each_with_index { |o,i| # fminer outputs occurrences sorted reverse by activity.
actual = o.size.to_f/nr_o
expected = db_instances[i].to_f/nr_db
if actual > expected
if ((actual - expected) / actual) > max_value
max_value = (actual - expected) / actual # 'Schleppzeiger'
max = i
end
end
}
max
end
# neighbors
module Neighbors
# Get confidence.
# @param[Hash] Required keys: :sims, :acts
# @return[Float] Confidence
def self.get_confidence(params)
conf = params[:sims].inject{|sum,x| sum + x }
confidence = conf/params[:sims].size
LOGGER.debug "Confidence is: '" + confidence.to_s + "'."
return confidence
end
end
# Similarity calculations
module Similarity
# Tanimoto similarity
# @param [Hash, Array] fingerprints of first compound
# @param [Hash, Array] fingerprints of second compound
# @return [Float] (Weighted) tanimoto similarity
def self.tanimoto(fingerprints_a,fingerprints_b,weights=nil,params=nil)
common_p_sum = 0.0
all_p_sum = 0.0
# fingerprints are hashes
if fingerprints_a.class == Hash && fingerprints_b.class == Hash
common_features = fingerprints_a.keys & fingerprints_b.keys
all_features = (fingerprints_a.keys + fingerprints_b.keys).uniq
if common_features.size > 0
common_features.each{ |f| common_p_sum += [ fingerprints_a[f], fingerprints_b[f] ].min }
all_features.each{ |f| all_p_sum += [ fingerprints_a[f],fingerprints_b[f] ].compact.max } # compact, since one fp may be empty at that pos
end
# fingerprints are arrays
elsif fingerprints_a.class == Array && fingerprints_b.class == Array
size = [ fingerprints_a.size, fingerprints_b.size ].min
LOGGER.warn "fingerprints don't have equal size" if fingerprints_a.size != fingerprints_b.size
(0...size).each { |idx|
common_p_sum += [ fingerprints_a[idx], fingerprints_b[idx] ].min
all_p_sum += [ fingerprints_a[idx], fingerprints_b[idx] ].max
}
end
(all_p_sum > 0.0) ? (common_p_sum/all_p_sum) : 0.0
end
# Cosine similarity
# @param [Hash] properties_a key-value properties of first compound
# @param [Hash] properties_b key-value properties of second compound
# @return [Float] cosine of angle enclosed between vectors induced by keys present in both a and b
def self.cosine(fingerprints_a,fingerprints_b,weights=nil)
# fingerprints are hashes
if fingerprints_a.class == Hash && fingerprints_b.class == Hash
a = []; b = []
common_features = fingerprints_a.keys & fingerprints_b.keys
if common_features.size > 1
common_features.each do |p|
a << fingerprints_a[p]
b << fingerprints_b[p]
end
end
# fingerprints are arrays
elsif fingerprints_a.class == Array && fingerprints_b.class == Array
a = fingerprints_a
b = fingerprints_b
end
(a.size > 0 && b.size > 0) ? self.cosine_num(a.to_gv, b.to_gv) : 0.0
end
# Cosine similarity
# @param [GSL::Vector] a
# @param [GSL::Vector] b
# @return [Float] cosine of angle enclosed between a and b
def self.cosine_num(a, b)
if a.size>12 && b.size>12
a = a[0..11]
b = b[0..11]
end
a.dot(b) / (a.norm * b.norm)
end
# Outlier detection based on Mahalanobis distances
# Multivariate detection on X, univariate detection on y
# Uses an existing Rinruby instance, if possible
# @param[Hash] Keys query_matrix, data_matrix, acts are required; r, p_outlier optional
# @return[Array] indices identifying outliers (may occur several times, this is intended)
def self.outliers(params)
outlier_array = []
data_matrix = params[:data_matrix]
query_matrix = params[:query_matrix]
acts = params[:acts]
begin
LOGGER.debug "Outliers (p=#{params[:p_outlier] || 0.9999})..."
r = ( params[:r] || RinRuby.new(false,false) )
r.eval "suppressPackageStartupMessages(library(\"robustbase\"))"
r.eval "outlier_threshold = #{params[:p_outlier] || 0.999}"
nr_cases, nr_features = data_matrix.to_a.size, data_matrix.to_a[0].size
r.odx = data_matrix.to_a.flatten
r.q = query_matrix.to_a.flatten
r.y = acts.to_a.flatten
r.eval "odx = matrix(odx, #{nr_cases}, #{nr_features}, byrow=T)"
r.eval 'odx = rbind(q,odx)' # query is nr 0 (1) in ruby (R)
r.eval 'mah = covMcd(odx)$mah' # run MCD alg
r.eval "mah = pchisq(mah,#{nr_features})"
r.eval 'outlier_array = which(mah>outlier_threshold)' # multivariate outliers using robust mahalanobis
outlier_array = r.outlier_array.to_a.collect{|v| v-2 } # translate to ruby index (-1 for q, -1 due to ruby)
r.eval 'fqu = matrix(summary(y))[2]'
r.eval 'tqu = matrix(summary(y))[5]'
r.eval 'outlier_array = which(y>(tqu+1.5*IQR(y)))' # univariate outliers due to Tukey (http://goo.gl/mwzNH)
outlier_array += r.outlier_array.to_a.collect{|v| v-1 } # translate to ruby index (-1 due to ruby)
r.eval 'outlier_array = which(y<(fqu-1.5*IQR(y)))'
outlier_array += r.outlier_array.to_a.collect{|v| v-1 }
rescue Exception => e
LOGGER.debug "#{e.class}: #{e.message}"
#LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
end
outlier_array
end
end
end
end
|