annotate pyPRADA_1.2/tools/bwa-0.5.7-mh/bwtio.c @ 3:f17965495ec9 draft default tip

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