forked from samtools/samtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bam2depth.c
999 lines (892 loc) · 33.1 KB
/
bam2depth.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
/* bam2depth.c -- depth subcommand.
Copyright (C) 2011, 2012 Broad Institute.
Copyright (C) 2012-2016, 2018, 2019-2022 Genome Research Ltd.
Author: Heng Li <[email protected]> (to 2020)
Author: James Bonfield <[email protected]> (2021 rewrite)
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 program demonstrates how to generate pileup from multiple BAMs
* simultaneously, to achieve random access and to use the BED interface.
* To compile this program separately, you may:
*
* gcc -g -O2 -Wall -o bam2depth -D_MAIN_BAM2DEPTH bam2depth.c -lhts -lz
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include "htslib/sam.h"
#include "samtools.h"
#include "bedidx.h"
#include "sam_opts.h"
#include "htslib/khash.h"
// From bam_plcmd.c
int read_file_list(const char *file_list, int *n, char **argv[]);
// We accumulate to hist[pos & (size-1)]. This is a ring-buffer.
// We track where we last got to in output and what the biggest value
// we've written to so far (in absolute unmasked coordinates) in
// "last_output" and "end_pos" respectively.
// For each new record we just flush anything we haven't written yet
// already, between "last_output" and this read's start position, and
// initialise any newly seen positions between "end_pos" and this read's
// end position.
typedef struct {
size_t size;
int **hist; // hist[nfiles][size]
hts_pos_t *end_pos; // end_pos[nfiles]
hts_pos_t last_output;
int last_ref;
int nfiles;
const char *ref;
kstring_t ks;
hts_pos_t beg, end; // limit to region
int tid;
} depth_hist;
typedef struct {
int header;
int flag;
int incl_flag;
int require_flag;
int min_qual;
int min_mqual;
int min_len;
int skip_del;
int all_pos;
int remove_overlaps;
FILE *out;
char *reg;
void *bed;
} depth_opt;
static void zero_region(depth_opt *opt, depth_hist *dh,
const char *name, hts_pos_t start, hts_pos_t end) {
hts_pos_t i;
kstring_t *ks = &dh->ks;
kputs(name, ks_clear(ks));
kputc('\t', ks);
size_t cur_l = ks->l;
if (dh->beg >= 0 && start < dh->beg)
start = dh->beg;
if (dh->end >= 0 && end > dh->end)
end = dh->end;
for (i = start; i < end; i++) {
// Could be optimised, but needs better API to skip to next
// bed region.
if (opt->bed && bed_overlap(opt->bed, name, i, i+1) == 0)
continue;
ks->l = cur_l;
kputll(i+1, ks);
int n;
for (n = 0; n < dh->nfiles; n++) {
kputc_('\t', ks);
kputc_('0', ks);
}
kputc('\n', ks);
fputs(ks->s, opt->out);
}
ks->l = cur_l;
}
// A variation of bam_cigar2qlen which doesn't count soft-clips in to the
// equation. Basically it's the number of bases in query that are aligned
// in some way to the reference (including insertions, which are considered
// to be aligned by dint of being anchored either side).
hts_pos_t qlen_used(bam1_t *b) {
int n_cigar = b->core.n_cigar;
const uint32_t *cigar = bam_get_cigar(b);
hts_pos_t l;
if (b->core.l_qseq) {
// Known SEQ permits of short cut of l_qseq minus CSOFT_CLIPs.
// Full scan not needed, which helps on excessively long CIGARs.
l = b->core.l_qseq;
int kl, kr;
for (kl = 0; kl < n_cigar; kl++)
if (bam_cigar_op(cigar[kl]) == BAM_CSOFT_CLIP)
l -= bam_cigar_oplen(cigar[kl]);
else
break;
for (kr = n_cigar-1; kr > kl; kr--)
if (bam_cigar_op(cigar[kr]) == BAM_CSOFT_CLIP)
l -= bam_cigar_oplen(cigar[kr]);
else
break;
} else {
// Unknown SEQ ("*") needs a full scan through the CIGAR string.
static int query[16] = {
//M I D N S H P = X B ? ? ? ? ? ?
1,1,0,0, 0,0,0,1, 1,0,0,0, 0,0,0,0
};
int k;
for (k = l = 0; k < n_cigar; k++)
if (query[bam_cigar_op(cigar[k])])
l += bam_cigar_oplen(cigar[k]);
}
return l;
}
// Adds the depth for a single read to a depth_hist struct.
// For just one file, this is easy. We just have a circular buffer
// where we increment values for bits that overlap existing data
// and initialise values for coordinates which we're seeing for the first
// time. This is tracked by "end_pos" to know where we've got to.
//
// As the input is sorted, we can flush output from "last_output" to
// b->core.pos.
//
// With multiple files, we must feed data in sorted order as if all files
// are merged, but track depth per file. This also means "end_pos" is per
// file too, but "last_output" is global as it corresponds to rows printed.
static int add_depth(depth_opt *opt, depth_hist *dh, sam_hdr_t *h, bam1_t *b,
int overlap_clip, int file) {
hts_pos_t i;
size_t hmask = dh->size-1;
int n;
if (!b || b->core.tid != dh->last_ref) {
// New ref
if (dh->last_ref >= 0) {
// do end
size_t cur_l = dh->ks.l;
int nf = dh->nfiles;
i = dh->last_output;
for (i = dh->last_output; nf; i++) {
nf = 0;
for (n = 0; n < dh->nfiles; n++) {
if (i < dh->end_pos[n])
nf++;
}
if (!nf)
break;
if (opt->bed && bed_overlap(opt->bed, dh->ref, i, i+1) == 0)
continue;
dh->ks.l = cur_l;
kputll(i+1, &dh->ks);
for (n = 0; n < dh->nfiles; n++) {
kputc_('\t', &dh->ks);
int d = i < dh->end_pos[n]
? dh->hist[n][i & hmask]
: 0;
kputuw(d, &dh->ks);
}
kputc('\n', &dh->ks);
fputs(dh->ks.s, opt->out);
}
if (opt->all_pos) {
// End of last ref
zero_region(opt, dh,
sam_hdr_tid2name(h, dh->last_ref),
i, sam_hdr_tid2len(h, dh->last_ref));
}
dh->ks.l = cur_l;
}
if (opt->all_pos > 1 && !opt->reg) {
// Any previous unused refs
int lr = dh->last_ref < 0 ? 0 : dh->last_ref+1;
int rr = b ? b->core.tid : sam_hdr_nref(h), r;
for (r = lr; r < rr; r++)
zero_region(opt, dh,
sam_hdr_tid2name(h, r),
0, sam_hdr_tid2len(h, r));
}
if (!b) {
// we're just flushing to end of file
if (opt->all_pos && opt->reg && dh->last_ref < 0)
// -a or -aa without a single read being output yet
zero_region(opt, dh, sam_hdr_tid2name(h, dh->tid), dh->beg,
MIN(dh->end, sam_hdr_tid2len(h, dh->tid)));
return 0;
}
for (n = 0; dh->end_pos && n < dh->nfiles; n++)
dh->end_pos[n] = 0;
dh->last_output = dh->beg >= 0
? MAX(b->core.pos, dh->beg)
: b->core.pos;
dh->last_ref = b->core.tid;
dh->ref = sam_hdr_tid2name(h, b->core.tid);
kputs(dh->ref, ks_clear(&dh->ks));
kputc('\t', &dh->ks);
if (opt->all_pos)
// Start of ref
zero_region(opt, dh, dh->ref, 0, b->core.pos);
} else {
if (dh->last_output < b->core.pos) {
// Flush any depth outputs up to start of new read
size_t cur_l = dh->ks.l;
int nf = dh->nfiles;
for (i = dh->last_output; i < b->core.pos; i++) {
nf = 0;
for (n = 0; n < dh->nfiles; n++) {
if (i < dh->end_pos[n])
nf++;
}
if (!nf)
break;
if (opt->bed && bed_overlap(opt->bed, dh->ref, i, i+1) == 0)
continue;
dh->ks.l = cur_l;
kputll(i+1, &dh->ks);
for (n = 0; n < dh->nfiles; n++) {
kputc_('\t', &dh->ks);
int d = i < dh->end_pos[n]
? dh->hist[n][i & hmask]
: 0;
kputuw(d, &dh->ks);
}
kputc('\n', &dh->ks);
fputs(dh->ks.s, opt->out);
}
if (opt->all_pos && i < b->core.pos)
// Hole in middle of ref
zero_region(opt, dh, dh->ref, i, b->core.pos);
dh->ks.l = cur_l;
dh->last_output = b->core.pos;
}
}
hts_pos_t end_pos = bam_endpos(b); // 0 based, 1 past end.
//printf("%d %d\n", (int)b->core.pos+1, (int)end_pos);
if (b->core.tid < dh->last_ref ||
(dh->last_ref == b->core.tid && end_pos < dh->last_output)) {
print_error_errno("depth", "Data is not position sorted");
return -1;
}
// If needed, grow the circular buffer.
if (end_pos+1 - b->core.pos >= dh->size) {
size_t old_size = dh->size;
size_t old_hmask = hmask;
while (end_pos+1 - b->core.pos >= dh->size)
dh->size = dh->size ? 2*dh->size : 2048;
hmask = dh->size-1;
if (!dh->hist) {
dh->hist = calloc(dh->nfiles, sizeof(*dh->hist));
dh->end_pos = calloc(dh->nfiles, sizeof(*dh->end_pos));
if (!dh->hist || !dh->end_pos)
return -1;
}
for (n = 0; n < dh->nfiles; n++) {
int *hist = calloc(dh->size, sizeof(*dh->hist[n]));
if (!hist)
return -1;
// Simple approach for now; copy over old histogram verbatim.
for (i = dh->last_output; i < dh->last_output + old_size; i++)
hist[i & hmask] = dh->hist[n][i & old_hmask];
free(dh->hist[n]);
dh->hist[n] = hist;
}
}
// Accumulate depth, based on CIGAR
uint32_t *cig = bam_get_cigar(b);
int ncig = b->core.n_cigar, j, k, spos = 0;
// Zero new (previously unseen) coordinates so increment works later.
hts_pos_t end = MAX(dh->end_pos[file], b->core.pos);
if (end_pos > end && (end & hmask) < (end_pos & hmask)) {
memset(&dh->hist[file][end & hmask], 0,
sizeof(**dh->hist) * (end_pos - end));
} else {
for (i = end; i < end_pos; i++)
dh->hist[file][i & hmask] = 0;
}
i = b->core.pos;
uint8_t *qual = bam_get_qual(b);
int min_qual = opt->min_qual;
for (j = 0; j < ncig; j++) {
int op = bam_cigar_op(cig[j]);
int oplen = bam_cigar_oplen(cig[j]);
switch (op) {
case BAM_CDEL:
case BAM_CREF_SKIP:
if (op != BAM_CDEL || opt->skip_del) {
// don't increment reference location
if (i + oplen >= dh->end_pos[file]) {
for (k = 0; k < oplen; k++, i++) {
if (i >= dh->end_pos[file])
// redundant due to zero new elements above?
dh->hist[file][i & hmask] = 0;
}
} else {
i += oplen;
}
} else { // op == BAM_CDEL and we count them (-J option),
// We don't incr spos here, but we still use qual.
// This doesn't make much sense, but it's for compatibility
// with the old code. Arguably DEL shouldn't have a min
// qual and should always pass (as we've explicitly asked to
// include them).
int *hist = dh->hist[file];
k = 0;
if (overlap_clip) {
if (i+oplen < overlap_clip) {
i += oplen;
break;
} else if (i < overlap_clip) {
k = overlap_clip - i;
i = overlap_clip;
}
}
// Question: should we even check quality values for DEL?
// We've explicitly asked to include them, and the quality
// is wrong anyway (it's the neighbouring base). We do this
// for now for compatibility with the old depth command.
if (spos < b->core.l_qseq)
for (; k < oplen; k++, i++)
hist[i & hmask]+=qual[spos]>=min_qual;
else
for (; k < oplen; k++, i++)
hist[i & hmask]++;
}
break;
case BAM_CMATCH:
case BAM_CEQUAL:
case BAM_CDIFF:
if ((i & hmask) < ((i+oplen) & hmask)) {
// Optimisation when not wrapping around
// Unrolling doesn't help clang, but helps gcc,
// especially when not using -O3.
int *hist = &dh->hist[file][i & hmask];
if (min_qual || overlap_clip) {
k = 0;
if (overlap_clip) {
if (i+oplen < overlap_clip) {
i += oplen;
spos += oplen;
break;
} else if (i < overlap_clip) {
oplen -= overlap_clip - i;
spos += overlap_clip - i;
hist += overlap_clip - i;
i = overlap_clip;
}
}
// approx 50% of this func cpu time in this loop
for (; k < (oplen & ~7); k+=8) {
hist[k+0]+=qual[spos+0]>=min_qual;
hist[k+1]+=qual[spos+1]>=min_qual;
hist[k+2]+=qual[spos+2]>=min_qual;
hist[k+3]+=qual[spos+3]>=min_qual;
hist[k+4]+=qual[spos+4]>=min_qual;
hist[k+5]+=qual[spos+5]>=min_qual;
hist[k+6]+=qual[spos+6]>=min_qual;
hist[k+7]+=qual[spos+7]>=min_qual;
spos += 8;
}
} else {
// easier to vectorize when no min_qual
for (k = 0; k < (oplen & ~7); k+=8) {
hist[k+0]++;
hist[k+1]++;
hist[k+2]++;
hist[k+3]++;
hist[k+4]++;
hist[k+5]++;
hist[k+6]++;
hist[k+7]++;
}
spos += k;
}
for (; k < oplen && spos < b->core.l_qseq; k++, spos++)
hist[k]+=qual[spos]>=min_qual;
for (; k < oplen; k++, spos++)
hist[k]++;
i += oplen;
} else {
// Simple to understand case, but slower.
// We use this only for reads with wrap-around.
int *hist = dh->hist[file];
k = 0;
if (overlap_clip) {
if (i+oplen < overlap_clip) {
i += oplen;
break;
} else if (i < overlap_clip) {
oplen -= overlap_clip - i;
spos += overlap_clip - i;
i = overlap_clip;
}
}
for (; k < oplen && spos < b->core.l_qseq; k++, i++, spos++)
hist[i & hmask]+=qual[spos]>=min_qual;
for (; k < oplen; k++, i++, spos++)
hist[i & hmask]++;
}
break;
case BAM_CINS:
case BAM_CSOFT_CLIP:
spos += oplen;
break;
case BAM_CPAD:
case BAM_CHARD_CLIP:
// ignore
break;
default:
print_error("depth", "Unsupported cigar op '%d'", op);
return -1;
}
}
if (dh->end >= 0 && end_pos > dh->end)
end_pos = dh->end;
if (dh->end_pos[file] < end_pos)
dh->end_pos[file] = end_pos;
return 0;
}
// Hash on name -> alignment end pos. This permits a naive overlap removal.
// Note it cannot analyse the overlapping sequence and qualities, so the
// interaction of basecalls/qualities and the -Q parameter cannot be
// applied here (unlike the full mpileup algorithm).
KHASH_MAP_INIT_STR(olap_hash, hts_pos_t)
typedef khash_t(olap_hash) olap_hash_t;
static int fastdepth_core(depth_opt *opt, uint32_t nfiles, char **fn,
samFile **fp, hts_itr_t **itr, sam_hdr_t **h) {
int ret = -1, err = 1, i;
olap_hash_t **overlaps = NULL;
depth_hist dh = {0};
// An array of bam structs, one per input file, to hold the next entry
bam1_t **b = calloc(nfiles, sizeof(*b));
int *finished = calloc(nfiles, sizeof(*finished)), to_go = nfiles;
if (!b || !finished)
goto err;
for (i = 0; i < nfiles; i++)
if (!(b[i] = bam_init1()))
goto err;
// Do we need one overlap hash per file? Or shared?
if (opt->remove_overlaps) {
if (!(overlaps = calloc(nfiles, sizeof(*overlaps))))
return -1;
for (i = 0; i < nfiles; i++) {
if (!(overlaps[i] = kh_init(olap_hash)))
return -1;
}
}
// Create the initial histogram
dh.nfiles = nfiles;
dh.size = 0;
dh.hist = NULL;
dh.last_ref = -99;
dh.end_pos = NULL;
dh.last_output = itr && itr[0] ? itr[0]->beg : 0;
ks_initialize(&dh.ks);
// Clip results to region if specified
dh.beg = -1;
dh.end = -1;
dh.tid = 0;
if (itr && itr[0]) {
dh.tid = itr[0]->tid;
dh.beg = itr[0]->beg;
dh.end = itr[0]->end;
}
if (opt->header) {
fprintf(opt->out, "#CHROM\tPOS");
for (i = 0; i < nfiles; i++)
fprintf(opt->out, "\t%s", fn[i]);
fputc('\n', opt->out);
}
// Populate first record per file
for (i = 0; i < nfiles; i++) {
for(;;) {
ret = itr && itr[i]
? sam_itr_next(fp[i], itr[i], b[i])
: sam_read1(fp[i], h[i], b[i]);
if (ret < -1)
goto err;
if (ret == -1) {
to_go--;
finished[i] = 1;
break;
}
if (b[i]->core.tid < 0)
continue;
if (b[i]->core.flag & opt->flag)
continue; // must have none of the flags set
if (opt->incl_flag && (b[i]->core.flag & opt->incl_flag) == 0)
continue; // must have at least one flag set
if ((b[i]->core.flag & opt->require_flag) != opt->require_flag)
continue; // must have all lags set
if (b[i]->core.qual < opt->min_mqual)
continue;
// Original samtools depth used the total sequence (l_qseq)
// including soft-clips. This doesn't feel like a useful metric
// to be filtering on. We now only count sequence bases that
// form the used part of the alignment.
if (opt->min_len) {
if (qlen_used(b[i]) < opt->min_len)
continue;
}
break;
}
}
// Loop through input files, merging in order so we're
// always adding the next record in sequence
while (to_go) {
// Find next record in file list
int best_tid = INT_MAX, best_file = 0;
hts_pos_t best_pos = HTS_POS_MAX;
for (i = 0; i < nfiles; i++) {
if (finished[i])
continue;
if (best_tid > b[i]->core.tid) {
best_tid = b[i]->core.tid;
best_pos = b[i]->core.pos;
best_file = i;
} else if (best_tid == b[i]->core.tid &&
best_pos > b[i]->core.pos) {
best_pos = b[i]->core.pos;
best_file = i;
}
}
i = best_file;
hts_pos_t clip = 0;
if (overlaps && (b[i]->core.flag & BAM_FPAIRED) &&
!(b[i]->core.flag & BAM_FMUNMAP)) {
khiter_t k = kh_get(olap_hash, overlaps[i], bam_get_qname(b[i]));
if (k == kh_end(overlaps[i])) {
// not seen before
hts_pos_t endpos = bam_endpos(b[i]);
// Don't add if mate location is known and can't overlap.
if (b[i]->core.mpos == -1 ||
(b[i]->core.tid == b[i]->core.mtid &&
b[i]->core.mpos <= endpos)) {
k = kh_put(olap_hash, overlaps[i], bam_get_qname(b[i]),
&ret);
if (ret < 0)
return -1;
kh_key(overlaps[i], k) = strdup(bam_get_qname(b[i]));
kh_value(overlaps[i], k) = endpos;
}
} else {
// seen before
clip = kh_value(overlaps[i], k);
free((char *)kh_key(overlaps[i], k));
kh_del(olap_hash, overlaps[i], k);
}
}
// Add the next merged BAM record to the depth plot
if ((ret = add_depth(opt, &dh, h[i], b[i], clip, i)) < 0) {
ret = -1;
goto err;
}
// Populate next record from this file
for(;!finished[i];) {
ret = itr && itr[i]
? sam_itr_next(fp[i], itr[i], b[i])
: sam_read1(fp[i], h[i], b[i]);
if (ret < -1) {
ret = -1;
goto err;
}
if (ret == -1) {
to_go--;
finished[i] = 1;
break;
}
if (b[i]->core.tid < 0)
continue;
if (b[i]->core.flag & opt->flag)
continue; // must have none of the flags set
if (opt->incl_flag && (b[i]->core.flag & opt->incl_flag) == 0)
continue; // must have at least one flag set
if ((b[i]->core.flag & opt->require_flag) != opt->require_flag)
continue; // must have all lags set
if (b[i]->core.qual < opt->min_mqual)
continue;
if (opt->min_len) {
if (qlen_used(b[i]) < opt->min_len)
continue;
}
break;
}
}
// Tidy up end.
ret = add_depth(opt, &dh, h[0], NULL, 0, 0);
err = 0;
err:
if (ret == 0 && err)
ret = -1;
for (i = 0; i < nfiles; i++) {
if (b[i])
bam_destroy1(b[i]);
if (dh.hist && dh.hist[i])
free(dh.hist[i]);
}
free(b);
free(finished);
ks_free(&dh.ks);
free(dh.hist);
free(dh.end_pos);
if (overlaps) {
khiter_t k;
for (i = 0; i < nfiles; i++) {
if (!overlaps[i])
continue;
for (k = kh_begin(overlaps[i]); k < kh_end(overlaps[i]); k++)
if (kh_exist(overlaps[i], k))
free((char *)kh_key(overlaps[i], k));
kh_destroy(olap_hash, overlaps[i]);
}
free(overlaps);
}
return ret;
}
static void usage_exit(FILE *fp, int exit_status)
{
fprintf(fp, "Usage: samtools depth [options] in.bam [in.bam ...]\n");
fprintf(fp, "\nOptions:\n");
fprintf(fp, " -a Output all positions (including zero depth)\n");
fprintf(fp, " -a -a, -aa Output absolutely all positions, including unused ref seqs\n");
fprintf(fp, " -r REG Specify a region in chr or chr:from-to syntax\n");
fprintf(fp, " -b FILE Use bed FILE for list of regions\n");
fprintf(fp, " -f FILE Specify list of input BAM/SAM/CRAM filenames\n");
fprintf(fp, " -X Use custom index files (in -X *.bam *.bam.bai order)\n");
fprintf(fp, " -g INT Remove specified flags from default filter-out flag list\n");
fprintf(fp, " -G, --excl-flags FLAGS\n");
fprintf(fp, " Add specified flags to the default filter-out flag list\n");
fprintf(fp, " [UNMAP,SECONDARY,QCFAIL,DUP]\n");
fprintf(fp, " --incl-flags FLAGS\n");
fprintf(fp, " Only include records with at least one the FLAGs present [0]\n");
fprintf(fp, " --require-flags FLAGS\n");
fprintf(fp, " Only include records with all of the FLAGs present [0]\n");
fprintf(fp, " -H Print a file header line\n");
fprintf(fp, " -l INT Minimum read length [0]\n");
fprintf(fp, " -o FILE Write output to FILE [stdout]\n");
fprintf(fp, " -q, --min-BQ INT\n"
" Filter bases with base quality smaller than INT [0]\n");
fprintf(fp, " -Q, --min-MQ INT\n"
" Filter alignments with mapping quality smaller than INT [0]\n");
fprintf(fp, " -J Include reads with deletions in depth computation\n");
fprintf(fp, " -s Do not count overlapping reads within a template\n");
sam_global_opt_help(fp, "-.--.@-.");
exit(exit_status);
}
int main_depth(int argc, char *argv[])
{
int nfiles, i;
samFile **fp;
sam_hdr_t **header;
int c, has_index_file = 0;
char *file_list = NULL, **fn = NULL;
char *out_file = NULL;
depth_opt opt = {
.flag = BAM_FUNMAP | BAM_FSECONDARY | BAM_FDUP | BAM_FQCFAIL,
.incl_flag = 0,
.require_flag = 0,
.min_qual = 0,
.min_mqual = 0,
.skip_del = 1,
.header = 0,
.min_len = 0,
.out = stdout,
.all_pos = 0,
.remove_overlaps = 0,
.reg = NULL,
.bed = NULL,
};
sam_global_args ga = SAM_GLOBAL_ARGS_INIT;
static const struct option lopts[] = {
{"min-MQ", required_argument, NULL, 'Q'},
{"min-mq", required_argument, NULL, 'Q'},
{"min-BQ", required_argument, NULL, 'q'},
{"min-bq", required_argument, NULL, 'q'},
{"excl-flags", required_argument, NULL, 'G'},
{"incl-flags", required_argument, NULL, 1},
{"require-flags", required_argument, NULL, 2},
SAM_OPT_GLOBAL_OPTIONS('-', 0, '-', '-', 0, '@'),
{NULL, 0, NULL, 0}
};
while ((c = getopt_long(argc, argv, "@:q:Q:JHd:m:l:g:G:o:ar:Xf:b:s",
lopts, NULL)) >= 0) {
switch (c) {
case 'a':
opt.all_pos++;
break;
case 'b':
opt.bed = bed_read(optarg);
if (!opt.bed) {
print_error_errno("depth", "Could not read file \"%s\"",
optarg);
return 1;
}
break;
case 'f':
file_list = optarg;
break;
case 'd':
case 'm':
// depth limit - now ignored
break;
case 'g':
opt.flag &= ~bam_str2flag(optarg);
break;
case 'G': // reject if any set
opt.flag |= bam_str2flag(optarg);
break;
case 1: // reject unless at least one set (0 means ignore option)
opt.incl_flag |= bam_str2flag(optarg);
break;
case 2: // reject unless all set
opt.require_flag |= bam_str2flag(optarg);
break;
case 'l':
opt.min_len = atoi(optarg);
break;
case 'H':
opt.header = 1;
break;
case 'q':
opt.min_qual = atoi(optarg);
break;
case 'Q':
opt.min_mqual = atoi(optarg);
break;
case 'J':
opt.skip_del = 0;
break;
case 'o':
if (opt.out != stdout)
break;
opt.out = fopen(out_file = optarg, "w");
if (!opt.out) {
print_error_errno("depth", "Cannot open \"%s\" for writing.",
optarg);
return EXIT_FAILURE;
}
break;
case 'r':
opt.reg = optarg;
break;
case 's':
opt.remove_overlaps = 1;
break;
case 'X':
has_index_file = 1;
break;
default: if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break;
/* else fall-through */
case '?':
usage_exit(stderr, EXIT_FAILURE);
}
}
if (argc < optind+1 && !file_list) {
if (argc == optind)
usage_exit(stdout, EXIT_SUCCESS);
else
usage_exit(stderr, EXIT_FAILURE);
}
if (file_list) {
if (has_index_file) {
print_error("depth", "The -f option cannot be combined with -X");
return 1;
}
if (read_file_list(file_list, &nfiles, &fn))
return 1;
argv = fn;
argc = nfiles;
optind = 0;
} else {
nfiles = argc - optind;
}
if (has_index_file) {
if (nfiles%1) {
print_error("depth", "-X needs one index specified per bam file");
return 1;
}
nfiles /= 2;
}
fp = malloc(nfiles * sizeof(*fp));
header = malloc(nfiles * sizeof(*header));
if (!fp || !header) {
print_error_errno("depth", "Out of memory");
return 1;
}
hts_itr_t **itr = NULL;
if (opt.reg) {
itr = calloc(nfiles, sizeof(*itr));
if (!itr)
return 1;
}
for (i = 0; i < nfiles; i++, optind++) {
fp[i] = sam_open_format(argv[optind], "r", &ga.in);
if (fp[i] == NULL) {
print_error_errno("depth",
"Cannot open input file \"%s\"", argv[optind]);
return 1;
}
if (ga.nthreads > 0)
hts_set_threads(fp[i], ga.nthreads);
if (hts_set_opt(fp[i], CRAM_OPT_REQUIRED_FIELDS,
SAM_FLAG | SAM_RNAME | SAM_POS | SAM_CIGAR
| (opt.remove_overlaps ? SAM_QNAME|SAM_RNEXT|SAM_PNEXT
: 0)
| (opt.min_mqual ? SAM_MAPQ : 0)
| (opt.min_len ? SAM_SEQ : 0)
| (opt.min_qual ? SAM_QUAL : 0))) {
fprintf(stderr, "Failed to set CRAM_OPT_REQUIRED_FIELDS value\n");
return 1;
}
if (hts_set_opt(fp[i], CRAM_OPT_DECODE_MD, 0)) {
fprintf(stderr, "Failed to set CRAM_OPT_DECODE_MD value\n");
return 1;
}
// FIXME: what if headers differ?
header[i] = sam_hdr_read(fp[i]);
if (header == NULL) {
fprintf(stderr, "Failed to read header for \"%s\"\n",
argv[optind]);
return 1;
}
if (opt.reg) {
hts_idx_t *idx = has_index_file
? sam_index_load2(fp[i], argv[optind], argv[optind+nfiles])
: sam_index_load(fp[i], argv[optind]);
if (!idx) {
print_error("depth", "cannot load index for \"%s\"",
argv[optind]);
return 1;
}
if (!(itr[i] = sam_itr_querys(idx, header[i], opt.reg))) {
print_error("depth", "cannot parse region \"%s\"", opt.reg);
return 1;
}
hts_idx_destroy(idx);
}
}
int ret = fastdepth_core(&opt, nfiles, &argv[argc-nfiles], fp, itr, header)
? 1 : 0;
for (i = 0; i < nfiles; i++) {
sam_hdr_destroy(header[i]);
sam_close(fp[i]);
if (itr && itr[i])
hts_itr_destroy(itr[i]);
}
free(header);
free(fp);
free(itr);
if (file_list) {
for (i=0; i<nfiles; i++)
free(fn[i]);
free(fn);
}
if (opt.bed)
bed_destroy(opt.bed);
sam_global_args_free(&ga);
if (opt.out != stdout) {
if (fclose(opt.out) != 0 && ret == 0) {
print_error_errno("depth", "error on closing \"%s\"", out_file);
ret = 1;
}
}
return ret;
}
#ifdef _MAIN_BAM2DEPTH
int main(int argc, char *argv[])
{
return main_depth(argc, argv);
}
#endif