| 1 |
#include <stdio.h> |
| 2 |
#include <zlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <errno.h> |
| 5 |
#include <assert.h> |
| 6 |
#include "bwamem.h" |
| 7 |
#include "kseq.h" // for the FASTA/Q parser |
| 8 |
KSEQ_DECLARE(gzFile) |
| 9 |
|
| 10 |
int main(int argc, char *argv[]) |
| 11 |
{ |
| 12 |
bwaidx_t *idx; |
| 13 |
gzFile fp; |
| 14 |
kseq_t *ks; |
| 15 |
mem_opt_t *opt; |
| 16 |
|
| 17 |
if (argc < 3) { |
| 18 |
fprintf(stderr, "Usage: bwamem-lite <idx.base> <reads.fq>\n"); |
| 19 |
return 1; |
| 20 |
} |
| 21 |
|
| 22 |
idx = bwa_idx_load(argv[1], BWA_IDX_ALL); // load the BWA index |
| 23 |
if (NULL == idx) { |
| 24 |
fprintf(stderr, "Index load failed.\n"); |
| 25 |
exit(EXIT_FAILURE); |
| 26 |
} |
| 27 |
fp = strcmp(argv[2], "-")? gzopen(argv[2], "r") : gzdopen(fileno(stdin), "r"); |
| 28 |
if (NULL == fp) { |
| 29 |
fprintf(stderr, "Couldn't open %s : %s\n", |
| 30 |
strcmp(argv[2], "-") ? argv[2] : "stdin", |
| 31 |
errno ? strerror(errno) : "Out of memory"); |
| 32 |
exit(EXIT_FAILURE); |
| 33 |
} |
| 34 |
ks = kseq_init(fp); // initialize the FASTA/Q parser |
| 35 |
opt = mem_opt_init(); // initialize the BWA-MEM parameters to the default values |
| 36 |
|
| 37 |
while (kseq_read(ks) >= 0) { // read one sequence |
| 38 |
mem_alnreg_v ar; |
| 39 |
int i, k; |
| 40 |
ar = mem_align1(opt, idx->bwt, idx->bns, idx->pac, ks->seq.l, ks->seq.s); // get all the hits |
| 41 |
for (i = 0; i < ar.n; ++i) { // traverse each hit |
| 42 |
mem_aln_t a; |
| 43 |
if (ar.a[i].secondary >= 0) continue; // skip secondary alignments |
| 44 |
a = mem_reg2aln(opt, idx->bns, idx->pac, ks->seq.l, ks->seq.s, &ar.a[i]); // get forward-strand position and CIGAR |
| 45 |
// print alignment |
| 46 |
printf("%s\t%c\t%s\t%ld\t%d\t", ks->name.s, "+-"[a.is_rev], idx->bns->anns[a.rid].name, (long)a.pos, a.mapq); |
| 47 |
for (k = 0; k < a.n_cigar; ++k) // print CIGAR |
| 48 |
printf("%d%c", a.cigar[k]>>4, "MIDSH"[a.cigar[k]&0xf]); |
| 49 |
printf("\t%d\n", a.NM); // print edit distance |
| 50 |
free(a.cigar); // don't forget to deallocate CIGAR |
| 51 |
} |
| 52 |
free(ar.a); // and deallocate the hit list |
| 53 |
} |
| 54 |
|
| 55 |
free(opt); |
| 56 |
kseq_destroy(ks); |
| 57 |
gzclose(fp); |
| 58 |
bwa_idx_destroy(idx); |
| 59 |
return 0; |
| 60 |
} |