annotate pyPRADA_1.2/tools/bwa-0.5.7-mh/bwtmisc.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 /* The MIT License
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 Copyright (c) 2008 Genome Research Ltd (GRL).
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 a copy of this software and associated documentation files (the
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 "Software"), to deal in the Software without restriction, including
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 without limitation the rights to use, copy, modify, merge, publish,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 distribute, sublicense, and/or sell copies of the Software, and to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 permit persons to whom the Software is furnished to do so, subject to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 the following conditions:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 The above copyright notice and this permission notice shall be
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 included in all copies or substantial portions of the Software.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 SOFTWARE.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 /* Contact: Heng Li <lh3@sanger.ac.uk> */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 #include <stdio.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 #include <unistd.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 #include "bntseq.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 #include "utils.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 #include "main.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 #include "bwt.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 #ifdef _DIVBWT
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 #include "divsufsort.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 int is_bwt(ubyte_t *T, int n);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 int64_t bwa_seq_len(const char *fn_pac)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 FILE *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 int64_t pac_len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 ubyte_t c;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 fp = xopen(fn_pac, "rb");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 fseek(fp, -1, SEEK_END);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 pac_len = ftell(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 fread(&c, 1, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 fclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 return (pac_len - 1) * 4 + (int)c;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 bwt_t *bwt_pac2bwt(const char *fn_pac, int use_is)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 bwt_t *bwt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 ubyte_t *buf, *buf2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 int i, pac_size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 FILE *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 // initialization
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 bwt = (bwt_t*)calloc(1, sizeof(bwt_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 bwt->seq_len = bwa_seq_len(fn_pac);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 bwt->bwt_size = (bwt->seq_len + 15) >> 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 fp = xopen(fn_pac, "rb");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 // prepare sequence
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 pac_size = (bwt->seq_len>>2) + ((bwt->seq_len&3) == 0? 0 : 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 buf2 = (ubyte_t*)calloc(pac_size, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 fread(buf2, 1, pac_size, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 fclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 memset(bwt->L2, 0, 5 * 4);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 buf = (ubyte_t*)calloc(bwt->seq_len + 1, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 for (i = 0; i < bwt->seq_len; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 buf[i] = buf2[i>>2] >> ((3 - (i&3)) << 1) & 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 ++bwt->L2[1+buf[i]];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 for (i = 2; i <= 4; ++i) bwt->L2[i] += bwt->L2[i-1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 free(buf2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 // Burrows-Wheeler Transform
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 if (use_is) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 bwt->primary = is_bwt(buf, bwt->seq_len);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 #ifdef _DIVBWT
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 bwt->primary = divbwt(buf, buf, 0, bwt->seq_len);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 err_fatal_simple("libdivsufsort is not compiled in.");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 bwt->bwt = (u_int32_t*)calloc(bwt->bwt_size, 4);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 for (i = 0; i < bwt->seq_len; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 bwt->bwt[i>>4] |= buf[i] << ((15 - (i&15)) << 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 free(buf);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 return bwt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 int bwa_pac2bwt(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 bwt_t *bwt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 int c, use_is = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 while ((c = getopt(argc, argv, "d")) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 switch (c) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 case 'd': use_is = 0; break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 default: return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 if (optind + 2 > argc) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 fprintf(stderr, "Usage: bwa pac2bwt [-d] <in.pac> <out.bwt>\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 bwt = bwt_pac2bwt(argv[optind], use_is);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 bwt_dump_bwt(argv[optind+1], bwt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 bwt_destroy(bwt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 #define bwt_B00(b, k) ((b)->bwt[(k)>>4]>>((~(k)&0xf)<<1)&3)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 void bwt_bwtupdate_core(bwt_t *bwt)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 bwtint_t i, k, c[4], n_occ;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 uint32_t *buf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 n_occ = (bwt->seq_len + OCC_INTERVAL - 1) / OCC_INTERVAL + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 bwt->bwt_size += n_occ * 4; // the new size
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 buf = (uint32_t*)calloc(bwt->bwt_size, 4); // will be the new bwt
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 c[0] = c[1] = c[2] = c[3] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 for (i = k = 0; i < bwt->seq_len; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 if (i % OCC_INTERVAL == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 memcpy(buf + k, c, sizeof(bwtint_t) * 4);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 k += 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 if (i % 16 == 0) buf[k++] = bwt->bwt[i/16];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 ++c[bwt_B00(bwt, i)];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 // the last element
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 memcpy(buf + k, c, sizeof(bwtint_t) * 4);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 xassert(k + 4 == bwt->bwt_size, "inconsistent bwt_size");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 // update bwt
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 free(bwt->bwt); bwt->bwt = buf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 int bwa_bwtupdate(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 bwt_t *bwt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 if (argc < 2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 fprintf(stderr, "Usage: bwa bwtupdate <the.bwt>\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 bwt = bwt_restore_bwt(argv[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 bwt_bwtupdate_core(bwt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 bwt_dump_bwt(argv[1], bwt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 bwt_destroy(bwt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 void bwa_pac_rev_core(const char *fn, const char *fn_rev)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 int64_t seq_len, i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 bwtint_t pac_len, j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 ubyte_t *bufin, *bufout, ct;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 FILE *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166 seq_len = bwa_seq_len(fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 pac_len = (seq_len >> 2) + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 bufin = (ubyte_t*)calloc(pac_len, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 bufout = (ubyte_t*)calloc(pac_len, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 fp = xopen(fn, "rb");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 fread(bufin, 1, pac_len, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 fclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 for (i = seq_len - 1, j = 0; i >= 0; --i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174 int c = bufin[i>>2] >> ((~i&3)<<1) & 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 bwtint_t j = seq_len - 1 - i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 bufout[j>>2] |= c << ((~j&3)<<1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 free(bufin);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 fp = xopen(fn_rev, "wb");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
180 fwrite(bufout, 1, pac_len, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
181 ct = seq_len % 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
182 fwrite(&ct, 1, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
183 fclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
184 free(bufout);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
185 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
186
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
187 int bwa_pac_rev(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
188 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
189 if (argc < 3) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
190 fprintf(stderr, "Usage: bwa pac_rev <in.pac> <out.pac>\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
191 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
192 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
193 bwa_pac_rev_core(argv[1], argv[2]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
194 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
195 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
196
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
197 const int nst_color_space_table[] = { 4, 0, 0, 1, 0, 2, 3, 4, 0, 3, 2, 4, 1, 4, 4, 4};
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
198
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
199 /* this function is not memory efficient, but this will make life easier
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
200 Ideally we should also change .amb files as one 'N' in the nucleotide
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
201 sequence leads to two ambiguous colors. I may do this later... */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
202 uint8_t *bwa_pac2cspac_core(const bntseq_t *bns)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
203 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
204 uint8_t *pac, *cspac;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
205 bwtint_t i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
206 int c1, c2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
207 pac = (uint8_t*)calloc(bns->l_pac/4 + 1, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
208 cspac = (uint8_t*)calloc(bns->l_pac/4 + 1, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
209 fread(pac, 1, bns->l_pac/4+1, bns->fp_pac);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
210 rewind(bns->fp_pac);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
211 c1 = pac[0]>>6; cspac[0] = c1<<6;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
212 for (i = 1; i < bns->l_pac; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
213 c2 = pac[i>>2] >> (~i&3)*2 & 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
214 cspac[i>>2] |= nst_color_space_table[(1<<c1)|(1<<c2)] << (~i&3)*2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
215 c1 = c2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
216 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
217 free(pac);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
218 return cspac;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
219 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
220
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
221 int bwa_pac2cspac(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
222 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
223 bntseq_t *bns;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
224 uint8_t *cspac, ct;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
225 char *str;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
226 FILE *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
227
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
228 if (argc < 3) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
229 fprintf(stderr, "Usage: bwa pac2cspac <in.nt.prefix> <out.cs.prefix>\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
230 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
231 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
232 bns = bns_restore(argv[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
233 cspac = bwa_pac2cspac_core(bns);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
234 bns_dump(bns, argv[2]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
235 // now write cspac
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
236 str = (char*)calloc(strlen(argv[2]) + 5, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
237 strcat(strcpy(str, argv[2]), ".pac");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
238 fp = xopen(str, "wb");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
239 fwrite(cspac, 1, bns->l_pac/4 + 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
240 ct = bns->l_pac % 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
241 fwrite(&ct, 1, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
242 fclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
243 bns_destroy(bns);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
244 free(cspac);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
245 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
246 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
247
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
248 int bwa_bwt2sa(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
249 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
250 bwt_t *bwt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
251 int c, sa_intv = 32;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
252 while ((c = getopt(argc, argv, "i:")) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
253 switch (c) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
254 case 'i': sa_intv = atoi(optarg); break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
255 default: return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
256 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
257 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
258 if (optind + 2 > argc) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
259 fprintf(stderr, "Usage: bwa bwt2sa [-i %d] <in.bwt> <out.sa>\n", sa_intv);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
260 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
261 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
262 bwt = bwt_restore_bwt(argv[optind]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
263 bwt_cal_sa(bwt, sa_intv);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
264 bwt_dump_sa(argv[optind+1], bwt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
265 bwt_destroy(bwt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
266 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
267 }