-
Notifications
You must be signed in to change notification settings - Fork 2
/
dupsifter.c
1312 lines (1130 loc) · 48.8 KB
/
dupsifter.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
/* Mark duplicates for both WGS and WGBS reads
*
* The MIT License (MIT)
*
* Copyright (c) 2022-2023 [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.
*
*
* This utility is largely based on samblaster's methodology for marking
* duplicates, as it is fast and memory efficient. However, there are some
* cytosine-conversion-aware things that must be taken into account that
* samblaster does not take into account.
*
* Original Header from Samblaster samblaster.cpp
*
* -*- mode: C++ ; indent-tabs-mode: nil ; c-file-style: "stroustrup" -*-
*
* Project: samblaster
* Fast mark duplicates in read-ID grouped SAM file.
* Also, optionally pull discordants, splitters, and/or unmappend/clipped reads.
* Author: Greg Faust ([email protected])
* Date: October 2013
* Cite: SAMBLASTER: fast duplicate marking and structural variant read extraction
* GG Faust, IM Hall
* Bioinformatics 30 (17), 2503-2505
* https://academic.oup.com/bioinformatics/article/30/17/2503/2748175
*
* File: samblaster.cpp code file for the main routine and most of the other code.
*
* License Information:
*
* Copyright 2013-2020 Gregory G. Faust
*
* Licensed under the MIT license (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
*
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
#include "sam.h"
#include "khashl.h"
#include "util.h"
#include "refcache.h"
#include "version.h"
//---------------------------------------------------------------------------------------------------------------------
// Config struct, initialization function, and output function
#define DEF_STAT_NAME "dupsifter.stat"
#define TAG_STAT_NAME ".dupsifter.stat"
typedef struct {
char *reffn; /* reference file name */
char *infn; /* name of input file */
char *outfn; /* name of output file */
char *statfn; /* name of statistics file */
char out_mode[6]; /* write mode of output file */
char *arg_list; /* input argument list for PG tag */
uint32_t min_base_qual; /* threshold for high quality bases */
uint32_t max_length; /* max read length allowed */
uint8_t rm_dup; /* flag to remove duplicates */
uint8_t is_wgs; /* process reads as WGS instead of WGBS */
uint8_t verbose; /* level of messages to print */
uint8_t single_end; /* process all reads as single-end */
uint8_t add_mate_tags; /* add MC and MQ tags to mate reads */
uint8_t has_barcode; /* barcode included in reads */
uint32_t n_reads_read; /* Number of reads read from file */
uint32_t cnt_id_both_map; /* number of PE reads */
uint32_t cnt_id_forward; /* number of reads where only one read is mapped and it's on the forward strand */
uint32_t cnt_id_reverse; /* number of reads where only one read is mapped and it's on the reverse strand */
uint32_t cnt_id_both_dup; /* number of PE reads marked as dup */
uint32_t cnt_id_forward_dup; /* number of duplicates on the forward strand */
uint32_t cnt_id_reverse_dup; /* number of duplicates on the reverse strand */
uint32_t cnt_id_no_map; /* number of reads with both reads unmapped (PE) or the read is unmapped (SE) */
uint32_t cnt_id_no_prim; /* number of reads with no primary reads */
uint32_t cnt_id_n_primary; /* number of primary alignments */
uint32_t cnt_id_n_sec_sup; /* number of secondary and supplementary alignments */
} ds_conf_t;
void ds_conf_init(ds_conf_t *conf) {
strcpy(conf->out_mode, "w");
conf->outfn = (char *)"-";
conf->arg_list = NULL;
conf->statfn = NULL;
conf->min_base_qual = 0;
conf->max_length = 10000;
conf->rm_dup = 0;
conf->is_wgs = 0;
conf->verbose = 0;
conf->single_end = 0;
conf->add_mate_tags = 0;
conf->has_barcode = 0;
conf->n_reads_read = 0;
conf->cnt_id_both_map = 0;
conf->cnt_id_forward = 0;
conf->cnt_id_reverse = 0;
conf->cnt_id_both_dup = 0;
conf->cnt_id_forward_dup = 0;
conf->cnt_id_reverse_dup = 0;
conf->cnt_id_no_map = 0;
conf->cnt_id_no_prim = 0;
conf->cnt_id_n_primary = 0;
conf->cnt_id_n_sec_sup = 0;
}
void ds_conf_destroy(ds_conf_t *conf) {
free(conf->arg_list);
if (conf->statfn != NULL) { free(conf->statfn); }
}
void ds_conf_print(ds_conf_t *conf) {
FILE *statfh = fopen(conf->statfn, "w");
fprintf(statfh, "[dupsifter] processing mode: %s\n", (conf->single_end) ? "single-end" : "paired-end");
fprintf(statfh, "[dupsifter] number of individual reads processed: %u\n", conf->n_reads_read);
fprintf(statfh, "[dupsifter] number of reads with both reads mapped: %u\n", conf->cnt_id_both_map);
fprintf(statfh, "[dupsifter] number of reads with only one read mapped to the forward strand: %u\n", conf->cnt_id_forward);
fprintf(statfh, "[dupsifter] number of reads with only one read mapped to the reverse strand: %u\n", conf->cnt_id_reverse);
fprintf(statfh, "[dupsifter] number of reads with both reads marked as duplicates: %u\n", conf->cnt_id_both_dup);
fprintf(statfh, "[dupsifter] number of reads on the forward strand marked as duplicates: %u\n", conf->cnt_id_forward_dup);
fprintf(statfh, "[dupsifter] number of reads on the reverse strand marked as duplicates: %u\n", conf->cnt_id_reverse_dup);
fprintf(statfh, "[dupsifter] number of individual primary-alignment reads: %u\n", conf->cnt_id_n_primary);
fprintf(statfh, "[dupsifter] number of individual secondary- and supplementary-alignment reads: %u\n", conf->cnt_id_n_sec_sup);
fprintf(statfh, "[dupsifter] number of reads with no reads mapped: %u\n", conf->cnt_id_no_map);
fprintf(statfh, "[dupsifter] number of reads with no primary reads: %u\n", conf->cnt_id_no_prim);
fclose(statfh);
}
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Struct for storing all reads matching a given read name
typedef struct bam1_chain bam1_chain_t;
struct bam1_chain {
bam1_t *read;
struct bam1_chain *next;
};
bam1_chain_t *bam1_chain_init(bam1_t *b) {
bam1_chain_t *out = (bam1_chain_t *)malloc(sizeof(bam1_chain_t));
if (b == NULL) { out->read = bam_init1(); }
else { out->read = b; }
out->next = NULL;
return out;
}
void destroy_bam1_chain(bam1_chain_t *b) {
if (b == NULL) { return; }
struct bam1_chain *temp = b;
while (temp != NULL) {
struct bam1_chain *next = temp->next;
if (temp->read != NULL) { bam_destroy1(temp->read); }
free(temp);
temp = next;
}
if (temp != NULL) { free(temp); }
}
void add_new_read_to_chain(bam1_chain_t **head, bam1_t *b) {
bam1_chain_t *new = bam1_chain_init(b);
bam1_chain_t *last = *head;
if (*head == NULL) {
*head = new;
return;
}
while (last->next != NULL) { last = last->next; }
last->next = new;
return;
}
uint32_t get_count(bam1_chain_t *bc) {
uint32_t count = 0;
bam1_chain_t *curr = bc;
while (curr != NULL) {
count++;
curr = curr->next;
}
return count;
}
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Structs for parsing CIGAR string and the signature, plus any init functions that are needed
typedef struct {
uint32_t rlen; /* reference length */
uint32_t qlen; /* query (read) length */
uint32_t sclp; /* soft/hard clips at start of read */
uint32_t eclp; /* soft/hard clips at end of read */
} parsed_cigar_t;
// Read orientation possibilities (r1-r2: forward-forward, reverse-reverse, forward-reverse, reverse-forward)
#define OO_FF 0
#define OO_RR 1
#define OO_FR 2
#define OO_RF 3
// Is read single (or paired with unmapped mate) or paired?
#define S_N 0
#define S_Y 1
// Is read1 in paired reads the leftmost read (single/paired with unmapped mate always L_N)?
#define L_N 0
#define L_Y 1
typedef struct {
uint8_t l_or_s; /* packed variable, (from leftmost bit) bit 5 = is r1 leftmost read, bit 6/7 = orientation, bit 8 = is single */
uint16_t r1_bin; /* bin number for r1 position */
uint16_t r2_bin; /* bin number for r2 position */
uint32_t r1_pos; /* bin position for r1 position */
uint32_t r2_pos; /* bin position for r2 position */
uint64_t barcod; /* packed cell barcode */
} signature_t;
signature_t signature_init() {
// Chances of the bin or position being the max value are very low, so set default value to max value
signature_t sig = {0};
sig.l_or_s = 128;
sig.r1_bin = UINT16_MAX;
sig.r2_bin = UINT16_MAX;
sig.r1_pos = UINT32_MAX;
sig.r2_pos = UINT32_MAX;
sig.barcod = 0;
return sig;
}
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Initialize hash maps and define functions needed for initialization
khint_t hash_sig(signature_t s) {
khint_t to_hash;
// Create input value for hashing function
// Pull off last 16 bits of r1_pos
uint16_t pos = s.r1_pos & 0xffff;
// Pull off last 12 bits of r1_bin
uint16_t bin = s.r1_bin & 0x0fff;
// Bit pattern in to_hash
// PPPP PPPP PPPP PPPP BBBB BBBB BBBB LOOS
// |_________________| |____________| |__|
// | | |
// |-----------------| |------------| |--|
// Position Bin r1_leftmost (L)/orientation (OO)/is_single (S)
to_hash = (pos << 16) + (bin << 4) + (s.l_or_s & 0xf);
return kh_hash_uint32(to_hash);
}
int sig_equal(signature_t s1, signature_t s2) {
if (s1.l_or_s != s2.l_or_s) { return 0; }
if (s1.r1_bin != s2.r1_bin) { return 0; }
if (s1.r1_pos != s2.r1_pos) { return 0; }
if (s1.r2_bin != s2.r2_bin) { return 0; }
if (s1.r2_pos != s2.r2_pos) { return 0; }
if (s1.barcod != s2.barcod) { return 0; }
return 1;
}
KHASHL_SET_INIT(, SigHash, sh, signature_t, hash_sig, sig_equal)
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Genome bins/Hash map struct, init and destroy functions
typedef struct {
uint32_t n_contigs; /* number of contigs */
uint64_t total_length; /* total length of genome */
uint32_t *length; /* length of contigs */
uint64_t *offset; /* offset from start of contigs */
uint32_t n_bins; /* number of bins in genome */
} ds_bins_t;
ds_bins_t *ds_bins_init() {
return (ds_bins_t *)calloc(1, sizeof(ds_bins_t));
}
void destroy_ds_bins(ds_bins_t *b) {
if (b->length != NULL) { free(b->length); }
if (b->offset != NULL) { free(b->offset); }
free(b);
}
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Padding functions
// These functions account for any reads that may run off the start or end of a chromosome, which may happen due to
// soft clipped reads
static inline uint32_t pad_chrom_length(uint32_t length, uint32_t max_length) {
return length + (2 * max_length);
}
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Bisulfite strand functions
uint8_t infer_bsstrand(refcache_t *rs, bam_hdr_t *hdr, bam1_t *b, uint32_t min_base_qual) {
bam1_core_t *c = &b->core;
// Cache reference
refcache_fetch(rs, hdr->target_name[c->tid], c->pos > 100 ? c->pos-100 : 1, c->pos + c->l_qseq + 100);
uint32_t rpos = c->pos+1, qpos = 0, nC2T = 0, nG2A = 0;
uint32_t i, op, oplen;
char rb, qb;
uint32_t j;
for (i=0; i<c->n_cigar; ++i) {
op = bam_cigar_op(bam_get_cigar(b)[i]);
oplen = bam_cigar_oplen(bam_get_cigar(b)[i]);
switch(op) {
// While in theory Equal (=) should mean the query base matches the reference base,
// it's possible that some WGBS aligners use '=' to denote bases where cytosine
// conversion occurred. Therefore, group Match (M), Equal (=), and Different (X)
// together for processing
case BAM_CMATCH:
case BAM_CEQUAL:
case BAM_CDIFF:
for (j=0; j<oplen; ++j) {
rb = refcache_getbase_upcase(rs, rpos+j);
qb = bscall(b, qpos+j);
if (bam_get_qual(b)[qpos+j] < min_base_qual) { continue; }
if (rb == 'C' && qb == 'T') { nC2T++; }
if (rb == 'G' && qb == 'A') { nG2A++; }
}
rpos += oplen;
qpos += oplen;
break;
case BAM_CINS:
qpos += oplen;
break;
case BAM_CDEL:
rpos += oplen;
break;
case BAM_CSOFT_CLIP:
qpos += oplen;
break;
case BAM_CHARD_CLIP:
qpos += oplen;
break;
default:
fatal_error("[dupsifter] ERROR: Unknown cigar operation, %u\n", op);
}
}
if (nC2T >= nG2A) { return 0; }
else { return 1; }
}
uint8_t get_bsstrand(refcache_t *rs, bam_hdr_t *hdr, bam1_t *b, uint32_t min_base_qual, uint8_t allow_u, uint8_t verbose) {
uint8_t *s;
/* bwa-meth flag has highest priority */
s = bam_aux_get(b, "YD");
if (s) {
s++;
if (*s == 'f') { return 0; }
else if (*s == 'r') { return 1; }
else if (*s == 'u' && allow_u) { return 2; }
}
/* bsmap flag */
s = bam_aux_get(b, "ZS");
if (s) {
s++;
if (*s == '+') { return 0; }
else if (*s == '-') { return 1; }
}
/* bismark flag */
s = bam_aux_get(b, "XG");
if (s) {
s++;
if (strcmp((char*)s, "CT")==0) { return 0; }
else if (strcmp((char*)s, "GA")==0) { return 1; }
}
/* otherwise, guess the bsstrand from nCT and nGA */
uint8_t bss = infer_bsstrand(rs, hdr, b, min_base_qual);
if (verbose) {
fprintf(stderr, "[dupsifter] DEBUG: read name: %s, inferred bisulfite strand: %s\n",
bam_get_qname(b), bss ? "OB/CTOB" : "OT/CTOT");
}
return bss;
}
uint8_t determine_bsstrand(refcache_t *rs, bam_hdr_t *hdr, bam1_t *one, bam1_t *two,
ds_conf_t *conf, uint8_t allow_u) {
int8_t bss1 = -1, bss2 = -1;
if (one && !is_unmapped(one)) {
bss1 = get_bsstrand(rs, hdr, one, conf->min_base_qual, allow_u, conf->verbose);
}
if (two && !is_unmapped(two)) {
bss2 = get_bsstrand(rs, hdr, two, conf->min_base_qual, allow_u, conf->verbose);
}
if (bss1 == bss2 && bss1 >= 0) {
return (uint8_t)bss1;
} else {
// Resolve which bisulfite strand to use
if (bss1 < 0 && bss2 >= 0) { return (uint8_t)bss2; }
else if (bss2 < 0 && bss1 >= 0) { return (uint8_t)bss1; }
else if (bss1 < 0 && bss2 < 0) {
// Rare case, assume OT/CTOT
if (conf->verbose) {
fprintf(stderr,
"[dupsifter] DEBUG: No valid reads to determine bisulfite strand info. Assuming OT/CTOT strand.\n");
fflush(stderr);
}
return 0;
}
else {
if (conf->verbose) {
fprintf(stderr, "[dupsifter] DEBUG: Inconsistent bisulfite strand info. ");
fprintf(stderr, "Taking strand from read with higher total base quality.\n");
fflush(stderr);
}
uint8_t out = total_qual(one) > total_qual(two) ? (uint8_t)bss1 : (uint8_t)bss2;
return out;
}
}
}
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Following samblaster's method for handling contigs (particularly genomes with many, many contigs), concatenate all
// contigs in the genome (using the BAM header to define the contigs) into a single full-genome contig. Then, partition
// the full-genome contig out into roughly evenly sized bins (see https://github.com/GregoryFaust/samblaster/issues/21)
// for rationale for this choice of binning.
// Value that samblaster uses for resizing length and offset arrays
// This value may be able to be optimized, but use it for now
#define BIN_RESIZE 32768
// To recap samblaster, 27 bits was large enough to hold human chromosome 1 offset
// This value could be optimized to balance memory/time trade-offs, but for now
// this can stay as it is
#define BIN_SHIFT ((uint64_t)27)
#define BIN_MASK ((uint64_t)((1 << BIN_SHIFT) - 1))
ds_bins_t *prepare_bin_offsets(bam_hdr_t *hdr, ds_conf_t *conf) {
ds_bins_t *b = ds_bins_init();
b->n_contigs = hdr->n_targets;
uint64_t sum_length = 0;
int32_t i;
for (i=0; i<hdr->n_targets; i++) {
// Resize length and offset arrays
if (i % BIN_RESIZE == 0) {
b->length = (uint32_t *)realloc(b->length, (i+BIN_RESIZE)*sizeof(uint32_t));
b->offset = (uint64_t *)realloc(b->offset, (i+BIN_RESIZE)*sizeof(uint64_t));
}
// Fill length and offset values for specified contig
b->length[i] = pad_chrom_length(hdr->target_len[i], conf->max_length);
b->offset[i] = sum_length;
sum_length += (uint64_t)(b->length[i]);
}
b->total_length = sum_length;
b->n_bins = (sum_length >> BIN_SHIFT) + 1;
if (b->n_bins >= BIN_RESIZE) {
// NOTE: exiting here may cause a memory leak, depending on how clean up is handled
fprintf(stderr, "[dupsifter] ERROR: Too many contigs found in BAM header\n");
exit(1);
}
return b;
}
//---------------------------------------------------------------------------------------------------------------------
parsed_cigar_t parse_cigar(bam1_t *b) {
bam1_core_t *c = &b->core;
uint32_t i, op, oplen;
uint32_t rlen = 0, qlen = 0, sclip = 0, eclip = 0;
uint8_t first_op = 1;
for (i=0; i<c->n_cigar; ++i) {
op = bam_cigar_op(bam_get_cigar(b)[i]);
oplen = bam_cigar_oplen(bam_get_cigar(b)[i]);
switch(op) {
case BAM_CMATCH:
rlen += oplen;
qlen += oplen;
first_op = 0;
break;
case BAM_CINS:
qlen += oplen;
first_op = 0;
break;
case BAM_CDEL:
rlen += oplen;
first_op = 0;
break;
case BAM_CREF_SKIP:
rlen += oplen;
first_op = 0;
break;
case BAM_CSOFT_CLIP:
if (first_op) { sclip += oplen; }
else { eclip += oplen; }
break;
case BAM_CHARD_CLIP:
if (first_op) { sclip += oplen; }
else { eclip += oplen; }
break;
case BAM_CEQUAL:
rlen += oplen;
qlen += oplen;
first_op = 0;
break;
case BAM_CDIFF:
rlen += oplen;
qlen += oplen;
first_op = 0;
break;
default:
fatal_error("[dupsifter] ERROR: Unknown cigar operation: %u\n", op);
}
}
parsed_cigar_t out = {0};
out.rlen = rlen;
out.qlen = qlen;
out.sclp = sclip;
out.eclp = eclip;
return out;
}
signature_t create_single_signature(bam1_t *read, ds_conf_t *conf, ds_bins_t *bins, uint64_t packed_barcode) {
if (read == NULL) {
fatal_error("[dupsifter] ERROR: Read information for single or mate-unmapped read is missing\n");
}
parsed_cigar_t cigar = parse_cigar(read);
uint32_t total_length = cigar.sclp + cigar.qlen + cigar.eclp;
if (total_length > conf->max_length) {
fatal_error("[dupsifter] ERROR: Read with length %u is longer than max read length %u\n",
total_length, conf->max_length);
}
uint64_t position;
uint8_t orientation;
if (bam_is_rev(read)) {
position = bins->offset[read->core.tid] + read->core.pos + cigar.rlen + cigar.eclp;
orientation = OO_RR;
} else {
position = bins->offset[read->core.tid] + read->core.pos - cigar.sclp;
orientation = OO_FF;
}
signature_t sig = signature_init();
sig.r1_bin = position >> BIN_SHIFT;
sig.r1_pos = position & BIN_MASK;
sig.l_or_s = (L_N << 3) + (orientation << 1) + S_Y;
sig.barcod = packed_barcode;
return sig;
}
signature_t create_paired_signature(bam1_t *read1, bam1_t *read2, ds_conf_t *conf, ds_bins_t *bins,
uint64_t packed_barcode) {
if (read1 == NULL || read2 == NULL) {
fatal_error("[dupsifter] ERROR: Read information for paired end read is missing\n");
}
parsed_cigar_t cigar1 = parse_cigar(read1);
parsed_cigar_t cigar2 = parse_cigar(read2);
uint32_t total_length_1 = cigar1.sclp + cigar1.qlen + cigar1.eclp;
uint32_t total_length_2 = cigar2.sclp + cigar2.qlen + cigar2.eclp;
if (total_length_1 > conf->max_length) {
fatal_error("[dupsifter] ERROR: Read with length %u is longer than max read length %u\n",
total_length_1, conf->max_length);
}
if (total_length_2 > conf->max_length) {
fatal_error("[dupsifter] ERROR: Read with length %u is longer than max read length %u\n",
total_length_2, conf->max_length);
}
uint64_t pos1, pos2, beg1, beg2, end1, end2;
uint32_t ref1, ref2;
uint8_t r1_leftmost, orientation;
beg1 = bins->offset[read1->core.tid] + read1->core.pos - cigar1.sclp;
beg2 = bins->offset[read2->core.tid] + read2->core.pos - cigar2.sclp;
end1 = bins->offset[read1->core.tid] + read1->core.pos + cigar1.rlen + cigar1.eclp;
end2 = bins->offset[read2->core.tid] + read2->core.pos + cigar2.rlen + cigar2.eclp;
ref1 = read1->core.tid;
ref2 = read2->core.tid;
// Work out if read1 is leftmost in pair
if (ref1 != ref2) {
r1_leftmost = ref1 < ref2;
} else {
if (bam_is_rev(read1) == bam_is_rev(read1)) {
if (!bam_is_rev(read1)) {
r1_leftmost = beg1 <= beg2;
} else {
r1_leftmost = end1 <= end2;
}
} else {
if (bam_is_rev(read1)) {
r1_leftmost = end1 <= end2;
} else {
r1_leftmost = beg1 <= beg2;
}
}
}
r1_leftmost = r1_leftmost ? L_Y : L_N;
// Determine orientation of reads
if (bam_is_rev(read1) == bam_is_rev(read2)) {
if (!bam_is_rev(read1)) {
orientation = OO_FF;
} else {
orientation = OO_RR;
}
} else {
if (!bam_is_rev(read1)) {
orientation = OO_FR;
} else {
orientation = OO_RF;
}
}
// Find final positions
switch (orientation) {
case OO_FF:
pos1 = beg1;
pos2 = beg2;
break;
case OO_RR:
pos1 = end1;
pos2 = end2;
break;
case OO_FR:
pos1 = beg1;
pos2 = end2;
break;
case OO_RF:
pos1 = end1;
pos2 = beg2;
break;
default:
fatal_error("[dupsifter] ERROR: Unknown orientation found.\n");
}
signature_t sig = signature_init();
sig.r1_bin = pos1 >> BIN_SHIFT;
sig.r1_pos = pos1 & BIN_MASK;
sig.r2_bin = pos2 >> BIN_SHIFT;
sig.r2_pos = pos2 & BIN_MASK;
sig.l_or_s = (r1_leftmost << 3) + (orientation << 1) + S_N;
sig.barcod = packed_barcode;
return sig;
}
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Functions for adding mate (MC/MQ) tags
static uint8_t create_cigar_string(bam1_t *b, kstring_t *str) {
// Empty CIGAR gets a "*"
if (b->core.n_cigar == 0) {
return (kputc('*', str) == EOF) ? 0 : 1;
}
uint32_t *cigar = bam_get_cigar(b);
uint32_t i;
for (i=0; i<b->core.n_cigar; i++) {
if (kputw(bam_cigar_oplen(cigar[i]), str) == EOF) { return 0; }
if (kputc(bam_cigar_opchr(cigar[i]), str) == EOF) { return 0; }
}
return 1;
}
void add_MCMQ(bam1_chain_t *bc, bam1_t *b_read, bam1_t *b_mate) {
if (is_unmapped(b_read)) { return; }
int mask = (b_mate->core.flag & (0x40 | 0x80));
kstring_t mc = { 0, 0, NULL };
if (!create_cigar_string(b_read, &mc)) { return; }
uint32_t mq = b_read->core.qual;
for (bam1_chain_t *next = bc; next != NULL; next = next->next) {
bam1_t *read = next->read;
if (read->core.flag & mask) {
if (bam_aux_get(read, "MC") == NULL) {
bam_aux_append(read, "MC", 'Z', ks_len(&mc)+1, (uint8_t *)ks_str(&mc));
}
if (bam_aux_get(read, "MQ") == NULL) {
bam_aux_append(read, "MQ", 'i', sizeof(uint32_t), (uint8_t *)&mq);
}
}
}
free(mc.s);
}
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
void problem_chain(bam1_chain_t *bc, uint32_t count) {
fatal_error("[dupsifter] ERROR: Can't find read 1 and/or read 2 in %u reads with read ID: %s. Are these reads coordinate sorted?\n",
count, bam_get_qname(bc->read));
}
uint64_t get_packed_barcode(bam1_t *read1, ds_conf_t *conf) {
// Barcode is packed by placing two bases per byte (1 base = 4 bytes)
// Bases are placed from right to left in the packed integer:
//
// Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6 Byte 7 Byte 8
// FFFFEEEE DDDDCCCC BBBBAAAA 99998888 77776666 55554444 33332222 11110000
//
// for bases 0 through 15 (in hexadecimal). By doing this, shorter barcodes
// will have smaller values.
uint64_t out = 0;
uint8_t *s;
// Since we can only pack 16 bases in the integer, need to track how many bases
// we've looked at.
uint8_t count = 0;
// TODO: Right now, for CB/CR/parse if it's a barcode with multiple elements (separated by either "+" or "-")
// the separator is treated as an "N". It would be better to handle multiple parts separately, whether
// that is a unique packed int for each one, or packing everything into one int as best as possible
// TODO: Add ability to handle UMIs (MI and OX SAM tags), also found in read name at times
// Right now, there are few (if any) WGBS preps that include UMIs, so this is low priority at this
// time (June 2023)
// Start by looking for the CB auxiliary tag (cell identifier)
// This could be a corrected sequence, which should hopefully avoid any basecalling errors
s = bam_aux_get(read1, "CB");
if (s) {
uint8_t *full = s+1;
s++;
while (*s) {
if (*s != 'N' && *s != 'A' && *s != 'C' && *s != 'G' && *s != 'T' && *s != '+' && *s != '-') {
fprintf(stderr, "[dupsifter] WARNING: Unable to parse barcode from CB tag ('barcode': %s). Using default barcode.\n", full);
return 0;
}
out |= (nuc_to_uint8[*s] << 4*count);
count++;
if (count == 16) {
// Maxed out the number of characters allowed in the packed int
if (conf->verbose) {
fprintf(stderr, "[dupsifter] DEBUG: Barcode length > 16. Capping length at 16 for signature.\n");
}
break;
}
s++;
}
return out;
}
// If CB cannot be found, try finding the CR tag, which is the uncorrected sequence that comes off the sequencer
s = bam_aux_get(read1, "CR");
if (s) {
uint8_t *full = s+1;
s++;
while (*s) {
if (*s != 'N' && *s != 'A' && *s != 'C' && *s != 'G' && *s != 'T' && *s != '+' && *s != '-') {
fprintf(stderr, "[dupsifter] WARNING: Unable to parse barcode from CB tag ('barcode': %s). Using default barcode.\n", full);
return 0;
}
out |= (nuc_to_uint8[*s] << 4*count);
count++;
if (count == 16) {
// Maxed out the number of characters allowed in the packed int
if (conf->verbose) {
fprintf(stderr, "[dupsifter] DEBUG: Barcode length > 16. Capping length at 16 for signature.\n");
}
break;
}
s++;
}
return out;
}
// If CB and CR can't be found, then try parsing the read name
// For now, follow the Bismark requirement where the barcode must be the last element in the read name
// (separator = ":")
char *qname = bam_get_qname(read1);
char *last = strrchr(qname, ':');
char *full = last+1;
if (last) {
last++;
while (*last) {
if (*last != 'N' && *last != 'A' && *last != 'C' && *last != 'G' && *last != 'T' && *last != '+' && *last != '-') {
fprintf(stderr, "[dupsifter] WARNING: Unable to parse barcode from read name ('barcode': %s). Using default barcode.\n", full);
return 0;
}
out |= (nuc_to_uint8[(uint8_t)*last] << 4*count);
count++;
if (count == 16) {
// Maxed out the number of characters allowed in the packed int
if (conf->verbose) {
fprintf(stderr, "[dupsifter] DEBUG: Barcode length > 16. Capping length at 16 for signature.\n");
}
break;
}
last++;
}
return out;
}
// Default to 0 if we couldn't find the CB or CR tags or parse from the read name
fprintf(stderr, "[dupsifter] WARNING: Could not find CB or CR SAM tags or parse barcode from read name. Using default barcode.\n");
return out;
}
void mark_dup(bam1_chain_t *bc, bam_hdr_t *hdr, ds_conf_t *conf, refcache_t *rs, ds_bins_t *bins,
SigHash *ot_map, SigHash *ot_for, SigHash *ot_rev,
SigHash *ob_map, SigHash *ob_for, SigHash *ob_rev) {
bam1_t *r1 = NULL;
bam1_t *r2 = NULL;
uint32_t count = 0;
for (bam1_chain_t *curr = bc; curr != NULL; curr = curr->next) {
count++;
// Remove any duplicate marks that have already been applied
curr->read->core.flag &= ~BAM_FDUP;
// Secondary and supplementary alignments are not used to determine duplicates
if (!is_primary(curr->read)) { conf->cnt_id_n_sec_sup++; continue; }
// If unpaired, set as r1 for creating signature
if (!is_paired(curr->read)) { r1 = curr->read; }
// Set reads 1 and 2
else if (is_first_read(curr->read)) { r1 = curr->read; }
else if (is_second_read(curr->read)) { r2 = curr->read; }
conf->cnt_id_n_primary++;
}
if (r1 == NULL && r2 == NULL) {
conf->cnt_id_no_prim++;
fprintf(stderr, "[dupsifter] WARNING: No valid primary alignments found for read ID: %s", bam_get_qname(bc->read));
return;
}
// Check for singleton (either SE read or PE read with unmapped mate) reads
// Generate packed barcode now as the barcode will be on read 1, but in the case where read 1 is unmapped, then
// read 2 will be treated as a singleton "read 1" for the signature creation
uint8_t is_single = 0;
uint64_t packed_barcode = 0;
if (r1 == NULL || r2 == NULL) {
if (r1 == NULL) { swap_bam1_pointers(&r1, &r2); }
// If there is only one read that says it's paired, but is unmapped or
// it's mate is mapped, then something went wrong
if (is_paired(r1) && (is_unmapped(r1) || !is_mate_unmapped(r1))) {
problem_chain(bc, count);
}
// If the only read is unmapped, then it isn't a duplicate
if (is_unmapped(r1) && !conf->single_end) {
problem_chain(bc, count);
} else if (is_unmapped(r1) && conf->single_end) {
conf->cnt_id_no_map++;
return;
}
// Extract packed barcode for single-end reads
if (conf->single_end && conf->has_barcode) {
packed_barcode = get_packed_barcode(r1, conf);
}
is_single = 1;
} else {
// Add MC and MQ tags if desired
if (conf->add_mate_tags) {
add_MCMQ(bc, r1, r2);
add_MCMQ(bc, r2, r1);
}
// Don't mark reads as duplicates if both are unmapped
if (is_unmapped(r1) && is_unmapped(r2)) {
conf->cnt_id_no_map++;
return;
}
// Extract packed barcode before shifting around singletons
if (conf->has_barcode) {
packed_barcode = get_packed_barcode(r1, conf);
}
// Check for unmapped mate
is_single = (is_unmapped(r1) || is_unmapped(r2));
if (is_unmapped(r1) && !is_unmapped(r2)) {
swap_bam1_pointers(&r1, &r2);
}
}
// If unable to determine OT/CTOT or OB/CTOB, defaults to OT/CTOT
// For WGS case, only use OT/CTOT
uint8_t bss = 0;
if (!conf->is_wgs) {
bss = determine_bsstrand(rs, hdr, r1, r2, conf, 0);
}
uint8_t is_forward = 0; // used only for single reads to determine strand
signature_t sig = signature_init();
if (is_single) {
if (bam_is_rev(r1)) {
conf->cnt_id_reverse++;
} else {
conf->cnt_id_forward++;
is_forward = 1;
}
// Singleton signature's still need a barcode because paired-end reads
// with one unmapped read will still have a barcode. Single-end reads
// are okay to have a barcode entered, since it will default to zero
// for all reads.
sig = create_single_signature(r1, conf, bins, packed_barcode);
} else {
conf->cnt_id_both_map++;
sig = create_paired_signature(r1, r2, conf, bins, packed_barcode);
}
// Do duplicate marking, either all reads in chain will marked as dups or none will
int not_a_dup;
if (!is_single) {
if (bss) {
sh_put(ob_map, sig, ¬_a_dup);
} else {
sh_put(ot_map, sig, ¬_a_dup);
}
if (!not_a_dup) { // signature found!
// Both reads need to marked as duplicates in this case
conf->cnt_id_both_dup++;
for (bam1_chain_t *curr = bc; curr != NULL; curr = curr->next) {
curr->read->core.flag |= BAM_FDUP;
}
}
} else {
if (is_forward) {
if (bss) {
sh_put(ob_for, sig, ¬_a_dup);
} else {
sh_put(ot_for, sig, ¬_a_dup);
}
if (!not_a_dup) { // signature found!
conf->cnt_id_forward_dup++;
for (bam1_chain_t *curr = bc; curr != NULL; curr = curr->next) {
curr->read->core.flag |= BAM_FDUP;