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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
# R integration
# workaround to initialize R non-interactively (former rinruby versions did this by default)
# avoids compiling R with X
R = nil
require "rinruby"
require "statsample"
require 'uri'
require 'transform.rb'
require 'utils.rb'
module OpenTox
# Wrapper for OpenTox Algorithms
module Algorithm
include OpenTox
# Execute algorithm with parameters, consult OpenTox API and webservice documentation for acceptable parameters
# @param [optional,Hash] params Algorithm parameters
# @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly
# @return [String] URI of new resource (dataset, model, ...)
def run(params=nil, waiting_task=nil)
LOGGER.info "Running algorithm '"+@uri.to_s+"' with params: "+params.inspect
RestClientWrapper.post(@uri, params, {:accept => 'text/uri-list'}, waiting_task).to_s
end
# Get OWL-DL representation in RDF/XML format
# @return [application/rdf+xml] RDF/XML representation
def to_rdfxml
s = Serializer::Owl.new
s.add_algorithm(@uri,@metadata)
s.to_rdfxml
end
# Generic Algorithm class, should work with all OpenTox webservices
class Generic
include Algorithm
# Find Generic Opentox Algorithm via URI, and loads metadata, could raise NotFound/NotAuthorized error
# @param [String] uri Algorithm URI
# @return [OpenTox::Algorithm::Generic] Algorithm instance
def self.find(uri, subjectid=nil)
return nil unless uri
alg = Generic.new(uri)
alg.load_metadata( subjectid )
raise "cannot load algorithm metadata" if alg.metadata==nil or alg.metadata.size==0
alg
end
end
# Fminer algorithms (https://github.com/amaunz/fminer2)
class Fminer
include Algorithm
attr_accessor :prediction_feature, :training_dataset, :minfreq, :compounds, :db_class_sizes, :all_activities, :smi
def check_params(params,per_mil,subjectid=nil)
raise OpenTox::NotFoundError.new "Please submit a dataset_uri." unless params[:dataset_uri] and !params[:dataset_uri].nil?
@training_dataset = OpenTox::Dataset.find "#{params[:dataset_uri]}", subjectid
unless params[:prediction_feature] # try to read prediction_feature from dataset
raise OpenTox::NotFoundError.new "Please provide a prediction_feature parameter" unless @training_dataset.features.size == 1
prediction_feature = OpenTox::Feature.find(@training_dataset.features.keys.first,@subjectid)
params[:prediction_feature] = prediction_feature.uri
end
@prediction_feature = OpenTox::Feature.find params[:prediction_feature], subjectid
raise OpenTox::NotFoundError.new "No feature #{params[:prediction_feature]} in dataset #{params[:dataset_uri]}" unless @training_dataset.features and @training_dataset.features.include?(params[:prediction_feature])
unless params[:min_frequency].nil?
# check for percentage
if params[:min_frequency].include? "pc"
per_mil=params[:min_frequency].gsub(/pc/,"")
if OpenTox::Algorithm.numeric? per_mil
per_mil = per_mil.to_i * 10
else
bad_request=true
end
# check for per-mil
elsif params[:min_frequency].include? "pm"
per_mil=params[:min_frequency].gsub(/pm/,"")
if OpenTox::Algorithm.numeric? per_mil
per_mil = per_mil.to_i
else
bad_request=true
end
# set minfreq directly
else
if OpenTox::Algorithm.numeric? params[:min_frequency]
@minfreq=params[:min_frequency].to_i
LOGGER.debug "min_frequency #{@minfreq}"
else
bad_request=true
end
end
raise OpenTox::BadRequestError.new "Minimum frequency must be integer [n], or a percentage [n]pc, or a per-mil [n]pm , with n greater 0" if bad_request
end
if @minfreq.nil?
@minfreq=OpenTox::Algorithm.min_frequency(@training_dataset,per_mil)
LOGGER.debug "min_frequency #{@minfreq} (input was #{per_mil} per-mil)"
end
end
def add_fminer_data(fminer_instance, value_map)
# detect nr duplicates per compound
compound_sizes = {}
@training_dataset.compounds.each do |compound|
entries=@training_dataset.data_entries[compound]
entries.each do |feature, values|
compound_sizes[compound] || compound_sizes[compound] = []
compound_sizes[compound] << values.size unless values.size == 0
end
compound_sizes[compound].uniq!
raise "Inappropriate data for fminer" if compound_sizes[compound].size > 1
compound_sizes[compound] = compound_sizes[compound][0] # integer instead of array
end
id = 1 # fminer start id is not 0
@training_dataset.compounds.each do |compound|
entry=@training_dataset.data_entries[compound]
begin
smiles = OpenTox::Compound.new(compound).to_smiles
rescue
LOGGER.warn "No resource for #{compound.to_s}"
next
end
if smiles == '' or smiles.nil?
LOGGER.warn "Cannot find smiles for #{compound.to_s}."
next
end
entry.each do |feature,values|
if feature == @prediction_feature.uri
(0...compound_sizes[compound]).each { |i|
if values[i].nil?
LOGGER.warn "No #{feature} activity for #{compound.to_s}."
else
if @prediction_feature.feature_type == "classification"
activity= value_map.invert[values[i]].to_i # activities are mapped to 1..n
@db_class_sizes[activity-1].nil? ? @db_class_sizes[activity-1]=1 : @db_class_sizes[activity-1]+=1 # AM effect
elsif @prediction_feature.feature_type == "regression"
activity= values[i].to_f
end
begin
fminer_instance.AddCompound(smiles,id) if fminer_instance
fminer_instance.AddActivity(activity, id) if fminer_instance
@all_activities[id]=activity # DV: insert global information
@compounds[id] = compound
@smi[id] = smiles
id += 1
rescue Exception => e
LOGGER.warn "Could not add " + smiles + "\t" + values[i].to_s + " to fminer"
LOGGER.warn e.backtrace
end
end
}
end
end
end
end
end
# Backbone Refinement Class mining (http://bbrc.maunz.de/)
class BBRC < Fminer
# Initialize bbrc algorithm
def initialize(subjectid=nil)
super File.join(CONFIG[:services]["opentox-algorithm"], "fminer/bbrc")
load_metadata(subjectid)
end
end
# LAtent STructure Pattern Mining (http://last-pm.maunz.de)
class LAST < Fminer
# Initialize last algorithm
def initialize(subjectid=nil)
super File.join(CONFIG[:services]["opentox-algorithm"], "fminer/last")
load_metadata(subjectid)
end
end
# Create lazar prediction model
class Lazar
include Algorithm
# Initialize lazar algorithm
def initialize(subjectid=nil)
super File.join(CONFIG[:services]["opentox-algorithm"], "lazar")
load_metadata(subjectid)
end
end
# Structural Graph Clustering by TU Munich
# Finds clusters similar to a query structure in a given training dataset
# May be queried for cluster membership of an unknown compound
class StructuralClustering
attr_accessor :training_dataset_uri, :training_threshold, :query_dataset_uri, :query_threshold, :target_clusters_array
# @params[String] Training dataset_uri
# @params[Float] Similarity threshold for training (optional)
# @params[String] Cluster service uri (no AA)
def initialize training_dataset_uri, training_threshold=0.8, cluster_service_uri = "http://opentox-dev.informatik.tu-muenchen.de:8080/OpenTox/algorithm/StructuralClustering"
if (training_dataset_uri =~ URI::regexp).nil? || (cluster_service_uri =~ URI::regexp).nil?
raise "Invalid URI."
end
@training_dataset_uri = training_dataset_uri
if !self.numeric? training_threshold || training_threshold <0 || training_threshold >1
raise "Training threshold out of bounds."
end
@training_threshold = training_threshold.to_f
# Train a cluster model
params = {:dataset_uri => @training_dataset_uri, :threshold => @training_threshold }
@cluster_model_uri = OpenTox::RestClientWrapper.post cluster_service_uri, params
cluster_model_rdf = OpenTox::RestClientWrapper.get @cluster_model_uri
@datasets = OpenTox::Parser::Owl.from_rdf cluster_model_rdf, OT.Dataset, true # must extract OT.Datasets from model
# Process parsed OWL objects
@clusterid_dataset_map = Hash.new
@datasets.each { |d|
begin
d.metadata[OT.hasSource]["Structural Clustering cluster "] = "" # must parse in metadata for string (not elegant)
@clusterid_dataset_map[d.metadata[OT.hasSource].to_i] = d.uri
rescue Exception => e
# ignore other entries!
end
}
end
# Whether a model has been trained
def trained?
!@cluster_model_uri.nil?
end
# Instance query: clusters for a compound
# @params[String] Query compound
# @params[Float] Similarity threshold for query to clusters (optional)
def get_clusters query_compound_uri, query_threshold = 0.5
if !self.numeric? query_threshold || query_threshold <0 || query_threshold >1
raise "Query threshold out of bounds."
end
@query_threshold = query_threshold.to_f
# Preparing a query dataset
query_dataset = OpenTox::Dataset.new
@query_dataset_uri = query_dataset.save
query_dataset = OpenTox::Dataset.find @query_dataset_uri
query_dataset.add_compound query_compound_uri
@query_dataset_uri = query_dataset.save
# Obtaining a clustering for query compound
params = { :dataset_uri => @query_dataset_uri, :threshold => @query_threshold }
cluster_query_dataset_uri = OpenTox::RestClientWrapper.post @cluster_model_uri, params
cluster_query_dataset = OpenTox::Dataset.new cluster_query_dataset_uri
cluster_query_dataset.load_all
# Reading cluster ids for features from metadata
feature_clusterid_map = Hash.new
pattern="Prediction feature for cluster assignment " # must parse for string in metadata (not elegant)
cluster_query_dataset.features.each { |feature_uri,metadata|
metadata[DC.title][pattern]=""
feature_clusterid_map[feature_uri] = metadata[DC.title].to_i
}
# Integrity check
unless cluster_query_dataset.compounds.size == 1
raise "Number of predicted compounds is != 1."
end
# Process data entry
query_compound_uri = cluster_query_dataset.compounds[0]
@target_clusters_array = Array.new
cluster_query_dataset.features.keys.each { |cluster_membership_feature|
# Getting dataset URI for cluster
target_cluster = feature_clusterid_map[cluster_membership_feature]
dataset = @clusterid_dataset_map[target_cluster]
# Finally look up presence
data_entry = cluster_query_dataset.data_entries[query_compound_uri]
present = data_entry[cluster_membership_feature][0]
# Store result
@target_clusters_array << dataset if present > 0.5 # 0.0 for absence, 1.0 for presence
}
end
end
module Neighbors
# Classification with majority vote from neighbors weighted by similarity
# @param [Hash] params Keys `:acts, :sims, :value_map` are required
# @return [Numeric] A prediction value.
def self.weighted_majority_vote(params)
neighbor_contribution = 0.0
confidence_sum = 0.0
confidence = 0.0
prediction = nil
LOGGER.debug "Weighted Majority Vote Classification."
params[:acts].each_index do |idx|
neighbor_weight = params[:sims][1][idx]
neighbor_contribution += params[:acts][idx] * neighbor_weight
if params[:value_map].size == 2 # AM: provide compat to binary classification: 1=>false 2=>true
case params[:acts][idx]
when 1
confidence_sum -= neighbor_weight
when 2
confidence_sum += neighbor_weight
end
else
confidence_sum += neighbor_weight
end
end
if params[:value_map].size == 2
if confidence_sum >= 0.0
prediction = 2 unless params[:acts].size==0
elsif confidence_sum < 0.0
prediction = 1 unless params[:acts].size==0
end
else
prediction = (neighbor_contribution/confidence_sum).round unless params[:acts].size==0 # AM: new multinomial prediction
end
LOGGER.debug "Prediction is: '" + prediction.to_s + "'." unless prediction.nil?
confidence = (confidence_sum/params[:acts].size).abs if params[:acts].size > 0
LOGGER.debug "Confidence is: '" + confidence.to_s + "'." unless prediction.nil?
return {:prediction => prediction, :confidence => confidence.abs}
end
# Local support vector regression from neighbors
# @param [Hash] params Keys `:props, :acts, :sims, :min_train_performance` are required
# @return [Numeric] A prediction value.
def self.local_svm_regression(params)
begin
confidence = 0.0
prediction = nil
LOGGER.debug "Local SVM."
if params[:acts].size>0
if params[:props]
n_prop = params[:props][0].collect
q_prop = params[:props][1].collect
props = [ n_prop, q_prop ]
end
acts = params[:acts].collect
prediction = local_svm_prop( props, acts, params[:min_train_performance]) # params[:props].nil? signals non-prop setting
prediction = nil if (!prediction.nil? && prediction.infinite?)
LOGGER.debug "Prediction is: '" + prediction.to_s + "'."
confidence = get_confidence({:sims => params[:sims][1], :acts => params[:acts]})
confidence = 0.0 if prediction.nil?
end
{:prediction => prediction, :confidence => confidence}
rescue Exception => e
LOGGER.debug "#{e.class}: #{e.message}"
LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
end
end
# Local support vector regression from neighbors
# @param [Hash] params Keys `:props, :acts, :sims, :min_train_performance` are required
# @return [Numeric] A prediction value.
def self.local_svm_classification(params)
begin
confidence = 0.0
prediction = nil
LOGGER.debug "Local SVM."
if params[:acts].size>0
if params[:props]
n_prop = params[:props][0].collect
q_prop = params[:props][1].collect
props = [ n_prop, q_prop ]
end
acts = params[:acts].collect
acts = acts.collect{|v| "Val" + v.to_s} # Convert to string for R to recognize classification
prediction = local_svm_prop( props, acts, params[:min_train_performance], params[:weights_option]) # params[:props].nil? signals non-prop setting
prediction = prediction.sub(/Val/,"") if prediction # Convert back to Float
confidence = 0.0 if prediction.nil?
LOGGER.debug "Prediction is: '" + prediction.to_s + "'."
confidence = get_confidence({:sims => params[:sims][1], :acts => params[:acts]})
end
{:prediction => prediction, :confidence => confidence}
rescue Exception => e
LOGGER.debug "#{e.class}: #{e.message}"
LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
end
end
# Local support vector prediction from neighbors.
# Uses propositionalized setting.
# Not to be called directly (use local_svm_regression or local_svm_classification).
# @param [Array] props, propositionalization of neighbors and query structure e.g. [ Array_for_q, two-nested-Arrays_for_n ]
# @param [Array] acts, activities for neighbors.
# @param [Float] min_train_performance, parameter to control censoring
# @param [integer] weights_option, parameter to select a weight function
# @return [Numeric] A prediction value.
def self.local_svm_prop(props, acts, min_train_performance, weights_option=nil)
LOGGER.debug "Local SVM (Propositionalization / Kernlab Kernel)."
n_prop = props[0] # is a matrix, i.e. two nested Arrays.
q_prop = props[1] # is an Array.
prediction = nil
if Algorithm::zero_variance? acts
prediction = acts[0]
else
#LOGGER.debug gram_matrix.to_yaml
@r = RinRuby.new(true,false) # global R instance leads to Socket errors after a large number of requests
@r.eval "suppressPackageStartupMessages(library('caret'))" # requires R packages "caret" and "kernlab"
@r.eval "suppressPackageStartupMessages(library('doMC'))" # requires R packages "multicore"
@r.eval "registerDoMC()" # switch on parallel processing
@r.eval "set.seed(1)"
begin
# set data
LOGGER.debug "Setting R data ..."
@r.n_prop = n_prop.flatten
@r.n_prop_x_size = n_prop.size
@r.n_prop_y_size = n_prop[0].size
@r.y = acts
@r.q_prop = q_prop
weights_option.nil? ? @r.weights_opt = 0 : @r.weights_opt = weights_option
#@r.eval "y = matrix(y)"
@r.eval "prop_matrix = matrix(n_prop, n_prop_x_size, n_prop_y_size, byrow=T)"
@r.eval "q_prop = matrix(q_prop, 1, n_prop_y_size, byrow=T)"
# prepare data
LOGGER.debug "Preparing R data ..."
@r.eval <<-EOR
weights=NULL
if (!(class(y) == 'numeric')) {
y = factor(y)
suppressPackageStartupMessages(library('class'))
weights=unlist(as.list(prop.table(table(y))))
set_weights <- function(weights, option) {
if (option==1){
return(weights)
} else if (option==2){
return(1/weights)
} else if (option==3){
return(1-weights)
} else if (option==4){
return(-(weights^2)+1)
} else if (option==5){
return((weights-1)^2)
}else {
return(NULL)
}
}
weights=set_weights(weights,weights_opt)
}
EOR
@r.eval <<-EOR
rem = nearZeroVar(prop_matrix)
if (length(rem) > 0) {
prop_matrix = prop_matrix[,-rem,drop=F]
q_prop = q_prop[,-rem,drop=F]
}
rem = findCorrelation(cor(prop_matrix))
if (length(rem) > 0) {
prop_matrix = prop_matrix[,-rem,drop=F]
q_prop = q_prop[,-rem,drop=F]
}
EOR
# model + support vectors
LOGGER.debug "Creating R SVM model ..."
train_success = @r.eval <<-EOR
# AM: TODO: evaluate class weight effect by altering:
# AM: comment in 'weights' above run and class.weights=weights vs. class.weights=1-weights
# AM: vs
# AM: comment out 'weights' above (status quo), thereby disabling weights
model = train(prop_matrix,y,
method="svmradial",
preProcess=c("center", "scale"),
class.weights=weights,
trControl=trainControl(method="LGOCV",number=10),
tuneLength=8
)
perf = ifelse ( class(y)!='numeric', max(model$results$Accuracy), model$results[which.min(model$results$RMSE),]$Rsquared )
EOR
# prediction
LOGGER.debug "Predicting ..."
@r.eval "p = predict(model,q_prop)"
@r.eval "if (class(y)!='numeric') p = as.character(p)"
prediction = @r.p
# censoring
prediction = nil if ( @r.perf.nan? || @r.perf < min_train_performance )
prediction = nil unless train_success
LOGGER.debug "Performance: #{sprintf("%.2f", @r.perf)}"
rescue Exception => e
LOGGER.debug "#{e.class}: #{e.message}"
LOGGER.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
end
@r.quit # free R
end
prediction
end
end
module FeatureSelection
include Algorithm
# Recursive Feature Elimination using caret
# @param [Hash] required keys: ds_csv_file, prediction_feature, fds_csv_file (dataset CSV file, prediction feature column name, and feature dataset CSV file), optional: del_missing (delete rows with missing values).
# @return [String] feature dataset CSV file composed of selected features.
def self.rfe(params)
@r=RinRuby.new(false,false)
@r.ds_csv_file = params[:ds_csv_file].to_s
@r.prediction_feature = params[:prediction_feature].to_s
@r.fds_csv_file = params[:fds_csv_file].to_s
@r.del_missing = params[:del_missing] == true ? 1 : 0
r_result_file = params[:fds_csv_file].sub("rfe_", "rfe_R_")
@r.f_fds_r = r_result_file.to_s
# need packs 'randomForest', 'RANN'
@r.eval <<-EOR
suppressPackageStartupMessages(library('caret'))
suppressPackageStartupMessages(library('randomForest'))
suppressPackageStartupMessages(library('RANN'))
suppressPackageStartupMessages(library('doMC'))
registerDoMC()
set.seed(1)
acts = read.csv(ds_csv_file, check.names=F)
feats = read.csv(fds_csv_file, check.names=F)
ds = merge(acts, feats, by="SMILES") # duplicates features for duplicate SMILES :-)
features = ds[,(dim(acts)[2]+1):(dim(ds)[2])]
y = ds[,which(names(ds) == prediction_feature)]
# assumes a data matrix 'features' and a vector 'y' of target values
row.names(features)=NULL
# features with all values missing removed
na_col = names ( which ( apply ( features, 2, function(x) all ( is.na ( x ) ) ) ) )
features = features[,!names(features) %in% na_col]
# features with infinite values removed
inf_col = names ( which ( apply ( features, 2, function(x) any ( is.infinite ( x ) ) ) ) )
features = features[,!names(features) %in% inf_col]
# features with zero variance removed
zero_var = names ( which ( apply ( features, 2, function(x) var(x, na.rm=T) ) == 0 ) )
features = features[,!names(features) %in% zero_var]
pp = NULL
if (del_missing) {
# needed if rows should be removed
na_ids = apply ( features,1,function(x) any ( is.na ( x ) ) )
features = features[!na_ids,]
y = y[!na_ids]
pp = preProcess(features, method=c("scale", "center"))
} else {
# Use imputation if NA's random (only then!)
pp = preProcess(features, method=c("scale", "center", "knnImpute"))
}
features = predict(pp, features)
# features with nan values removed (sometimes preProcess return NaN values)
nan_col = names ( which ( apply ( features, 2, function(x) any ( is.nan ( x ) ) ) ) )
features = features[,!names(features) %in% nan_col]
# determine subsets
subsets = dim(features)[2]*c(0.3, 0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.7)
#subsets = dim(features)[2]*c(0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7)
#subsets = c(2,3,4,5,7,10,subsets)
#subsets = c(2,3,4,5,7,10,13,16,19,22,25,28,30)
subsets = unique(sort(round(subsets)))
subsets = subsets[subsets<=dim(features)[2]]
subsets = subsets[subsets>1]
# Recursive feature elimination
rfProfile = rfe( x=features, y=y, rfeControl=rfeControl(functions=rfFuncs, number=150), sizes=subsets)
# read existing dataset and select most useful features
csv=feats[,c("SMILES", rfProfile$optVariables)]
write.csv(x=csv,file=f_fds_r, row.names=F, quote=F, na='')
EOR
r_result_file
end
end
module Substructure
include Algorithm
# Substructure matching
# @param [Hash] required keys: compound, features
# @return [Array] Array with matching Smarts
def self.match(params)
params[:compound].match(params[:features])
end
# Substructure matching with number of non-unique hits
# @param [Hash] required keys: compound, features
# @return [Hash] Hash with matching Smarts and number of hits
def self.match_hits(params)
params[:compound].match_hits(params[:features])
end
# Substructure matching with number of non-unique hits
# @param [Hash] required keys: compound, features, feature_dataset_uri, pc_type
# @return [Hash] Hash with matching Smarts and number of hits
def self.lookup(params)
params[:compound].lookup(params[:features], params[:feature_dataset_uri], params[:pc_type], params[:lib], params[:subjectid])
end
end
module Dataset
include Algorithm
# API should match Substructure.match
def features(dataset_uri,compound_uri)
end
end
end
end
class Array
# collect method extended for parallel processing.
# Note: assign return value as: ans = arr.pcollect(n) { |obj| ... }
# @param n the number of processes to spawn (default: unlimited)
def pcollect(n = nil)
nproc = 0
result = collect do |*a|
r, w = IO.pipe
fork do
r.close
w.write( Marshal.dump( yield(*a) ) )
end
if n and (nproc+=1) >= n
Process.wait ; nproc -= 1
end
[ w.close, r ].last
end
Process.waitall
result.collect{|r| Marshal.load [ r.read, r.close ].first}
end
end
|