Debian Codegraph @

bwa-apache2/fastmap.c

raw
1 /* The MIT License
2
3 Copyright (c) 2018- Dana-Farber Cancer Institute
4 2009-2018 Broad Institute, Inc.
5 2008-2009 Genome Research Ltd. (GRL)
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26 */
27 #include <zlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <ctype.h>
34 #include <math.h>
35 #include "bwa.h"
36 #include "bwamem.h"
37 #include "kvec.h"
38 #include "utils.h"
39 #include "bntseq.h"
40 #include "kseq.h"
41 KSEQ_DECLARE(gzFile)
42
43 extern unsigned char nst_nt4_table[256];
44
45 void *kopen(const char *fn, int *_fd);
46 int kclose(void *a);
47 void kt_pipeline(int n_threads, void *(*func)(void*, int, void*), void *shared_data, int n_steps);
48
49 typedef struct {
50 kseq_t *ks, *ks2;
51 mem_opt_t *opt;
52 mem_pestat_t *pes0;
53 int64_t n_processed;
54 int copy_comment, actual_chunk_size;
55 bwaidx_t *idx;
56 } ktp_aux_t;
57
58 typedef struct {
59 ktp_aux_t *aux;
60 int n_seqs;
61 bseq1_t *seqs;
62 } ktp_data_t;
63
64 static void *process(void *shared, int step, void *_data)
65 {
66 ktp_aux_t *aux = (ktp_aux_t*)shared;
67 ktp_data_t *data = (ktp_data_t*)_data;
68 int i;
69 if (step == 0) {
70 ktp_data_t *ret;
71 int64_t size = 0;
72 ret = calloc(1, sizeof(ktp_data_t));
73 ret->seqs = bseq_read(aux->actual_chunk_size, &ret->n_seqs, aux->ks, aux->ks2);
74 if (ret->seqs == 0) {
75 free(ret);
76 return 0;
77 }
78 if (!aux->copy_comment)
79 for (i = 0; i < ret->n_seqs; ++i) {
80 free(ret->seqs[i].comment);
81 ret->seqs[i].comment = 0;
82 }
83 for (i = 0; i < ret->n_seqs; ++i) size += ret->seqs[i].l_seq;
84 if (bwa_verbose >= 3)
85 fprintf(stderr, "[M::%s] read %d sequences (%ld bp)...\n", __func__, ret->n_seqs, (long)size);
86 return ret;
87 } else if (step == 1) {
88 const mem_opt_t *opt = aux->opt;
89 const bwaidx_t *idx = aux->idx;
90 if (opt->flag & MEM_F_SMARTPE) {
91 bseq1_t *sep[2];
92 int n_sep[2];
93 mem_opt_t tmp_opt = *opt;
94 bseq_classify(data->n_seqs, data->seqs, n_sep, sep);
95 if (bwa_verbose >= 3)
96 fprintf(stderr, "[M::%s] %d single-end sequences; %d paired-end sequences\n", __func__, n_sep[0], n_sep[1]);
97 if (n_sep[0]) {
98 tmp_opt.flag &= ~MEM_F_PE;
99 mem_process_seqs(&tmp_opt, idx->bwt, idx->bns, idx->pac, aux->n_processed, n_sep[0], sep[0], 0);
100 for (i = 0; i < n_sep[0]; ++i)
101 data->seqs[sep[0][i].id].sam = sep[0][i].sam;
102 }
103 if (n_sep[1]) {
104 tmp_opt.flag |= MEM_F_PE;
105 mem_process_seqs(&tmp_opt, idx->bwt, idx->bns, idx->pac, aux->n_processed + n_sep[0], n_sep[1], sep[1], aux->pes0);
106 for (i = 0; i < n_sep[1]; ++i)
107 data->seqs[sep[1][i].id].sam = sep[1][i].sam;
108 }
109 free(sep[0]); free(sep[1]);
110 } else mem_process_seqs(opt, idx->bwt, idx->bns, idx->pac, aux->n_processed, data->n_seqs, data->seqs, aux->pes0);
111 aux->n_processed += data->n_seqs;
112 return data;
113 } else if (step == 2) {
114 for (i = 0; i < data->n_seqs; ++i) {
115 if (data->seqs[i].sam) err_fputs(data->seqs[i].sam, stdout);
116 free(data->seqs[i].name); free(data->seqs[i].comment);
117 free(data->seqs[i].seq); free(data->seqs[i].qual); free(data->seqs[i].sam);
118 }
119 free(data->seqs); free(data);
120 return 0;
121 }
122 return 0;
123 }
124
125 static void update_a(mem_opt_t *opt, const mem_opt_t *opt0)
126 {
127 if (opt0->a) { // matching score is changed
128 if (!opt0->b) opt->b *= opt->a;
129 if (!opt0->T) opt->T *= opt->a;
130 if (!opt0->o_del) opt->o_del *= opt->a;
131 if (!opt0->e_del) opt->e_del *= opt->a;
132 if (!opt0->o_ins) opt->o_ins *= opt->a;
133 if (!opt0->e_ins) opt->e_ins *= opt->a;
134 if (!opt0->zdrop) opt->zdrop *= opt->a;
135 if (!opt0->pen_clip5) opt->pen_clip5 *= opt->a;
136 if (!opt0->pen_clip3) opt->pen_clip3 *= opt->a;
137 if (!opt0->pen_unpaired) opt->pen_unpaired *= opt->a;
138 }
139 }
140
141 int main_mem(int argc, char *argv[])
142 {
143 mem_opt_t *opt, opt0;
144 int fd, fd2, i, c, ignore_alt = 0, no_mt_io = 0;
145 int fixed_chunk_size = -1;
146 gzFile fp, fp2 = 0;
147 char *p, *rg_line = 0, *hdr_line = 0;
148 const char *mode = 0;
149 void *ko = 0, *ko2 = 0;
150 mem_pestat_t pes[4];
151 ktp_aux_t aux;
152
153 memset(&aux, 0, sizeof(ktp_aux_t));
154 memset(pes, 0, 4 * sizeof(mem_pestat_t));
155 for (i = 0; i < 4; ++i) pes[i].failed = 1;
156
157 aux.opt = opt = mem_opt_init();
158 memset(&opt0, 0, sizeof(mem_opt_t));
159 while ((c = getopt(argc, argv, "51qpaMCSPVYjuk:c:v:s:r:t:R:A:B:O:E:U:w:L:d:T:Q:D:m:I:N:o:f:W:x:G:h:y:K:X:H:F:z:")) >= 0) {
160 if (c == 'k') opt->min_seed_len = atoi(optarg), opt0.min_seed_len = 1;
161 else if (c == '1') no_mt_io = 1;
162 else if (c == 'x') mode = optarg;
163 else if (c == 'w') opt->w = atoi(optarg), opt0.w = 1;
164 else if (c == 'A') opt->a = atoi(optarg), opt0.a = 1;
165 else if (c == 'B') opt->b = atoi(optarg), opt0.b = 1;
166 else if (c == 'T') opt->T = atoi(optarg), opt0.T = 1;
167 else if (c == 'U') opt->pen_unpaired = atoi(optarg), opt0.pen_unpaired = 1;
168 else if (c == 't') opt->n_threads = atoi(optarg), opt->n_threads = opt->n_threads > 1? opt->n_threads : 1;
169 else if (c == 'P') opt->flag |= MEM_F_NOPAIRING;
170 else if (c == 'a') opt->flag |= MEM_F_ALL;
171 else if (c == 'p') opt->flag |= MEM_F_PE | MEM_F_SMARTPE;
172 else if (c == 'M') opt->flag |= MEM_F_NO_MULTI;
173 else if (c == 'S') opt->flag |= MEM_F_NO_RESCUE;
174 else if (c == 'Y') opt->flag |= MEM_F_SOFTCLIP;
175 else if (c == 'V') opt->flag |= MEM_F_REF_HDR;
176 else if (c == '5') opt->flag |= MEM_F_PRIMARY5 | MEM_F_KEEP_SUPP_MAPQ; // always apply MEM_F_KEEP_SUPP_MAPQ with -5
177 else if (c == 'q') opt->flag |= MEM_F_KEEP_SUPP_MAPQ;
178 else if (c == 'u') opt->flag |= MEM_F_XB;
179 else if (c == 'c') opt->max_occ = atoi(optarg), opt0.max_occ = 1;
180 else if (c == 'd') opt->zdrop = atoi(optarg), opt0.zdrop = 1;
181 else if (c == 'v') bwa_verbose = atoi(optarg);
182 else if (c == 'j') ignore_alt = 1;
183 else if (c == 'r') opt->split_factor = atof(optarg), opt0.split_factor = 1.;
184 else if (c == 'D') opt->drop_ratio = atof(optarg), opt0.drop_ratio = 1.;
185 else if (c == 'm') opt->max_matesw = atoi(optarg), opt0.max_matesw = 1;
186 else if (c == 's') opt->split_width = atoi(optarg), opt0.split_width = 1;
187 else if (c == 'G') opt->max_chain_gap = atoi(optarg), opt0.max_chain_gap = 1;
188 else if (c == 'N') opt->max_chain_extend = atoi(optarg), opt0.max_chain_extend = 1;
189 else if (c == 'o' || c == 'f') xreopen(optarg, "wb", stdout);
190 else if (c == 'W') opt->min_chain_weight = atoi(optarg), opt0.min_chain_weight = 1;
191 else if (c == 'y') opt->max_mem_intv = atol(optarg), opt0.max_mem_intv = 1;
192 else if (c == 'C') aux.copy_comment = 1;
193 else if (c == 'K') fixed_chunk_size = atoi(optarg);
194 else if (c == 'X') opt->mask_level = atof(optarg);
195 else if (c == 'F') bwa_dbg = atoi(optarg);
196 else if (c == 'h') {
197 opt0.max_XA_hits = opt0.max_XA_hits_alt = 1;
198 opt->max_XA_hits = opt->max_XA_hits_alt = strtol(optarg, &p, 10);
199 if (*p != 0 && ispunct(*p) && isdigit(p[1]))
200 opt->max_XA_hits_alt = strtol(p+1, &p, 10);
201 }
202 else if (c == 'z') opt->XA_drop_ratio = atof(optarg);
203 else if (c == 'Q') {
204 opt0.mapQ_coef_len = 1;
205 opt->mapQ_coef_len = atoi(optarg);
206 opt->mapQ_coef_fac = opt->mapQ_coef_len > 0? log(opt->mapQ_coef_len) : 0;
207 } else if (c == 'O') {
208 opt0.o_del = opt0.o_ins = 1;
209 opt->o_del = opt->o_ins = strtol(optarg, &p, 10);
210 if (*p != 0 && ispunct(*p) && isdigit(p[1]))
211 opt->o_ins = strtol(p+1, &p, 10);
212 } else if (c == 'E') {
213 opt0.e_del = opt0.e_ins = 1;
214 opt->e_del = opt->e_ins = strtol(optarg, &p, 10);
215 if (*p != 0 && ispunct(*p) && isdigit(p[1]))
216 opt->e_ins = strtol(p+1, &p, 10);
217 } else if (c == 'L') {
218 opt0.pen_clip5 = opt0.pen_clip3 = 1;
219 opt->pen_clip5 = opt->pen_clip3 = strtol(optarg, &p, 10);
220 if (*p != 0 && ispunct(*p) && isdigit(p[1]))
221 opt->pen_clip3 = strtol(p+1, &p, 10);
222 } else if (c == 'R') {
223 if ((rg_line = bwa_set_rg(optarg)) == 0) return 1; // FIXME: memory leak
224 } else if (c == 'H') {
225 if (optarg[0] != '@') {
226 FILE *fp;
227 if ((fp = fopen(optarg, "r")) != 0) {
228 char *buf;
229 buf = calloc(1, 0x10000);
230 while (fgets(buf, 0xffff, fp)) {
231 i = strlen(buf);
232 assert(buf[i-1] == '\n'); // a long line
233 buf[i-1] = 0;
234 hdr_line = bwa_insert_header(buf, hdr_line);
235 }
236 free(buf);
237 fclose(fp);
238 }
239 } else hdr_line = bwa_insert_header(optarg, hdr_line);
240 } else if (c == 'I') { // specify the insert size distribution
241 aux.pes0 = pes;
242 pes[1].failed = 0;
243 pes[1].avg = strtod(optarg, &p);
244 pes[1].std = pes[1].avg * .1;
245 if (*p != 0 && ispunct(*p) && isdigit(p[1]))
246 pes[1].std = strtod(p+1, &p);
247 pes[1].high = (int)(pes[1].avg + 4. * pes[1].std + .499);
248 pes[1].low = (int)(pes[1].avg - 4. * pes[1].std + .499);
249 if (pes[1].low < 1) pes[1].low = 1;
250 if (*p != 0 && ispunct(*p) && isdigit(p[1]))
251 pes[1].high = (int)(strtod(p+1, &p) + .499);
252 if (*p != 0 && ispunct(*p) && isdigit(p[1]))
253 pes[1].low = (int)(strtod(p+1, &p) + .499);
254 if (bwa_verbose >= 3)
255 fprintf(stderr, "[M::%s] mean insert size: %.3f, stddev: %.3f, max: %d, min: %d\n",
256 __func__, pes[1].avg, pes[1].std, pes[1].high, pes[1].low);
257 }
258 else return 1;
259 }
260
261 if (rg_line) {
262 hdr_line = bwa_insert_header(rg_line, hdr_line);
263 free(rg_line);
264 }
265
266 if (opt->n_threads < 1) opt->n_threads = 1;
267 if (optind + 1 >= argc || optind + 3 < argc) {
268 fprintf(stderr, "\n");
269 fprintf(stderr, "Usage: bwa mem [options] <idxbase> <in1.fq> [in2.fq]\n\n");
270 fprintf(stderr, "Algorithm options:\n\n");
271 fprintf(stderr, " -t INT number of threads [%d]\n", opt->n_threads);
272 fprintf(stderr, " -k INT minimum seed length [%d]\n", opt->min_seed_len);
273 fprintf(stderr, " -w INT band width for banded alignment [%d]\n", opt->w);
274 fprintf(stderr, " -d INT off-diagonal X-dropoff [%d]\n", opt->zdrop);
275 fprintf(stderr, " -r FLOAT look for internal seeds inside a seed longer than {-k} * FLOAT [%g]\n", opt->split_factor);
276 fprintf(stderr, " -y INT seed occurrence for the 3rd round seeding [%ld]\n", (long)opt->max_mem_intv);
277 // fprintf(stderr, " -s INT look for internal seeds inside a seed with less than INT occ [%d]\n", opt->split_width);
278 fprintf(stderr, " -c INT skip seeds with more than INT occurrences [%d]\n", opt->max_occ);
279 fprintf(stderr, " -D FLOAT drop chains shorter than FLOAT fraction of the longest overlapping chain [%.2f]\n", opt->drop_ratio);
280 fprintf(stderr, " -W INT discard a chain if seeded bases shorter than INT [0]\n");
281 fprintf(stderr, " -m INT perform at most INT rounds of mate rescues for each read [%d]\n", opt->max_matesw);
282 fprintf(stderr, " -S skip mate rescue\n");
283 fprintf(stderr, " -P skip pairing; mate rescue performed unless -S also in use\n");
284 fprintf(stderr, "\nScoring options:\n\n");
285 fprintf(stderr, " -A INT score for a sequence match, which scales options -TdBOELU unless overridden [%d]\n", opt->a);
286 fprintf(stderr, " -B INT penalty for a mismatch [%d]\n", opt->b);
287 fprintf(stderr, " -O INT[,INT] gap open penalties for deletions and insertions [%d,%d]\n", opt->o_del, opt->o_ins);
288 fprintf(stderr, " -E INT[,INT] gap extension penalty; a gap of size k cost '{-O} + {-E}*k' [%d,%d]\n", opt->e_del, opt->e_ins);
289 fprintf(stderr, " -L INT[,INT] penalty for 5'- and 3'-end clipping [%d,%d]\n", opt->pen_clip5, opt->pen_clip3);
290 fprintf(stderr, " -U INT penalty for an unpaired read pair [%d]\n\n", opt->pen_unpaired);
291 fprintf(stderr, " -x STR read type. Setting -x changes multiple parameters unless overridden [null]\n");
292 fprintf(stderr, " pacbio: -k17 -W40 -r10 -A1 -B1 -O1 -E1 -L0 (PacBio reads to ref)\n");
293 fprintf(stderr, " ont2d: -k14 -W20 -r10 -A1 -B1 -O1 -E1 -L0 (Oxford Nanopore 2D-reads to ref)\n");
294 fprintf(stderr, " intractg: -B9 -O16 -L5 (intra-species contigs to ref)\n");
295 fprintf(stderr, "\nInput/output options:\n\n");
296 fprintf(stderr, " -p smart pairing (ignoring in2.fq)\n");
297 fprintf(stderr, " -R STR read group header line such as '@RG\\tID:foo\\tSM:bar' [null]\n");
298 fprintf(stderr, " -H STR/FILE insert STR to header if it starts with @; or insert lines in FILE [null]\n");
299 fprintf(stderr, " -o FILE sam file to output results to [stdout]\n");
300 fprintf(stderr, " -j treat ALT contigs as part of the primary assembly (i.e. ignore <idxbase>.alt file)\n");
301 fprintf(stderr, " -5 for split alignment, take the alignment with the smallest query (not genomic) coordinate as primary\n");
302 fprintf(stderr, " -q don't modify mapQ of supplementary alignments\n");
303 fprintf(stderr, " -K INT process INT input bases in each batch regardless of nThreads (for reproducibility) []\n");
304 fprintf(stderr, "\n");
305 fprintf(stderr, " -v INT verbosity level: 1=error, 2=warning, 3=message, 4+=debugging [%d]\n", bwa_verbose);
306 fprintf(stderr, " -T INT minimum score to output [%d]\n", opt->T);
307 fprintf(stderr, " -h INT[,INT] if there are <INT hits with score >%.2f%% of the max score, output all in XA [%d,%d]\n",
308 opt->XA_drop_ratio * 100.0,
309 opt->max_XA_hits, opt->max_XA_hits_alt);
310 fprintf(stderr, " A second value may be given for alternate sequences.\n");
311 fprintf(stderr, " -z FLOAT The fraction of the max score to use with -h [%f].\n", opt->XA_drop_ratio);
312 fprintf(stderr, " specify the mean, standard deviation (10%% of the mean if absent), max\n");
313 fprintf(stderr, " -a output all alignments for SE or unpaired PE\n");
314 fprintf(stderr, " -C append FASTA/FASTQ comment to SAM output\n");
315 fprintf(stderr, " -V output the reference FASTA header in the XR tag\n");
316 fprintf(stderr, " -Y use soft clipping for supplementary alignments\n");
317 fprintf(stderr, " -M mark shorter split hits as secondary\n\n");
318 fprintf(stderr, " -I FLOAT[,FLOAT[,INT[,INT]]]\n");
319 fprintf(stderr, " specify the mean, standard deviation (10%% of the mean if absent), max\n");
320 fprintf(stderr, " (4 sigma from the mean if absent) and min of the insert size distribution.\n");
321 fprintf(stderr, " FR orientation only. [inferred]\n");
322 fprintf(stderr, " -u output XB instead of XA; XB is XA with the alignment score and mapping quality added.\n");
323 fprintf(stderr, "\n");
324 fprintf(stderr, "Note: Please read the man page for detailed description of the command line and options.\n");
325 fprintf(stderr, "\n");
326 free(opt);
327 return 1;
328 }
329
330 if (mode) {
331 if (strcmp(mode, "intractg") == 0) {
332 if (!opt0.o_del) opt->o_del = 16;
333 if (!opt0.o_ins) opt->o_ins = 16;
334 if (!opt0.b) opt->b = 9;
335 if (!opt0.pen_clip5) opt->pen_clip5 = 5;
336 if (!opt0.pen_clip3) opt->pen_clip3 = 5;
337 } else if (strcmp(mode, "pacbio") == 0 || strcmp(mode, "pbref") == 0 || strcmp(mode, "ont2d") == 0) {
338 if (!opt0.o_del) opt->o_del = 1;
339 if (!opt0.e_del) opt->e_del = 1;
340 if (!opt0.o_ins) opt->o_ins = 1;
341 if (!opt0.e_ins) opt->e_ins = 1;
342 if (!opt0.b) opt->b = 1;
343 if (opt0.split_factor == 0.) opt->split_factor = 10.;
344 if (strcmp(mode, "ont2d") == 0) {
345 if (!opt0.min_chain_weight) opt->min_chain_weight = 20;
346 if (!opt0.min_seed_len) opt->min_seed_len = 14;
347 if (!opt0.pen_clip5) opt->pen_clip5 = 0;
348 if (!opt0.pen_clip3) opt->pen_clip3 = 0;
349 } else {
350 if (!opt0.min_chain_weight) opt->min_chain_weight = 40;
351 if (!opt0.min_seed_len) opt->min_seed_len = 17;
352 if (!opt0.pen_clip5) opt->pen_clip5 = 0;
353 if (!opt0.pen_clip3) opt->pen_clip3 = 0;
354 }
355 } else {
356 fprintf(stderr, "[E::%s] unknown read type '%s'\n", __func__, mode);
357 return 1; // FIXME memory leak
358 }
359 } else update_a(opt, &opt0);
360 bwa_fill_scmat(opt->a, opt->b, opt->mat);
361
362 aux.idx = bwa_idx_load_from_shm(argv[optind]);
363 if (aux.idx == 0) {
364 if ((aux.idx = bwa_idx_load(argv[optind], BWA_IDX_ALL)) == 0) return 1; // FIXME: memory leak
365 } else if (bwa_verbose >= 3)
366 fprintf(stderr, "[M::%s] load the bwa index from shared memory\n", __func__);
367 if (ignore_alt)
368 for (i = 0; i < aux.idx->bns->n_seqs; ++i)
369 aux.idx->bns->anns[i].is_alt = 0;
370
371 ko = kopen(argv[optind + 1], &fd);
372 if (ko == 0) {
373 if (bwa_verbose >= 1) fprintf(stderr, "[E::%s] fail to open file `%s'.\n", __func__, argv[optind + 1]);
374 return 1;
375 }
376 fp = gzdopen(fd, "r");
377 aux.ks = kseq_init(fp);
378 if (optind + 2 < argc) {
379 if (opt->flag&MEM_F_PE) {
380 if (bwa_verbose >= 2)
381 fprintf(stderr, "[W::%s] when '-p' is in use, the second query file is ignored.\n", __func__);
382 } else {
383 ko2 = kopen(argv[optind + 2], &fd2);
384 if (ko2 == 0) {
385 if (bwa_verbose >= 1) fprintf(stderr, "[E::%s] fail to open file `%s'.\n", __func__, argv[optind + 2]);
386 return 1;
387 }
388 fp2 = gzdopen(fd2, "r");
389 aux.ks2 = kseq_init(fp2);
390 opt->flag |= MEM_F_PE;
391 }
392 }
393 bwa_print_sam_hdr(aux.idx->bns, hdr_line);
394 aux.actual_chunk_size = fixed_chunk_size > 0? fixed_chunk_size : opt->chunk_size * opt->n_threads;
395 kt_pipeline(no_mt_io? 1 : 2, process, &aux, 3);
396 free(hdr_line);
397 free(opt);
398 bwa_idx_destroy(aux.idx);
399 kseq_destroy(aux.ks);
400 err_gzclose(fp); kclose(ko);
401 if (aux.ks2) {
402 kseq_destroy(aux.ks2);
403 err_gzclose(fp2); kclose(ko2);
404 }
405 return 0;
406 }
407
408 int main_fastmap(int argc, char *argv[])
409 {
410 int c, i, min_iwidth = 20, min_len = 17, print_seq = 0, min_intv = 1, max_len = INT_MAX;
411 uint64_t max_intv = 0;
412 kseq_t *seq;
413 bwtint_t k;
414 gzFile fp;
415 smem_i *itr;
416 const bwtintv_v *a;
417 bwaidx_t *idx;
418
419 while ((c = getopt(argc, argv, "w:l:pi:I:L:")) >= 0) {
420 switch (c) {
421 case 'p': print_seq = 1; break;
422 case 'w': min_iwidth = atoi(optarg); break;
423 case 'l': min_len = atoi(optarg); break;
424 case 'i': min_intv = atoi(optarg); break;
425 case 'I': max_intv = atol(optarg); break;
426 case 'L': max_len = atoi(optarg); break;
427 default: return 1;
428 }
429 }
430 if (optind + 1 >= argc) {
431 fprintf(stderr, "\n");
432 fprintf(stderr, "Usage: bwa fastmap [options] <idxbase> <in.fq>\n\n");
433 fprintf(stderr, "Options: -l INT min SMEM length to output [%d]\n", min_len);
434 fprintf(stderr, " -w INT max interval size to find coordiantes [%d]\n", min_iwidth);
435 fprintf(stderr, " -i INT min SMEM interval size [%d]\n", min_intv);
436 fprintf(stderr, " -L INT max MEM length [%d]\n", max_len);
437 fprintf(stderr, " -I INT stop if MEM is longer than -l with a size less than INT [%ld]\n", (long)max_intv);
438 fprintf(stderr, "\n");
439 return 1;
440 }
441
442 fp = xzopen(argv[optind + 1], "r");
443 seq = kseq_init(fp);
444 if ((idx = bwa_idx_load(argv[optind], BWA_IDX_BWT|BWA_IDX_BNS)) == 0) return 1;
445 itr = smem_itr_init(idx->bwt);
446 smem_config(itr, min_intv, max_len, max_intv);
447 while (kseq_read(seq) >= 0) {
448 err_printf("SQ\t%s\t%ld", seq->name.s, seq->seq.l);
449 if (print_seq) {
450 err_putchar('\t');
451 err_puts(seq->seq.s);
452 } else err_putchar('\n');
453 for (i = 0; i < seq->seq.l; ++i)
454 seq->seq.s[i] = nst_nt4_table[(int)seq->seq.s[i]];
455 smem_set_query(itr, seq->seq.l, (uint8_t*)seq->seq.s);
456 while ((a = smem_next(itr)) != 0) {
457 for (i = 0; i < a->n; ++i) {
458 bwtintv_t *p = &a->a[i];
459 if ((uint32_t)p->info - (p->info>>32) < min_len) continue;
460 err_printf("EM\t%d\t%d\t%ld", (uint32_t)(p->info>>32), (uint32_t)p->info, (long)p->x[2]);
461 if (p->x[2] <= min_iwidth) {
462 for (k = 0; k < p->x[2]; ++k) {
463 bwtint_t pos;
464 int len, is_rev, ref_id;
465 len = (uint32_t)p->info - (p->info>>32);
466 pos = bns_depos(idx->bns, bwt_sa(idx->bwt, p->x[0] + k), &is_rev);
467 if (is_rev) pos -= len - 1;
468 bns_cnt_ambi(idx->bns, pos, len, &ref_id);
469 err_printf("\t%s:%c%ld", idx->bns->anns[ref_id].name, "+-"[is_rev], (long)(pos - idx->bns->anns[ref_id].offset) + 1);
470 }
471 } else err_puts("\t*");
472 err_putchar('\n');
473 }
474 }
475 err_puts("//");
476 }
477
478 smem_itr_destroy(itr);
479 bwa_idx_destroy(idx);
480 kseq_destroy(seq);
481 err_gzclose(fp);
482 return 0;
483 }