annotate bwa-0.6.2/bwtindex.c @ 2:a294fbfcb1db draft default tip

Uploaded BWA
author ashvark
date Fri, 18 Jul 2014 07:55:59 -0400
parents dd1186b11b3b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
1 /* The MIT License
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
2
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
3 Copyright (c) 2008 Genome Research Ltd (GRL).
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
4
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
6 a copy of this software and associated documentation files (the
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
7 "Software"), to deal in the Software without restriction, including
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
8 without limitation the rights to use, copy, modify, merge, publish,
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
9 distribute, sublicense, and/or sell copies of the Software, and to
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
10 permit persons to whom the Software is furnished to do so, subject to
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
11 the following conditions:
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
12
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
13 The above copyright notice and this permission notice shall be
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
14 included in all copies or substantial portions of the Software.
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
15
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
23 SOFTWARE.
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
24 */
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
25
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
26 /* Contact: Heng Li <lh3@sanger.ac.uk> */
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
27
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
28 #include <stdio.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
29 #include <stdlib.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
30 #include <string.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
31 #include <unistd.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
32 #include <time.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
33 #include <zlib.h>
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
34 #include "bntseq.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
35 #include "bwt.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
36 #include "main.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
37 #include "utils.h"
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
38
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
39 bwt_t *bwt_pac2bwt(const char *fn_pac, int use_is);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
40 void bwa_pac_rev_core(const char *fn, const char *fn_rev);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
41
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
42 int bwa_index(int argc, char *argv[])
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
43 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
44 char *prefix = 0, *str, *str2, *str3;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
45 int c, algo_type = 0, is_color = 0, is_64 = 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
46 clock_t t;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
47 int64_t l_pac;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
48
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
49 while ((c = getopt(argc, argv, "6ca:p:")) >= 0) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
50 switch (c) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
51 case 'a': // if -a is not set, algo_type will be determined later
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
52 if (strcmp(optarg, "div") == 0) algo_type = 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
53 else if (strcmp(optarg, "bwtsw") == 0) algo_type = 2;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
54 else if (strcmp(optarg, "is") == 0) algo_type = 3;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
55 else err_fatal(__func__, "unknown algorithm: '%s'.", optarg);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
56 break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
57 case 'p': prefix = strdup(optarg); break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
58 case 'c': is_color = 1; break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
59 case '6': is_64 = 1; break;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
60 default: return 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
61 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
62 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
63
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
64 if (optind + 1 > argc) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
65 fprintf(stderr, "\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
66 fprintf(stderr, "Usage: bwa index [-a bwtsw|is] [-c] <in.fasta>\n\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
67 fprintf(stderr, "Options: -a STR BWT construction algorithm: bwtsw or is [auto]\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
68 fprintf(stderr, " -p STR prefix of the index [same as fasta name]\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
69 fprintf(stderr, " -6 index files named as <in.fasta>.64.* instead of <in.fasta>.* \n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
70 // fprintf(stderr, " -c build color-space index\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
71 fprintf(stderr, "\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
72 fprintf(stderr, "Warning: `-a bwtsw' does not work for short genomes, while `-a is' and\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
73 fprintf(stderr, " `-a div' do not work not for long genomes. Please choose `-a'\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
74 fprintf(stderr, " according to the length of the genome.\n\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
75 return 1;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
76 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
77 if (prefix == 0) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
78 prefix = malloc(strlen(argv[optind]) + 4);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
79 strcpy(prefix, argv[optind]);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
80 if (is_64) strcat(prefix, ".64");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
81 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
82 str = (char*)calloc(strlen(prefix) + 10, 1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
83 str2 = (char*)calloc(strlen(prefix) + 10, 1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
84 str3 = (char*)calloc(strlen(prefix) + 10, 1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
85
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
86 if (is_color == 0) { // nucleotide indexing
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
87 gzFile fp = xzopen(argv[optind], "r");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
88 t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
89 fprintf(stderr, "[bwa_index] Pack FASTA... ");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
90 l_pac = bns_fasta2bntseq(fp, prefix, 0);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
91 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
92 gzclose(fp);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
93 } else { // color indexing
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
94 gzFile fp = xzopen(argv[optind], "r");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
95 strcat(strcpy(str, prefix), ".nt");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
96 t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
97 fprintf(stderr, "[bwa_index] Pack nucleotide FASTA... ");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
98 l_pac = bns_fasta2bntseq(fp, str, 0);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
99 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
100 gzclose(fp);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
101 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
102 char *tmp_argv[3];
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
103 tmp_argv[0] = argv[0]; tmp_argv[1] = str; tmp_argv[2] = prefix;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
104 t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
105 fprintf(stderr, "[bwa_index] Convert nucleotide PAC to color PAC... ");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
106 bwa_pac2cspac(3, tmp_argv);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
107 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
108 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
109 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
110 if (algo_type == 0) algo_type = l_pac > 50000000? 2 : 3; // set the algorithm for generating BWT
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
111 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
112 strcpy(str, prefix); strcat(str, ".pac");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
113 strcpy(str2, prefix); strcat(str2, ".bwt");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
114 t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
115 fprintf(stderr, "[bwa_index] Construct BWT for the packed sequence...\n");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
116 if (algo_type == 2) bwt_bwtgen(str, str2);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
117 else if (algo_type == 1 || algo_type == 3) {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
118 bwt_t *bwt;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
119 bwt = bwt_pac2bwt(str, algo_type == 3);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
120 bwt_dump_bwt(str2, bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
121 bwt_destroy(bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
122 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
123 fprintf(stderr, "[bwa_index] %.2f seconds elapse.\n", (float)(clock() - t) / CLOCKS_PER_SEC);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
124 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
125 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
126 bwt_t *bwt;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
127 strcpy(str, prefix); strcat(str, ".bwt");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
128 t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
129 fprintf(stderr, "[bwa_index] Update BWT... ");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
130 bwt = bwt_restore_bwt(str);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
131 bwt_bwtupdate_core(bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
132 bwt_dump_bwt(str, bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
133 bwt_destroy(bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
134 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
135 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
136 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
137 gzFile fp = xzopen(argv[optind], "r");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
138 t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
139 fprintf(stderr, "[bwa_index] Pack forward-only FASTA... ");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
140 l_pac = bns_fasta2bntseq(fp, prefix, 1);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
141 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
142 gzclose(fp);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
143 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
144 {
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
145 bwt_t *bwt;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
146 strcpy(str, prefix); strcat(str, ".bwt");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
147 strcpy(str3, prefix); strcat(str3, ".sa");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
148 t = clock();
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
149 fprintf(stderr, "[bwa_index] Construct SA from BWT and Occ... ");
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
150 bwt = bwt_restore_bwt(str);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
151 bwt_cal_sa(bwt, 32);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
152 bwt_dump_sa(str3, bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
153 bwt_destroy(bwt);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
154 fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
155 }
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
156 free(str3); free(str2); free(str); free(prefix);
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
157 return 0;
dd1186b11b3b Uploaded BWA
ashvark
parents:
diff changeset
158 }