-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfasta.c
156 lines (118 loc) · 4.74 KB
/
rfasta.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
#include "rfasta.h"
pthread_mutex_t console_mutex_rfasta;
void read_fastas(struct gargs *genome_arguments, struct pargs *program_arguments) {
struct tpool *tm;
tm = tpool_create(program_arguments->thread_number);
for (int i=0; i<program_arguments->number_of_genomes; i++) {
tpool_add_work(tm, read_fasta, genome_arguments+i);
}
tpool_wait(tm);
tpool_destroy(tm);
}
void read_fasta(void *arg) {
struct gargs *genome_arguments = (struct gargs *)arg;
// open fasta file
FILE *in = fopen(genome_arguments->inFileName, "r");
if (in == NULL) {
log1(ERROR, "Error opening file %s", genome_arguments->inFileName);
return;
}
fseek(in, 0, SEEK_END); // seek to end of file
uint64_t size = ftell(in); // get current file pointer
fseek(in, 0, SEEK_SET); // seek back to beginning of fil
uint64_t estimated_core_size = (int)(size / pow(MAGIC_LCP_FA_CONSTANT, genome_arguments->lcp_level));
genome_arguments->cores = (simple_core*)malloc(estimated_core_size * sizeof(simple_core));
if (genome_arguments->cores == NULL) {
pthread_mutex_lock(&console_mutex_rfasta);
log1(INFO, "Thread ID: %ld couldn't allocate memory of size %ld for cores", pthread_self(), size);
pthread_mutex_unlock(&console_mutex_rfasta);
}
// create file for writing cores
FILE *out = NULL;
if (genome_arguments->write_lcpt) {
out = fopen(genome_arguments->inFileName, "wb");
if (out == NULL) {
log1(ERROR, "Error opening file for saving into file %s", genome_arguments->outFileName);
return;
}
}
if (genome_arguments->verbose) {
pthread_mutex_lock(&console_mutex_rfasta);
log1(INFO, "Thread ID: %ld, in: %s, cc: %ld", pthread_self(), genome_arguments->inFileName, estimated_core_size);
pthread_mutex_unlock(&console_mutex_rfasta);
}
// read file
char *sequence = (char*)malloc(INITIAL_SEQUENCE_SIZE);
if (!sequence) {
log1(ERROR, "Memory allocation failed for sequence buffer\n");
exit(EXIT_FAILURE);
}
uint64_t sequence_size = 0;
uint64_t sequence_capacity = INITIAL_SEQUENCE_SIZE;
char line[1024];
while (fgets(line, sizeof(line), in)) {
line[strcspn(line, "\n")] = '\0';
if (line[0] == '>') {
if (sequence_size != 0) {
process_chrom(sequence, sequence_size, &estimated_core_size, genome_arguments, out);
sequence_size = 0;
}
} else {
size_t line_len = strlen(line);
while (sequence_size + line_len >= sequence_capacity) {
sequence_capacity = (size_t)(sequence_capacity * 1.5);
sequence = realloc(sequence, sequence_capacity);
if (!sequence) {
log1(ERROR, "Memory reallocation failed\n");
exit(EXIT_FAILURE);
}
}
memcpy(sequence + sequence_size, line, line_len);
sequence_size += line_len;
}
}
if (sequence_size != 0) {
process_chrom(sequence, sequence_size, &estimated_core_size, genome_arguments, out);
}
free(sequence);
fclose(in);
// end writing cores to file if user specified to do so
if (genome_arguments->write_lcpt) {
done(out);
fclose(out);
}
// sort and filter the cores
genSign(genome_arguments, genome_arguments->sct);
// log ending of processing fasta
if (genome_arguments->verbose) {
log1(INFO, "Thread ID: %ld ended processing %s, size: %ld", pthread_self(), genome_arguments->inFileName, genome_arguments->cores_len);
}
}
void process_chrom(char *sequence, size_t seq_size, uint64_t *capacity, struct gargs *genome_arguments, FILE *out) {
// struct lps str;
// init_lps4(&str, sequence, seq_size, genome_arguments->lcp_level, 10000000);
struct lps str;
init_lps(&str, sequence, seq_size);
lps_deepen(&str, genome_arguments->lcp_level);
if (genome_arguments->write_lcpt) {
save(out, &str);
}
uint64_t len = genome_arguments->cores_len;
if (*capacity <= len+str.size) {
*capacity = *capacity * 1.5;
simple_core* temp = (simple_core*)realloc(genome_arguments->cores, *capacity);
if (temp == NULL) {
log1(ERROR, "Couldn't increase cores array size.");
return;
}
genome_arguments->cores = temp;
}
simple_core *cores = genome_arguments->cores;
for (int i=0; i<str.size; i++) {
cores[len] = ((uint64_t)str.cores[i].label << 32) + (str.cores[i].end-str.cores[i].start);
// cores[len] = ((uint64_t)str.cores[i].label);
len++;
}
genome_arguments->cores_len = len;
free_lps(&str);
}