-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
331 lines (286 loc) · 13 KB
/
main.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
#William Huang
#Bioinformatics Data Project
#Dependencies: ChemBL and rdkit: (conda install -c rdkit rdkit -y)
# Bash: conda install -c conda-forge bash
#or Run Bash from Git Desktop
# TextWrap3: pip install textwrap3
from textwrap3 import wrap
import pandas as pd
import numpy as np
from chembl_webresource_client.new_client import new_client
from rdkit import Chem
from rdkit.Chem import Descriptors, Lipinski
from lipinski_plots import lipinski_plots as lp
from numpy.random import seed
from numpy.random import randn
from scipy.stats import mannwhitneyu
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.feature_selection import VarianceThreshold
import seaborn as sns
sns.set(style='ticks')
import matplotlib.pyplot as plt
#this function retrieves data (compounds and their molecular info) for a specific target (e.g coronavirus)
def retrievedata():
print("\n\n", "-" * 75)
search_query = input("What target would you like to search for? ")
#CHEMBL325
#search_query = "Coronavirus"
print("-" * 75,"\n\n")
target = new_client.target
target_query = target.search(search_query)
targets = pd.DataFrame.from_dict(target_query)
selected_target = targets.target_chembl_id[0]
print("-" * 75)
print("The target selected is: ", selected_target)
print("-" * 75,"\n\n")
activity = new_client.activity
res = activity.filter(target_chembl_id=selected_target).filter(standard_type="IC50")
print("Loading Compounds Into Data Frame...\n")
df = pd.DataFrame.from_dict(res)
if df.empty or len(df) < 25:
print("-" * 75)
print("This data frame has little to no IC50 values.")
print("-" * 75, "\n\n")
return
preprocess(df)
#this function preprocesses the data by subsetting to a more compatible dataset
def preprocess(df):
print("Subsetting Data Frame of IC50 Values...\n")
#subset for non NAs
subset = df[df.standard_value.notna()]
#subset for non 0 IC50
subset = subset.loc[subset.standard_value != '0.0']
#subset for those with canonical smiles data
selection = df.canonical_smiles.notna()
subset = subset.loc[selection]
#drop duplicates
subset = subset.drop_duplicates(['canonical_smiles'])
#we only want the chem ID, canonical smiles info, and the IC50
selection = ['molecule_chembl_id', 'canonical_smiles', 'standard_value']
subset = subset[selection]
if subset.empty:
print("-" * 75)
print("This data frame has little to no IC50 values.")
print("-" * 75, "\n\n")
#now we label compounds based on their activity
labelcompounds(subset)
#This function labels compounds based on their IC50: inactive, active, or intermediate and "cleans"
#canonical smiles data for the longest segment in between '.'s
def labelcompounds(df):
print("Labeling Compounds...\n")
bioactivity_threshold = []
for i in df.standard_value:
if float(i) >= 10000:
bioactivity_threshold.append("inactive")
elif float(i) <= 1000:
bioactivity_threshold.append("active")
else:
bioactivity_threshold.append("intermediate")
#turn the list into a series
bioactivity_class = pd.Series(bioactivity_threshold, name='class')
df.reset_index(drop=True, inplace=True)
#adds the class column to df
labeled = pd.concat([df, bioactivity_class], axis=1)
#removes the current canonical smiles
labeled_no_smiles = labeled.drop(columns='canonical_smiles')
smiles = []
#get the highlighted parts of the canonical smiles data
for i in df.canonical_smiles.tolist():
cpd = str(i).split('.')
cpd_longest = max(cpd, key=len)
smiles.append(cpd_longest)
#adding the highlighted parts of can. smiles data
smiles = pd.Series(smiles, name='canonical_smiles')
df_cleaned_smiles = pd.concat([labeled_no_smiles, smiles], axis=1)
#now, we evaluate based on lipinski descriptors
evaluate_drug(labeled, df_cleaned_smiles)
#this function uses the 5 lipinski descriptors to evaluate the compounds via a Mann-whitney test
# and graphs the data with matplotlib
def evaluate_drug(df, df_cleaned_smiles):
#appends lipinski descriptors to dataframe
df_lipinski = lipinski_descriptors(df_cleaned_smiles.canonical_smiles)
df_combined = pd.concat([df, df_lipinski], axis=1)
#normalizes the IC50
df_normal = norm_values(df_combined)
#turns IC50 to pIC50
df_final = to_pIC50(df_normal)
#saves final data to csv for reference
df_final.to_csv('bioactivity_final.csv', index=False)
#testing_df subsets for active and inactive values
testing_df = df_final[df_final['class'] != 'intermediate']
print("\n\n", "-"*75)
print("Mann-Whitney Test Results For Each Lipinski Descriptor:")
print("-" * 75, "\n\n")
lipinskiplot = lp(testing_df)
lipinskiplot.bar_graph(testing_df)
lipinskiplot.scatter_plot(testing_df)
lipinskiplot.pIC_50_plot(testing_df)
print(mannwhitney(df, testing_df, 'pIC50'))
lipinskiplot.mol_weight(testing_df)
print(mannwhitney(df, testing_df, 'MW'))
lipinskiplot.logP(testing_df)
print(mannwhitney(df, testing_df, 'LogP'))
lipinskiplot.num_hdonors(testing_df)
print(mannwhitney(df, testing_df, 'NumHDonors'))
lipinskiplot.num_hacceptors(testing_df)
print(mannwhitney(df, testing_df, 'NumHAcceptors'))
predict_from_pIC50()
# changes IC50 to pIC50
def to_pIC50(input):
pIC50 = []
for i in input['standard_value_norm']:
molar = i * (10 ** -9) # Converts nM to M
pIC50.append(-np.log10(molar))
input['pIC50'] = pIC50
x = input.drop('standard_value_norm', axis = 1)
return x
#normalizes the IC50
def norm_values(input):
norm = []
for i in input['standard_value']:
i = float(i)
if i > 100000000:
i = 100000000
norm.append(i)
input['standard_value_norm'] = norm
x = input.drop('standard_value', axis = 1)
return x
#calculates the lipinski descriptors based on chemical information
def lipinski_descriptors(smiles, verbose=False):
# Inspired by: https://codeocean.com/explore/capsules?query=tag:data-curation
moldata = []
for elem in smiles:
mol = Chem.MolFromSmiles(elem)
moldata.append(mol)
baseData = np.arange(1, 1)
i = 0
for mol in moldata:
desc_MolWt = Descriptors.MolWt(mol)
desc_MolLogP = Descriptors.MolLogP(mol)
desc_NumHDonors = Lipinski.NumHDonors(mol)
desc_NumHAcceptors = Lipinski.NumHAcceptors(mol)
row = np.array([desc_MolWt,
desc_MolLogP,
desc_NumHDonors,
desc_NumHAcceptors])
if (i == 0):
baseData = row
else:
baseData = np.vstack([baseData, row])
i = i + 1
columnNames = ["MW", "LogP", "NumHDonors", "NumHAcceptors"]
descriptors = pd.DataFrame(data=baseData, columns=columnNames)
return descriptors
#performs Mann Whitney test and outputs null hypothesis reject/fail to reject
def mannwhitney(df, df_2class, descriptor, verbose=False):
# https://machinelearningmastery.com/nonparametric-statistical-significance-tests-in-python/
# seed the random number generator
seed(1)
# actives and inactives
selection = [descriptor, 'class']
df = df_2class[selection]
active = df[df['class'] == 'active']
active = active[descriptor]
selection = [descriptor, 'class']
df = df_2class[selection]
inactive = df[df['class'] == 'inactive']
inactive = inactive[descriptor]
# compare samples
stat, p = mannwhitneyu(active, inactive)
# print('Statistics=%.3f, p=%.3f' % (stat, p))
# interpret
alpha = 0.05
if p > alpha:
interpretation = 'Same distribution (fail to reject H0)'
else:
interpretation = 'Different distribution (reject H0)'
results = pd.DataFrame({'Descriptor': descriptor,
'Statistics': stat,
'p': p,
'alpha': alpha,
'Interpretation': interpretation}, index=[0])
filename = 'mannwhitneyu_' + descriptor + '.csv'
results.to_csv(filename, index=False)
return results
def predict_from_pIC50():
df = pd.read_csv('bioactivity_final.csv')
selection = ['canonical_smiles', 'molecule_chembl_id']
df_selection = df[selection]
df_selection.to_csv('molecule.smi', sep='\t', index=False, header=False)
print("\n\n", "-" * 75)
print("Now, you need to run \" bash padel.sh\" in Git for Windows or another shell/command prompt that supports bash.")
print("This is necessary to retrieve the PaDEL descriptors for the Random Forest regression!")
print("-" * 75,"\n\n")
valid_ans = False
while(not valid_ans):
ans = input("\n\nDid you run \" bash padel.sh\" yet? (Y/N): ")
if(ans.upper() == "Y"):
valid_ans = True
elif(ans.upper() == "N"):
print("Run the \" bash padel.sh\" command to get the PaDEL descriptors.")
else:
print("Please enter a valid command: (Y/N)")
X = pd.read_csv('descriptors_output.csv').drop(columns=['Name'])
Y = df['pIC50']
#relating pIC50 to the molecular descriptors
dataset = pd.concat([X, Y], axis=1)
dataset.to_csv('bioactivity_data_pubchem_padel.csv')
selection = VarianceThreshold(threshold=(.8 * (1 - .8)))
X = selection.fit_transform(X)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)
model = RandomForestRegressor(n_estimators=100)
print("\nRunning Random Forest Regression...\n")
model.fit(X_train, Y_train)
r2 = model.score(X_test, Y_test)
predictions = model.predict(X_test)
plt.clf()
sns.set(color_codes=True)
sns.set_style("white")
ax = sns.regplot(x = Y_test, y = predictions, scatter_kws={'alpha': 0.4})
ax.set_xlabel(xlabel = 'Experimental pIC50', fontsize='large', fontweight='bold')
ax.set_ylabel(ylabel = 'Predicted pIC50', fontsize='large', fontweight='bold')
ax.set_xlim(min(Y_test), max(Y_test))
ax.set_ylim(min(predictions), max(predictions))
ax.figure.set_size_inches(10, 10)
ax.get_figure().savefig('predicted_experimental_pIC50.pdf')
print("-"*75)
print("Regression Plot Saved As 'predicted_experimental_pIC50.pdf' ")
print("-" * 75, "\n\n")
if __name__ == '__main__':
print("\n\n","-"*75)
print(" _ _ _ __ _ _ ")
print("| |__ (_) ___ (_)_ __ / _| ___ _ __ _ __ ___ __ _| |_(_) ___ ___ ")
print("| '_ \| |/ _ \| | '_ \| |_ / _ \| '__| '_ ` _ \ / _` | __| |/ __/ __|")
print("| |_) | | (_) | | | | | _| (_) | | | | | | | | (_| | |_| | (__\__ \ ")
print("|_.__/|_|\___/|_|_| |_|_| \___/|_| |_| |_| |_|\__,_|\__|_|\___|___/")
print(" _ _")
print(" _ __ _ __ ___ (_) ___ ___| |_")
print("| '_ \| '__/ _ \| |/ _ \/ __| __|")
print("| |_) | | | (_) | | __/ (__| |_")
print("| .__/|_| \___// |\___|\___|\__|")
print("|_| |__/ ")
print("-" * 75,"\n\n")
print("-" * 75)
x = wrap("This program is designed to query the ChemBL Target Database for a specific target, " +
"and then retrieve data on compounds that show activity with that target, evaluated by a metric called the 'IC50'." +
" The IC50 essentially quantifies the concentration of a drug necessary to inhibit a biological process by half, thus " +
"making it a good marker for drug efficacy, along with other variables that altogether are part of the Lipinski Molecular " +
"Descriptors, which evaluate a compound's overall bioactivity. We will be calculating how well the interacting compounds score" +
" on the Lipinski Molecular Descriptors, comparing an active group (IC50 >= 10000) and an inactive group (IC50 <= 1000). ", 70)
for line in x:
print(line)
print("\n")
x = wrap("We will then use another collection of descriptors, the PaDEL Descriptors, to describe the data and then try to 'predict' an IC50 value for each compound " +
"using Random Forest Regression, and compare it to the actual IC50.", 70)
for line in x:
print(line)
print("\n")
x = wrap("This predicted IC50 value can be useful in that we can attempt to gauge a compound's bioactivity towards a certain inhibitor" +
" without having to actually experimentally determine the IC50, saving costs and increasing the accessibility of IC50 data.", 70)
for line in x:
print(line)
print("\nCreators: William Huang")
print("Inspired by Data Professor on YT and machinelearningmastery.com")
print("-" * 75, "\n\n")
retrievedata()