8 typedef bam1_t *bam1_p;
11 KHASH_SET_INIT_STR(name)
12 KHASH_MAP_INIT_INT64(pos, bam1_p)
14 #define BUFFER_SIZE 0x40000
17 uint64_t n_checked, n_removed;
18 khash_t(pos) *best_hash;
20 KHASH_MAP_INIT_STR(lib, lib_aux_t)
27 static inline void stack_insert(tmp_stack_t *stack, bam1_t *b)
29 if (stack->n == stack->max) {
30 stack->max = stack->max? stack->max<<1 : 0x10000;
31 stack->a = (bam1_t**)realloc(stack->a, sizeof(bam1_t*) * stack->max);
33 stack->a[stack->n++] = b;
36 static inline void dump_best(tmp_stack_t *stack, samfile_t *out)
39 for (i = 0; i != stack->n; ++i) {
40 samwrite(out, stack->a[i]);
41 bam_destroy1(stack->a[i]);
46 static void clear_del_set(khash_t(name) *del_set)
49 for (k = kh_begin(del_set); k < kh_end(del_set); ++k)
50 if (kh_exist(del_set, k))
51 free((char*)kh_key(del_set, k));
52 kh_clear(name, del_set);
55 static lib_aux_t *get_aux(khash_t(lib) *aux, const char *lib)
57 khint_t k = kh_get(lib, aux, lib);
58 if (k == kh_end(aux)) {
60 char *p = strdup(lib);
62 k = kh_put(lib, aux, p, &ret);
64 q->n_checked = q->n_removed = 0;
65 q->best_hash = kh_init(pos);
67 } else return &kh_val(aux, k);
70 static void clear_best(khash_t(lib) *aux, int max)
73 for (k = kh_begin(aux); k != kh_end(aux); ++k) {
74 if (kh_exist(aux, k)) {
75 lib_aux_t *q = &kh_val(aux, k);
76 if (kh_size(q->best_hash) >= max)
77 kh_clear(pos, q->best_hash);
82 static inline int sum_qual(const bam1_t *b)
85 uint8_t *qual = bam1_qual(b);
86 for (i = q = 0; i < b->core.l_qseq; ++i) q += qual[i];
90 void bam_rmdup_core(samfile_t *in, samfile_t *out)
93 int last_tid = -1, last_pos = -1;
97 khash_t(name) *del_set;
100 del_set = kh_init(name);
102 memset(&stack, 0, sizeof(tmp_stack_t));
104 kh_resize(name, del_set, 4 * BUFFER_SIZE);
105 while (samread(in, b) >= 0) {
106 bam1_core_t *c = &b->core;
107 if (c->tid != last_tid || last_pos != c->pos) {
108 dump_best(&stack, out); // write the result
109 clear_best(aux, BUFFER_SIZE);
110 if (c->tid != last_tid) {
112 if (kh_size(del_set)) { // check
113 fprintf(stderr, "[bam_rmdup_core] %llu unmatched pairs\n", (long long)kh_size(del_set));
114 clear_del_set(del_set);
116 if ((int)c->tid == -1) { // append unmapped reads
118 while (samread(in, b) >= 0) samwrite(out, b);
122 fprintf(stderr, "[bam_rmdup_core] processing reference %s...\n", in->header->target_name[c->tid]);
125 if (!(c->flag&BAM_FPAIRED) || (c->flag&(BAM_FUNMAP|BAM_FMUNMAP)) || (c->mtid >= 0 && c->tid != c->mtid)) {
127 } else if (c->isize > 0) { // paired, head
128 uint64_t key = (uint64_t)c->pos<<32 | c->isize;
132 lib = bam_get_library(in->header, b);
133 q = lib? get_aux(aux, lib) : get_aux(aux, "\t");
135 k = kh_put(pos, q->best_hash, key, &ret);
136 if (ret == 0) { // found in best_hash
137 bam1_t *p = kh_val(q->best_hash, k);
139 if (sum_qual(p) < sum_qual(b)) { // the current alignment is better; this can be accelerated in principle
140 kh_put(name, del_set, strdup(bam1_qname(p)), &ret); // p will be removed
141 bam_copy1(p, b); // replaced as b
142 } else kh_put(name, del_set, strdup(bam1_qname(b)), &ret); // b will be removed
144 fprintf(stderr, "[bam_rmdup_core] inconsistent BAM file for pair '%s'. Continue anyway.\n", bam1_qname(b));
145 } else { // not found in best_hash
146 kh_val(q->best_hash, k) = bam_dup1(b);
147 stack_insert(&stack, kh_val(q->best_hash, k));
149 } else { // paired, tail
150 k = kh_get(name, del_set, bam1_qname(b));
151 if (k != kh_end(del_set)) {
152 free((char*)kh_key(del_set, k));
153 kh_del(name, del_set, k);
154 } else samwrite(out, b);
159 for (k = kh_begin(aux); k != kh_end(aux); ++k) {
160 if (kh_exist(aux, k)) {
161 lib_aux_t *q = &kh_val(aux, k);
162 dump_best(&stack, out);
163 fprintf(stderr, "[bam_rmdup_core] %lld / %lld = %.4lf in library '%s'\n", (long long)q->n_removed,
164 (long long)q->n_checked, (double)q->n_removed/q->n_checked, kh_key(aux, k));
165 kh_destroy(pos, q->best_hash);
166 free((char*)kh_key(aux, k));
169 kh_destroy(lib, aux);
171 clear_del_set(del_set);
172 kh_destroy(name, del_set);
177 void bam_rmdupse_core(samfile_t *in, samfile_t *out, int force_se);
179 int bam_rmdup(int argc, char *argv[])
181 int c, is_se = 0, force_se = 0;
183 while ((c = getopt(argc, argv, "sS")) >= 0) {
185 case 's': is_se = 1; break;
186 case 'S': force_se = is_se = 1; break;
189 if (optind + 2 > argc) {
190 fprintf(stderr, "\n");
191 fprintf(stderr, "Usage: samtools rmdup [-sS] <input.srt.bam> <output.bam>\n\n");
192 fprintf(stderr, "Option: -s rmdup for SE reads\n");
193 fprintf(stderr, " -S treat PE reads as SE in rmdup (force -s)\n\n");
196 in = samopen(argv[optind], "rb", 0);
197 out = samopen(argv[optind+1], "wb", in->header);
198 if (in == 0 || out == 0) {
199 fprintf(stderr, "[bam_rmdup] fail to read/write input files\n");
202 if (is_se) bam_rmdupse_core(in, out, force_se);
203 else bam_rmdup_core(in, out);
204 samclose(in); samclose(out);