-
Notifications
You must be signed in to change notification settings - Fork 37
/
create_dataset.py
211 lines (180 loc) · 7.87 KB
/
create_dataset.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
from sklearn.cluster import KMeans
from sklearn.cluster import MiniBatchKMeans
from sklearn import preprocessing
import math
import random
import numpy as np
from math import floor
import pandas as pd
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import os
import time
from sklearn.neighbors import NearestNeighbors
import random
def label_encode(strains):
amino_acids = ['A', 'F', 'Q', 'R', 'T', 'Y', 'V', 'I', 'H', 'K', 'P', 'N', 'E', 'G', 'S', 'M', 'D', 'W', 'C', 'L', '-', 'B', 'J', 'Z', 'X']
le = preprocessing.LabelEncoder()
le.fit(amino_acids)
encoded_strains = []
for strain in strains:
chars = list(strain)
encoded_strains.append(le.transform(chars))
return encoded_strains
def label_decode(encoded_strains):
amino_acids = ['A', 'F', 'Q', 'R', 'T', 'Y', 'V', 'I', 'H', 'K', 'P', 'N', 'E', 'G', 'S', 'M', 'D', 'W', 'C', 'L', '-', 'B', 'J', 'Z', 'X']
le = preprocessing.LabelEncoder()
le.fit(amino_acids)
strains = []
for one_year_encoded_strains in encoded_strains:
one_year_strains = []
for encoded_strain in one_year_encoded_strains:
temp = le.inverse_transform(encoded_strain)
one_year_strains.append(''.join(temp))
strains.append(one_year_strains)
return strains
#strains : df['Sequence']
def strain_cluster(strains,num_clusters=2):
encoded_strains = label_encode(strains)
kmeans = KMeans(n_clusters=num_clusters, random_state=0)
kmeans.fit(encoded_strains)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
result = {'data':encoded_strains, 'labels':labels, 'centroids':centroids}
return result
def show_cluster(cluster,save_fig_path='none'):
encoded_strains = cluster['data']
pca = PCA(n_components=2)
reduced_data = pca.fit_transform(encoded_strains)
fig = plt.figure()
colors = 10 * ['r.', 'g.', 'y.', 'c.', 'm.', 'b.', 'k.']
labels = cluster['labels']
for i in range(len(reduced_data)):
plt.plot(reduced_data[i][0], reduced_data[i][1], colors[labels[i]], markersize=10)
plt.show()
if save_fig_path!='none':
plt.savefig(save_fig_path)
def link_clusters(clusters):
no_years = len(clusters)
neigh = NearestNeighbors(n_neighbors=2)
for year_idx in range(no_years):
if (year_idx == no_years - 1): # last year doesn't link
clusters[year_idx]['links'] = []
break
links = []
current_centroids = clusters[year_idx]['centroids']
next_year_centroids = clusters[year_idx + 1]['centroids']
neigh.fit(next_year_centroids)
idxs_by_centroid = neigh.kneighbors(current_centroids, return_distance=False)
for label in clusters[year_idx]['labels']:
links.append(idxs_by_centroid[label]) # centroid idx corresponds to label
clusters[year_idx]['links'] = links
def sample_from_clusters(clusters_by_years, sample_size):
sampled_strains = []
for i in range(sample_size):
one_sample = []
start_idx = random.randint(0, len(clusters_by_years[0]['data'])-1)
start_strain = clusters_by_years[0]['data'][start_idx]
one_sample.append(start_strain)
num_years = len(clusters_by_years)
idx = start_idx
for i in range(num_years - 1):
next_nearest_label = clusters_by_years[i]['links'][idx][0]
candidate_idx = np.where(clusters_by_years[i + 1]['labels'] == next_nearest_label)[0]
idx = random.choice(candidate_idx)
one_sample.append(clusters_by_years[i + 1]['data'][idx])
sampled_strains.append(one_sample)
return sampled_strains
# input: list of list, n_sample*n_year, each item is a strain(str)
# output: return/write a df/csv file, format
# like 'data/processed/H1N1/triplet_cluster_train.csv'
def create_dataset(strains, position, window_size=10, output_path='None'):
# create label
label = []
for sample in strains:
if sample[-1][position] == sample[-2][position]:
label.append(0)
else:
label.append(1)
# read prot embedding
df = pd.read_csv('/home/zh/codes/rnn_virus_source_code/data/raw/H1N1/protVec_100d_3grams.csv', sep='\t')
triple2idx = {}
for index, row in df.iterrows():
triple2idx[row['words']] = index
# create data
start_year_idx = len(strains[0]) - window_size - 1
data = []
for i in range(len(strains)):
one_sample_data = []
for year in range(start_year_idx, len(strains[0]) - 1):
tritri = []
if (position == 0):
tritri.append(9047)
tritri.append(9047)
if strains[i][year][position:position + 3] in triple2idx:
tritri.append(triple2idx[strains[i][year][position:position + 3]])
else:
tritri.append(9047)
elif (position == 1):
tritri.append(9047)
if strains[i][year][position - 1:position + 2] in triple2idx:
tritri.append(triple2idx[strains[i][year][position - 1:position + 2]])
else:
tritri.append(9047)
if strains[i][year][position:position + 3] in triple2idx:
tritri.append(triple2idx[strains[i][year][position:position + 3]])
else:
tritri.append(9047)
elif (position == 1271):
if strains[i][year][position - 2:position + 1] in triple2idx:
tritri.append(triple2idx[strains[i][year][position - 2:position + 1]])
else:
tritri.append(9047)
if strains[i][year][position - 1:position + 2] in triple2idx:
tritri.append(triple2idx[strains[i][year][position - 1:position + 2]])
else:
tritri.append(9047)
tritri.append(9047)
elif (position == 1272):
if strains[i][year][position - 2:position + 1] in triple2idx:
tritri.append(triple2idx[strains[i][year][position - 2:position + 1]])
else:
tritri.append(9047)
tritri.append(9047)
tritri.append(9047)
else:
if strains[i][year][position - 2:position + 1] in triple2idx:
tritri.append(triple2idx[strains[i][year][position - 2:position + 1]])
else:
tritri.append(9047)
if strains[i][year][position - 1:position + 2] in triple2idx:
tritri.append(triple2idx[strains[i][year][position - 1:position + 2]])
else:
tritri.append(9047)
if strains[i][year][position:position + 3] in triple2idx:
tritri.append(triple2idx[strains[i][year][position:position + 3]])
else:
tritri.append(9047)
one_sample_data.append(str(tritri))
data.append(one_sample_data)
dataset = pd.DataFrame(data)
print(dataset.shape)
print(len(label))
dataset.insert(0, 'y', label)
return dataset
def main():
path = '/data/zh/sprot_years/'
files = os.listdir(path)
files.sort()
cluster_years = []
start_time = time.time()
for file in files:
df = pd.read_csv(path + file)
strains = df['Sequence'].sample(1000, replace=True)
cluster = strain_cluster(strains, num_clusters=4)
cluster_years.append(cluster)
print("{:.1f}: {} processed.".format(time.time() - start_time, file))
link_clusters(cluster_years)
ss = sample_from_clusters(cluster_years, 10)
dss = label_decode(ss)
datasetdf = create_dataset(dss, 12)