forked from streamride/CapsNet-keras-imdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_data.py
562 lines (442 loc) · 18.7 KB
/
ml_data.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 31 15:44:05 2017
@author: fnord
"""
import numpy as np
# ================================================================
# BaseData =======================================================
# ================================================================
class BaseData(object):
def __init__(self, npath, ppath):
self.ppath = ppath
self.npath = npath
# def get_sequences_from_fasta(self, path):
# seqs = list()
# with open(path) as f:
# for l in f.readlines()[1::2]:
# seqs.append(l[:-1])
# return seqs
def get_sequences_from_fasta(self, path):
from Bio import SeqIO
seqs = list()
for seq_record in SeqIO.parse(path, "fasta"):
s = str(seq_record.seq.upper())
if 'N' not in s:
seqs.append( s )
return seqs
def get_kmers(self, seq, k=1, step=1):
numChunks = ((len(seq)-k)/step)+1
mers = list()
for i in range(0, numChunks*step-1, step):
mers.append(seq[i:i+k])
return mers
def encode_sequences(self, seqs):
raise NotImplementedError()
def enconde_positives(self):
return self.encode_sequences(self.get_sequences_from_fasta(self.ppath))
def enconde_negatives(self):
return self.encode_sequences(self.get_sequences_from_fasta(self.npath))
def set_XY(self, negdata, posdata):
from numpy import array
from numpy import vstack
Y = array([0 for x in range(negdata.shape[0])] + [1 for x in range(posdata.shape[0])])
#Y = Y.transpose()
self.neg = negdata
self.pos = posdata
X = vstack((negdata, posdata))
assert X.shape[0]==Y.shape[0]
self.X = X
self.Y = Y
return X, Y
def getX(self, frame=0):
if frame<0 or frame>3:
return
elif frame==0:
return self.X
else:
return self.X[:, frame::3]
def getY(self):
return self.Y
def get_negative_samples(self):
samples = self.X[:(self.n_samples_neg)]
assert samples.shape[0]==self.n_samples_neg
return samples
def get_positive_samples(self):
samples = self.X[self.n_samples_pos:]
assert samples.shape[0]==self.n_samples_pos
return samples
def set_data(self):
# import pandas as pd
# from sklearn.cluster import KMeans
# from sklearn.decomposition import PCA
# import numpy as np
#
posdata = self.enconde_positives()
negdata = self.enconde_negatives()
#
# D = pd.DataFrame(posdata)
# D_ = D.iloc[:,59:61]
#
# pca = PCA(n_components=2)
# pca.fit(D_)
# existing_2d = pca.transform(D_)
# existing_df_2d = pd.DataFrame(existing_2d)
# existing_df_2d.index = D_.index
# existing_df_2d.columns = ['PC1','PC2']
# existing_df_2d.head()
# print existing_df_2d
# print(pca.explained_variance_ratio_)
#
#
# kmeans = KMeans(n_clusters=3)
# clusters = kmeans.fit(D_)
# existing_df_2d['cluster'] = pd.Series(clusters.labels_, index=existing_df_2d.index)
# existing_df_2d.plot(
# kind='scatter',
# x='PC2',y='PC1',
# c=existing_df_2d.cluster.astype(np.float),
# figsize=(16,8))
assert negdata.shape[1]==posdata.shape[1]
self.n_samples_pos = posdata.shape[0]
self.n_samples_neg = negdata.shape[0]
self.sample_size = posdata.shape[1]
self.set_XY(negdata, posdata)
def set_kmers_encoder(self):
from itertools import product
from numpy import array
from sklearn.preprocessing import LabelEncoder
nucs = ['G','A','C','T']
tups = list(product(nucs, repeat=self.k))
data = array([''.join(x) for x in tups])
self.label_encoder = LabelEncoder()
self.label_encoder.fit(data)
def value2label_dinuc_converter(self):
import numpy as np
tab = np.loadtxt('dinuc_values', delimiter=',', dtype=np.float32)
conv = dict()
cnt = 0
for i in range(len(tab)):
conv[i] = dict()
for elem in tab[i]:
if not elem in conv[i].keys():
conv[i][elem] = cnt
cnt += 1
T = list()
for i in range(len(tab)):
R = list()
for elem in tab[i]:
R.append( conv[i][elem] )
T.append( R )
return np.array( T )
# ================================================================
# SequenceProteinData ============================================
# ================================================================
class SequenceProteinData(BaseData):
STANDARD_GENETIC_CODE = {
'TTT':0, 'TTC':0, 'TCT':10, 'TCC':10,
'TAT':1, 'TAC':1, 'TGT':13, 'TGC':13,
'TTA':2, 'TCA':10, 'TAA':20, 'TGA':20,
'TTG':2, 'TCG':10, 'TAG':20, 'TGG':19,
'CTT':2, 'CTC':2, 'CCT':14, 'CCC':14,
'CAT':3, 'CAC':3, 'CGT':15, 'CGC':15,
'CTA':2, 'CTG':2, 'CCA':14, 'CCG':14,
'CAA':4, 'CAG':4, 'CGA':15, 'CGG':15,
'ATT':5, 'ATC':5, 'ACT':11, 'ACC':11,
'AAT':6, 'AAC':6, 'AGT':10, 'AGC':10,
'ATA':5, 'ACA':11, 'AAA':16, 'AGA':15,
'ATG':7, 'ACG':11, 'AAG':16, 'AGG':15,
'GTT':8, 'GTC':8, 'GCT':17, 'GCC':17,
'GAT':12, 'GAC':12, 'GGT':18, 'GGC':18,
'GTA':8, 'GTG':8, 'GCA':17, 'GCG':17,
'GAA':9, 'GAG':9, 'GGA':18, 'GGG':18}
def __init__(self, npath, ppath):
super(SequenceProteinData, self).__init__(npath, ppath)
self.set_data()
def transform(self, trimers):
return [self.STANDARD_GENETIC_CODE[trimer] for trimer in trimers]
def encode_sequences(self, seqs):
from numpy import vstack
return vstack([self.transform(self.get_kmers(seq, k=3)) for seq in seqs])
# ================================================================
# SequenceNucsData ===============================================
# ================================================================
class SequenceNucsData(BaseData):
def __init__(self, npath, ppath, k=1):
super(SequenceNucsData, self).__init__(npath, ppath)
self.k = k
self.set_kmers_encoder()
self.set_data()
def encode_sequences(self, seqs):
from numpy import vstack
return vstack([self.label_encoder.transform(self.get_kmers(seq, k=self.k)) for seq in seqs])
#
#npath = "fasta/Ecoli_neg.fa"
#ppath = "fasta/Ecoli_pos.fa"
#
#
#data = SequenceNucsData(npath, ppath, k=1)
#X = data.getX()
#Y = data.getY()
# ================================================================
# SequenceSimpleData =============================================
# ================================================================
class SequenceSimpleData(BaseData):
def __init__(self, npath, ppath, k=1):
super(SequenceSimpleData, self).__init__(npath, ppath)
self.k = k
self.set_data()
def enconde_seq(self, seq):
return [0 if x=='A' or x=='T' else 1 for x in seq]
def encode_sequences(self, seqs):
from numpy import vstack
return vstack([self.enconde_seq(seq) for seq in seqs])
# ================================================================
# SequenceNucHotvector ===========================================
# ================================================================
class SequenceNucHotvector(BaseData):
NUC_HOT_VECTOR = {
'A' : np.array([[1.], [0.], [0.], [0.]]),
'C' : np.array([[0.], [1.], [0.], [0.]]),
'G' : np.array([[0.], [0.], [1.], [0.]]),
'T' : np.array([[0.], [0.], [0.], [1.]])
}
def __init__(self, npath, ppath):
super(SequenceNucHotvector, self).__init__(npath, ppath)
self.set_data()
def enconde_seq(self, seq):
convHot = lambda x : self.NUC_HOT_VECTOR[nuc]
return np.hstack([ convHot(nuc) for nuc in seq ]).reshape(1, 4, len(seq), 1)
def encode_sequences(self, seqs):
return np.vstack([self.enconde_seq(seq) for seq in seqs])
# ================================================================
# SequenceMotifHot ===============================================
# ================================================================
class SequenceMotifHot(BaseData):
NUC_HOT_VECTOR = {
'A' : np.array([[1.], [0.], [0.], [0.]]),
'C' : np.array([[0.], [1.], [0.], [0.]]),
'G' : np.array([[0.], [0.], [1.], [0.]]),
'T' : np.array([[0.], [0.], [0.], [1.]])
}
def __init__(self, npath, ppath):
super(SequenceMotifHot, self).__init__(npath, ppath)
self.set_motifs(ppath)
self.set_data()
def enconde_seq(self, seq):
convHot = lambda x, i : self.mot[x][i] * self.mot[x][i] * 100 * self.NUC_HOT_VECTOR[x]
return np.hstack([ convHot(seq[i], i) for i in range(len(seq)) ]).reshape(1, 4, len(seq), 1)
def encode_sequences(self, seqs):
return np.vstack([self.enconde_seq(seq) for seq in seqs])
def set_motifs(self, ppath):
from Bio import motifs
seqs = self.get_sequences_from_fasta(ppath)
mot = motifs.create(seqs)
self.mot = {'A':mot.pwm.log_odds()['A'], 'C':mot.pwm.log_odds()['C'], 'G':mot.pwm.log_odds()['G'], 'T':mot.pwm.log_odds()['T']}
# print mot.counts.normalize()
# mot.weblogo('weblogo.png')
# ===============================================================
# ================================================================
# SequenceDinucLabelsProperties ==================================
# ================================================================
class SequenceDinucLabelsProperties(BaseData):
def __init__(self, npath, ppath):
super(SequenceDinucLabelsProperties, self).__init__(npath, ppath)
self.convtable2 = self.value2label_dinuc_converter()
self.k = 2
self.set_kmers_encoder()
self.set_data()
def encode_seq(self, seq):
import numpy as np
convProp = lambda x, prop : np.array([ self.convtable2[prop, x[i]] for i in range(len(x)) ])
return np.hstack([ convProp(seq, i) for i in range(38) ])
def encode_sequences(self, seqs):
from numpy import vstack, array
mat = vstack([self.label_encoder.transform(self.get_kmers(seq, k=self.k)) for seq in seqs])
return array([ self.encode_seq(seq) for seq in mat ])
# ================================================================
# SequenceDinucProperties ========================================
# ================================================================
class SequenceDinucProperties(BaseData):
def __init__(self, npath, ppath):
from Bio import motifs
from sklearn.preprocessing import MinMaxScaler
import numpy as np
super(SequenceDinucProperties, self).__init__(npath, ppath)
self.k = 2
self.set_motifs(ppath)
self.set_kmers_encoder()
# Setup tables for convert nucleotides to 2d properties matrix - multichannel signals
self.convtable2 = np.loadtxt('dinuc_values', delimiter=',', dtype=np.float32)
scaler = MinMaxScaler(feature_range=(0, 1))
# MIN-MAX SCALER
for prop in range(len(self.convtable2)):
data = np.vstack([ [x] for x in self.convtable2[prop, :] ])
scaler.fit(data)
self.convtable2[prop, :] = scaler.transform(data).transpose()[0]
#print self.convtable2[prop, :]
self.set_data()
# seqs = self.get_sequences_from_fasta(self.ppath)
# instances = motifs.create(seqs)
# instances.weblogo('test.png')
def set_motifs(self, ppath):
from Bio import motifs
seqs = self.get_sequences_from_fasta(ppath)
mot = motifs.create(seqs)
self.mot = {'A':mot.pwm['A'], 'C':mot.pwm['C'], 'G':mot.pwm['G'], 'T':mot.pwm['T']}
def encode_seq(self, seq):
import numpy as np
convProp = lambda x, prop : np.array([ self.convtable2[prop, x[i]] for i in range(len(x)) ])
return np.vstack([ convProp(seq, i) for i in range(38) ]).reshape(38,len(seq),1)
# return convertedseq.reshape(1, 38, len(seq))
def encode_sequences(self, seqs):
from numpy import vstack, array
mat = vstack([self.label_encoder.transform(self.get_kmers(seq, k=self.k)) for seq in seqs])
return array([ self.encode_seq(seq) for seq in mat ])
# ================================================================
# SimpleHistData =================================================
# ================================================================
class SimpleHistData(BaseData):
def __init__(self, npath, ppath, k=1, upto=False):
super(SimpleHistData, self).__init__(npath, ppath)
self.k = k
self.upto = upto
self.set_data()
def encode_sequences(self, seqs):
from repDNA.nac import Kmer
from numpy import vstack
kmer = Kmer(k=self.k, upto=self.upto, normalize=True)
return vstack(kmer.make_kmer_vec(seqs))
# ================================================================
# DinucAutoCovarData =============================================
# ================================================================
class DinucAutoCovarData(BaseData):
def __init__(self, npath, ppath, k=1, upto=False):
super(DinucAutoCovarData, self).__init__(npath, ppath)
self.k = k
self.upto = upto
self.set_data()
def encode_sequences(self, seqs):
from repDNA.ac import DAC
from numpy import vstack
dac = DAC(self.k)
return vstack(dac.make_dac_vec(seqs, all_property=True))
# ================================================================
# DinucCrossCovarData ============================================
# ================================================================
class DinucCrossCovarData(BaseData):
def __init__(self, npath, ppath, k=1, upto=False):
super(DinucCrossCovarData, self).__init__(npath, ppath)
self.k = k
self.upto = upto
self.set_data()
def encode_sequences(self, seqs):
from repDNA.ac import DCC
from numpy import vstack
dcc = DCC(self.k)
return vstack(dcc.make_dcc_vec(seqs, all_property=True))
# ================================================================
# ======================= TEST ===================================
# ================================================================
TEST = 0
if TEST==1:
#from pandas.tools.plotting import andrews_curves
from matplotlib.pyplot import *
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')
import pandas as pd
import matplotlib.pyplot as plt
# npath = "fasta/Bacillus_non_prom.fa"
# ppath = "fasta/Bacillus_prom.fa"
npath = "fasta/Ecoli_non_prom.fa"
ppath = "fasta/Ecoli_prom.fa"
#mldata = SequenceMotifHot(npath, ppath)
#print mldata.getX()
data = SequenceDinucProperties(npath, ppath)
X = data.getX()
Y = data.getY()
fsize=(18,12)
#for i in range(38):
# D = pd.DataFrame( data=X[:,i,:,0].reshape( X.shape[0], X.shape[2]) )
# D = D.iloc[:, 45:66]
# D[21] = Y
# print i
# pd.tools.plotting.parallel_coordinates(D, 21)
# pd.tools.plotting.andrews_curves(D, 21)
# plt.show()
i=1
D = pd.DataFrame( data=X[:,i,:,0].reshape( X.shape[0], X.shape[2]) )
F = D
F[F.shape[1]+1] = Y
#plt.figure(figsize=(20,10))
#pd.plotting.parallel_coordinates(F, F.shape[1], color=['#FD1999', '#00E6FE'] )
#plt.savefig('parallel'+'.pdf')
#plt.show()
# seg = range(25,66)
seg = range(25,70)
D = D.iloc[:, seg]
D[D.shape[1]+1] = Y
neg = D[D[D.shape[1]] == 0]
pos = D[D[D.shape[1]] == 1]
#plt.figure(figsize=fsize)
#pd.plotting.parallel_coordinates(pos, pos.shape[1], color='#00E6FE' )
#pd.plotting.parallel_coordinates(neg, neg.shape[1], color='#FD1999' )
#plt.show()
plt.figure(figsize=fsize)
pd.plotting.parallel_coordinates(neg, neg.shape[1], color='#FD1999' )
pd.plotting.parallel_coordinates(pos, pos.shape[1], color='#00E6FE' )
plt.show()
#plt.figure(figsize=(20,10))
#pd.tools.plotting.parallel_coordinates(D, D.shape[1])
#plt.savefig('parallel'+'.pdf')
#plt.figure(figsize=(20,10))
#pd.tools.plotting.andrews_curves(D, D.shape[1], colorbar)
#plt.savefig('andrews'+'.pdf')
plt.show()
elif TEST==2:
npath = "fasta/Bacillus_non_prom.fa"
ppath = "fasta/Bacillus_prom.fa"
data = SequenceDinucLabelsProperties(npath, ppath)
print data.getX()[0, -1, :, 0]
elif TEST==3:
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import RFE
from sklearn.feature_selection import RFECV
from sklearn.model_selection import StratifiedKFold
import pandas as pd
import matplotlib.pyplot as plt
npath = "fasta/Ecoli_non_prom.fa"
ppath = "fasta/Ecoli_prom.fa"
data = SequenceDinucProperties(npath, ppath)
X = data.getX()
Y = data.getY()
print 'X', X.shape
X = X.reshape(len(X), -1)
print 'X', X.shape
# Create the RFE object and rank each pixel
# svc = SVC(kernel="linear", C=1)
tree = RandomForestClassifier(n_jobs=-1, max_features='sqrt')
rfecv = RFECV(estimator=tree, step=0.1, cv=StratifiedKFold(3), scoring='accuracy')
rfecv.fit(X, Y)
ranking = rfecv.ranking_.reshape(38, 79)
print("Optimal number of features : %d" % rfecv.n_features_)
# Plot number of features VS. cross-validation scores
plt.figure()
plt.xlabel("Number of features selected")
plt.ylabel("Cross validation score (nb of correct classifications)")
plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
plt.show()
# Plot pixel ranking
plt.figure()
plt.matshow(ranking, cmap=plt.cm.Blues)
plt.colorbar()
plt.title("Ranking of pixels with RFE")
plt.show()
print ranking
elif TEST==4:
npath = "fasta/Bacillus_non_prom.fa"
ppath = "fasta/Bacillus_prom.fa"
data = SequenceNucsData(npath, ppath, k=3)
print data.getX()