-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_building_no_pseudoknots.py
173 lines (147 loc) · 5.06 KB
/
dataset_building_no_pseudoknots.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
import os
from random import shuffle
import numpy as np
from sklearn.model_selection import train_test_split
input_dotbrackets = []
input_sequences = []
input_ids = []
len_limit = 700
input_list = os.listdir('data')
shuffle(input_list)
np.random.seed(7)
for filename in input_list:
sequence = []
dotbracket = []
num_lines = sum(1 for line in open('data/'+filename)
if not line.startswith('# ')
and len(line)>0
and line != '\n')
raw_file = open('data/'+filename).read().splitlines()
for line in raw_file:
if (not line.startswith('# ') and len(line)>0 and line != '\n'):
if(len(sequence)>= (num_lines/2)):
dotbracket.append(line)
else:
sequence.append(line)
s = ''.join(sequence)
d = ''.join(dotbracket)
if(len(s)<len_limit):
input_sequences.append(s+'\n')
input_dotbrackets.append(d+'\n')
input_ids.append(filename)
token_index_seq = {}
token_index_db = {}
for sequence in input_sequences:
for base in sequence:
if base not in token_index_seq:
token_index_seq[base] = len(token_index_seq)+1
for db_sequence in input_dotbrackets:
for base in db_sequence:
if base not in token_index_db:
token_index_db[base] = len(token_index_db)+1
print("characters in the sequences dictionary (full): {}".format(len(token_index_seq)))
print("characters in the dotbrackets dictionary (full): {}".format(len(token_index_db)))
max_length = len(max(input_sequences, key=len))
allowed_dictionary_seq = ['\n', 'A', 'C', 'G', 'U', 'M', 'N', 'R', 'W', 'S', 'Y', 'K', 'V', 'H', 'D', 'B']
print(allowed_dictionary_seq)
print()
input_sequences_temp = []
input_dotbrackets_temp = []
input_ids_temp = []
token_index_seq_temp = dict(token_index_seq)
#token_index_seq
print("token_index_seq:")
sorted_by_value = sorted(token_index_seq.items(), key=lambda kv: kv[1])
print(sorted_by_value)
#for val in allowed_dictionary_seq:
# handleNull = token_index_seq.pop(val, None)
token_index_seq_final = {key: value+1 for (value, key) in enumerate(allowed_dictionary_seq)}
#token_index_seq['EOS'] = len(token_index_seq)
for char in token_index_seq_final.keys():
if char not in allowed_dictionary_seq:
handleNull = token_index_seq_final.pop(val, None)
print()
print("final dict for sequences:")
sorted_by_value = sorted(token_index_seq_final.items(), key=lambda kv: kv[1])
for sequence,db,fname in zip(input_sequences, input_dotbrackets, input_ids):
copy_it = True
for char in sequence:
if char not in token_index_seq_final.keys():
copy_it = False
if copy_it == True:
input_sequences_temp.append(sequence)
input_dotbrackets_temp.append(db)
input_ids_temp.append(fname)
input_sequences = input_sequences_temp
input_dotbrackets = input_dotbrackets_temp
input_ids = input_ids_temp
base_to_idx = set()
db_to_idx = set()
for sequence in input_sequences:
for base in sequence:
if (base not in base_to_idx):
base_to_idx.add(base)
for sequence in input_dotbrackets:
for dbr in sequence:
if (dbr not in db_to_idx):
db_to_idx.add(dbr)
base_to_idx = sorted(list(base_to_idx))
db_to_idx = sorted(list(db_to_idx))
base_to_idx_dict = {}
idx_to_base_dict = {}
for k, v in enumerate(base_to_idx):
idx_to_base_dict[k] = v
base_to_idx_dict[v] = k
db_to_idx_dict = {}
idx_to_db_dict = {}
for k, v in enumerate(db_to_idx):
idx_to_db_dict[k] = v
db_to_idx_dict[v] = k
max_len_sequence = max([len(line) for line in input_sequences])
max_len_db = max([len(line) for line in input_dotbrackets])
base_to_idx_dict
print()
print()
idx_to_db_dict
print()
max_len_sequence
print()
max_len_db
input_seq = []
input_db = []
n = 200
for z in zip(input_sequences, input_dotbrackets):
if(len(z[0])> 200):
splitteds = [z[0][i:i+n] for i in range(0, len(z[0]), n)]
splittedd = [z[1][i:i+n] for i in range(0, len(z[1]), n)]
for splittedt in splitteds:
input_seq.append(splittedt+'\n')
for splittedt in splittedd:
input_db.append(splittedt+'\n')
else:
input_seq.append(z[0]+'\n')
input_db.append(z[1]+'\n')
input_sequences = input_seq
input_dotbrackets = input_db
input_x = np.zeros((len(input_sequences), max_len_sequence, len(base_to_idx)))
for i, sequence in enumerate(input_sequences):
for j, base in enumerate(sequence):
index = base_to_idx_dict.get(base)
input_x[i, j, index] = 1
# added the Masking Layer:
#if j == len(sequence)-1:
#if j != max_len_sequence-1:
#input_x[i, j+1:max_len_sequence, 0] = 1
input_y = np.zeros((len(input_dotbrackets), max_len_db, len(db_to_idx)))
for i, sequence in enumerate(input_dotbrackets):
for j, base in enumerate(sequence):
index = db_to_idx_dict.get(base)
input_y[i, j, index] = 1
# added the Masking Layer:
# if j == len(sequence)-1:
# if j != max_len_db-1:
# input_y[i, j+1:max_len_db, 0] = 1
x_train, x_test, y_train, y_test = train_test_split(input_x, input_y, shuffle=True, test_size=50)
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, shuffle=True, test_size=0.20)
input_shape = x_train[0].shape
output_shape = y_train[0,0,:].shape[0]