-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
173 lines (99 loc) · 5.82 KB
/
test.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
# -*- coding: utf-8 -*-
import numpy as np
from Bio import SeqIO
import gzip
import os
from keras.models import Sequential
from keras.layers import Dense, Activation, Conv1D, MaxPooling1D, Flatten, GlobalAveragePooling1D, Dropout
from sklearn.model_selection import KFold
from sklearn.metrics import roc_auc_score, roc_curve
import pandas as pd
np.random.seed(454)
def load_data(path):
data = gzip.open(os.path.join(path,"sequences.fa.gz"),"rt")
return data
def get_seq(protein, t_data):
training_data = load_data("data/clip/%s/5000/training_sample_0"% protein)
x_train = np.zeros((5000,101,4))
r = 0
for record in SeqIO.parse(training_data,"fasta"):
sequence = list(record.seq)
nucleotide = {'A' : 0, 'T' : 1, 'G' : 2, 'C' : 3, 'N' : 4}
num_seq = list() #sekvenca v številskem formatu
for i in range(0,len(sequence)):
num_seq.append(nucleotide[sequence[i]])
X = np.zeros((1,len(num_seq),4))
for i in range (len(num_seq)):
if num_seq[i] <= 3:
X[:,i,num_seq[i]] = 1
x_train[r,:,:] = X
r = r + 1
return x_train
def get_class(protein, t_data):
y_train = []
if t_data == 'train':
data = load_data("data/clip/%s/5000/training_sample_0"% protein)
elif t_data == 'test':
data = load_data("data/clip/%s/5000/test_sample_0"% protein)
for record in SeqIO.parse(data,"fasta"):
v = int((record.description).split(":")[1])
y_train.append([int(v == 0), int(v != 0)])
y_train = np.array(y_train)
return y_train
def get_cobinding(protein, t_data):
with gzip.open(("data/clip/%s/5000/training_sample_0/matrix_Cobinding.tab.gz"% protein), "rt") as f:
cobinding_data = np.loadtxt(f, skiprows=1)
cobinding = np.zeros((5000,101,cobinding_data.shape[1]/101),dtype=np.int) #ustvarim prazen array
for n in range(0,cobinding_data.shape[1],101):
a = cobinding_data[:,n:(n+101)]
cobinding[:,:,(n/101)] = a
return cobinding
def get_region (protein, t_data):
with gzip.open(("data/clip/%s/5000/training_sample_0/matrix_RegionType.tab.gz"% protein), "rt") as f:
region_data = np.loadtxt(f, skiprows=1)
region = np.zeros((5000,101,region_data.shape[1]/101),dtype=np.int) #ustvarim prazen array
for n in range(0,region_data.shape[1],101):
a = region_data[:,n:(n+101)]
region[:,:,(n/101)] = a
return region
def get_fold (protein, t_data):
with gzip.open(("data/clip/%s/5000/training_sample_0/matrix_RNAfold.tab.gz"% protein), "rt") as f:
fold_data = np.loadtxt(f, skiprows=1)
fold = np.zeros((5000,101,fold_data.shape[1]/101),dtype=np.int) #ustvarim prazen array
for n in range(0,fold_data.shape[1],101):
a = fold_data[:,n:(n+101)]
fold[:,:,(n/101)] = a
return fold
score_list = []
protein_list = ["1_PARCLIP_AGO1234_hg19", "2_PARCLIP_AGO2MNASE_hg19","3_HITSCLIP_Ago2_binding_clusters","4_HITSCLIP_Ago2_binding_clusters_2","5_CLIPSEQ_AGO2_hg19", "6_CLIP-seq-eIF4AIII_1","7_CLIP-seq-eIF4AIII_2","8_PARCLIP_ELAVL1_hg19","9_PARCLIP_ELAVL1MNASE_hg19", "10_PARCLIP_ELAVL1A_hg19", "10_PARCLIP_ELAVL1A_hg19", "12_PARCLIP_EWSR1_hg19", "13_PARCLIP_FUS_hg19", "14_PARCLIP_FUS_mut_hg19", "15_PARCLIP_IGF2BP123_hg19", "16_ICLIP_hnRNPC_Hela_iCLIP_all_clusters", "17_ICLIP_HNRNPC_hg19", "18_ICLIP_hnRNPL_Hela_group_3975_all-hnRNPL-Hela-hg19_sum_G_hg19--ensembl59_from_2337-2339-741_bedGraph-cDNA-hits-in-genome", "19_ICLIP_hnRNPL_U266_group_3986_all-hnRNPL-U266-hg19_sum_G_hg19--ensembl59_from_2485_bedGraph-cDNA-hits-in-genome", "20_ICLIP_hnRNPlike_U266_group_4000_all-hnRNPLlike-U266-hg19_sum_G_hg19--ensembl59_from_2342-2486_bedGraph-cDNA-hits-in-genome", "21_PARCLIP_MOV10_Sievers_hg19", "22_ICLIP_NSUN2_293_group_4007_all-NSUN2-293-hg19_sum_G_hg19--ensembl59_from_3137-3202_bedGraph-cDNA-hits-in-genome", "23_PARCLIP_PUM2_hg19", "24_PARCLIP_QKI_hg19", "25_CLIPSEQ_SFRS1_hg19","26_PARCLIP_TAF15_hg19", "27_ICLIP_TDP43_hg19", "28_ICLIP_TIA1_hg19", "29_ICLIP_TIAL1_hg19", "30_ICLIP_U2AF65_Hela_iCLIP_ctrl_all_clusters", "31_ICLIP_U2AF65_Hela_iCLIP_ctrl+kd_all_clusters"]
for x in range (0,31):
protein = protein_list[x]
X = np.dstack((get_seq(protein,"train"),get_region(protein, "train"),get_fold(protein,"train"),get_cobinding(protein,"train")))
y = get_class(protein,"train")
size = X.shape[2]
kf = KFold(n_splits=10) #10 različnih delitev podatkov
score = []
for train_index, test_index in kf.split(X):
# sestavljanje modela
model = Sequential()
model.add(Conv1D(60,6 ,data_format='channels_last', input_shape=(101, size) , strides = 1, padding='valid'))
model.add(MaxPooling1D(pool_size=20, strides=1, padding='valid'))
model.add(Conv1D(60,4 , activation='relu'))
model.add(Dropout(0.2))
model.add(MaxPooling1D(pool_size=40, strides=1, padding='valid'))
model.add(Conv1D(60,4 , activation='relu'))
model.add(MaxPooling1D(pool_size=30, strides=1, padding='valid'))
model.add(Conv1D(60,3 , activation='relu'))
model.add(GlobalAveragePooling1D())
model.add(Dense(200, activation='relu'))
model.add(Dense(2, activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
model.fit(X_train, y_train, epochs=10, batch_size=16, verbose=0)
y_scores =(model.predict(X_test))# pridobim napovedi za sekvence iz cobinding
score.append(roc_auc_score(y_test, y_scores))
print ("%s finishhed" % (protein))
score_list.append(np.mean(score))
print (np.mean(score))
print(score_list)