-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathk_means_clustering.c
356 lines (310 loc) · 11.5 KB
/
k_means_clustering.c
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
//
// k_means_clustering.c
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "mTR.h"
void print_one_TR_with_read(TR aTR, int pretty_print){
repeat_in_read rr = repeats_in_all_reads[aTR.ID];
rr.actual_rep_period = aTR.repTR->actual_rep_period;
strcpy(rr.string, repeats_in_all_reads[aTR.repTR->ID].string);
if(pretty_print == 1){
printf("repID = %i,\t",aTR.repID);
pretty_print_one_repeat_in_read(rr);
}else{
printf("%d\t",aTR.repID);
print_one_repeat_in_read(rr);
}
}
void print_one_TR(TR aTR){
char message[2000] = "";
if(aTR.actual_rep_period > 0){
if(aTR.repTR == NULL){
sprintf(message, "%i,\tUnit len = %i,\t#Units = %i,\trepID = %i,\tfreq = %i,\t2mers = ",
aTR.ID,
aTR.actual_rep_period,
aTR.Num_freq_unit,
aTR.repID,
aTR.freq
);
}else{
sprintf(message, "%i,\tUnit len = %i,\t#Units = %i,\trepID = %i,\tfreq = %i,\t2mers = ",
aTR.ID,
aTR.actual_rep_period,
aTR.Num_freq_unit,
aTR.repTR->ID,
aTR.repTR->freq
);
}
char tmp_msg[6]="";
for(int i=0; i<16; i++){
sprintf(tmp_msg, "%i,", aTR.freq_2mer[i]);
strcat(message, tmp_msg);
}
strcat(message, "\tstring = ");
strcat(message, repeats_in_all_reads[aTR.ID].string);
}
printf("%s\n", message);
}
int cmp_TR(TR aTR, TR bTR, int mode){
// mode = 0: Ordered by <actual_rep_period, freq_2mer> of aTR and bTR
// mode = 1: Ordered by <actual_rep_period, freq_2mer, Num_freq_unit> of aTR and bTR
// mode = 2: Ordered by <frequency, identifier> of the representative TRs of aTR and bTR
int diff;
if(mode == 0 || mode == 1){
// Compute the differenece between the unit lengths (actual_rep_period)
diff = aTR.actual_rep_period - bTR.actual_rep_period;
// If the lengths of the two units are equal, compute the difference between the 2mer distribution (freq_2mer)
if(diff != 0){
return(diff); // negative: <, positive means: >
}else{ // diff == 0
for(int i=0; i<16; i++){
diff = aTR.freq_2mer[i] - bTR.freq_2mer[i];
if(diff != 0){
return(diff); // negative: <, positive means: >
}
}
// If aTR.freq_2mer == bTR.freq_2mer; namely, aTR.freq_2mer[i] == bTR.freq_2mer[i] for all i.
if( mode == 1){
diff = aTR.Num_freq_unit - bTR.Num_freq_unit;
return(diff);
}else{
return(0);
}
}
}else if(mode == 2){
// Ordering according to the frequency and identifier of the representative TR
diff = -(aTR.repTR->freq - bTR.repTR->freq);
if(diff == 0){
diff = aTR.repTR->ID - bTR.repTR->ID;
return(diff);
}else{
return(diff);
}
}else{
return(0);
}
}
int partition_TR_list(int left, int right, int mode) {
// Select a number between left and right at random.
int random = rand() % (right-left+1) + left;
// Exchange target[right] and target[random].
TR tmp = TR_list[right]; TR_list[right] = TR_list[random]; TR_list[random] = tmp;
TR pivot = TR_list[right];
int i = left-1; // i scans the array from the left.
int j = right; // j scans the array from the right.
for (;;) {
// Move from the left until hitting a value no less than the pivot.
for(i++; cmp_TR(TR_list[i], pivot, mode) < 0; i++){}
// Move from the right until hitting a value no greater than the pivot.
for(j--; cmp_TR(pivot, TR_list[j], mode) < 0 && i < j; j--){}
if (i >= j) break;
// Exchange target[i] and target[j].
tmp = TR_list[i]; TR_list[i] = TR_list[j]; TR_list[j] = tmp;
}
// Exchange target[i] and target[right].
tmp = TR_list[i]; TR_list[i] = TR_list[right]; TR_list[right] = tmp;
return i;
}
// We set "mode" to 1 in the first call of this function.
// Afterwards, we set "mode" to 2 in the second call.
void sort_TR_List(int left, int right, int mode){
if (left < right) {
int i = partition_TR_list(left, right, mode); // i: Position of the pivot.
sort_TR_List(left, i - 1, mode);
sort_TR_List(i + 1, right, mode);
}
}
int select_repTR_list(int Num_qualified_TRs){
int freq = 1;
int j = 0;
int i;
for(i=0; i<(Num_qualified_TRs - 1); i++){
if(cmp_TR(TR_list[i], TR_list[i+1], 0) == 0){
// mode = 0: Ordered by <actual_rep_period, freq_2mer>
// If two consecutive TRs are identical in terms of <actual_rep_period, freq_2mer>,
// put them into an identical group, and increment its size, denoted by freq.
freq++;
}else if(MIN_NUM_repTR <= freq){ // End of a block of identical TRs
repTR_list[j] = TR_list[i]; // Set the last TR in one group to the reresentative
repTR_list[j].freq = freq;
for(int k=i; i-freq+1<=k; k--){ // Update representative IDs
TR_list[k].repID = repTR_list[j].ID;
TR_list[k].repTR = &repTR_list[j];
}
j++;
freq = 1;
}
}
if(i == Num_qualified_TRs - 1 && MIN_NUM_repTR <= freq){
repTR_list[j] = TR_list[i];
repTR_list[j].freq = freq;
for(int k=i; i-freq+1<=k; k--){ // Update representative IDs
TR_list[k].repID = repTR_list[j].ID;
TR_list[k].repTR = &repTR_list[j];
}
j++;
}
return(j); // Number of representative TRs
}
int TRs_in_neighborhood(TR aTR, TR repTR){
// return +1 if they are in neighborhood, and -1 otherwise
int diff = 0;
for(int i=0; i<16; i++){
diff += abs(aTR.freq_2mer[i] - repTR.freq_2mer[i]);
}
// We here set the threshold to a temporary value, but it should be revised.
if( MH_distance_threshold * repTR.actual_rep_period < diff)
return(-1);
else
return(1);
}
void revise_repTR_list(int Num_repTRs){
// Revise the clustering to merge groups whose 2mer distributions are similar according to the measure defined in TRs_in_neighborhood. Select the largest group as the representative.
for(int i=0; i<Num_repTRs; i++){
TR aTR = repTR_list[i];
// Search the list of TRs for those similar to the focal TR.
// Candidate TRs are of length within 10% of the focal TR length.
int lb = aTR.actual_rep_period - (int)(aTR.actual_rep_period * 0.1);
int ub = aTR.actual_rep_period + (int)(aTR.actual_rep_period * 0.1);
int max_freq = aTR.freq;
int max_i = i;
// Search backward
for(int j=i-1; 0<=j; j--){ // Search positions
TR repTR = repTR_list[j];
if(lb <= repTR.actual_rep_period){
if(TRs_in_neighborhood(aTR, repTR) == 1 && max_freq < repTR.freq){
max_freq = repTR.freq;
max_i = j;
}
}else{
break;
}
}
// Search forward
for(int j=i+1; j<Num_repTRs; j++){
TR repTR = repTR_list[j];
if(repTR.actual_rep_period <= ub){
if(TRs_in_neighborhood(aTR, repTR) == 1 && max_freq < repTR.freq){
max_freq = repTR.freq;
max_i = j;
}
}else{
break;
}
}
repTR_list[i].repID = repTR_list[max_i].ID;
repTR_list[i].repTR = &repTR_list[max_i];
}
for(int i=0; i<Num_repTRs; i++){
TR *aTR = &repTR_list[i];
if(aTR->ID != aTR->repID){
// Search the root of representative groups recursively.
while(aTR->ID != aTR->repID){
aTR = aTR->repTR;
}
repTR_list[i].repID = aTR->ID;
repTR_list[i].repTR = aTR;
aTR->freq += repTR_list[i].freq;
}
}
}
void update_repTRs_in_TR_list(int Num_TRs){
for(int i=0; i<Num_TRs; i++){
TR *aTR = TR_list[i].repTR;
if(aTR->ID != aTR->repID){
// Search the root of representative groups recursively.
while(aTR->ID != aTR->repID){
aTR = aTR->repTR;
}
TR_list[i].repID = aTR->ID;
TR_list[i].freq = aTR->freq;
TR_list[i].repTR = aTR;
}
}
}
void k_means_clustering(int read_cnt, int pretty_print){
TR_list = (TR*) malloc(read_cnt*sizeof(TR));
if( TR_list == NULL ){
fprintf(stderr, "cannot allocate space for one of global variables in the heap.\n");
exit(EXIT_FAILURE);
}
repTR_list = (TR*) malloc(read_cnt*sizeof(TR));
if( repTR_list == NULL ){
fprintf(stderr, "cannot allocate space for one of global variables in the heap.\n");
exit(EXIT_FAILURE);
}
repeat_in_read aRR;
// Select qualified TRs
int j = 0;
for(int i = 0; i < read_cnt; i++){
aRR = repeats_in_all_reads[i];
float match_ratio = (float)aRR.Num_matches / aRR.actual_repeat_len;
if(MIN_REP_LEN < (aRR.actual_rep_period * aRR.Num_freq_unit) &&
MIN_MATCH_RATIO < match_ratio &&
1 < aRR.Num_freq_unit )
{ // qualifed
TR_list[j].ID = aRR.ID;
TR_list[j].repID = aRR.ID;
TR_list[j].repTR = NULL;
TR_list[j].freq = 1;
TR_list[j].actual_rep_period = aRR.actual_rep_period;
for(int k =0; k<16; k++){
TR_list[j].freq_2mer[k] = aRR.freq_2mer[k];
}
TR_list[j].Num_freq_unit = aRR.Num_freq_unit;
// initialization
j++;
}
}
int Num_qualified_TRs = j;
#ifdef DEBUG_clustering1
printf("Before being sorted\n");
for(j = 0; j < Num_qualified_TRs; j++){
print_one_TR(TR_list[j]);
}
#endif
// Sort according to actual_rep_period, freq_2mer, and Num_freq_unit by setting mode to 1
sort_TR_List(0, Num_qualified_TRs - 1, 1);
#ifdef DEBUG_clustering1
printf("After being sorted\n");
for(j = 0; j < Num_qualified_TRs; j++){
print_one_TR(TR_list[j]);
}
#endif
// Cluster identical TRs in terms of actual_rep_period and freq_2mer
// Determine the size of each group
int Num_repTRs = select_repTR_list(Num_qualified_TRs);
#ifdef DEBUG_clustering1
printf("Representative TRs\n");
for(j = 0; j < Num_repTRs; j++){
print_one_TR(repTR_list[j]);
}
#endif
revise_repTR_list(Num_repTRs);
#ifdef DEBUG_clustering2
printf("Representative TRs\n");
for(j = 0; j < Num_repTRs; j++){
print_one_TR(repTR_list[j]);
}
#endif
update_repTRs_in_TR_list(Num_qualified_TRs);
// Sort according to frequency and repID by setting mode to 2
sort_TR_List(0, Num_qualified_TRs - 1, 2);
#ifdef DEBUG_clustering2
printf("All TRs\n");
for(j = 0; j < Num_qualified_TRs; j++){
print_one_TR(TR_list[j]);
}
#endif
for(int j = 0; j < Num_qualified_TRs; j++){
print_one_TR_with_read(TR_list[j], pretty_print);
if(j % BLK == 0){
fflush( stdout );
}
}
free(TR_list);
free(repTR_list);
}