Debian Codegraph @

samtools/bam_rmdupse.c.pysam.c

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