comparison pyPRADA_1.2/tools/bwa-0.5.7-mh/bwtio.c @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:acc2ca1a3ba4
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "bwt.h"
5 #include "utils.h"
6
7 void bwt_dump_bwt(const char *fn, const bwt_t *bwt)
8 {
9 FILE *fp;
10 fp = xopen(fn, "wb");
11 fwrite(&bwt->primary, sizeof(bwtint_t), 1, fp);
12 fwrite(bwt->L2+1, sizeof(bwtint_t), 4, fp);
13 fwrite(bwt->bwt, sizeof(bwtint_t), bwt->bwt_size, fp);
14 fclose(fp);
15 }
16
17 void bwt_dump_sa(const char *fn, const bwt_t *bwt)
18 {
19 FILE *fp;
20 fp = xopen(fn, "wb");
21 fwrite(&bwt->primary, sizeof(bwtint_t), 1, fp);
22 fwrite(bwt->L2+1, sizeof(bwtint_t), 4, fp);
23 fwrite(&bwt->sa_intv, sizeof(bwtint_t), 1, fp);
24 fwrite(&bwt->seq_len, sizeof(bwtint_t), 1, fp);
25 fwrite(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp);
26 fclose(fp);
27 }
28
29 void bwt_restore_sa(const char *fn, bwt_t *bwt)
30 {
31 char skipped[256];
32 FILE *fp;
33 bwtint_t primary;
34
35 fp = xopen(fn, "rb");
36 fread(&primary, sizeof(bwtint_t), 1, fp);
37 xassert(primary == bwt->primary, "SA-BWT inconsistency: primary is not the same.");
38 fread(skipped, sizeof(bwtint_t), 4, fp); // skip
39 fread(&bwt->sa_intv, sizeof(bwtint_t), 1, fp);
40 fread(&primary, sizeof(bwtint_t), 1, fp);
41 xassert(primary == bwt->seq_len, "SA-BWT inconsistency: seq_len is not the same.");
42
43 bwt->n_sa = (bwt->seq_len + bwt->sa_intv) / bwt->sa_intv;
44 bwt->sa = (bwtint_t*)calloc(bwt->n_sa, sizeof(bwtint_t));
45 bwt->sa[0] = -1;
46
47 fread(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp);
48 fclose(fp);
49 }
50
51 bwt_t *bwt_restore_bwt(const char *fn)
52 {
53 bwt_t *bwt;
54 FILE *fp;
55
56 bwt = (bwt_t*)calloc(1, sizeof(bwt_t));
57 fp = xopen(fn, "rb");
58 fseek(fp, 0, SEEK_END);
59 bwt->bwt_size = (ftell(fp) - sizeof(bwtint_t) * 5) >> 2;
60 bwt->bwt = (uint32_t*)calloc(bwt->bwt_size, 4);
61 fseek(fp, 0, SEEK_SET);
62 fread(&bwt->primary, sizeof(bwtint_t), 1, fp);
63 fread(bwt->L2+1, sizeof(bwtint_t), 4, fp);
64 fread(bwt->bwt, 4, bwt->bwt_size, fp);
65 bwt->seq_len = bwt->L2[4];
66 fclose(fp);
67 bwt_gen_cnt_table(bwt);
68
69 return bwt;
70 }
71
72 void bwt_destroy(bwt_t *bwt)
73 {
74 if (bwt == 0) return;
75 free(bwt->sa); free(bwt->bwt);
76 free(bwt);
77 }