forked from samtools/samtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bam_markdup.c
1070 lines (853 loc) · 33.1 KB
/
bam_markdup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* bam_markdup.c -- Mark duplicates from a coord sorted file that has gone
through fixmates with the mate scoring option on.
Copyright (C) 2017-18 Genome Research Ltd.
Author: Andrew Whitwham <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <sys/stat.h>
#include "htslib/thread_pool.h"
#include "htslib/sam.h"
#include "sam_opts.h"
#include "samtools.h"
#include "htslib/khash.h"
#include "htslib/klist.h"
#include "htslib/kstring.h"
#include "tmp_file.h"
typedef struct {
int32_t single;
int32_t this_ref;
int32_t this_coord;
int32_t other_ref;
int32_t other_coord;
int32_t leftmost;
int32_t orientation;
} key_data_t;
typedef struct {
bam1_t *p;
} in_hash_t;
typedef struct {
bam1_t *b;
int32_t pos;
key_data_t pair_key;
key_data_t single_key;
} read_queue_t;
static khint32_t do_hash(unsigned char *key, khint32_t len);
static khint_t hash_key(key_data_t key) {
int i = 0;
khint_t hash;
if (key.single) {
unsigned char sig[12];
memcpy(sig + i, &key.this_ref, 4); i += 4;
memcpy(sig + i, &key.this_coord, 4); i += 4;
memcpy(sig + i, &key.orientation, 4); i += 4;
hash = do_hash(sig, i);
} else {
unsigned char sig[24];
memcpy(sig + i, &key.this_ref, 4); i += 4;
memcpy(sig + i, &key.this_coord, 4); i += 4;
memcpy(sig + i, &key.other_ref, 4); i += 4;
memcpy(sig + i, &key.other_coord, 4); i += 4;
memcpy(sig + i, &key.leftmost, 4); i += 4;
memcpy(sig + i, &key.orientation, 4); i += 4;
hash = do_hash(sig, i);
}
return hash;
}
static int key_equal(key_data_t a, key_data_t b) {
int match = 1;
if (a.this_coord != b.this_coord)
match = 0;
else if (a.orientation != b.orientation)
match = 0;
else if (a.this_ref != b.this_ref)
match = 0;
else if (a.single != b.single)
match = 0;
if (!a.single) {
if (a.other_coord != b.other_coord)
match = 0;
else if (a.leftmost != b.leftmost)
match = 0;
else if (a.other_ref != b.other_ref)
match = 0;
}
return match;
}
#define __free_queue_element(p)
#define O_FF 2
#define O_RR 3
#define O_FR 5
#define O_RF 7
KHASH_INIT(reads, key_data_t, in_hash_t, 1, hash_key, key_equal) // read map hash
KLIST_INIT(read_queue, read_queue_t, __free_queue_element) // the reads buffer
KHASH_MAP_INIT_STR(duplicates, int) // map of duplicates for supplementary dup id
/* Calculate the mate's unclipped start based on position and cigar string from MC tag. */
static int32_t unclipped_other_start(int32_t op, char *cigar) {
char *c = cigar;
int32_t clipped = 0;
while (*c && *c != '*') {
long num = 0;
if (isdigit((int)*c)) {
num = strtol(c, &c, 10);
} else {
num = 1;
}
if (*c == 'S' || *c == 'H') { // clips
clipped += num;
} else {
break;
}
c++;
}
return op - clipped + 1;
}
/* Calculate the current read's start based on the stored cigar string. */
static int32_t unclipped_start(bam1_t *b) {
uint32_t *cigar = bam_get_cigar(b);
int32_t clipped = 0;
uint32_t i;
for (i = 0; i < b->core.n_cigar; i++) {
char c = bam_cigar_opchr(cigar[i]);
if (c == 'S' || c == 'H') { // clips
clipped += bam_cigar_oplen(cigar[i]);
} else {
break;
}
}
return b->core.pos - clipped + 1;
}
/* Calculate the mate's unclipped end based on start position and cigar string from MC tag.*/
static int32_t unclipped_other_end(int32_t op, char *cigar) {
char *c = cigar;
int32_t refpos = 0;
int skip = 1;
while (*c && *c != '*') {
long num = 0;
if (isdigit((int)*c)) {
num = strtol(c, &c, 10);
} else {
num = 1;
}
switch (*c) {
case 'M':
case 'D':
case 'N':
case '=':
case 'X':
refpos += num;
skip = 0; // ignore initial clips
break;
case 'S':
case 'H':
if (!skip) {
refpos += num;
}
break;
}
c++;
}
return op + refpos;
}
/* Calculate the current read's end based on the stored cigar string. */
static int32_t unclipped_end(bam1_t *b) {
uint32_t *cigar = bam_get_cigar(b);
int32_t end_pos, clipped = 0;
int32_t i;
end_pos = bam_endpos(b);
// now get the clipped end bases (if any)
// if we get to the beginning of the cigar string
// without hitting a non-clip then the results are meaningless
for (i = b->core.n_cigar - 1; i >= 0; i--) {
char c = bam_cigar_opchr(cigar[i]);
if (c == 'S' || c == 'H') { // clips
clipped += bam_cigar_oplen(cigar[i]);
} else {
break;
}
}
return end_pos + clipped;
}
/* The Bob Jenkins one_at_a_time hash to reduce the key to a 32 bit value. */
static khint32_t do_hash(unsigned char *key, khint32_t len) {
khint32_t hash, i;
for (hash = 0, i = 0; i < len; ++i) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
/* Get mate score from tag. */
static int64_t get_mate_score(bam1_t *b) {
uint8_t *data;
int64_t score;
if ((data = bam_aux_get(b, "ms"))) {
score = bam_aux2i(data);
} else {
fprintf(stderr, "[markdup] error: no ms score tag. Please run samtools fixmate on file first.\n");
return -1;
}
return score;
}
/* Calc current score from quality. */
static int64_t calc_score(bam1_t *b)
{
int64_t score = 0;
uint8_t *qual = bam_get_qual(b);
int i;
for (i = 0; i < b->core.l_qseq; i++) {
if (qual[i] >= 15) score += qual[i];
}
return score;
}
/* Create a signature hash of the current read and its pair.
Uses the unclipped start (or end depending on orientation),
the reference id, orientation and whether the current
read is leftmost of the pair. */
static int make_pair_key(key_data_t *key, bam1_t *bam) {
int32_t this_ref, this_coord, this_end;
int32_t other_ref, other_coord, other_end;
int32_t orientation, leftmost;
uint8_t *data;
char *cig;
this_ref = bam->core.tid + 1; // avoid a 0 being put into the hash
other_ref = bam->core.mtid + 1;
this_coord = unclipped_start(bam);
this_end = unclipped_end(bam);
if ((data = bam_aux_get(bam, "MC"))) {
cig = bam_aux2Z(data);
other_end = unclipped_other_end(bam->core.mpos, cig);
other_coord = unclipped_other_start(bam->core.mpos, cig);
} else {
fprintf(stderr, "[markdup] error: no MC tag. Please run samtools fixmate on file first.\n");
return 1;
}
// work out orientations
if (this_ref != other_ref) {
leftmost = this_ref < other_ref;
} else {
if (bam_is_rev(bam) == bam_is_mrev(bam)) {
if (!bam_is_rev(bam)) {
leftmost = this_coord <= other_coord;
} else {
leftmost = this_end <= other_end;
}
} else {
if (bam_is_rev(bam)) {
leftmost = this_end <= other_coord;
} else {
leftmost = this_coord <= other_end;
}
}
}
// pair orientation
if (leftmost) {
if (bam_is_rev(bam) == bam_is_mrev(bam)) {
other_coord = other_end;
if (!bam_is_rev(bam)) {
if (bam->core.flag & BAM_FREAD1) {
orientation = O_FF;
} else {
orientation = O_RR;
}
} else {
if (bam->core.flag & BAM_FREAD1) {
orientation = O_RR;
} else {
orientation = O_FF;
}
}
} else {
if (!bam_is_rev(bam)) {
orientation = O_FR;
other_coord = other_end;
} else {
orientation = O_RF;
this_coord = this_end;
}
}
} else {
if (bam_is_rev(bam) == bam_is_mrev(bam)) {
this_coord = this_end;
if (!bam_is_rev(bam)) {
if (bam->core.flag & BAM_FREAD1) {
orientation = O_RR;
} else {
orientation = O_FF;
}
} else {
if (bam->core.flag & BAM_FREAD1) {
orientation = O_FF;
} else {
orientation = O_RR;
}
}
} else {
if (!bam_is_rev(bam)) {
orientation = O_RF;
other_coord = other_end;
} else {
orientation = O_FR;
this_coord = this_end;
}
}
}
if (!leftmost)
leftmost = 13;
else
leftmost = 11;
key->single = 0;
key->this_ref = this_ref;
key->this_coord = this_coord;
key->other_ref = other_ref;
key->other_coord = other_coord;
key->leftmost = leftmost;
key->orientation = orientation;
return 0;
}
/* Create a signature hash of single read (or read with an unmatched pair).
Uses unclipped start (or end depending on orientation), reference id,
and orientation. */
static void make_single_key(key_data_t *key, bam1_t *bam) {
int32_t this_ref, this_coord;
int32_t orientation;
this_ref = bam->core.tid + 1; // avoid a 0 being put into the hash
if (bam_is_rev(bam)) {
this_coord = unclipped_end(bam);
orientation = O_RR;
} else {
this_coord = unclipped_start(bam);
orientation = O_FF;
}
key->single = 1;
key->this_ref = this_ref;
key->this_coord = this_coord;
key->orientation = orientation;
}
/* Add the duplicate name to a hash if it does not exist. */
static int add_duplicate(khash_t(duplicates) *d_hash, bam1_t *dupe) {
khiter_t d;
int ret;
d = kh_get(duplicates, d_hash, bam_get_qname(dupe));
if (d == kh_end(d_hash)) {
d = kh_put(duplicates, d_hash, strdup(bam_get_qname(dupe)), &ret);
if (ret > 0) {
kh_value(d_hash, d) = 1;
} else if (ret == 0) {
kh_value(d_hash, d)++;
} else {
fprintf(stderr, "[markdup] error: unable to store supplementary duplicates.\n");
return 1;
}
}
return 0;
}
/* Compare the reads near each other (coordinate sorted) and try to spot the duplicates.
Generally the highest quality scoring is chosen as the original and all others the duplicates.
The score is based on the sum of the quality values (<= 15) of the read and its mate (if any).
While single reads are compared to only one read of a pair, the pair will chosen as the original.
The comparison is done on position and orientation, see above for details.
Marking the supplementary reads of a duplicate as also duplicates takes an extra file read/write
step. This is because the duplicate can occur before the primary read.*/
static int bam_mark_duplicates(samFile *in, samFile *out, char *prefix, int remove_dups, int32_t max_length, int do_stats, int supp, int tag) {
bam_hdr_t *header = NULL;
khiter_t k;
khash_t(reads) *pair_hash = kh_init(reads);
khash_t(reads) *single_hash = kh_init(reads);
klist_t(read_queue) *read_buffer = kl_init(read_queue);
kliter_t(read_queue) *rq;
khash_t(duplicates) *dup_hash = kh_init(duplicates);
int32_t prev_tid, prev_coord;
read_queue_t *in_read;
int ret;
int reading, writing, excluded, duplicate, single, pair, single_dup, examined;
tmp_file_t temp;
if (!pair_hash || !single_hash || !read_buffer || !dup_hash) {
fprintf(stderr, "[markdup] out of memory\n");
goto fail;
}
if ((header = sam_hdr_read(in)) == NULL) {
fprintf(stderr, "[markdup] error reading header\n");
goto fail;
}
// accept unknown, unsorted or coordinate sort order, but error on queryname sorted.
// only really works on coordinate sorted files.
if ((header->l_text > 3) && (strncmp(header->text, "@HD", 3) == 0)) {
char *p, *q;
p = strstr(header->text, "\tSO:queryname");
q = strchr(header->text, '\n');
// looking for SO:queryname within @HD only
// (e.g. must ignore in a @CO comment line later in header)
if ((p != 0) && (p < q)) {
fprintf(stderr, "[markdup] error: queryname sorted, must be sorted by coordinate.\n");
goto fail;
}
}
if (sam_hdr_write(out, header) < 0) {
fprintf(stderr, "[markdup] error writing header.\n");
goto fail;
}
// used for coordinate order checks
prev_tid = prev_coord = 0;
// get the buffer going
in_read = kl_pushp(read_queue, read_buffer);
if (!in_read) {
fprintf(stderr, "[markdup] out of memory\n");
goto fail;
}
// handling supplementary reads needs a temporary file
if (supp) {
if (tmp_file_open_write(&temp, prefix, 1)) {
fprintf(stderr, "[markdup] error: unable to open tmp file %s.\n", prefix);
goto fail;
}
}
if ((in_read->b = bam_init1()) == NULL) {
fprintf(stderr, "[markdup] error: unable to allocate memory for alignment.\n");
goto fail;
}
reading = writing = excluded = single_dup = duplicate = examined = pair = single = 0;
while ((ret = sam_read1(in, header, in_read->b)) >= 0) {
// do some basic coordinate order checks
if (in_read->b->core.tid >= 0) { // -1 for unmapped reads
if (in_read->b->core.tid < prev_tid ||
((in_read->b->core.tid == prev_tid) && (in_read->b->core.pos < prev_coord))) {
goto fail;
}
}
prev_coord = in_read->pos = in_read->b->core.pos;
prev_tid = in_read->b->core.tid;
in_read->pair_key.single = 1;
in_read->single_key.single = 0;
reading++;
// read must not be secondary, supplementary, unmapped or failed QC
if (!(in_read->b->core.flag & (BAM_FSECONDARY | BAM_FSUPPLEMENTARY | BAM_FUNMAP | BAM_FQCFAIL))) {
examined++;
// look at the pairs first
if ((in_read->b->core.flag & BAM_FPAIRED) && !(in_read->b->core.flag & BAM_FMUNMAP)) {
int ret, mate_tmp;
key_data_t pair_key;
key_data_t single_key;
in_hash_t *bp;
if (make_pair_key(&pair_key, in_read->b)) {
fprintf(stderr, "[markdup] error: unable to assign pair hash key.\n");
goto fail;
}
make_single_key(&single_key, in_read->b);
pair++;
in_read->pos = single_key.this_coord; // cigar/orientation modified pos
// put in singles hash for checking against non paired reads
k = kh_put(reads, single_hash, single_key, &ret);
if (ret > 0) { // new
// add to single duplicate hash
bp = &kh_val(single_hash, k);
bp->p = in_read->b;
in_read->single_key = single_key;
} else if (ret == 0) { // exists
// look at singles only for duplication marking
bp = &kh_val(single_hash, k);
if (!(bp->p->core.flag & BAM_FPAIRED) || (bp->p->core.flag & BAM_FMUNMAP)) {
bam1_t *dup = bp->p;
// singleton will always be marked duplicate even if
// scores more than one read of the pair
bp->p = in_read->b;
dup->core.flag |= BAM_FDUP;
single_dup++;
if (tag) {
if (bam_aux_append(dup, "do", 'Z', strlen(bam_get_qname(bp->p)) + 1, (uint8_t*)bam_get_qname(bp->p))) {
fprintf(stderr, "[markdup] error: unable to append 'do' tag.\n");
goto fail;
}
}
if (supp) {
if (bam_aux_get(dup, "SA") || (dup->core.flag & BAM_FMUNMAP)) {
if (add_duplicate(dup_hash, dup))
goto fail;
}
}
}
} else {
fprintf(stderr, "[markdup] error: single hashing failure.\n");
goto fail;
}
// now do the pair
k = kh_put(reads, pair_hash, pair_key, &ret);
if (ret > 0) { // new
// add to the pair hash
bp = &kh_val(pair_hash, k);
bp->p = in_read->b;
in_read->pair_key = pair_key;
} else if (ret == 0) {
int64_t old_score, new_score, tie_add = 0;
bam1_t *dup;
bp = &kh_val(pair_hash, k);
if ((mate_tmp = get_mate_score(bp->p)) == -1) {
fprintf(stderr, "[markdup] error: no ms score tag. Please run samtools fixmate on file first.\n");
goto fail;
} else {
old_score = calc_score(bp->p) + mate_tmp;
}
if ((mate_tmp = get_mate_score(in_read->b)) == -1) {
fprintf(stderr, "[markdup] error: no ms score tag. Please run samtools fixmate on file first.\n");
goto fail;
} else {
new_score = calc_score(in_read->b) + mate_tmp;
}
// choose the highest score as the original
// and add it to the pair hash, mark the other as duplicate
if (new_score == old_score) {
if (strcmp(bam_get_qname(in_read->b), bam_get_qname(bp->p)) < 0) {
tie_add = 1;
} else {
tie_add = -1;
}
}
if (new_score + tie_add > old_score) { // swap reads
dup = bp->p;
bp->p = in_read->b;
} else {
dup = in_read->b;
}
dup->core.flag |= BAM_FDUP;
if (tag) {
if (bam_aux_append(dup, "do", 'Z', strlen(bam_get_qname(bp->p)) + 1, (uint8_t*)bam_get_qname(bp->p))) {
fprintf(stderr, "[markdup] error: unable to append 'do' tag.\n");
goto fail;
}
}
if (supp) {
if (bam_aux_get(dup, "SA") || (dup->core.flag & BAM_FMUNMAP)) {
if (add_duplicate(dup_hash, dup))
goto fail;
}
}
duplicate++;
} else {
fprintf(stderr, "[markdup] error: pair hashing failure.\n");
goto fail;
}
} else { // do the single (or effectively single) reads
int ret;
key_data_t single_key;
in_hash_t *bp;
make_single_key(&single_key, in_read->b);
single++;
in_read->pos = single_key.this_coord; // cigar/orientation modified pos
k = kh_put(reads, single_hash, single_key, &ret);
if (ret > 0) { // new
bp = &kh_val(single_hash, k);
bp->p = in_read->b;
in_read->single_key = single_key;
} else if (ret == 0) { // exists
bp = &kh_val(single_hash, k);
if ((bp->p->core.flag & BAM_FPAIRED) && !(bp->p->core.flag & BAM_FMUNMAP)) {
// if matched against one of a pair just mark as duplicate
if (tag) {
if (bam_aux_append(in_read->b, "do", 'Z', strlen(bam_get_qname(bp->p)) + 1, (uint8_t*)bam_get_qname(bp->p))) {
fprintf(stderr, "[markdup] error: unable to append 'do' tag.\n");
goto fail;
}
}
if (supp) {
if (bam_aux_get(in_read->b, "SA") || (in_read->b->core.flag & BAM_FMUNMAP)) {
if (add_duplicate(dup_hash, in_read->b))
goto fail;
}
}
in_read->b->core.flag |= BAM_FDUP;
} else {
int64_t old_score, new_score;
bam1_t *dup;
old_score = calc_score(bp->p);
new_score = calc_score(in_read->b);
// choose the highest score as the original, add it
// to the single hash and mark the other as duplicate
if (new_score > old_score) { // swap reads
dup = bp->p;
bp->p = in_read->b;
} else {
dup = in_read->b;
}
dup->core.flag |= BAM_FDUP;
if (tag) {
if (bam_aux_append(dup, "do", 'Z', strlen(bam_get_qname(bp->p)) + 1, (uint8_t*)bam_get_qname(bp->p))) {
fprintf(stderr, "[markdup] error: unable to append 'do' tag.\n");
goto fail;
}
}
if (supp) {
if (bam_aux_get(dup, "SA") || (dup->core.flag & BAM_FMUNMAP)) {
if (add_duplicate(dup_hash, dup))
goto fail;
}
}
}
single_dup++;
} else {
fprintf(stderr, "[markdup] error: single hashing failure.\n");
goto fail;
}
}
} else {
excluded++;
}
// loop through the stored reads and write out those we
// no longer need
rq = kl_begin(read_buffer);
while (rq != kl_end(read_buffer)) {
in_read = &kl_val(rq);
/* keep a moving window of reads based on coordinates and max read length. Any unaligned reads
should just be written as they cannot be matched as duplicates. */
if (in_read->pos + max_length > prev_coord && in_read->b->core.tid == prev_tid && (prev_tid != -1 || prev_coord != -1)) {
break;
}
if (!remove_dups || !(in_read->b->core.flag & BAM_FDUP)) {
if (supp) {
if (tmp_file_write(&temp, in_read->b)) {
fprintf(stderr, "[markdup] error: writing temp output failed.\n");
goto fail;
}
} else {
if (sam_write1(out, header, in_read->b) < 0) {
fprintf(stderr, "[markdup] error: writing output failed.\n");
goto fail;
}
}
writing++;
}
// remove from hash
if (in_read->pair_key.single == 0) {
k = kh_get(reads, pair_hash, in_read->pair_key);
kh_del(reads, pair_hash, k);
}
if (in_read->single_key.single == 1) {
k = kh_get(reads, single_hash, in_read->single_key);
kh_del(reads, single_hash, k);
}
kl_shift(read_queue, read_buffer, NULL);
bam_destroy1(in_read->b);
rq = kl_begin(read_buffer);
}
// set the next one up for reading
in_read = kl_pushp(read_queue, read_buffer);
if (!in_read) {
fprintf(stderr, "[markdup] out of memory\n");
goto fail;
}
if ((in_read->b = bam_init1()) == NULL) {
fprintf(stderr, "[markdup] error: unable to allocate memory for alignment.\n");
goto fail;
}
}
if (ret < -1) {
fprintf(stderr, "[markdup] error: truncated input file.\n");
goto fail;
}
// write out the end of the list
rq = kl_begin(read_buffer);
while (rq != kl_end(read_buffer)) {
in_read = &kl_val(rq);
if (bam_get_qname(in_read->b)) { // last entry will be blank
if (!remove_dups || !(in_read->b->core.flag & BAM_FDUP)) {
if (supp) {
if (tmp_file_write(&temp, in_read->b)) {
fprintf(stderr, "[markdup] error: writing temp output failed.\n");
goto fail;
}
} else {
if (sam_write1(out, header, in_read->b) < 0) {
fprintf(stderr, "[markdup] error: writing output failed.\n");
goto fail;
}
}
writing++;
}
}
kl_shift(read_queue, read_buffer, NULL);
bam_destroy1(in_read->b);
rq = kl_begin(read_buffer);
}
if (supp) {
bam1_t *b;
if (tmp_file_end_write(&temp)) {
fprintf(stderr, "[markdup] error: unable to end tmp writing.\n");
goto fail;
}
// read data from temp file and mark duplicate supplementary alignments
if (tmp_file_begin_read(&temp)) {
goto fail;
}
b = bam_init1();
while ((ret = tmp_file_read(&temp, b)) > 0) {
if ((b->core.flag & BAM_FSUPPLEMENTARY) || (b->core.flag & BAM_FUNMAP)) {
k = kh_get(duplicates, dup_hash, bam_get_qname(b));
if (k != kh_end(dup_hash)) {
b->core.flag |= BAM_FDUP;
}
}
if (!remove_dups || !(b->core.flag & BAM_FDUP)) {
if (sam_write1(out, header, b) < 0) {
fprintf(stderr, "[markdup] error: writing final output failed.\n");
goto fail;
}
}
}
if (ret == -1) {
fprintf(stderr, "[markdup] error: failed to read tmp file.\n");
goto fail;
}
for (k = kh_begin(dup_hash); k != kh_end(dup_hash); ++k) {
if (kh_exist(dup_hash, k)) {
free((char *)kh_key(dup_hash, k));
kh_key(dup_hash, k) = NULL;
}
}
tmp_file_destroy(&temp);
bam_destroy1(b);
}
if (do_stats) {
fprintf(stderr,
"READ: %d\n"
"WRITTEN: %d\n"
"EXCLUDED: %d\n"
"EXAMINED: %d\n"
"PAIRED: %d\n"
"SINGLE: %d\n"
"DULPICATE PAIR: %d\n"
"DUPLICATE SINGLE: %d\n"
"DUPLICATE TOTAL: %d\n", reading, writing, excluded, examined, pair, single,
duplicate, single_dup, single_dup + duplicate);
}
kh_destroy(reads, pair_hash);
kh_destroy(reads, single_hash);
kl_destroy(read_queue, read_buffer);
kh_destroy(duplicates, dup_hash);
bam_hdr_destroy(header);
return 0;
fail:
for (rq = kl_begin(read_buffer); rq != kl_end(read_buffer); rq = kl_next(rq))
bam_destroy1(kl_val(rq).b);
kl_destroy(read_queue, read_buffer);
for (k = kh_begin(dup_hash); k != kh_end(dup_hash); ++k) {
if (kh_exist(dup_hash, k)) {
free((char *)kh_key(dup_hash, k));
}
}
kh_destroy(duplicates, dup_hash);
kh_destroy(reads, pair_hash);
kh_destroy(reads, single_hash);
bam_hdr_destroy(header);
return 1;
}
static int markdup_usage(void) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: samtools markdup <input.bam> <output.bam>\n\n");
fprintf(stderr, "Option: \n");
fprintf(stderr, " -r Remove duplicate reads\n");
fprintf(stderr, " -l INT Max read length (default 300 bases)\n");
fprintf(stderr, " -S Mark supplemenary alignments of duplicates as duplicates (slower).\n");
fprintf(stderr, " -s Report stats.\n");
fprintf(stderr, " -T PREFIX Write temporary files to PREFIX.samtools.nnnn.nnnn.tmp.\n");
fprintf(stderr, " -t Mark primary duplicates with the name of the original in a \'do\' tag."
" Mainly for information and debugging.\n");
sam_global_opt_help(stderr, "-.O..@");
fprintf(stderr, "\nThe input file must be coordinate sorted and must have gone"
" through fixmates with the mate scoring option on.\n");
return 1;
}
int bam_markdup(int argc, char **argv) {
int c, ret, remove_dups = 0, report_stats = 0, include_supplementary = 0, tag_dup = 0;
int32_t max_length = 300;
samFile *in = NULL, *out = NULL;
char wmode[3] = {'w', 'b', 0};
sam_global_args ga = SAM_GLOBAL_ARGS_INIT;
htsThreadPool p = {NULL, 0};
kstring_t tmpprefix = {0, 0, NULL};
struct stat st;
unsigned int t;
static const struct option lopts[] = {
SAM_OPT_GLOBAL_OPTIONS('-', 0, 'O', 0, 0, '@'),
{NULL, 0, NULL, 0}
};
while ((c = getopt_long(argc, argv, "rsl:StT:O:@:", lopts, NULL)) >= 0) {
switch (c) {
case 'r': remove_dups = 1; break;
case 'l': max_length = atoi(optarg); break;
case 's': report_stats = 1; break;
case 'T': kputs(optarg, &tmpprefix); break;