-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
333 lines (264 loc) · 10.6 KB
/
utils.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
#include "utils.h"
void calcUISize(const struct gargs *argument1, const struct gargs *argument2, uint64_t *interSize, uint64_t *unionSize) {
uint64_t is = 0;
uint64_t us = 0;
uint64_t size1 = argument1->cores_len;
uint64_t size2 = argument2->cores_len;
uint64_t index1 = 0;
uint64_t index2 = 0;
const simple_core *cores1 = argument1->cores;
const simple_core *cores2 = argument2->cores;
while (index1 < size1 && index2 < size2) {
us++;
if (cores1[index1] == cores2[index2]) {
is++;
index1++;
index2++;
} else if (cores1[index1] < cores2[index2]) {
index1++;
} else {
index2++;
}
}
us += (size1-index1);
us += (size2-index2);
*interSize = is;
*unionSize = us;
}
double calcJaccardSim(uint64_t interSize, uint64_t unionSize) {
return (double)interSize / (double)unionSize;
}
double calcDiceSim(uint64_t interSize, uint64_t size1, uint64_t size2) {
return 2 * (double)interSize / ((double)size1+(double)size2);
}
double calcHammDist(double jaccardSim, double avgLen) {
return 1 - pow(jaccardSim, 1.0/avgLen);
}
double calcJukesCantorCor(double hammingDist) {
return - 3.0/4.0 * log(1 - hammingDist * 4.0/3.0);
}
void calcDistances(const struct gargs *genome_arguments, const struct pargs* program_arguments) {
log1(INFO, "Calculating distance matrices...");
// Initialize similarity matrices
double jaccard[program_arguments->number_of_genomes][program_arguments->number_of_genomes];
double dice[program_arguments->number_of_genomes][program_arguments->number_of_genomes];
double jukes_cantor[program_arguments->number_of_genomes][program_arguments->number_of_genomes];
for (int i = 0; i < program_arguments->number_of_genomes; i++) {
jaccard[i][i] = 0.0;
dice[i][i] = 0.0;
jukes_cantor[i][i]= 0.0;
}
// Compute similarity scores
for (int i=0; i<program_arguments->number_of_genomes; i++) {
for (int j=i+1; j<program_arguments->number_of_genomes; j++) {
size_t interSize, unionSize;
calcUISize(&(genome_arguments[i]), &(genome_arguments[j]), &interSize, &unionSize);
double diceDist = 1.0 - calcDiceSim(interSize, genome_arguments[i].cores_len, genome_arguments[j].cores_len);
double jaccardDist = 1.0 - calcJaccardSim(interSize, unionSize);
double avg_len = (genome_arguments[i].total_len+genome_arguments[j].total_len)/(genome_arguments[i].cores_len+genome_arguments[j].cores_len);
double jukesCantorDist = calcHammDist(1.0 - jaccardDist, avg_len);
jukesCantorDist = calcJukesCantorCor(jukesCantorDist);
dice[i][j] = diceDist;
jaccard[i][j] = jaccardDist;
jukes_cantor[i][j] = jukesCantorDist;
// set values to transposed locations
dice[j][i] = diceDist;
jaccard[j][i] = jaccardDist;
jukes_cantor[j][i] = jukesCantorDist;
}
}
log1(INFO, "Writing distance matrices to files...");
int lcp_level = genome_arguments[0].lcp_level;
// Write outputs to files
char *program_type = genome_arguments[0].sct == SET ? "set" : "vec";
FILE *dice_out, *jaccard_out, *jukes_cantor_out;
char filename_buffer[256];
if (snprintf(filename_buffer, 256, "%s.%s.%s%d.phy", program_arguments->prefix, program_type, "dice.lvl", lcp_level) < 0) {
log1(ERROR, "Filename buffer for dice overflow.");
exit(EXIT_FAILURE);
}
dice_out = fopen(filename_buffer, "w");
if (snprintf(filename_buffer, 256, "%s.%s.%s%d.phy", program_arguments->prefix, program_type, "jaccard.lvl", lcp_level) < 0) {
log1(ERROR, "Filename buffer for jaccard overflow.");
exit(EXIT_FAILURE);
}
jaccard_out = fopen(filename_buffer, "w");
if (snprintf(filename_buffer, 256, "%s.%s.%s%d.phy", program_arguments->prefix, program_type, "jc.lvl", lcp_level) < 0) {
log1(ERROR, "Filename buffer for jc overflow.");
exit(EXIT_FAILURE);
}
jukes_cantor_out = fopen(filename_buffer, "w");
// write dice
if (dice_out) {
fprintf(dice_out, "%d\n", program_arguments->number_of_genomes);
for (int i = 0; i < program_arguments->number_of_genomes; i++) {
fprintf(dice_out, "%10s", genome_arguments[i].shortName);
for (int j = 0; j < program_arguments->number_of_genomes; j++) {
fprintf(dice_out, " %.15f", dice[i][j]);
}
fprintf(dice_out, "\n");
}
fclose(dice_out);
}
// write jaccard
if (jaccard_out) {
fprintf(jaccard_out, "%d\n", program_arguments->number_of_genomes);
for (int i = 0; i < program_arguments->number_of_genomes; i++) {
fprintf(jaccard_out, "%10s", genome_arguments[i].shortName);
for (int j = 0; j < program_arguments->number_of_genomes; j++) {
fprintf(jaccard_out, " %.15f", jaccard[i][j]);
}
fprintf(jaccard_out, "\n");
}
fclose(jaccard_out);
}
// write jukes cantor
if (jukes_cantor_out) {
fprintf(jukes_cantor_out, "%d\n", program_arguments->number_of_genomes);
for (int i = 0; i < program_arguments->number_of_genomes; i++) {
fprintf(jukes_cantor_out, "%10s", genome_arguments[i].shortName);
for (int j = 0; j < program_arguments->number_of_genomes; j++) {
fprintf(jukes_cantor_out, " %.15f", jukes_cantor[i][j]);
}
fprintf(jukes_cantor_out, "\n");
}
fclose(jukes_cantor_out);
}
}
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// MARK: LCP cores related functions
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
void quicksort(simple_core *array, int low, int high) {
if (low < high) {
simple_core pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] < pivot) {
i++;
simple_core temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
simple_core temp = array[i+1];
array[i+1] = array[high];
array[high] = temp;
quicksort(array, low, i);
quicksort(array, i + 2, high);
}
}
void genSign(struct gargs *genome_arguments, sim_calculation_type mode) {
simple_core *cores = genome_arguments->cores;
uint64_t len = genome_arguments->cores_len;
double total_len = genome_arguments->total_len;
quicksort(cores, 0, len);
if (genome_arguments->apply_filter) {
uint32_t min_cc = genome_arguments->min_cc;
uint32_t max_cc = genome_arguments->max_cc;
uint64_t index = 0;
uint64_t i = 0;
while (i<len) {
uint64_t freq = 1;
for (uint64_t j=i+1; j<len && cores[i]==cores[j]; j++, freq++);
if (min_cc<=freq && freq<=max_cc) {
memcpy(&(cores[index]), &(cores[i]), freq * sizeof(simple_core));
index += freq;
}
i += freq;
}
genome_arguments->cores_len = index;
len = index;
}
if (mode == VECTOR) {
return;
}
uint64_t index = 0;
uint64_t i=1;
total_len += cores[0] & 0xFFFFFFFF;
while (i<len) {
if (cores[index] != cores[i]) {
index++;
cores[index] = cores[i];
total_len += cores[i] & 0xFFFFFFFF;
}
i++;
}
if (index) {
simple_core *new_cores = (simple_core *)realloc(cores, index * sizeof(simple_core));
if (new_cores) {
genome_arguments->cores = new_cores;
} else {
free(cores);
genome_arguments->cores = NULL;
index = 0;
}
} else {
free(cores);
genome_arguments->cores = NULL;
}
genome_arguments->cores_len = index;
genome_arguments->total_len = total_len;
}
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// MARK: File I/O operations
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
void save(FILE *out, struct lps *str) {
int isDone = 0;
// notify that there is an output to be written
fwrite(&isDone, sizeof(int), 1, out);
// write lps object
write_lps(str, out);
}
void done(FILE *out) {
int isDone = 1;
// notify that there will be no output after this
fwrite(&isDone, sizeof(int), 1, out);
}
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// MARK: Logging
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
int log1(LogLevel level, const char *format, ...) {
time_t now;
time(&now);
struct tm *local = localtime(&now);
printf("[%02d-%02d-%04d %02d:%02d:%02d] ", local->tm_mday, local->tm_mon + 1, local->tm_year + 1900,
local->tm_hour, local->tm_min, local->tm_sec);
switch (level) {
case INFO:
printf("[INFO] ");
break;
case WARN:
printf("[WARN] ");
break;
case ERROR:
printf("[ERROR] ");
break;
}
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
printf("\n");
return 1;
}
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
// MARK: Cleanup
// ---------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------
void free_args(struct gargs * genome_arguments, struct pargs * program_arguments) {
for (int i=0; i<program_arguments->number_of_genomes; i++) {
if (genome_arguments[i].cores_len) {
if (genome_arguments[i].cores_len)
free(genome_arguments[i].cores);
genome_arguments[i].cores_len = 0;
}
}
free(genome_arguments);
}