forked from pockerman/hidden_markov_modeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply_clustering.py
executable file
·267 lines (197 loc) · 8.94 KB
/
apply_clustering.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
import argparse
import logging
import numpy as np
import time
from helpers import read_configuration_file
from helpers import set_up_logger
from helpers import WindowType
from helpers import INFO
from helpers import timefn
from region import Region
from analysis_helpers import save_clusters
from analysis_helpers import save_windows_statistic
from cluster import Cluster
from cluster_utils import build_cluster_mean_and_std
from preprocess_utils import build_clusterer
from exceptions import Error
@timefn
def make_window_regions(configuration):
print("{0} Creating window regions...".format(INFO))
windowsize = configuration["window_size"]
chromosome = configuration["chromosome"]
print("{0} Window size: {1}".format(INFO, windowsize))
print("{0} Chromosome: {1}".format(INFO, chromosome))
regions = configuration["regions"]
print("{0} Regions used {1}".format(INFO, regions))
regions_list = [ (start, end) for start, end
in zip(regions["start"], regions["end"])]
regions_created = []
counter=0
for r in regions_list:
start_idx = r[0]
end_idx = r[1]
print("{0} Start index: {1}".format(INFO, start_idx))
print("{0} End index: {1}".format(INFO, end_idx))
region = Region(idx=counter,
start=start_idx,
end=end_idx,
window_size=windowsize)
kwargs = {}
if "quality_theshold" in configuration:
kwargs["quality_theshold"] = configuration["quality_theshold"]
if "debug" in configuration:
kwargs["debug"] = configuration["debug"]
print("{0} Creating WGA Windows...".format(INFO))
region.make_wga_windows(chromosome=chromosome,
ref_filename=configuration["reference_file"]["filename"],
test_filename=configuration["test_file"]["filename"],
**kwargs)
if region.get_n_windows(type_=WindowType.WGA) == 0:
raise Error("WGA windows have not been created")
else:
print("{0} Number of WGA windows: {1}".format(INFO,
region.get_n_windows(type_=WindowType.WGA)))
print("{0} Creating No WGA Windows...".format(INFO))
region.make_no_wga_windows(chromosome=chromosome,
ref_filename=configuration["reference_file"]["filename"],
test_filename=configuration["no_wga_file"]["filename"],
**kwargs)
if region.get_n_windows(type_=WindowType.NO_WGA) == 0:
raise Error("Non-WGA windows have not been created")
else:
print("{0} Number of non-wga"
" windows: {1}".format(INFO,
region.get_n_windows(type_=WindowType.NO_WGA)))
if "check_windowing_sanity" in configuration and \
configuration["check_windowing_sanity"]:
region.check_windows_sanity()
# compute the mixed windows for the region
region.get_mixed_windows()
# filter the windows for N's
if "remove_windows_with_N" in configuration and\
configuration["remove_windows_with_N"]:
print("{0} Filtering windows for Ns...".format(INFO))
region.remove_windows_with_ns()
print("{0} Number of wga windows"
" after filtering: {1}".format(INFO,
region.get_n_windows(type_=WindowType.WGA)))
print("{0} Number of non-wga windows"
" after filtering: {1}".format(INFO,
region.get_n_windows(type_=WindowType.NO_WGA)))
print("{0} Done...".format(INFO))
elif "mark_N_windows" in configuration and\
configuration["mark_N_windows"]:
print("{0} Marking N "
" windows with: {1}".format(INFO,
configuration["mark_for_N_windows"]))
counter_ns = \
region.mark_windows_with_ns(n_mark=configuration["mark_for_N_windows"])
print("{0} Marked as N {1} Windows".format(INFO, counter_ns))
else:
print("{0} No filtering windows"
" for Ns requested...".format(INFO))
print("{0} Number of mixed "
"windows: {1}".format(INFO,
region.get_n_mixed_windows()))
print("{0} Number of N windows: {1}".format(INFO,
region.count_n_windows()))
if "outlier_remove" in configuration and\
configuration["outlier_remove"]:
region.remove_outliers(configuration=configuration)
print("{0} Number of windows "
"after outlier removal: {1}".format(INFO,
region.get_n_mixed_windows()))
print("{0} Number of N windows "
"after outlier removal {1}".format(INFO,
region.count_n_windows()))
else:
print("{0} No outlier "
"removal performed".format(INFO))
# save the region statistics
region.save_mixed_windows_statistic(statistic="mean")
regions_created.append(region)
counter += 1
return regions_created
@timefn
def create_clusters(regions, configuration):
print("{0} Start clustering....".format(INFO))
kwargs = configuration["clusterer"]
# assemble all the windows
windows = []
for region in regions:
for window in region:
if not window.is_n_window():
windows.append(window)
# create the clusters
clusterer, initial_index_medoids = \
build_clusterer(data=windows,
nclusters=kwargs["config"]["n_clusters"],
method="kmedoids", **kwargs)
print("{0} Initial medoids indexes: {1}".format(INFO,
initial_index_medoids))
# get the window indexes
clusters_indexes = clusterer.get_clusters()
centers = clusterer.get_medoids()
clusters = []
for i in range(len(clusters_indexes)):
clusters.append(Cluster(id_ = i,
indexes=clusters_indexes[i],
windows=windows,
center_idx=centers[i],
dist_metric=kwargs["config"]["metric"]))
print("{0} Saving clusters means".format(INFO))
save_clusters(clusters=clusters, statistic="mean")
print("{0} Done...".format(INFO))
return clusters
@timefn
def make_clusters_mean_and_std(clusters, configuration):
print("{0} Create clusters mean/std...".format(INFO))
kwargs = {}
print("{0} Make clusters mu/std...".format(INFO) )
build_cluster_mean_and_std(clusters=clusters, **kwargs)
print("{0} Done...".format(INFO))
def main(configuration):
print("{0} Set up logger".format(INFO))
set_up_logger(configuration=configuration)
logging.info("Checking if logger is sane...")
print("{0} Done...".format(INFO))
regions = make_window_regions(configuration=configuration)
print("{0} Saving regions...".format(INFO))
time_start = time.perf_counter()
for region in regions:
region.save()
time_end = time.perf_counter()
print("{0} Done. Execution time {1} secs".format(INFO, time_end - time_start))
clusters = create_clusters(regions=regions,
configuration=configuration)
make_clusters_mean_and_std(clusters=clusters,
configuration=configuration)
print("{0} Save clusters...".format(INFO))
time_start = time.perf_counter()
if "save_cluster_dbi" in configuration and\
configuration["save_cluster_dbi"]:
for cluster in clusters:
cluster.diameter
for other in clusters:
cluster.distance_from_other(other=other)
for cluster in clusters:
cluster.save()
time_end = time.perf_counter()
print("{0} Done. Execution time {1} secs".format(INFO, time_end - time_start))
if __name__ == '__main__':
print("{0} Start clustering...".format(INFO))
total_start = time.perf_counter()
description = "Check the README file for "
"information on how to use the script"
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--config', type=str, default='config.json',
help="You must specify a json "
"formatted configuration file")
print("{0} Read configuration file".format(INFO))
args = parser.parse_args()
configuration = read_configuration_file(args.config)
print("{0} Done...".format(INFO))
main(configuration=configuration)
total_end = time.perf_counter()
print("{0} Finished clustering. "
"Execution time {1} secs".format(INFO, total_end - total_start))