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
|
class String
def numeric?
Float(self) != nil rescue false
end
end
class Model
attr_reader :train, :dependent_variable_name, :independent_variable_names, :minsim
def initialize file
puts "Reading training data from #{file}."
@train = File.readlines(file).collect{|l| l.chomp.split(",")}
header = @train.shift
@dependent_variable_name = header[1]
@independent_variable_names = header[2..-1]
model_type
end
def model_type
puts "Determining model type."
if dependent_variables.uniq.sort == ["0","1"]
@dependent_variable_type = "binary"
@train.each {|t| t[1] = t[1].to_i}
elsif dependent_variables.collect{|v| v.numeric?}.uniq == [true]
@dependent_variable_type = "numeric"
@train.each {|t| t[1] = t[1].to_f }
else
raise "Incorrect model dependent variables [#{dependent_variables.uniq}]. Must be either [0,1] or numeric."
end
if independent_variables.flatten.collect{|v| v.numeric?}.uniq == [false]
@independent_variable_type = "set"
@minsim = [0.5,0.2]
elsif independent_variables.flatten.collect{|v| v.numeric?}.uniq == [true]
@independent_variable_type = "numeric"
@train.each {|t| t[2..-1] = t[2..-1].collect{|v| v = v.to_f}}
@minsim = [0.9,0.7]
else
raise "Incorrect model independent variables [#{independent_variables.flatten.uniq}]. Must be either a set (fingerprints) or numeric."
end
end
def predict file
puts "Reading prediction data from #{file}."
@batch = File.readlines(file).collect{|l| l.chomp.split(",")}
header = @batch.shift
unless (@batch.collect{|b| b[1..-1]}.flatten.collect{|v| v.numeric?}.uniq == [false] and @independent_variable_type == "set") or (batch_independent_variables.flatten.collect{|v| v.numeric?}.uniq == [true] and @independent_variable_type == "numeric")
raise "Incorrect batch independent variables [#{independent_variables.flatten.uniq}]. Must be #{@independent_variable_type}."
end
if @independent_variable_type == "numeric"
@batch_independent_variable_names = header[1..-1]
@batch.each {|t| t[1..-1] = t[1..-1].collect{|v| v = v.to_f}}
select(@independent_variable_names & @batch_independent_variable_names)
File.open(File.join(File.dirname(file),"common-variables.csv"),"w+") do |f|
f.print "CANSMI,dataset,"
f.puts @independent_variable_names.join(",")
@train.each do |row|
f.puts ([row[0],"train"]+row[2..-1]).join(",")
end
@batch.each do |row|
f.puts ([row[0],"predict"]+row[1..-1]).join(",")
end
end
puts "Feature selection and scaling."
puts `Rscript #{File.join(File.dirname(__FILE__),"..","bin","preprocessing.R")} #{File.join(File.dirname(file),"common-variables.csv")} #{File.join(File.dirname(file),"scaled-variables.csv")}`
puts "Reading scaled features."
lines = File.readlines(File.join(File.dirname(file),"scaled-variables.csv"))
@independent_variable_names = @batch_independent_variable_names = lines.shift.chomp.split(",")[2..-1]
@scaled_train = []
@scaled_batch = []
lines.each_with_index do |line,i|
items = line.chomp.split(",")
if items[1] == "train"
items[1] = @train[i][1]
@scaled_train << items.collect{|i| i.to_s.numeric? ? i.to_f : i}
elsif items[1] == "predict"
items.delete_at(1)
@scaled_batch << items.collect{|i| i.to_s.numeric? ? i.to_f : i}
end
end
puts "Predicting #{file}."
File.open(file.sub(".csv","-prediction.csv"),"w+") do |f|
f.puts ["Canonical SMILES","Experimental","Prediction","p-inactive","p-active","Max Simimilarity","Nr. Neighbors"].join(",")
@scaled_batch.each do |pred|
classification(pred[0], @scaled_train.collect{|row| row[0..1] + [cosine([row[2..-1],pred[1..-1]])]}).each do |pred|
f.puts pred.join(",")
end
end
end
elsif @independent_variable_type == "set"
puts "Predicting #{file}."
File.open(file.sub(".csv","-prediction.csv"),"w+") do |f|
f.puts ["Canonical SMILES","Experimental","Prediction","p-inactive","p-active","Max Simimilarity","Nr. Neighbors"].join(",")
@batch.each do |fingerprints|
smi = fingerprints.shift
classification(smi, @train.collect{|row| row[0..1] + [tanimoto([row[2..-1],fingerprints])]}).each do |pred|
f.puts pred.join(",")
end
end
end
end
end
def select variable_names
model_variable_idx = [0,1]+variable_names.collect{|n| @independent_variable_names.index(n)+2}
batch_variable_idx = [0]+variable_names.collect{|n| @batch_independent_variable_names.index(n)+1}
@train.collect!{|r| model_variable_idx.collect{|i| r[i]}}
@batch.collect!{|r| batch_variable_idx.collect{|i| r[i]}}
@independent_variable_names = variable_names
@batch_independent_variable_names = variable_names
end
def classification smiles, train
experimental = train.select{|row| row[0] == smiles}
train = train-experimental
n = train.select{|row| row[2] > @minsim[0]}
n = train.select!{|row| row[2] > @minsim[1]} if n.size < 2
train = n
#p train.sort_by{|r| r[2]}[0..5]#.collect{|r| r.join(",")}.join("\n")
if train.size < 2
classification = nil
probabilities = [nil,nil]
else
probabilities = weighted_majority_vote(train)
probabilities[1] > probabilities[0] ? classification = 1 : classification = 0
end
experimental = [[nil,nil,nil]] if experimental.empty?
experimental.collect do
[ smiles, experimental[1], classification ] + probabilities + [ train.collect{|t| t[2]}.max, train.size ]
end
end
# Get Euclidean distance
# @param [Array<Array<Float>>]
# @return [Float]
def euclid variables
sq = variables[0].zip(variables[1]).map{|a,b| (a - b) ** 2}
Math.sqrt(sq.inject(0) {|s,c| s + c})
end
# Get Tanimoto similarity
# @param [Array<Array<String>>]
# @return [Float]
def tanimoto fingerprints
( fingerprints[0] & fingerprints[1] ).size/( fingerprints[0] | fingerprints[1] ).size.to_f
end
# Get cosine similarity
# http://stackoverflow.com/questions/1838806/euclidean-distance-vs-pearson-correlation-vs-cosine-similarity
# @param [Array<Array<Float>>]
# @return [Float]
def cosine variables
variables[0].dot_product(variables[1]) / (variables[0].magnitude * variables[1].magnitude)
end
def weighted_majority_vote neighbors
w = [neighbors.select{|n| n[1] == 0}.collect{|n| n[2]}, neighbors.select{|n| n[1] == 1}.collect{|n| n[2]}]
weights_sum = neighbors.collect{|n| n[2]}.sum.to_f
weights_max = neighbors.collect{|n| n[2]}.max.to_f
[weights_max*w[0].sum/weights_sum, weights_max*w[1].sum/weights_sum]
end
def smiles
@train.collect{|t| t[0]}
end
def dependent_variables
@train.collect{|t| t[1]}
end
def independent_variables
@train.collect{|t| t[2..-1]}
end
def batch_smiles
@batch.collect{|t| t[0]}
end
def batch_independent_variables
@batch.collect{|t| t[1..-1]}
end
end
|