-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_code2
More file actions
390 lines (266 loc) · 11.3 KB
/
test_code2
File metadata and controls
390 lines (266 loc) · 11.3 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 26 15:10:25 2021
@author: himangisrivastava
"""
from numpy import mean
from numpy import std
from numpy import absolute
from pandas import read_csv
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold
from sklearn.linear_model import Lasso
import random
import scipy
from scipy.io import arff
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# import scikitplot as skplt
import sklearn
from sklearn import preprocessing
from sklearn import datasets
from sklearn.impute import SimpleImputer
from sklearn.svm import SVR
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import DotProduct,WhiteKernel,RBF,Matern,RationalQuadratic,ExpSineSquared,ConstantKernel,PairwiseKernel
from sklearn.linear_model import LinearRegression
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error, r2_score
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
import re
import tqdm
import numpy as np
import os
import datetime
from multiprocessing import Pool, cpu_count
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.ensemble import RandomForestRegressor, VotingRegressor
from sklearn.linear_model import LinearRegression, ElasticNet, LassoCV, Lasso, ElasticNetCV
from sklearn.linear_model import Lasso
import re
import numpy as np
import cptac
import pandas as pd
from predict_protein import get_proteins, learn_cptac
from sklearn.preprocessing import StandardScaler, RobustScaler
cptac.download(dataset="ovarian")
cptac.download(dataset="brca")
ov = cptac.Ovarian()
br = cptac.Brca()
ov_rna = ov.get_transcriptomics()
ov_pro = ov.get_proteomics()
b = ov.join_omics_to_omics('transcriptomics', 'proteomics')
b.columns = b.columns.droplevel(1)
br_rna = br.get_transcriptomics()
br_pro = br.get_proteomics()
d = br.join_omics_to_omics('transcriptomics', 'proteomics')
d.columns = d.columns.droplevel(1)
b_std = b.copy()
b_std = b_std.loc[:, ~b_std.columns.duplicated(keep='first')]
b_tx_cols = [col for col in b_std.columns if col.endswith('transcriptomics')]
b_std[b_tx_cols] = StandardScaler().fit_transform(b_std[b_tx_cols])
b_std.index = 'OV' + b_std.index
file2=pd.read_csv("tidy_stringdb_homosapiens_250.txt", sep='\t')
data=file2[file2['combined_score'] > 800]
# to obtain protien data
class protein_data(object):
def __init__(self, cptac_df=None):
if (cptac_df.empty == True):
print('data not available')
else:
self.df = cptac_df
self.df2 =cptac_df
self.all_proteomics = [re.sub('_proteomics', "", protein) for protein in self.df.columns if
protein.endswith('_proteomics')]
self.all_transcriptomics = [re.sub('_transcriptomics', "", transcript) for transcript in self.df.columns if
transcript.endswith('_transcriptomics')]
self.shared_proteins = [protein for protein in self.all_proteomics if protein in self.all_transcriptomics]
self.transciptomics_df=self.df2[self.df2.columns[self.df2.columns.to_series().str.contains('_transcriptomics')]]
self.proteomics_df=self.df2[self.df2.columns[self.df2.columns.to_series().str.contains('_proteomics')]]
self.transciptomics_df = self.transciptomics_df.rename(columns=lambda x: re.sub('_transcriptomics', "",x))
self.proteomics_df = self.proteomics_df.rename(columns=lambda x: re.sub('_proteomics', "",x))
self.transciptomics_df = self.transciptomics_df.loc[:,~self.transciptomics_df.columns.duplicated(keep='first')]
self.proteomics_df = self.proteomics_df.loc[:,~self.proteomics_df.columns.duplicated(keep='first')]
# ----------------------- Getters -----------------------
#AB 03.14.2023 fixing spelling ie -> ei
def get_proteins(self):
return self.all_proteomics
def get_transcritomics(self):
return self.all_transcriptomics
def get_shared_protein(self):
return self.shared_proteins
def get_transcriptomics_df(self):
return self.transciptomics_df
def get_proteomics_df(self):
return self.proteomics_df
#AB 03.07.2024 Fix spelling
def get_inividual_protein_data(self,single_protein):
self.each_protein_df= pd.concat([pd.DataFrame(self.transciptomics_df[single_protein]),pd.DataFrame(self.proteomics_df[single_protein])],axis=1).dropna()
return self.each_protein_df
#AB 03.07.2024 fix spelling
def get_multiple_protein_data(self,protein):
self.x_df=self.transciptomics_df
self.y_df=self.proteomics_df[protein]
self.xy_df=pd.concat([self.x_df,self.y_df],axis=1,join='inner').dropna()
return self.xy_df
#AB 03.07.2024 fix spelling
class learning_data_inividual_protein_data(protein_data):
def __init__(self, protein_data_,single_protein):
self.xy_df=protein_data_.get_inividual_protien_data(single_protein)
self.x = self.xy_df.iloc[:, :-1] # .values
self.y = self.xy_df.iloc[:, -1]
def test_train(self):
self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(self.x, self.y, test_size=0.2, random_state=2)
return self.x_train, self.x_test, self.y_train, self.y_test
def LinearRegression(self):
x_train, x_test, y_train, y_test = self.test_train()
linear = LinearRegression(n_jobs=-1)
linear.fit(x_train, y_train)
return linear
def LassoRegression(self):
x_train, x_test, y_train, y_test = self.test_train()
lasso = Lasso()
lasso.fit(x_train, y_train)
return lasso
def RandomForest(self):
x_train, x_test, y_train, y_test = self.test_train()
forest = RandomForestRegressor(n_estimators=1000,criterion='mse',max_depth=10,random_state=1,n_jobs=-1)
forest.fit(x_train, y_train)
return forest
def run_model(self):
x_train, x_test, y_train, y_test = self.test_train()
model = []
model_names = []
regr1 = self.LinearRegression()
model.append(regr1)
model_names.append('Linear Regression')
regr2 = self.LassoRegression()
model.append(regr2)
model_names.append('lasso_regression')
regr3 = self.RandomForest()
model.append(regr3)
model_names.append('random_forest_regression')
return model, model_names
def results(self):
x_train, x_test, y_train, y_test = self.test_train()
model, model_names=self.run_model()
rmse_scores = dict()
training_scores = []
test_scores = []
r2_scores = dict()
training_scores_r2 = []
test_scores_r2 = []
for regr, regr_name in zip(model, model_names):
y_train_preds = regr.predict(x_train)
y_test_preds = regr.predict(x_test)
train_score = round(np.sqrt(mean_squared_error(y_train, y_train_preds)/ (np.max(y_train) - np.min(y_train))),4)
test_score = round(np.sqrt(mean_squared_error(y_test, y_test_preds)/ (np.max(y_test) - np.min(y_test))),4)
training_scores.append(train_score)
test_scores.append(test_score)
rmse_scores[regr_name] = test_score
train_score_r2 = r2_score(y_train, y_train_preds)
test_score_r2 = r2_score(y_test, y_test_preds)
training_scores_r2.append(train_score_r2)
test_scores_r2.append(test_score_r2)
r2_scores[regr_name] = test_score
return r2_scores
self.xy_df=protein_data_.get_inividual_protien_data(single_protein)
self.x = self.xy_df.iloc[:, :-1] # .values
self.y = self.xy_df.iloc[:, -1]
#AB: fix spelling
#for checking the results on single protein dats
single_protein_results=[]
for i in protein_data(b_std).get_shared_protein():
rr=learning_data_inividual_protein_data(protein_data(b_std),i).results()
single_protein_results.append(rr)
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()
lin=[]
las=[]
ran=[]
for fo in single_protein_results:
lin.append(fo['Linear Regression'])
las.append(fo['lasso_regression'])
ran.append(fo['random_forest_regression'])
plt.plot(lin,las,ran,np.arange(len(single_protien_results)) )
plt.show()
width=0.2
x=np.arange(len(single_protein_results))
fig, ax = plt.subplots()
ax.bar(x-width/2, lin, width, color='b', label='Linear_Regression')
ax.bar(x, las, width, color='g',label='Lasso_Regression')
ax.bar(x+width/2, ran, width, color='r', label='Random_forest_Regression')
ax.autoscale(tight=True)
ax.set_ylabel('R^2 results')
ax.set_title('single transcriptomics vs proteomics')
ax.legend()
plt.ylim([0, 2])
plt.show()
from itertools import product
aa=[]
for e1,e2 in product(proteins,transcriptomics):
aa.append(e1+str(e2))
aa=pd.DataFrame(aa)
d=aa[aa.isin(list(data['Name1']))]
df_m.columns[]
data['Name'] = data['p1'].str.cat(data['p2'],sep="")
data['Name1'] = data['p2'].str.cat(data['p1'],sep="")
proteins=protein_data(b_std).get_protiens()
transcriptomics=protein_data(b_std).get_transcritomics()
data = data[data['p1'].isin(proteins)]
o=list(data['Name1'])
m=aa
dd=[i for i, j in zip(o, m) if i == j]
rr=set(o).intersection(m)
group=data.groupby(by="p1")
dfs = [group for _, group in data.groupby('p1')]
pp=dfs[0].p1.unique()[0]
for e in proteins:
if e[0] in list1:
list3.append(e)
ss=protein_data(b_std).get_multiple_protein_data(pp)
ld=list(dfs[0].p2)
pp=dfs[0].p1.unique()[0]
for i in proteins:
if(dfs[1].p1.unique()==i):
tt=[]
for i in range(len(dfs)):
if any(dfs[i].p1.unique()[0] in s for s in proteins):
print(dfs[i].p2)
protein_data(b_std).get_multiple_protein_data(pp)
ss=ss.loc[:, ss.columns.isin(ld)]
pp=protein_data_filtered(b_std,data).get_grouped_df1()[1].p1.unique()[0]
ld=list(protein_data_filtered(b_std,data).get_grouped_df1()[1].p2)
protein_data_filtered(b_std,data).get_multiple_protein_data_filtered1(pp,ld)
#for checking the results on single protein dats
single_protein_results=[]
for i in tqdm(protein_data(b_std).get_shared_protein()):
rr=learning_data_inividual_protein_data(protein_data(b_std),i).results()
single_protein_results.append(rr)
import csv
csv_columns = ['lasso_regression', 'random_forest_regression', 'Linear Regression']
csv_file = "single_protein_data.csv"
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in single_protein_results:
writer.writerow(data)
except IOError:
print("I/O error")
proteins=[]
for i in tqdm(protein_data(b_std).get_shared_protein()):
proteins.append(i)
aa = pd.read_csv("single_protein_data.csv")
aa["proteins"] = proteins
aa.to_csv("single_protein.csv", sep='\t')