forked from andrelmartins/TwoBit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twobit.c
379 lines (302 loc) · 7.45 KB
/
twobit.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "twobit.h"
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
/* 2bit file format http://genome.ucsc.edu/FAQ/FAQformat#format7 */
typedef unsigned int uint32;
struct twobit_index {
struct twobit_index * next;
char * name;
int size;
int n_blocks;
uint32 * n_block_starts;
uint32 * n_block_sizes;
const unsigned char * sequence;
};
struct twobit_ {
int fd;
struct stat sb;
const char * data; /* MMAP pointer */
struct twobit_index * index;
};
/* returns sequence count if valid, -1 otherwise */
int validate_header(const char * data) {
const uint32 * ptr = (const uint32*) data;
int sequenceCount;
/* signature */
if (*ptr != 0x1A412743) {
fprintf(stderr, "Invalid signature or wrong architecture.\n");
return -1;
}
++ptr;
/* version */
if (*ptr != 0) {
fprintf(stderr, "Unknown file version: %d.\n", *ptr);
return -1;
}
++ptr;
/* sequence count */
sequenceCount = *ptr;
++ptr;
/* reserved bytes */
if (*ptr != 0) {
fprintf(stderr, "Reserved bytes not zero: %d.\n", *ptr);
return -1;
}
return sequenceCount;
}
static struct twobit_index * tbi_push(struct twobit_index * head, int size, const char * ptr, const char * data, uint32 offset) {
struct twobit_index * res = (struct twobit_index*) malloc(sizeof(struct twobit_index));
uint32 * rec;
uint32 aux;
res->next = head;
res->name = strndup(ptr, size * sizeof(char));
/* read record start */
rec = (uint32*) (data + offset);
/* size */
res->size = *rec;
++rec;
/* blocks of Ns */
res->n_blocks = *rec;
++rec;
if (res->n_blocks > 0) {
res->n_block_starts = rec;
rec += res->n_blocks;
res->n_block_sizes = rec;
rec += res->n_blocks;
}
/* skip masks */
aux = *rec;
rec += 2*aux + 1;
/* skip reserved */
++rec;
res->sequence = (unsigned char *) rec;
return res;
}
static struct twobit_index * read_index(const char * data, int sequenceCount) {
/* index has:
unsigned char nameSize;
char * name;
uint32 offset;
*/
unsigned char nameSize;
uint32 offset;
struct twobit_index * result = NULL;
const char * start_file = data;
/* skip header */
data += sizeof(uint32)*4; /* header has 4 uint32 entries */
/* read index entries */
for (; sequenceCount > 0; --sequenceCount) {
const char * name;
nameSize = *data;
++data;
name = data;
data += nameSize;
offset = *((uint32*) data);
data += sizeof(uint32);
/* build entry */
result = tbi_push(result, (int) nameSize, name, start_file, offset);
}
return result;
}
void free_index(struct twobit_index * index) {
struct twobit_index * ptr;
while (index != NULL) {
ptr = index->next;
free(index->name);
free(index);
index = ptr;
}
}
TwoBit * twobit_open(const char * filename) {
int fd;
struct stat sb;
char * data;
int sequenceCount;
TwoBit * result;
/* open file */
fd = open(filename, O_RDONLY);
if (fd == -1) {
perror("twobit_open");
return NULL;
}
/* get file size */
if (fstat(fd, &sb) == -1) { /* To obtain file size */
perror("fstat failed");
return NULL;
}
/* mmap file */
data = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (data == MAP_FAILED) {
perror("mmap failed");
return NULL;
}
if (madvise(data, sb.st_size, MADV_RANDOM) == -1) {
perror("advise failed");
/* no need to exit */
}
/* validate header */
if ((sequenceCount = validate_header(data)) < 0) {
munmap((void*)data, sb.st_size);
close(fd);
return NULL;
}
/* fill structure */
result = (TwoBit*) malloc(sizeof(TwoBit));
result->fd = fd;
result->sb = sb;
result->data = data;
result->index = read_index(data, sequenceCount);
return result;
}
void twobit_close(TwoBit * ptr) {
munmap((void*)ptr->data, ptr->sb.st_size);
free_index(ptr->index);
close(ptr->fd);
free(ptr);
}
static struct twobit_index * find_sequence(struct twobit_index * index, const char * name) {
while (index != NULL) {
if (!strcmp(index->name, name))
return index;
index = index->next;
}
return NULL;
}
int twobit_sequence_size(TwoBit * ptr, const char * name) {
struct twobit_index * seq = find_sequence(ptr->index, name);
if (!seq)
return -1;
return seq->size;
}
/* byte has three 2 bit bases, offset can be 0, 1, 2 or 3 */
char byte_to_base(unsigned char byte, int offset) {
int rev_offset = 3 - offset;
unsigned char mask = 3 << (rev_offset * 2);
int idx = (byte & mask) >> (rev_offset * 2);
char * bases = "TCAG";
return bases[idx];
}
char * twobit_sequence(TwoBit * ptr, const char * name, int start, int end) {
struct twobit_index * seq = find_sequence(ptr->index, name);
int size, rsize;
char * result, * seq_dest;
int i;
if (!seq)
return NULL;
if (start > end)
return NULL;
size = seq->size;
rsize = end - start + 1;
seq_dest = result = (char*) malloc((rsize + 1) * sizeof(char));
if(result == NULL) return NULL;
memset(result, 'N', rsize * sizeof(char)); /* initialize */
result[rsize] = '\0';
if (end >= size) {
end = size - 1;
rsize = end - start + 1;
}
if (start < 0)
{
rsize += start;
seq_dest -= start;
start = 0;
}
if (end < 0)
{
rsize = 0;
seq_dest = result;
end = 0;
};
/* fill sequence */
{
const unsigned char * block;
int first_block = start / 4;
int offset;
block = seq->sequence + first_block;
offset = start % 4;
i = 0;
while (i < rsize) {
seq_dest[i] = byte_to_base(*block, offset);
++i;
++offset;
if (offset == 4) {
offset = 0;
++block;
}
}
}
/* fill in Ns */
for (i = 0; i < seq->n_blocks; ++i) {
uint32 bstart = seq->n_block_starts[i];
uint32 bsize = seq->n_block_sizes[i];
uint32 bend = bstart + bsize - 1;
if (bstart <= end && bend >= start) {
int j, k;
if (bstart < start) {
bsize -= (start - bstart);
bstart = start;
}
for (j = 0, k = bstart; j < bsize && k <= end; ++j, ++k)
seq_dest[k - start] = 'N';
}
}
return result;
}
double * twobit_base_frequencies(TwoBit * ptr, const char * name) {
struct twobit_index * seq = find_sequence(ptr->index, name);
int size;
double * result;
int i;
if (!seq)
return NULL;
size = seq->size;
result = (double*) calloc(4, sizeof(double));
/* sum counts */
{
const unsigned char * block;
int offset;
block = seq->sequence;
offset = 0;
i = 0;
while (i < size) {
char base = byte_to_base(*block, offset);
switch (base) { /* remap alphabet */
case 'A': ++result[0]; break;
case 'C': ++result[1]; break;
case 'G': ++result[2]; break;
case 'T': ++result[3]; break;
}
++i;
++offset;
if (offset == 4) {
offset = 0;
++block;
}
}
}
/* turn counts to frequencies */
for (i = 0; i < 4; ++i)
result[i] = result[i] / size;
return result;
}
char ** twobit_sequence_names(TwoBit * ptr) {
char ** result;
int n_sequences = 0;
struct twobit_index * idx;
int i;
for (idx = ptr->index; idx != NULL; idx = idx->next)
++n_sequences;
result = (char**) calloc(n_sequences + 1, sizeof(char*));
for (i = 0, idx = ptr->index; idx != NULL; ++i, idx = idx->next) {
char * name = strdup(idx->name);
result[i] = name;
}
return result;
}