-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcircDeep.py
1346 lines (970 loc) · 42.2 KB
/
circDeep.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import keras
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Dropout, Merge, Input
from keras.callbacks import ModelCheckpoint, EarlyStopping
from sklearn import metrics
from keras import optimizers
from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence
from keras.layers import Input, Embedding, LSTM, Convolution1D
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from keras.layers import MaxPooling1D, AveragePooling1D, Bidirectional
from keras.layers.advanced_activations import PReLU
from keras.layers.normalization import BatchNormalization
from sklearn.externals import joblib
import gensim, logging
import multiprocessing
import random
from keras.utils import np_utils, generic_utils
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers import merge, Dropout, Flatten, Dense, Permute
from keras.models import Model, Sequential
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
import os
import pysam
from collections import defaultdict
import os
import argparse
import timeit
import re
import pyBigWig
import tempfile
import sys
import hashlib
from multiprocessing import Process
import keras
from sklearn import metrics
from gensim.models.word2vec import LineSentence
import gensim, logging
import pickle
import numpy as np
import keras
from keras.layers import Dense, LSTM, Dropout, Bidirectional
import gensim, logging
from keras.models import load_model
from keras.layers import Concatenate
def suffle_text(file_input, file_output):
f = open(file_input)
oo = open(file_output, 'w')
entire_file = f.read()
file_list = entire_file.split('\n')
num_lines = len(file_list)
random_nums = random.sample(xrange(num_lines), num_lines)
for i in random_nums:
oo.write(file_list[i] + "\n")
oo.close()
f.close()
def seq2ngram(seqs, k, s, dest, wv):
f = open(seqs)
lines = f.readlines()
f.close()
list22 = []
print('need to n-gram %d lines' % len(lines))
f = open(dest, 'w')
for num, line in enumerate(lines):
if num < 200000:
line = line[:-1].lower() # remove '\n' and lower ACGT
l = len(line) # length of line
list2 = []
for i in range(0, l, s):
if i + k >= l + 1:
break
list2.append(line[i:i + k])
f.write(''.join(line[i:i + k]))
f.write(' ')
f.write('\n')
list22.append(convert_data_to_index(list2, wv))
f.close()
return list22
def convert_sequences_to_index(list_of_seqiences, wv):
ll = []
for i in range(len(list_of_seqiences)):
ll.append(convert_data_to_index(list_of_seqiences[i], wv))
return ll
def convert_data_to_index(string_data, wv):
index_data = []
for word in string_data:
if word in wv:
index_data.append(wv.vocab[word].index)
return index_data
def seq2ngram2(seqs, k, s, dest):
f = open(seqs)
lines = f.readlines()
f.close()
print('need to n-gram %d lines' % len(lines))
f = open(dest, 'w')
for num, line in enumerate(lines):
if num < 100000:
line = line[:-1].lower() # remove '\n' and lower ACGT
l = len(line) # length of line
for i in range(0, l, s):
if i + k >= l + 1:
break
f.write(''.join(line[i:i + k]))
f.write(' ')
f.write('\n')
f.close()
def word2vect(k, s, vector_dim, root_path, pos_sequences):
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
seq2ngram2(pos_sequences, k, s, 'seq_pos_' + str(k) + '_' + str(s) + '.txt')
sentences = LineSentence('seq_pos_' + str(k) + '_' + str(s) + '.txt')
mode1 = gensim.models.Word2Vec(sentences, iter=20, window=int(18 / s), min_count=50, size=vector_dim,
workers=multiprocessing.cpu_count())
mode1.save(root_path + 'word2vec_model' + '_' + str(k) + '_' + str(s) + '_' + str(vector_dim))
def build_class_file(np, ng, class_file):
with open(class_file, 'w') as outfile:
outfile.write('label' + '\n')
for i in range(np):
outfile.write('1' + '\n')
for i in range(ng):
outfile.write('0' + '\n')
def build_ACNN_BLSTM_model(k, s, vector_dim, root_path, MAX_LEN, pos_sequences, neg_sequences, seq_file, class_file,model_dir):
model1 = gensim.models.Word2Vec.load(
model_dir + 'word2vec_model' + '_' + str(k) + '_' + str(s) + '_' + str(vector_dim))
pos_list = seq2ngram(pos_sequences, k, s, 'seq_pos_' + str(k) + '_' + str(s) + '.txt', model1.wv)
with open(str(k) + '_' + str(s) + 'listpos.pkl', 'wb') as pickle_file:
pickle.dump(pos_list, pickle_file, protocol=pickle.HIGHEST_PROTOCOL)
# with open(str(k) + '_' + str(s) + 'listpos.pkl', 'rb') as f:
# pos_list = pickle.load(f)
# pos_list = pos_list[:250]
# print(str(len(pos_list)))
neg_list = seq2ngram(neg_sequences, k, s, 'seq_neg_' + str(k) + '_' + str(s) + '.txt', model1.wv)
with open(str(k) + '_' + str(s) + 'listneg.pkl', 'wb') as pickle_file:
pickle.dump(neg_list, pickle_file, protocol=pickle.HIGHEST_PROTOCOL)
# with open(str(k) + '_' + str(s) + 'listneg.pkl', 'rb') as f1:
# neg_list = pickle.load(f1)
# neg_list = neg_list[:200]
# print (str(len(neg_list)))
seqs = pos_list + neg_list
X = pad_sequences(seqs, maxlen=MAX_LEN)
y = np.array([1] * len(pos_list) + [0] * len(neg_list))
build_class_file(len(pos_list), len(neg_list), class_file)
X1 = X
n_seqs = len(seqs)
indices = np.arange(n_seqs)
np.random.shuffle(indices)
X = X[indices]
y = y[indices]
n_tr = int(n_seqs * 0.8)
X_train = X[:n_tr]
y_train = y[:n_tr]
X_valid = X[n_tr:]
y_valid = y[n_tr:]
embedding_matrix = np.zeros((len(model1.wv.vocab), vector_dim))
for i in range(len(model1.wv.vocab)):
embedding_vector = model1.wv[model1.wv.index2word[i]]
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
model = Sequential()
model.add(Embedding(input_dim=embedding_matrix.shape[0],
output_dim=embedding_matrix.shape[1],
weights=[embedding_matrix],
input_length=MAX_LEN,
trainable=True))
model.add(Dropout(0.1))
# model.add(Convolution1D(nb_filter = 100,filter_length=1,activation='relu',border_mode = 'valid'))
model.add(Convolution1D(nb_filter=100,
filter_length=7,
activation='relu',
border_mode='valid'))
model.add(MaxPooling1D(4, 4))
model.add(Dropout(0.1))
# model.add(Convolution1D(nb_filter = 80,filter_length=1,activation='relu',border_mode = 'valid'))
model.add(Convolution1D(100, 1, activation='relu'))
model.add(MaxPooling1D(2, 2))
model.add(Dropout(0.1))
model.add(Bidirectional(LSTM(100, consume_less='gpu')))
model.add(Dropout(0.1))
model.add(Dense(80, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(20, activation='relu', name='myfeatures'))
model.add(Dropout(0.1))
model.add(Dense(1, activation='sigmoid'))
sgd = optimizers.SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy'])
print(model.summary())
# model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
# print(model.summary())
checkpointer = ModelCheckpoint(
filepath=model_dir+'bestmodel_ACNN_BLSTM_' + str(k) + ' ' + str(s) + ' ' + str(vector_dim) + str(MAX_LEN) + '.hdf5',
verbose=1,
save_best_only=True)
earlystopper = EarlyStopping(monitor='val_loss', patience=6, verbose=1)
print('Training model...')
history = model.fit(X_train, y_train, nb_epoch=2, batch_size=128, shuffle=True,
validation_data=(X_valid, y_valid),
callbacks=[checkpointer, earlystopper],
verbose=1)
# print(history.history.keys())
# summarize history for accuracy
# plt.figure()
# plt.plot(history.history['acc'])
# plt.plot(history.history['val_acc'])
# plt.title('model accuracy')
# plt.ylabel('accuracy')
# plt.xlabel('epoch')
# plt.legend(['train', 'test'], loc='upper left')
# plt.savefig('C:/Users/mohamed/Documents/text_mining/finaldata/myaccuracy-drop='+str(int(ls*10))+'s='+str(s)+'vectrdim='+str(vector_dim))
# summarize history for loss
# plt.figure()
# plt.plot(history.history['loss'])
# plt.plot(history.history['val_loss'])
# plt.title('model loss')
# plt.ylabel('loss')
# plt.xlabel('epoch')
# plt.legend(['train', 'test'], loc='upper left')
# plt.savefig('C:/Users/mohamed/Documents/text_mining/finaldata/myloss-drop='+str(int(ls*10))+'s='+str(s)+'vectrdim='+str(vector_dim))
# tresults = model.evaluate(X_test, y_test)
# print (tresults)
# y_pred = model.predict(X_test, batch_size=32, verbose=1)
# y = y_test
# print ('Calculating AUC...')
# auroc = metrics.roc_auc_score(y, y_pred)
# auprc = metrics.average_precision_score(y, y_pred)
# print (auroc, auprc)
intermediate_layer_model = Model(inputs=model.input,
outputs=model.get_layer('myfeatures').output)
np.savetxt(seq_file, intermediate_layer_model.predict(X1), delimiter=" ")
def extract_ACNN_BLSTM(k, s, vector_dim, root_path, MAX_LEN, testing_sequences, seq_file,model_dir):
model1 = gensim.models.Word2Vec.load(
model_dir + 'word2vec_model' + '_' + str(k) + '_' + str(s) + '_' + str(vector_dim))
seqs = seq2ngram(testing_sequences, k, s, 'seq_' + str(k) + '_' + str(s) + '.txt', model1.wv)
X = pad_sequences(seqs, maxlen=MAX_LEN)
model = load_model(model_dir+'bestmodel_ACNN_BLSTM_' + str(k) + ' ' + str(s) + ' ' + str(vector_dim) + str(MAX_LEN) + '.hdf5')
intermediate_layer_model = Model(inputs=model.input,
outputs=model.get_layer('myfeatures').output)
np.savetxt(seq_file, intermediate_layer_model.predict(X), delimiter=" ")
def bigwig_score_list(bw, chr, start, end):
score = []
kk = bw.intervals(chr, start, end)
if kk != None:
for t in bw.intervals(chr, start, end):
score.append(t[2])
if len(score) == 0:
for i in range(start, end):
score.append(0)
return score
def bigwig_mean(bw, chr, start, end):
score_sum = 0
mean_score = 0
kk = bw.intervals(chr, start, end)
if kk != None:
for t in bw.intervals(chr, start, end):
score_sum += t[2]
else:
print('yes')
if (end - start) != 0:
mean_score = score_sum / (end - start)
else:
mean_score = 0
return mean_score
def extract_exons(gtf_file):
gtf = open(gtf_file, 'r');
exons = defaultdict(list)
for line in gtf: ## process each line
if line[0] != '#':
ele = line.strip().split('\t');
if len(ele) > 7:
if ele[2] == 'exon':
chr = (ele[0])
strand = (ele[6])
start = int(ele[3])
end = int(ele[4])
exons[chr + strand].append([start, end])
return exons
def get_processed_conservation_score(score_whole_seq, thres):
ls = len(score_whole_seq)
score_array = np.array(score_whole_seq)
con_arr = (score_array >= thres)
con_str = ''
for val in con_arr:
if val:
con_str = con_str + '1'
else:
con_str = con_str + '0'
sat8_len = con_str.count('11111111')
sat7_len = con_str.count('1111111')
sat_6len = con_str.count('111111')
sat5_len = con_str.count('11111')
return float(sat5_len) * 1000 / ls, float(sat_6len) * 1000 / ls, float(sat7_len) * 1000 / ls, float(
sat8_len) * 1000 / ls
def point_overlap(min1, max1, min2, max2):
return max(0, min(max1, max2) - max(min1, min2))
def extract_feature_conservation_CCF(fasta_file, bigwig, gtf_file, out):
fp = open(fasta_file, 'r')
bw = pyBigWig.open(bigwig)
exons = extract_exons(gtf_file)
fw = open(out, 'w')
ii = 0
for line in fp:
ii = ii + 1
ele = line.strip().split(' ')
chr_name = ele[0]
start = int(ele[1])
end = int(ele[2]) - 1
strand = ele[3]
list_all_exons = exons[chr_name + strand]
list_exons = []
score = []
tt = True
for i in range(len(list_all_exons)):
start_exon = list_all_exons[i][0]
end_exon = list_all_exons[i][1]
if point_overlap(start_exon, end_exon, start, end):
for i in range(len(list_exons)):
if list_exons[i][0] == start_exon and list_exons[i][1] == end_exon:
tt = False
if tt:
list_exons.append((start_exon, end_exon))
b = []
for begin, end in sorted(list_exons):
if b and b[-1][1] >= begin - 1:
b[-1][1] = max(b[-1][1], end)
else:
b.append([begin, end])
list_exons = b
if len(list_exons) == 0:
list_exons.append([start, end])
for i in range(len(list_exons)):
score.append(bigwig_mean(bw, chr_name, list_exons[i][0], list_exons[i][1]))
score_array = np.array(score)
mean_score = score_array.mean()
max_score = score_array.max()
median_score = np.median(score_array)
fw.write(str(mean_score) + ' ' + str(max_score) + ' ' + str(
median_score))
score_whole_seq = bigwig_score_list(bw, chr_name, start, end)
l5, l6, l7, l8 = get_processed_conservation_score(score_whole_seq, 0.5)
fw.write(' ' + str(l5) + ' ' + str(l6) + ' ' + str(l7) + ' ' + str(l8))
l5, l6, l7, l8 = get_processed_conservation_score(score_whole_seq, 0.6)
fw.write(' ' + str(l5) + ' ' + str(l6) + ' ' + str(l7) + ' ' + str(l8))
l5, l6, l7, l8 = get_processed_conservation_score(score_whole_seq, 0.7)
fw.write(' ' + str(l5) + ' ' + str(l6) + ' ' + str(l7) + ' ' + str(l8))
l5, l6, l7, l8 = get_processed_conservation_score(score_whole_seq, 0.8)
fw.write(' ' + str(l5) + ' ' + str(l6) + ' ' + str(l7) + ' ' + str(l8))
l5, l6, l7, l8 = get_processed_conservation_score(score_whole_seq, 0.9)
fw.write(' ' + str(l5) + ' ' + str(l6) + ' ' + str(l7) + ' ' + str(l8))
fw.write('\n')
fp.close()
bw.close()
def complement(seq):
complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A', 'N': 'N'}
complseq = [complement[base] for base in seq]
return complseq
def reverse_complement(seq):
seq = list(seq)
seq.reverse()
return ''.join(complement(seq))
def bed_to_fasta(bed_file, fasta_file, sequence_file, genome_fasta):
bed = open(bed_file, 'r');
output = open(fasta_file, 'w');
output2 = open(sequence_file, 'w');
genome_fa = pysam.FastaFile(genome_fasta)
for line in bed:
values = line.split()
chr_n = values[0]
start = int(values[1])
end = int(values[2])
strand = values[3]
seq = genome_fa.fetch(chr_n, start, end)
seq = seq.upper()
if strand == '-':
seq = reverse_complement(seq)
output.write('>' + ':'.join([chr_n, str(start), str(end), strand]) + '\n')
output.write(seq + '\n')
output2.write(seq + '\n')
def extract_rcm(fasta_file, genome, out, kk, jj):
genome_fa = pysam.FastaFile(genome)
fp = open(fasta_file, 'r')
fw = open(out, 'w')
indices, number_kmers = get_dictionary_kmers(jj)
numline = 0
for line in fp:
ele = line.strip().split(':')
if line[0] == '>':
numline = numline + 1
if numline % 10 == 0:
print('%d lines made' % numline)
chr_name = ele[0][1:]
start = int(ele[1])
end = int(ele[2]) - 1
strand = ele[3]
scores = get_rcm_score(chr_name, strand, start, end, genome_fa, kk, jj, indices, number_kmers)
for jjj in range(len(scores)):
fw.write(str(scores[jjj]) + ' ')
fw.write('\n')
def extract_rcm2(fasta_file, genome, out, kk, jj):
genome_fa = pysam.FastaFile(genome)
fp = open(fasta_file, 'r')
fw = open(out, 'w')
indices, number_kmers = get_dictionary_kmers(jj)
numline = 0
for line in fp:
ele = line.strip().split(':')
if line[0] == '>':
numline = numline + 1
if numline % 10 == 0:
print('%d lines made' % numline)
chr_name = ele[0][1:]
start = int(ele[1])
end = int(ele[2]) - 1
strand = ele[3]
scores = get_rcm_score2(chr_name, strand, start, end, genome_fa, kk, jj, indices, number_kmers)
for jjj in range(len(scores)):
fw.write(str(scores[jjj]) + ' ')
fw.write('\n')
def match_score(alpha, beta):
match_award = 12
mismatch_penalty = -2
if alpha == beta:
return match_award
else:
return mismatch_penalty
def zeros(shape):
retval = []
for x in range(shape[0]):
retval.append([])
for y in range(shape[1]):
retval[-1].append(0)
return retval
def water(seq1, seq2):
m, n = len(seq1) - 1, len(seq2) - 1 # length of two sequences
# Generate DP table and traceback path pointer matrix
score = zeros((m + 1, n + 1)) # the DP table
pointer = zeros((m + 1, n + 1)) # to store the traceback path
max_score_500 = 0
max_score_750 = 0
max_score_1000 = 0
max_score_1250 = 0
# initial maximum score in DP table
# Calculate DP table and mark pointers
gap_penalty = -12
for i in range(1, min(m + 1, 1250)):
for j in range(1, min(n + 1, 1250)):
score_diagonal = score[i - 1][j - 1] + match_score(seq1[i], seq2[j]);
score[i][j] = max(0, score_diagonal)
if score[i][j] >= max_score_500 and i <= 500 and j <= 500:
max_score_500 = score[i][j];
if score[i][j] >= max_score_750 and i <= 750 and j <= 750:
max_score_750 = score[i][j];
if score[i][j] >= max_score_1000 and i <= 1000 and j <= 1000:
max_score_1000 = score[i][j];
if score[i][j] >= max_score_1250 and i <= 1250 and j <= 1250:
max_score_1250 = score[i][j];
return [max_score_500, max_score_750, max_score_1000, max_score_1250]
def get_rcm_score(chrr, strand, start, end, genome_fa, wsize, motifsize, indices, number_kmers):
fi_seq = genome_fa.fetch(chrr, start - wsize, start).upper()
si_seq = genome_fa.fetch(chrr, end, end + wsize).upper()
if strand == '-':
fi_seq = reverse_complement(fi_seq)
si_seq = reverse_complement(si_seq)
fi_seq_ind = sequence_to_indices(fi_seq, indices, motifsize)
fsi_seq_ind = sequence_to_indices_rc(si_seq, indices, motifsize)
results = water(fi_seq_ind, fsi_seq_ind)
return results
def get_rcm_score2(chrr, strand, start, end, genome_fa, wsize, motifsize, indices, number_kmers):
fi_seq = genome_fa.fetch(chrr, start - wsize, start).upper()
si_seq = genome_fa.fetch(chrr, end, end + wsize).upper()
if strand == '-':
fi_seq = reverse_complement(fi_seq)
si_seq = reverse_complement(si_seq)
fi_seq_ind = sequence_to_indices(fi_seq, indices, motifsize)
fsi_seq_ind = sequence_to_indices_rc(si_seq, indices, motifsize)
results2 = []
for size in [500, 750, 1000, 1250, 1500, 1750, 1990]:
results2.append(absolute_number_rcm(fi_seq_ind[:size], fsi_seq_ind[2000 - size:], number_kmers))
# ss=extract_absolute_number_rcm(fi_seq_ind,fsi_seq_ind):
return results2
def absolute_number_rcm(fi_seq_ind, fsi_seq_ind, end):
sum = 0
for i in range(end):
sum = sum + min(fi_seq_ind.count(i), fsi_seq_ind.count(i))
return sum
def sequence_to_indices(sequence, indices, k):
seq_list = []
for i in range(len(sequence) - k):
seq_list.append(indices[sequence[i:i + k]])
return seq_list
def sequence_to_indices_rc(sequence, indices, k):
seq_list = []
for i in range(len(sequence) - k):
seq_list.append(indices[reverse_complement(sequence[i:i + k])])
return seq_list
def get_dictionary_kmers(k):
indices = defaultdict(int)
chars = ['A', 'C', 'G', 'T']
base = len(chars)
end = len(chars) ** k
for i in range(0, end):
seq = ''
n = i
for j in range(k):
seq = seq + chars[n % base]
n = int(n / base)
indices[seq] = i
return indices, end
def extract_rcm_features(pos_data_fasta, neg_data_fasta, genome, rcm_file, data_dir):
if not os.path.exists(data_dir + 'rcm/'):
os.makedirs(data_dir + 'rcm/')
proc = []
for k in [1, 2, 3]:
out = data_dir + 'rcm/pos_rcm1_' + str(k)
print(str(k))
p = Process(target=extract_rcm, args=(pos_data_fasta, genome, out, 1250, k))
proc.append(p)
# extract_rcm(pos_data_fasta, genome, out, 1000, 3)
for p in proc:
p.start()
proc2 = []
for k in [1, 2, 3]:
out = data_dir + 'rcm/neg_rcm1_' + str(k)
print(str(k))
p2 = Process(target=extract_rcm, args=(neg_data_fasta, genome, out, 1250, k))
proc2.append(p2)
# extract_rcm(pos_data_fasta, genome, out, 1000, k)
for p2 in proc2:
p2.start()
proc3 = []
for k in [3, 4, 5, 6]:
out = data_dir + 'rcm/pos_rcm2_' + str(k)
print(str(k))
p3 = Process(target=extract_rcm2, args=(pos_data_fasta, genome, out, 2000, k))
proc3.append(p3)
# extract_rcm(pos_data_fasta, genome, out, 1000, 3)
for p3 in proc3:
p3.start()
proc4 = []
for k in [3, 4, 5, 6]:
out = data_dir + 'rcm/neg_rcm2_' + str(k)
print(str(k))
p4 = Process(target=extract_rcm2, args=(neg_data_fasta, genome, out, 2000, k))
proc4.append(p4)
# extract_rcm(pos_data_fasta, genome, out, 1000, k)
for p4 in proc4:
p4.start()
for p in proc:
p.join()
for p2 in proc2:
p2.join()
for p3 in proc3:
p3.join()
for p4 in proc4:
p4.join()
concatenate_rcm_files(rcm_file, data_dir)
def concatenate_rcm_files(rcm_file, data_dir):
file_dir = data_dir + 'rcm/'
root_range = [1, 2]
K_range = {1: [1, 2, 3],
2: [3, 4, 5, 6]}
# dict to store the data as the files a read out
lines = {'pos': list(), 'neg': list(), 'header': ""}
def append_data(filename, column_prefix, sign):
"""
take each line from file, and append that line to
respective string in lines[sign]
"""
with open(os.path.join(file_dir, filename)) as f:
# initialize the lines list if first time
new_lines = f.readlines()
if len(lines[sign]) == 0:
lines[sign] = ["" for _ in range(len(new_lines))]
# append every line to lines
last_line = str()
for i, (line, new_data) in enumerate(zip(lines[sign], new_lines)):
lines[sign][i] += new_data.strip() + ' '
last_line = new_data.strip()
# append headers
if sign == 'pos':
for i in range(len(last_line.split(' '))):
lines['header'] += column_prefix + '_' + str(i + 1) + ' '
# interate through all file names and call append_data on each
for root in root_range:
for K in K_range[root]:
for sign in ['pos', 'neg']:
column_prefix = str(root) + '_' + str(K)
filename = sign + '_rcm' + column_prefix
print(filename)
append_data(filename, column_prefix, sign)
# write to outfile.txt in pwd
with open(rcm_file, 'w') as outfile:
outfile.write(lines['header'] + '\n')
for line in lines['pos']:
outfile.write(line + '\n')
for line in lines['neg']:
outfile.write(line + '\n')
def extract_rcm_features_testing(data_fasta, genome, rcm_file, data_dir):
if not os.path.exists(data_dir + 'rcm/'):
os.makedirs(data_dir + 'rcm/')
proc = []
for k in [1, 2, 3]:
out = data_dir + 'rcm/rcm1_' + str(k)
p = Process(target=extract_rcm, args=(data_fasta, genome, out, 1250, k))
proc.append(p)
# extract_rcm(pos_data_fasta, genome, out, 1000, 3)
for p in proc:
p.start()
proc3 = []
for k in [3, 4, 5, 6]:
out = data_dir + 'rcm/rcm2_' + str(k)
print(str(k))
p3 = Process(target=extract_rcm2, args=(data_fasta, genome, out, 2000, k))
proc3.append(p3)
# extract_rcm(pos_data_fasta, genome, out, 1000, 3)
for p3 in proc3:
p3.start()
for p in proc:
p.join()
for p3 in proc3:
p3.join()
print('start concatenate')
concatenate_rcm_files_testing(rcm_file, data_dir)
def concatenate_rcm_files_testing(rcm_file, data_dir):
file_dir = data_dir + 'rcm/'
root_range = [1, 2]
K_range = {1: [1, 2, 3],
2: [3, 4, 5, 6]}
# dict to store the data as the files a read out
lines = {'data': list(), 'header': ""}
def append_data(filename, column_prefix, sign='data'):
"""
take each line from file, and append that line to
respective string in lines[sign]
"""
with open(os.path.join(file_dir, filename)) as f:
# initialize the lines list if first time
new_lines = f.readlines()
if len(lines[sign]) == 0:
lines[sign] = ["" for _ in range(len(new_lines))]
# append every line to lines
last_line = str()
for i, (line, new_data) in enumerate(zip(lines[sign], new_lines)):
lines[sign][i] += new_data.strip() + ' '
last_line = new_data.strip()
# append headers
if sign == 'data':
for i in range(len(last_line.split(' '))):
lines['header'] += column_prefix + '_' + str(i + 1) + ' '
# interate through all file names and call append_data on each
for root in root_range:
for K in K_range[root]:
column_prefix = str(root) + '_' + str(K)
filename = 'rcm' + column_prefix
print(filename)
append_data(filename, column_prefix, 'data')
# write to outfile.txt in pwd
with open(rcm_file, 'w') as outfile:
outfile.write(lines['header'] + '\n')
for line in lines['data']:
outfile.write(line + '\n')
def concatenate_cons_files(pos_conservation_feature_file, neg_conservation_feature_file, cons_file):
filenames = [pos_conservation_feature_file, neg_conservation_feature_file]
with open(cons_file, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
def extract_features_testing(testing_bed, genome, bigwig, gtf, data_dir,model_dir):
cons_file = data_dir + 'conservation_features_test.txt'
rcm_file = data_dir + 'rcm_features_test.txt'
seq_file = data_dir + 'seq_features_test.txt'
testing_fasta = testing_bed + '.fasta'
testing_sequences = testing_bed + '.seq.txt'
bed_to_fasta(testing_bed,testing_fasta ,testing_sequences, genome)
extract_feature_conservation_CCF(testing_bed, bigwig, gtf, cons_file)
extract_rcm_features_testing(testing_fasta, genome,rcm_file,data_dir)
extract_ACNN_BLSTM(3, 1, 40, data_dir, 8000, testing_sequences, seq_file,model_dir)
return testing_fasta, testing_sequences, cons_file, rcm_file, seq_file
def extract_features_training(pos_data_bed, neg_data_bed, genome, bigwig, gtf, data_dir,model_dir):
cons_file = data_dir + 'conservation_features.txt'
rcm_file = data_dir + 'rcm_features.txt'
seq_file = data_dir + 'seq_features.txt'
class_file = data_dir + 'class.txt'
pos_data_fasta = pos_data_bed + '.fasta'
pos_sequences = pos_data_bed + '.seq.txt'
neg_data_fasta = neg_data_bed + '.fasta'
neg_sequences = neg_data_bed + '.seq.txt'
pos_conservation_feature_file = pos_data_bed + '.cons'
neg_conservation_feature_file = neg_data_bed + '.cons'
bed_to_fasta(pos_data_bed,pos_data_fasta ,pos_sequences, genome)
bed_to_fasta(neg_data_bed, neg_data_fasta,neg_sequences, genome)
extract_feature_conservation_CCF(neg_data_bed, bigwig, gtf, neg_conservation_feature_file)
extract_feature_conservation_CCF(pos_data_bed, bigwig,gtf,pos_conservation_feature_file)
concatenate_cons_files(pos_conservation_feature_file,neg_conservation_feature_file,cons_file)
extract_rcm_features(pos_data_fasta, neg_data_fasta, genome,rcm_file,data_dir)
word2vect(3, 1, 40, model_dir, pos_sequences)
print('11111')
build_ACNN_BLSTM_model(3, 1, 40, data_dir, 8000, pos_sequences, neg_sequences, seq_file, class_file,model_dir)
print('22222')
return pos_data_fasta, pos_sequences, neg_data_fasta, neg_sequences, cons_file, rcm_file, seq_file
def load_data(path, seq=True, rcm=True, cons=False, test=False, cons_file=None, rcm_file=None, seq_file=None):
"""
Load data matrices from the specified folder.
"""
data = dict()
if seq: data["seq"] = np.loadtxt(seq_file, delimiter=' ', skiprows=0)
if rcm: data["rcm"] = np.loadtxt(rcm_file, skiprows=1)
if cons: data["cons"] = np.loadtxt(cons_file, skiprows=0)
if test:
data["Y"] = []
else:
data["Y"] = np.loadtxt(path + 'class.txt', skiprows=1)
print('data loaded')
return data
def split_training_validation(classes, validation_size=0.2, shuffle=False):
"""split sampels based on balnace classes"""
num_samples = len(classes)
classes = np.array(classes)
classes_unique = np.unique(classes)
num_classes = len(classes_unique)
indices = np.arange(num_samples)
# indices_folds=np.zeros([num_samples],dtype=int)
training_indice = []
training_label = []
validation_indice = []
validation_label = []
print(str(classes_unique))
for cl in classes_unique:
indices_cl = indices[classes == cl]
num_samples_cl = len(indices_cl)
# split this class into k parts
if shuffle:
random.shuffle(indices_cl) # in-place shuffle
# module and residual
num_samples_validation = int(num_samples_cl * validation_size)
res = num_samples_cl - num_samples_validation
training_indice = training_indice + [val for val in indices_cl[num_samples_validation:]]
training_label = training_label + [cl] * res
validation_indice = validation_indice + [val for val in indices_cl[:num_samples_validation]]
validation_label = validation_label + [cl] * num_samples_validation
training_index = np.arange(len(training_label))
random.shuffle(training_index)
training_indice = np.array(training_indice)[training_index]
training_label = np.array(training_label)[training_index]
validation_index = np.arange(len(validation_label))
random.shuffle(validation_index)
validation_indice = np.array(validation_indice)[validation_index]
validation_label = np.array(validation_label)[validation_index]
print(np.shape(training_indice))
print(np.shape(training_label))
print(np.shape(validation_indice))
print(np.shape(validation_label))
return training_indice, training_label, validation_indice, validation_label
def preprocess_data(X, scaler=None, stand=False):
if not scaler:
if stand: