-
Notifications
You must be signed in to change notification settings - Fork 2
/
lrtopsinglepredict.py
370 lines (282 loc) · 12.9 KB
/
lrtopsinglepredict.py
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
import random
import numpy as np
from sklearn import model_selection
from sklearn.model_selection import GroupKFold
import pandas as pd
import math
import time
import os
#from rdflib import Graph, URIRef, Literal, RDF, ConjunctiveGraph, Namespace
drugfeatfiles = ['drugs-fingerprint-sim.csv','drugs-se-sim.csv',
'drugs-ppi-sim.csv', 'drugs-target-go-sim.csv','drugs-target-seq-sim.csv']
diseasefeatfiles =['diseases-hpo-sim.csv', 'diseases-pheno-sim.csv' ]
feature_folder ="data/features"
drugfeatfiles = [ os.path.join(feature_folder, fn) for fn in drugfeatfiles]
diseasefeatfiles = [ os.path.join(feature_folder, fn) for fn in diseasefeatfiles]
drug_ind="data/input/openpredict-omim-drug.csv"
drugDiseaseKnown = pd.read_csv(drug_ind,delimiter=',')
drugDiseaseKnown.head()
drugDiseaseKnown.rename(columns={'drugid':'Drug','omimid':'Disease'}, inplace=True)
drugDiseaseKnown.Disease = drugDiseaseKnown.Disease.astype(str)
drugDiseaseKnown.head()
def adjcencydict2matrix(df, name1, name2):
df1 = df.copy()
df1= df1.rename(index=str, columns={name1: name2, name2: name1})
print (len(df))
df =df.append(df1)
print (len(df))
return df.pivot(index=name1, columns=name2)
def mergeFeatureMatrix(drugfeatfiles, diseasefeatfiles):
for i,featureFilename in enumerate(drugfeatfiles):
print (featureFilename)
df = pd.read_csv(featureFilename, delimiter=',')
cond = df.Drug1 > df.Drug2
df.loc[cond, ['Drug1', 'Drug2']] = df.loc[cond, ['Drug2', 'Drug1']].values
if i != 0:
drug_df=drug_df.merge(df,on=['Drug1','Drug2'],how='inner')
#drug_df=drug_df.merge(temp,how='outer',on='Drug')
else:
drug_df =df
drug_df.fillna(0, inplace=True)
drug_df = adjcencydict2matrix(drug_df, 'Drug1', 'Drug2')
drug_df = drug_df.fillna(1.0)
for i,featureFilename in enumerate(diseasefeatfiles):
print (featureFilename)
df=pd.read_csv(featureFilename, delimiter=',')
cond = df.Disease1 > df.Disease2
df.loc[cond, ['Disease1','Disease2']] = df.loc[cond, ['Disease2','Disease1']].values
if i != 0:
disease_df = disease_df.merge(df,on=['Disease1','Disease2'], how='inner')
#drug_df=drug_df.merge(temp,how='outer',on='Drug')
else:
disease_df = df
disease_df.fillna(0, inplace=True)
disease_df.Disease1 = disease_df.Disease1.astype(str)
disease_df.Disease2 = disease_df.Disease2.astype(str)
disease_df = adjcencydict2matrix(disease_df, 'Disease1', 'Disease2')
disease_df = disease_df.fillna(1.0)
return drug_df, disease_df
drug_df, disease_df = mergeFeatureMatrix(drugfeatfiles, diseasefeatfiles)
def generatePairs(drug_df, disease_df, drugDiseaseKnown):
drugwithfeatures = set(drug_df.columns.levels[1])
diseaseswithfeatures = set(disease_df.columns.levels[1])
drugDiseaseDict = set([tuple(x) for x in drugDiseaseKnown[['Drug','Disease']].values])
commonDrugs= drugwithfeatures.intersection( drugDiseaseKnown.Drug.unique())
commonDiseases= diseaseswithfeatures.intersection(drugDiseaseKnown.Disease.unique() )
print ("commonDrugs: %d commonDiseases : %d"%(len(commonDrugs),len(commonDiseases)))
#abridged_drug_disease = [(dr,di) for (dr,di) in drugDiseaseDict if dr in drugwithfeatures and di in diseaseswithfeatures ]
#commonDrugs = set( [ dr for dr,di in abridged_drug_disease])
#commonDiseases =set([ di for dr,di in abridged_drug_disease])
print ("Gold standard, associations: %d drugs: %d diseases: %d"%(len(drugDiseaseKnown),len(drugDiseaseKnown.Drug.unique()),len(drugDiseaseKnown.Disease.unique())))
print ("Drugs with features: %d Diseases with features: %d"%(len(drugwithfeatures),len(diseaseswithfeatures)))
print ("commonDrugs: %d commonDiseases : %d"%(len(commonDrugs),len(commonDiseases)))
pairs=[]
classes=[]
for dr in commonDrugs:
for di in commonDiseases:
cls = (1 if (dr,di) in drugDiseaseDict else 0)
pairs.append((dr,di))
classes.append(cls)
return pairs, classes
pairs, classes = generatePairs(drug_df, disease_df, drugDiseaseKnown)
from sklearn.model_selection import GroupKFold
from sklearn.model_selection import StratifiedKFold
def balance_data(pairs, classes, n_proportion):
classes = np.array(classes)
pairs = np.array(pairs)
indices_true = np.where(classes == 1)[0]
indices_false = np.where(classes == 0)[0]
np.random.shuffle(indices_false)
indices = indices_false[:(n_proportion*indices_true.shape[0])]
print ("+/-:", len(indices_true), len(indices), len(indices_false))
pairs = np.concatenate((pairs[indices_true], pairs[indices]), axis=0)
classes = np.concatenate((classes[indices_true], classes[indices]), axis=0)
return pairs, classes
n_proportion = 2
pairs, classes= balance_data(pairs, classes, n_proportion)
pairs_train, pairs_test, classes_train, classes_test = model_selection.train_test_split(pairs, classes, stratify=classes, test_size=0.2, shuffle=True)
len(pairs_train), len(pairs_test)
def calculateDrugMaxMean(drug, disease, knownDrugDisease, drugDF):
#print (drug, disease)
# get only diseases related to this drug
filteredDrugs=knownDrugDisease[knownDrugDisease[:,1]==disease,0]
similarities = drugDF.loc[filteredDrugs][drug].values
similarities2= np.where(similarities==1.0,0.0,similarities)
#knownDrugDisease[knownDrugDisease[:,1]==disease,0]
#c=np.where(a==1.0,0.0,a)
try:
maxSimilarity=float(np.max(similarities2))
except :
maxSimilarity=0.0
return maxSimilarity
#not used , we use best similar disease instead of diseases filtered wrt drugs
def calculateDiseaseMaxMeanFiltered(drug, disease, knownDrugDisease, diseaseDF):
#print (drug, disease)
# get only diseases related to this drug
filteredDiseases=knownDrugDisease[knownDrugDisease[:,0]==drug,1]
similarities = diseaseDF.loc[filteredDiseases][disease].values
similarities2= np.where(similarities==1.0,0.0,similarities)
#knownDrugDisease[knownDrugDisease[:,1]==disease,0]
#c=np.where(a==1.0,0.0,a)
try:
maxSimilarity=float(np.max(similarities2))
except :
maxSimilarity=0.0
return maxSimilarity
def calculateDiseaseMaxMean(drug, disease, knownDrugDisease, diseaseDF):
#print (drug, disease)
b = diseaseDF.loc[knownDrugDisease[:,1]][disease].values
#b= np.sqrt( np.multiply(b,b) ) #remove negative values
c=np.where(b==1.0,0.0,b)
return float(np.max(c))
def createSingleFeatureDF(pairs, classes, knownDrugDisease, drugDFs, diseaseDFs):
totalNumFeatures = len(drugDFs)*len(diseaseDFs)
#featureMatri x= np.empty((len(classes),totalNumFeatures), float)
df =pd.DataFrame(list(zip(pairs[:,0], pairs[:,1], classes)), columns =['Drug','Disease','Class'])
index = 0
for i,drug_col in enumerate(drugDFs.columns.levels[0]):
drugDF = drugDFs[drug_col]
df["Feature_"+str(drug_col)] = df.apply(lambda row: calculateDrugMaxMean( row.Drug, row.Disease, knownDrugDisease, drugDF), axis=1)
for j,disease_col in enumerate(diseaseDFs.columns.levels[0]):
diseaseDF = diseaseDFs[disease_col]
df["Feature_"+str(disease_col)] = df.apply(lambda row: calculateDiseaseMaxMean( row.Drug, row.Disease, knownDrugDisease, diseaseDF), axis=1)
return df
def calculateSingleSimilarity(pairs_train, pairs_test, classes_train, classes_test, drug_df, disease_df, knownDrugDisease):
train_df = createSingleFeatureDF(pairs_train, classes_train, knownDrugDisease, drug_df, disease_df)
test_df = createSingleFeatureDF(pairs_test, classes_test, knownDrugDisease, drug_df, disease_df)
return train_df, test_df
knownDrugDisease= pairs_train[classes_train==1]
train_df, test_df = calculateSingleSimilarity(pairs_train, pairs_test, classes_train, classes_test, drug_df, disease_df, knownDrugDisease)
from sklearn import tree, ensemble
from sklearn import svm, linear_model, neighbors
features= ['Feature_GO-SIM_HPO-SIM',
'Feature_GO-SIM_PHENO-SIM',
'Feature_PPI-SIM_HPO-SIM',
'Feature_PPI-SIM_PHENO-SIM',
'Feature_SE-SIM_HPO-SIM',
'Feature_SE-SIM_PHENO-SIM',
'Feature_TARGETSEQ-SIM_HPO-SIM',
'Feature_TARGETSEQ-SIM_PHENO-SIM',
'Feature_TC_HPO-SIM',
'Feature_TC_PHENO-SIM']
features= ['Feature_GO-SIM',
'Feature_PPI-SIM',
'Feature_SE-SIM',
'Feature_TARGETSEQ-SIM',
'Feature_TC',
'Feature_HPO-SIM',
'Feature_PHENO-SIM']
def trainModel(train_df, clf):
#features = list(train_df.columns.difference(['Drug','Disease','Class']))
X = train_df[features]
y = train_df['Class']
X.head()
print ('fiting classifier...')
clf.fit(X, y)
return clf
def trainSingleModel(train_df, clf):
#features = list(train_df.columns.difference(['Drug','Disease','Class']))
features= ['Feature_GO-SIM',
'Feature_PPI-SIM',
'Feature_SE-SIM',
'Feature_TARGETSEQ-SIM',
'Feature_TC',
'Feature_HPO-SIM',
'Feature_PHENO-SIM']
X = train_df[features]
y = train_df['Class']
print(X.head())
print ('fitting classifier...')
clf.fit(X, y)
return clf
features= ['Feature_GO-SIM',
'Feature_PPI-SIM',
'Feature_SE-SIM',
'Feature_TARGETSEQ-SIM',
'Feature_TC',
'Feature_HPO-SIM',
'Feature_PHENO-SIM']
X = train_df[features]
y = train_df['Class']
train_df[features].to_csv("Xdatasinglesim.csv")
train_df['Class'].to_csv("ydatasinglesim.csv")
test_df[features].to_csv("Xdatasinglesimtest.csv")
test_df['Class'].to_csv("ydatasinglesimtest.csv")
features
dataset_df=pd.concat([train_df,test_df])
dataset_df.to_csv("singlefeatures_deepdrug_repurposingpredictiondataset.csv")
dataset_df.to_csv("singlefeatures_deepdrug_repurposingpredictiondatasetDiseaseFiltered.csv")
n_seed = 100
clfx= linear_model.LogisticRegression(penalty='l2', dual=False, tol=0.0001, C=1.0, random_state=n_seed)
#clf = trainModel(train_df, clfx)
trainSingleModel(train_df, clfx)
from sklearn import metrics
import numbers
def multimetric_score(estimator, X_test, y_test, scorers):
"""Return a dict of score for multimetric scoring"""
scores = {}
for name, scorer in scorers.items():
if y_test is None:
score = scorer(estimator, X_test)
else:
score = scorer(estimator, X_test, y_test)
if hasattr(score, 'item'):
try:
# e.g. unwrap memmapped scalars
score = score.item()
except ValueError:
# non-scalar?
pass
scores[name] = score
if not isinstance(score, numbers.Number):
raise ValueError("scoring must return a number, got %s (%s) "
"instead. (scorer=%s)"
% (str(score), type(score), name))
return scores
def evaluate(test_df, clf):
#
# features = list(train_df.columns.difference(['Drug','Disease','Class']))
X_test = test_df[features]
y_test = test_df['Class']
scoring = ['precision', 'recall', 'accuracy', 'roc_auc', 'f1', 'average_precision']
#scorers, multimetric = metrics.scorer._check_multimetric_scoring(clf, scoring=scoring)
scorers = {}
for scorer in scoring:
scorers[scorer] = metrics.get_scorer(scorer)
scores = multimetric_score(clf, X_test, y_test, scorers)
return scores
disjoint = True
n_fold = 10
if disjoint:
print ('Disjoint')
groups = pairs[:,0] # group by drug
group_kfold = GroupKFold(n_splits=n_fold)
cv = group_kfold.split(pairs, classes, groups)
else:
print ('Non-disjoint')
skf = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=n_seed)
cv = skf.split(pairs, classes)
n_seed = 100
cv_results = pd.DataFrame()
clf = linear_model.LogisticRegression(penalty='l2', solver='lbfgs', dual=False, tol=0.0001, C=1.0, random_state=n_seed)
for i, (train, test) in enumerate(cv):
print ('Fold',i+1)
start_time = time.time()
pairs_train = pairs[train]
classes_train = classes[train]
pairs_test = pairs[test]
classes_test = classes[test]
knownDrugDisease= pairs_train[classes_train==1]
#train_df, test_df = calculateSingleSimilarity(pairs_train, pairs_test, classes_train, classes_test, drug_df, disease_df, knownDrugDisease)(pairs_train, pairs_test, classes_train, classes_test, drug_df, disease_df, knownDrugDisease)
train_df, test_df = calculateSingleSimilarity(pairs_train, pairs_test, classes_train, classes_test, drug_df, disease_df, knownDrugDisease)
elapsed_time = time.time() - start_time
print ('Time elapsed to generate features:',time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))
clf = trainSingleModel(train_df, clf)
scores = evaluate(test_df, clf)
#print ("Scores:",scores)
cv_results = cv_results.append(scores, ignore_index=True)
cv_results.mean()
resfolder='resultslrsingleDrugFiltered'
os.mkdir(resfolder)
cv_results.to_csv(resfolder+'/disjoint_lr.csv')
cv_results.head()