-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference_data.c
272 lines (237 loc) · 8.96 KB
/
reference_data.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
#include "reference_data.h"
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
extern int allowed_indels;
int IGNORE_LOWERCASE = 0;
/**
* Load reference sequence
*/
static int load_reference_sequence(FILE* ref_in, int readlen, ReferenceDBType* db) {
char line[MAX_LINE_LEN];
long i_start = ftell(ref_in);
db->name = NULL;
VERB_FILTER(VERBOSITY_ANNOYING, INFO__("pre-loading reference sequence chunk name ..."));
/* 1) read first line */
if (!fgets(line, MAX_LINE_LEN, ref_in)) {
ERROR__("Empty first dataline in the reference sequence.\n");
return RETURN_INPUT_ERR;
}
/* 2) fasta or not fasta ... */
if (get_input_line_type(line) == LINE_TYPE_SEQ_HEADER) {
/* 2.1) if fasta header skip (but keep the name of) the fasta header... */
int name_len = strcspn(line + 1, " \r\n");
if (name_len > 0) {
SAFE_FAILURE__ALLOC(db->name, (name_len + 1), char);
strncpy(db->name, line + 1, name_len);
if (db->name[0] == '=' || db->name[0] == '*') {
/* Not allowed as first char in SN : arbitrary replace with '-' */
db->name[0] = '-';
}
db->name[name_len] = '\0';
VERB_FILTER(VERBOSITY_ANNOYING, INFO__("fasta chunk name found : \"%s\"",db->name));
i_start = ftell(ref_in);
} else {
ERROR__("Empty fasta header line in the reference sequence.\n");
return RETURN_INPUT_ERR;
}
} else {
/* 2.2) if full ascii sequence, come back to the beginning of it */
WARNING__("No fasta header line in the reference sequence.\n");
fseek(ref_in, i_start, SEEK_SET);
}
VERB_FILTER(VERBOSITY_ANNOYING, INFO__("pre-loading reference sequence chunk ..."));
/* 3) read nucleotide data to measure its size */
int c;
long size = 0;
while ((c = (IGNORE_LOWERCASE ? fgetc(ref_in) : toupper(fgetc(ref_in)))) != EOF) {
if (isspace(c)) {
continue;
}
if (c == SEQ_HEADER_MARKER) {
/* started to read the next sequence; getting out */
fseek(ref_in, -1, SEEK_CUR);
break;
}
size++;
}
if (size == 0) {
ERROR__("Empty fasta chunk line in the reference sequence.\n");
return RETURN_INPUT_ERR;
}
VERB_FILTER(VERBOSITY_ANNOYING, INFO__("estimated chunk size : %ld, allocating ...",size));
/* 3.2) Allocate the estimated size (>= true size) for sequence and sequence_masked */
size_t size_shift = COMPRESSED_LEN_N_BYTES(readlen+allowed_indels,N_BYTES);
size_t size_allocated = 2*size_shift + COMPRESSED_LEN_N_BYTES(size,N_BYTES);
SAFE_FAILURE__ALLOC(db->sequence_alloc, size_allocated, CODE_TYPE);
memset(db->sequence_alloc, '\0', size_allocated);
db->sequence = db->sequence_alloc + size_shift;
SAFE_FAILURE__ALLOC(db->sequence_masked_alloc, size_allocated, CODE_TYPE);
memset(db->sequence_masked_alloc, '\0', size_allocated);
/* begin masked */
memset(db->sequence_masked_alloc, '\x55', size_shift);
/* (x) ... end will be masked once the precise size is known */
db->sequence_masked = db->sequence_masked_alloc + size_shift;
VERB_FILTER(VERBOSITY_ANNOYING, INFO__("loading reference sequence chunk ..."));
/* 3.3) Go back to first base and load the nucleotides sequence */
fseek(ref_in, i_start, SEEK_SET);
long i = 0;
#ifndef NUCLEOTIDES
CODE_TYPE code_prev = 0;
#endif
long masked_region_start = -1;
MaskedRegionType* crt_masked_region = db->masked_regions;
while ((c = (IGNORE_LOWERCASE ? fgetc(ref_in) : toupper(fgetc(ref_in)))) != EOF) {
CODE_TYPE code = 0;
if (isspace(c)) {
continue;
}
if (c == SEQ_HEADER_MARKER) {
/* started to read the next sequence; getting out */
fseek(ref_in, -1, SEEK_CUR);
break;
}
if (!IS_VALID_BASE_SYMBOL(c)) {
#ifndef NUCLEOTIDES
code = code_prev; /* masked regions, eg 'NNNNNNNNN' cannot be coded; instead, GNNNNNNNN is transformed in GGGGGGGGG */
#else
code = rand()&0x3; /* random to avoid too much false alignments in the SIMD filter ... */
#endif
/* does a masked region start here ? */
if (masked_region_start == -1) {
masked_region_start = i;
}
TO_NTH_CODE(db->sequence_masked, i, 0x1);
} else {
code = BASE_SYMBOL_TO_CODE(c);
/* does a masked region ends here ? */
if (masked_region_start != -1) {
if (crt_masked_region == NULL) {
/* this is the first one */
SAFE_FAILURE__ALLOC(crt_masked_region, 1, MaskedRegionType);
db->masked_regions = crt_masked_region;
} else {
SAFE_FAILURE__ALLOC(crt_masked_region->next, 1, MaskedRegionType);
crt_masked_region = crt_masked_region->next;
}
crt_masked_region->next = NULL;
crt_masked_region->start = masked_region_start;
crt_masked_region->end = i;
VERB_FILTER(VERBOSITY_HIGH, WARNING__("(%9d-%9d) region masked for reference sequence chunk \"%s\"", crt_masked_region->start+1, crt_masked_region->end, db->name?db->name:"(null)"););
masked_region_start = -1;
}
}
#ifdef NUCLEOTIDES
TO_NTH_CODE(db->sequence, i, code);
i++;
#else
if (db->first_base == 0xff) {
code_prev = db->first_base = code;
} else {
TO_NTH_CODE(db->sequence, i, COLOR(code_prev, code));
i++;
code_prev = code;
}
#endif
}
/* does a masked region ends here ? */
if (masked_region_start != -1) {
if (crt_masked_region == NULL) {
/* this is the first one */
SAFE_FAILURE__ALLOC(crt_masked_region, 1, MaskedRegionType);
db->masked_regions = crt_masked_region;
} else {
SAFE_FAILURE__ALLOC(crt_masked_region->next, 1, MaskedRegionType);
crt_masked_region = crt_masked_region->next;
}
crt_masked_region->next = NULL;
crt_masked_region->start = masked_region_start;
crt_masked_region->end = i;
VERB_FILTER(VERBOSITY_HIGH, WARNING__("(%9d-%9d) region masked for reference sequence chunk \"%s\"", crt_masked_region->start+1, crt_masked_region->end, db->name?db->name:"(null)"););
masked_region_start = -1;
}
db->size = i;
VERB_FILTER(VERBOSITY_ANNOYING, INFO__("true chunk size : %d",db->size));
/* (x) end mask here with the last byte, then size_shift */
while (COMPRESSED_OFFSET(i)) {
TO_NTH_CODE(db->sequence_masked, i, 0x1);
i++;
}
memset(db->sequence_masked+COMPRESSED_IDX(i), '\x55', size_shift);
return db->size;
}
/**
* Load reference sequence
*/
int load_reference_db(const char* reference_filename, int readlen, ReferenceDBType** db) {
char line[MAX_LINE_LEN];
FILE* ref_in = fopen(reference_filename, "r");
int i=0;
int sequence_count = 0;
long long int full_size = 0;
if (!ref_in) {
ERROR__("Failed to read input file %s (reference sequence).\n", reference_filename);
exit(RETURN_INPUT_ERR);
}
// browse the file once
/* how many sequences are there? */
/* Expected format: multifasta (one 'header' line, followed by the sequence) */
// So count headers...
VERB_FILTER(VERBOSITY_ANNOYING, INFO__("pre-loading reference sequence \"%s\"", reference_filename));
while (fgets(line, MAX_LINE_LEN, ref_in)) {
if (get_input_line_type(line) == LINE_TYPE_SEQ_HEADER) {
++sequence_count;
}
}
VERB_FILTER(VERBOSITY_ANNOYING, INFO__("reference sequence \"%s\" : #%d chunks", reference_filename, sequence_count));
if (sequence_count == 0) {
/* no header in the reference ... lets try without */
sequence_count = 1;
}
/* Allocate */
SAFE_FAILURE__ALLOC(*db, sequence_count, ReferenceDBType);
/* Go back to the beginning and start loading */
fseek (ref_in, 0, SEEK_SET);
for (i = 0; i < sequence_count; ++i) {
int size;
(*db)[i].masked_regions = NULL;
(*db)[i].name = NULL;
(*db)[i].size = 0;
(*db)[i].sequence_alloc = NULL;
(*db)[i].sequence = NULL;
(*db)[i].sequence_masked_alloc = NULL;
(*db)[i].sequence_masked = NULL;
#ifndef NUCLEOTIDES
(*db)[i].first_base = 0xff;
#endif
VERB_FILTER(VERBOSITY_ANNOYING, INFO__("reference sequence \"%s\" : loading chunk %d/%d ...", reference_filename, i+1,sequence_count));
if ((size = load_reference_sequence(ref_in, readlen, &((*db)[i]))) <= 0) {
/* something strange happened, stop loading and return what we have */
sequence_count = i;
WARNING__("problem when loading chunk %d for reference sequence \"%s\"", i+1, reference_filename);
break;
}
full_size += size;
}
fclose(ref_in);
VERB_FILTER(VERBOSITY_HIGH, INFO__("reference sequence \"%s\" : #%d chunks (full size:%lld)", reference_filename, sequence_count, full_size));
return sequence_count;
}
/**
* Destroys a reference database
*/
void clear_reference_db(ReferenceDBType* db) {
while (db->masked_regions != NULL) {
MaskedRegionType* tmp = db->masked_regions;
db->masked_regions = db->masked_regions->next;
free(tmp);
}
if (db->name) {
free(db->name);
db->name = NULL;
}
free(db->sequence_alloc);
db->sequence = db->sequence_alloc = NULL;
free(db->sequence_masked_alloc);
db->sequence_masked = db->sequence_masked_alloc = NULL;
}