Repository navigation
Expand file tree
/
Copy pathrload.c
More file actions
154 lines (112 loc) · 4.24 KB
/
Copy pathrload.c
File metadata and controls
154 lines (112 loc) · 4.24 KB
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
#include "rload.h"
pthread_mutex_t console_mutex_rload;
void read_lcpts(g_args_t *genome_args, p_args_t *program_args) {
struct tpool *tm;
tm = tpool_create(program_args->n_threads);
for (int i = 0; i < program_args->n_genomes; i++) {
tpool_add_work(tm, read_lcpt, genome_args+i);
}
tpool_wait(tm);
tpool_destroy(tm);
calcDistancesFrequencySweep(genome_args, program_args);
}
void read_lcpt(void *arg) {
g_args_t *genome_args = (g_args_t *)arg;
if (genome_args->verbose) {
pthread_mutex_lock(&console_mutex_rload);
log1(INFO, "Thread ID: %ld started processing %s", pthread_self(), genome_args->inFileName);
pthread_mutex_unlock(&console_mutex_rload);
}
// open binary file
FILE *in = fopen(genome_args->inFileName, "rb");
if (in == NULL) {
log1(ERROR, "Error opening file %s", genome_args->inFileName);
return;
}
uint64_t core_count = 0;
if (fread(&core_count, sizeof(uint64_t), 1, in) != 1) {
log1(ERROR, "Error reading core count from file %s", genome_args->inFileName);
fclose(in);
return;
}
genome_args->result.count = core_count;
genome_args->result.cores = (simple_core *)malloc(sizeof(simple_core) * core_count);
if (genome_args->result.cores == NULL && core_count > 0) {
log1(ERROR, "Memory allocation failed");
fclose(in);
return;
}
size_t read_count = fread(
genome_args->result.cores,
sizeof(simple_core),
core_count,
in
);
if (read_count != core_count) {
log1(ERROR, "Expected %lu cores, but read %zu", core_count, read_count);
free(genome_args->result.cores);
genome_args->result.cores = NULL;
genome_args->result.count = 0;
fclose(in);
return;
}
fclose(in);
// sort
time_t start, end;
time(&start);
sort_u64_radix(genome_args->result.cores, genome_args->result.count);
time(&end);
genome_args->time_stats.sorting = difftime(end, start);
// log ending of processing fasta
if (genome_args->verbose) {
log1(INFO, "Thread ID: %ld ended processing %s, size: %ld", pthread_self(), genome_args->inFileName, genome_args->result.count);
}
}
void calcDistancesFrequencySweep(g_args_t *genome_args, p_args_t *program_args) {
for (uint32_t min_cc = 0; min_cc <= 200; min_cc += 4) {
log1(INFO, "Running distance calculation for min_cc=%u", min_cc);
g_args_t *filtered_args = calloc(program_args->n_genomes, sizeof(g_args_t));
if (filtered_args == NULL) {
log1(ERROR, "Memory allocation failed for filtered genome args");
return;
}
int failed = 0;
for (int i = 0; i < program_args->n_genomes; i++) {
filtered_args[i] = genome_args[i];
uint32_t max_cc = genome_args[i].max_cc;
if (!genome_args[i].apply_filter) {
max_cc = UINT32_MAX;
}
if (build_filtered_result(&genome_args[i], &filtered_args[i], min_cc, max_cc) != 0) {
failed = 1;
break;
}
}
if (genome_args->verbose) {
for (int i = 0; i < program_args->n_genomes; i++) {
log1(INFO, "Processing ended for %s, size: %ld", genome_args[i].inFileName, filtered_args->result.count);
}
}
if (!failed) {
char *temp_prefix = program_args->prefix;
char filename_buffer[256];
if (snprintf(filename_buffer, 256, "%s.cc%u", temp_prefix, min_cc) < 0) {
log1(ERROR, "Filename buffer for dice overflow.");
exit(EXIT_FAILURE);
}
program_args->prefix = temp_prefix;
calcDistances(filtered_args, program_args);
program_args->prefix = temp_prefix;
}
for (int i = 0; i < program_args->n_genomes; i++) {
free(filtered_args[i].result.cores);
filtered_args[i].result.cores = NULL;
filtered_args[i].result.count = 0;
}
free(filtered_args);
if (failed) {
log1(ERROR, "Stopping frequency sweep because filtering failed");
return;
}
}
}