Debian Codegraph @

samtools/bam_rmdupse.c

raw symbols SCIP
1 /* bam_rmdupse.c -- duplicate read detection for unpaired reads.
2
3 Copyright (C) 2009, 2015, 2016, 2019 Genome Research Ltd.
4 Portions copyright (C) 2009 Broad Institute.
5
6 Author: Heng Li <[email protected]>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE. */
25
26 #include <config.h>
27
28 #include <math.h>
29 #include <stdio.h>
30 #include "bam.h" // for bam_get_library
31 #include "htslib/sam.h"
32 #include "htslib/khash.h"
33 #include "htslib/klist.h"
34 #include "samtools.h"
35
36 #define QUEUE_CLEAR_SIZE 0x100000
37 #define MAX_POS 0x7fffffff
38
39 typedef struct {
40 int endpos;
41 uint32_t score:31, discarded:1;
42 bam1_t *b;
43 } elem_t, *elem_p;
44 #define __free_elem(p) bam_destroy1((p)->data.b)
45 KLIST_INIT(q, elem_t, __free_elem)
46 typedef klist_t(q) queue_t;
47
48 KHASH_MAP_INIT_INT(best, elem_p)
49 typedef khash_t(best) besthash_t;
50
51 typedef struct {
52 uint64_t n_checked, n_removed;
53 besthash_t *left, *rght;
54 } lib_aux_t;
55 KHASH_MAP_INIT_STR(lib, lib_aux_t)
56
57 static lib_aux_t *get_aux(khash_t(lib) *aux, const char *lib)
58 {
59 khint_t k = kh_get(lib, aux, lib);
60 if (k == kh_end(aux)) {
61 int ret;
62 char *p = strdup(lib);
63 lib_aux_t *q;
64 k = kh_put(lib, aux, p, &ret);
65 q = &kh_val(aux, k);
66 q->left = kh_init(best);
67 q->rght = kh_init(best);
68 q->n_checked = q->n_removed = 0;
69 return q;
70 } else return &kh_val(aux, k);
71 }
72
73 static inline int sum_qual(const bam1_t *b)
74 {
75 int i, q;
76 uint8_t *qual = bam_get_qual(b);
77 for (i = q = 0; i < b->core.l_qseq; ++i) q += qual[i];
78 return q;
79 }
80
81 static inline elem_t *push_queue(queue_t *queue, const bam1_t *b, int endpos, int score)
82 {
83 elem_t *p = kl_pushp(q, queue);
84 p->discarded = 0;
85 p->endpos = endpos; p->score = score;
86 if (p->b == 0) p->b = bam_init1();
87 if (!p->b) { perror(NULL); exit(EXIT_FAILURE); }
88 if (bam_copy1(p->b, b) == NULL) { perror(NULL); exit(EXIT_FAILURE); }
89 return p;
90 }
91
92 static void clear_besthash(besthash_t *h, int32_t pos)
93 {
94 khint_t k;
95 for (k = kh_begin(h); k != kh_end(h); ++k)
96 if (kh_exist(h, k) && kh_val(h, k)->endpos <= pos)
97 kh_del(best, h, k);
98 }
99
100 static int dump_alignment(samFile *out, sam_hdr_t *hdr,
101 queue_t *queue, int32_t pos, khash_t(lib) *h)
102 {
103 if (queue->size > QUEUE_CLEAR_SIZE || pos == MAX_POS) {
104 khint_t k;
105 while (1) {
106 elem_t *q;
107 if (queue->head == queue->tail) break;
108 q = &kl_val(queue->head);
109 if (q->discarded) {
110 q->b->l_data = 0;
111 kl_shift(q, queue, 0);
112 continue;
113 }
114 if ((q->b->core.flag&BAM_FREVERSE) && q->endpos > pos) break;
115 if (sam_write1(out, hdr, q->b) < 0) return -1;
116 q->b->l_data = 0;
117 kl_shift(q, queue, 0);
118 }
119 for (k = kh_begin(h); k != kh_end(h); ++k) {
120 if (kh_exist(h, k)) {
121 clear_besthash(kh_val(h, k).left, pos);
122 clear_besthash(kh_val(h, k).rght, pos);
123 }
124 }
125 }
126 return 0;
127 }
128
129 int bam_rmdupse_core(samFile *in, sam_hdr_t *hdr, samFile *out, int force_se)
130 {
131 bam1_t *b = NULL;
132 queue_t *queue = NULL;
133 khint_t k;
134 int last_tid = -2, r;
135 khash_t(lib) *aux = NULL;
136
137 aux = kh_init(lib);
138 b = bam_init1();
139 queue = kl_init(q);
140 if (!aux || !b || !queue) {
141 perror(__func__);
142 goto fail;
143 }
144
145 while ((r = sam_read1(in, hdr, b)) >= 0) {
146 bam1_core_t *c = &b->core;
147 int endpos = bam_endpos(b);
148 int score = sum_qual(b);
149
150 if (last_tid != c->tid) {
151 if (last_tid >= 0) {
152 if (dump_alignment(out, hdr, queue, MAX_POS, aux) < 0)
153 goto write_fail;
154 }
155 last_tid = c->tid;
156 } else {
157 if (dump_alignment(out, hdr, queue, c->pos, aux) < 0)
158 goto write_fail;
159 }
160 if ((c->flag&BAM_FUNMAP) || ((c->flag&BAM_FPAIRED) && !force_se)) {
161 push_queue(queue, b, endpos, score);
162 } else {
163 const char *lib;
164 lib_aux_t *q;
165 besthash_t *h;
166 uint32_t key;
167 int ret;
168 lib = bam_get_library(hdr, b);
169 q = lib? get_aux(aux, lib) : get_aux(aux, "\t");
170 ++q->n_checked;
171 h = (c->flag&BAM_FREVERSE)? q->rght : q->left;
172 key = (c->flag&BAM_FREVERSE)? endpos : c->pos;
173 k = kh_put(best, h, key, &ret);
174 if (ret == 0) { // in the hash table
175 elem_t *p = kh_val(h, k);
176 ++q->n_removed;
177 if (p->score < score) {
178 if (c->flag&BAM_FREVERSE) { // mark "discarded" and push the queue
179 p->discarded = 1;
180 kh_val(h, k) = push_queue(queue, b, endpos, score);
181 } else { // replace
182 p->score = score; p->endpos = endpos;
183 if (bam_copy1(p->b, b) == NULL) {
184 perror(NULL); exit(EXIT_FAILURE);
185 }
186 }
187 } // otherwise, discard the alignment
188 } else kh_val(h, k) = push_queue(queue, b, endpos, score);
189 }
190 }
191 if (r < -1) {
192 fprintf(stderr, "[%s] error reading input file\n", __func__);
193 goto fail;
194 }
195
196 if (dump_alignment(out, hdr, queue, MAX_POS, aux) < 0) goto write_fail;
197
198 for (k = kh_begin(aux); k != kh_end(aux); ++k) {
199 if (kh_exist(aux, k)) {
200 lib_aux_t *q = &kh_val(aux, k);
201 fprintf(stderr, "[bam_rmdupse_core] %lld / %lld = %.4lf in library '%s'\n", (long long)q->n_removed,
202 (long long)q->n_checked, (double)q->n_removed/q->n_checked, kh_key(aux, k));
203 kh_destroy(best, q->left); kh_destroy(best, q->rght);
204 free((char*)kh_key(aux, k));
205 kh_del(lib, aux, k);
206 }
207 }
208 kh_destroy(lib, aux);
209 bam_destroy1(b);
210 kl_destroy(q, queue);
211 return 0;
212
213 write_fail:
214 print_error_errno("rmdup", "failed to write record");
215 fail:
216 if (aux) {
217 for (k = kh_begin(aux); k != kh_end(aux); ++k) {
218 if (kh_exist(aux, k)) {
219 lib_aux_t *q = &kh_val(aux, k);
220 kh_destroy(best, q->left);
221 kh_destroy(best, q->rght);
222 free((char*)kh_key(aux, k));
223 }
224 }
225 kh_destroy(lib, aux);
226 }
227 bam_destroy1(b);
228 kl_destroy(q, queue);
229 return 1;
230 }