forked from samtools/htslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsam.c
2213 lines (1863 loc) · 77.1 KB
/
sam.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
/* test/sam.c -- SAM/BAM/CRAM API test cases.
Copyright (C) 2014-2020 Genome Research Ltd.
Author: John Marshall <[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 <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <math.h>
#include <assert.h>
#include <unistd.h>
// Suppress message for faidx_fetch_nseq(), which we're intentionally testing
#include "../htslib/hts_defs.h"
#undef HTS_DEPRECATED
#define HTS_DEPRECATED(message)
#include "../htslib/sam.h"
#include "../htslib/faidx.h"
#include "../htslib/khash.h"
#include "../htslib/hts_log.h"
KHASH_SET_INIT_STR(keep)
typedef khash_t(keep) *keephash_t;
#ifndef HTS_VERSION
#error HTS_VERSION not defined
#endif
#if HTS_VERSION < 100900
#error HTS_VERSION comparison incorrect
#endif
int status;
static void HTS_FORMAT(HTS_PRINTF_FMT, 1, 2) fail(const char *fmt, ...)
{
va_list args;
fprintf(stderr, "Failed: ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
status = EXIT_FAILURE;
}
#define VERIFY(test, message) if (!(test)) { \
fail("%s: %s", __func__, (message)); \
goto cleanup; \
}
uint8_t *check_bam_aux_get(const bam1_t *aln, const char *tag, char type)
{
uint8_t *p = bam_aux_get(aln, tag);
if (p) {
if (*p == type) return p;
else fail("%s field of type '%c', expected '%c'", tag, *p, type);
}
else fail("can't find %s field", tag);
return NULL;
}
static void check_int_B_array(bam1_t *aln, char *tag,
uint32_t nvals, int64_t *vals) {
uint8_t *p;
if ((p = check_bam_aux_get(aln, tag, 'B')) != NULL) {
uint32_t i;
if (bam_auxB_len(p) != nvals)
fail("Wrong length reported for %s field, got %u, expected %u",
tag, bam_auxB_len(p), nvals);
for (i = 0; i < nvals; i++) {
if (bam_auxB2i(p, i) != vals[i]) {
fail("Wrong value from bam_auxB2i for %s field index %u, "
"got %"PRId64" expected %"PRId64,
tag, i, bam_auxB2i(p, i), vals[i]);
}
if (bam_auxB2f(p, i) != (double) vals[i]) {
fail("Wrong value from bam_auxB2f for %s field index %u, "
"got %f expected %f",
tag, i, bam_auxB2f(p, i), (double) vals[i]);
}
}
}
}
#define PI 3.141592653589793
#define E 2.718281828459045
#define HELLO "Hello, world!"
#define NEW_HELLO "Yo, dude"
#define NEW_HELLO2 "Bonjour, tout le monde"
#define BEEF "DEADBEEF"
#define str(x) #x
#define xstr(x) str(x)
#define NELE(x) (sizeof(x)/sizeof(x[0]))
static int test_update_int(bam1_t *aln,
const char target_id[2], int64_t target_val,
char expected_type,
const char next_id[2], int64_t next_val,
char next_type) {
uint8_t *p;
// Try updating target
if (bam_aux_update_int(aln, target_id, target_val) < 0) {
fail("update %.2s tag", target_id);
return -1;
}
// Check it's there and has the right type and value
p = bam_aux_get(aln, target_id);
if (!p) {
fail("find %.2s tag", target_id);
return -1;
}
if (*p != expected_type || bam_aux2i(p) != target_val) {
fail("%.2s field is %c:%"PRId64"; expected %c:%"PRId64,
target_id, *p, bam_aux2i(p), expected_type, target_val);
return -1;
}
// If given, check that the next tag hasn't been clobbered by the
// update above.
if (!*next_id) return 0;
p = bam_aux_get(aln, next_id);
if (!p) {
fail("find %.2s tag after updating %.2s", next_id, target_id);
return -1;
}
if (*p != next_type || bam_aux2i(p) != next_val) {
fail("after updating %.2s to %"PRId64":"
" %.2s field is %c:%"PRId64"; expected %c:%"PRId64,
target_id, target_val,
next_id, *p, bam_aux2i(p), next_type, next_val);
return -1;
}
return 0;
}
#define CHECK_ARRAY_VALS(T, GET_VAL, FMT1, FMT2) do { \
T * vals = (T *) data; \
uint32_t i; \
for (i = 0; i < nitems; i++) { \
if (GET_VAL(p, i) != vals[i]) { \
fail("Wrong value from %s for %.2s field index %u, " \
"got %" FMT1 " expected %" FMT2, \
xstr(GET_VAL), target_id, i, GET_VAL(p, i), vals[i]); \
return -1; \
} \
} \
} while (0)
static int test_update_array(bam1_t *aln, const char target_id[2],
uint8_t type, uint32_t nitems, void *data,
const char next_id[2], int64_t next_val,
char next_type)
{
uint8_t *p;
// Try updating target
if (bam_aux_update_array(aln, target_id, type, nitems, data) < 0) {
fail("update %2.s tag", target_id);
return -1;
}
// Check values
p = bam_aux_get(aln, target_id);
if (!p) {
fail("find %.2s tag", target_id);
return -1;
}
switch (type) {
case 'c':
CHECK_ARRAY_VALS(int8_t, bam_auxB2i, PRId64, PRId8); break;
case 'C':
CHECK_ARRAY_VALS(uint8_t, bam_auxB2i, PRId64, PRIu8); break;
case 's':
CHECK_ARRAY_VALS(int16_t, bam_auxB2i, PRId64, PRId16); break;
case 'S':
CHECK_ARRAY_VALS(uint16_t, bam_auxB2i, PRId64, PRIu16); break;
case 'i':
CHECK_ARRAY_VALS(int32_t, bam_auxB2i, PRId64, PRId32); break;
case 'I':
CHECK_ARRAY_VALS(uint32_t, bam_auxB2i, PRId64, PRIu32); break;
case 'f':
CHECK_ARRAY_VALS(float, bam_auxB2f, "e", "e"); break;
}
// If given, check that the next tag hasn't been clobbered by the
// update above.
if (!*next_id) return 0;
p = bam_aux_get(aln, next_id);
if (!p) {
fail("find %.2s tag after updating %.2s", next_id, target_id);
return -1;
}
if (*p != next_type || bam_aux2i(p) != next_val) {
fail("after updating %.2s:"
" %.2s field is %c:%"PRId64"; expected %c:%"PRId64,
target_id, next_id, *p, bam_aux2i(p), next_type, next_val);
return -1;
}
return 0;
}
// This function uses bam_hdr_t etc as a check ensuring the legacy typedef
// and functions continue to compile successfully.
static int aux_fields1(void)
{
static const char sam[] = "data:,"
"@SQ\tSN:one\tLN:1000\n"
"@SQ\tSN:two\tLN:500\n"
"r1\t0\tone\t500\t20\t8M\t*\t0\t0\tATGCATGC\tqqqqqqqq\tXA:A:k\tXi:i:37\tXf:f:" xstr(PI) "\tXd:d:" xstr(E) "\tXZ:Z:" HELLO "\tXH:H:" BEEF "\tXB:B:c,-2,0,+2\tB0:B:i,-2147483648,-1,0,1,2147483647\tB1:B:I,0,1,2147483648,4294967295\tB2:B:s,-32768,-1,0,1,32767\tB3:B:S,0,1,32768,65535\tB4:B:c,-128,-1,0,1,127\tB5:B:C,0,1,127,255\tBf:B:f,-3.14159,2.71828\tZZ:i:1000000\tF2:d:2.46801\tY1:i:-2147483648\tY2:i:-2147483647\tY3:i:-1\tY4:i:0\tY5:i:1\tY6:i:2147483647\tY7:i:2147483648\tY8:i:4294967295\n"
"r2\t0x8D\t*\t0\t0\t*\t*\t0\t0\tATGC\tqqqq\n"
;
// Canonical form of the alignment records above, as output by sam_format1()
static const char r1[] = "r1\t0\tone\t500\t20\t8M\t*\t0\t0\tATGCATGC\tqqqqqqqq\tXi:i:37\tXf:f:3.14159\tXd:d:2.71828\tXZ:Z:" NEW_HELLO "\tXH:H:" BEEF "\tXB:B:c,-2,0,2\tB0:B:i,-2147483648,-1,0,1,2147483647\tB1:B:I,0,1,2147483648,4294967295\tB2:B:s,-32768,-1,0,1,32767\tB3:B:S,0,1,32768,65535\tB4:B:c,-128,-1,0,1,127\tB5:B:C,0,1,127,255\tBf:B:f,-3.14159,2.71828\tZZ:i:1000000\tF2:f:9.8765\tY1:i:-2147483648\tY2:i:-2147483647\tY3:i:-1\tY4:i:0\tY5:i:1\tY6:i:2147483647\tY7:i:2147483648\tY8:i:4294967295\tN0:i:-1234\tN1:i:1234\tN2:i:-2\tN3:i:3\tF1:f:4.5678\tN4:B:S,65535,32768,1,0\tN5:i:4242\tZa:Z:" HELLO "\tZb:Z:" NEW_HELLO2;
static const char r2[] = "r2\t141\t*\t0\t0\t*\t*\t0\t0\tATGC\tqqqq";
samFile *in = sam_open(sam, "r");
bam_hdr_t *header = sam_hdr_read(in);
bam1_t *aln = bam_init1();
uint8_t *p;
kstring_t ks = { 0, 0, NULL };
int64_t b0vals[5] = { -2147483648LL,-1,0,1,2147483647LL }; // i
int64_t b1vals[4] = { 0,1,2147483648LL,4294967295LL }; // I
int64_t b2vals[5] = { -32768,-1,0,1,32767 }; // s
int64_t b3vals[4] = { 0,1,32768,65535 }; // S
int64_t b4vals[5] = { -128,-1,0,1,127 }; // c
int64_t b5vals[4] = { 0,1,127,255 }; // C
// NB: Floats not doubles below!
// See https://randomascii.wordpress.com/2012/06/26/doubles-are-not-floats-so-dont-compare-them/
float bfvals[2] = { -3.14159f, 2.71828f };
int8_t n4v1[] = { -128, -64, -32, -16, -8, -4, -2, -1,
0, 1, 2, 4, 8, 16, 32, 64, 127 };
uint32_t n4v2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1234, 5678, 1U << 31, 0 };
int16_t n4v3[] = { -32768, -1, 0, 1, 32767 };
float n4v4[] = { 0, 1, 2, 10, 20, 30, 1.5, -2.5 };
uint8_t n4v5[] = { 0, 255 };
int32_t n4v6[] = { -2147483647 - 1, 10, -1, 0, 1, 2147483647 };
uint16_t n4v7[] = { 65535, 32768, 1, 0 };
int32_t ival = -1234;
uint32_t uval = 1234;
float f1 = 4.5678;
float f2 = 9.8765;
const char *hose = "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO";
size_t nvals, i;
if (sam_read1(in, header, aln) >= 0) {
if ((p = check_bam_aux_get(aln, "XA", 'A')) && bam_aux2A(p) != 'k')
fail("XA field is '%c', expected 'k'", bam_aux2A(p));
bam_aux_del(aln,p);
if (bam_aux_get(aln,"XA"))
fail("XA field was not deleted");
if ((p = check_bam_aux_get(aln, "Xi", 'C')) && bam_aux2i(p) != 37)
fail("Xi field is %"PRId64", expected 37", bam_aux2i(p));
if ((p = check_bam_aux_get(aln, "Xf", 'f')) && fabs(bam_aux2f(p) - PI) > 1E-6)
fail("Xf field is %.12f, expected pi", bam_aux2f(p));
if ((p = check_bam_aux_get(aln, "Xd", 'd')) && fabs(bam_aux2f(p) - E) > 1E-6)
fail("Xf field is %.12f, expected e", bam_aux2f(p));
if ((p = check_bam_aux_get(aln, "XZ", 'Z')) && strcmp(bam_aux2Z(p), HELLO) != 0)
fail("XZ field is \"%s\", expected \"%s\"", bam_aux2Z(p), HELLO);
bam_aux_update_str(aln,"XZ",strlen(NEW_HELLO)+1,NEW_HELLO);
if ((p = check_bam_aux_get(aln, "XZ", 'Z')) && strcmp(bam_aux2Z(p), NEW_HELLO) != 0)
fail("XZ field is \"%s\", expected \"%s\"", bam_aux2Z(p), NEW_HELLO);
if (!check_bam_aux_get(aln, "XH", 'H'))
fail("bam_aux_update_str(,,strlen(NEW_HELLO)+1,NEW_HELLO) corrupted XH tag");
bam_aux_update_str(aln,"XZ",strlen(NEW_HELLO2), NEW_HELLO2);
if ((p = check_bam_aux_get(aln, "XZ", 'Z')) && strcmp(bam_aux2Z(p), NEW_HELLO2) != 0)
fail("XZ field is \"%s\", expected \"%s\"", bam_aux2Z(p), NEW_HELLO2);
if (!check_bam_aux_get(aln, "XH", 'H'))
fail("bam_aux_update_str(,,strlen(NEW_HELLO2),NEW_HELLO2) corrupted XH tag");
bam_aux_update_str(aln,"XZ",-1,NEW_HELLO);
if ((p = check_bam_aux_get(aln, "XZ", 'Z')) && strcmp(bam_aux2Z(p), NEW_HELLO) != 0)
fail("XZ field is \"%s\", expected \"%s\"", bam_aux2Z(p), NEW_HELLO);
if (!check_bam_aux_get(aln, "XH", 'H'))
fail("bam_aux_update_str(,,-1,NEW_HELLO) corrupted XH tag");
if ((p = check_bam_aux_get(aln, "XH", 'H')) && strcmp(bam_aux2Z(p), BEEF) != 0)
fail("XH field is \"%s\", expected \"%s\"", bam_aux2Z(p), BEEF);
if ((p = check_bam_aux_get(aln, "XB", 'B'))
&& ! (memcmp(p, "Bc", 2) == 0
&& memcmp(p + 2, "\x03\x00\x00\x00\xfe\x00\x02", 7) == 0))
fail("XB field is %c,..., expected c,-2,0,+2", p[1]);
check_int_B_array(aln, "B0", NELE(b0vals), b0vals);
check_int_B_array(aln, "B1", NELE(b1vals), b1vals);
check_int_B_array(aln, "B2", NELE(b2vals), b2vals);
check_int_B_array(aln, "B3", NELE(b3vals), b3vals);
check_int_B_array(aln, "B4", NELE(b4vals), b4vals);
check_int_B_array(aln, "B5", NELE(b5vals), b5vals);
nvals = NELE(bfvals);
if ((p = check_bam_aux_get(aln, "Bf", 'B')) != NULL) {
if (bam_auxB_len(p) != nvals)
fail("Wrong length reported for Bf field, got %d, expected %zd",
bam_auxB_len(p), nvals);
for (i = 0; i < nvals; i++) {
if (bam_auxB2f(p, i) != bfvals[i]) {
fail("Wrong value from bam_auxB2f for Bf field index %zd, "
"got %f expected %f",
i, bam_auxB2f(p, i), bfvals[i]);
}
}
}
if ((p = check_bam_aux_get(aln, "ZZ", 'I')) && bam_aux2i(p) != 1000000)
fail("ZZ field is %"PRId64", expected 1000000", bam_aux2i(p));
if ((p = bam_aux_get(aln, "Y1")) && bam_aux2i(p) != -2147483647-1)
fail("Y1 field is %"PRId64", expected -2^31", bam_aux2i(p));
if ((p = bam_aux_get(aln, "Y2")) && bam_aux2i(p) != -2147483647)
fail("Y2 field is %"PRId64", expected -2^31+1", bam_aux2i(p));
if ((p = bam_aux_get(aln, "Y3")) && bam_aux2i(p) != -1)
fail("Y3 field is %"PRId64", expected -1", bam_aux2i(p));
if ((p = bam_aux_get(aln, "Y4")) && bam_aux2i(p) != 0)
fail("Y4 field is %"PRId64", expected 0", bam_aux2i(p));
if ((p = bam_aux_get(aln, "Y5")) && bam_aux2i(p) != 1)
fail("Y5 field is %"PRId64", expected 1", bam_aux2i(p));
if ((p = bam_aux_get(aln, "Y6")) && bam_aux2i(p) != 2147483647)
fail("Y6 field is %"PRId64", expected 2^31-1", bam_aux2i(p));
if ((p = bam_aux_get(aln, "Y7")) && bam_aux2i(p) != 2147483648LL)
fail("Y7 field is %"PRId64", expected 2^31", bam_aux2i(p));
if ((p = bam_aux_get(aln, "Y8")) && bam_aux2i(p) != 4294967295LL)
fail("Y8 field is %"PRId64", expected 2^32-1", bam_aux2i(p));
// Try appending some new tags
if (bam_aux_append(aln, "N0", 'i', sizeof(ival), (uint8_t *) &ival) != 0)
fail("Failed to append N0:i tag");
if ((p = bam_aux_get(aln, "N0")) && bam_aux2i(p) != ival)
fail("N0 field is %"PRId64", expected %d", bam_aux2i(p), ival);
if (bam_aux_append(aln, "N1", 'I', sizeof(uval), (uint8_t *) &uval) != 0)
fail("failed to append N1:I tag");
if ((p = bam_aux_get(aln, "N1")) && bam_aux2i(p) != uval)
fail("N1 field is %"PRId64", expected %u", bam_aux2i(p), uval);
// Append tags with bam_aux_update_int()
if (bam_aux_update_int(aln, "N2", -2) < 0)
fail("failed to append N2:c tag");
if (bam_aux_update_int(aln, "N3", 3) < 0)
fail("failed to append N3:C tag");
p = bam_aux_get(aln, "N2");
if (!p)
fail("failed to retrieve N2 tag");
else if (*p != 'c' || bam_aux2i(p) != -2)
fail("N2 field is %c:%"PRId64", expected c:-2", *p, bam_aux2i(p));
p = bam_aux_get(aln, "N3");
if (!p)
fail("failed to retrieve N3 tag");
else if (*p != 'C' || bam_aux2i(p) != 3)
fail("N3 field is %c:%"PRId64", expected C:3", *p, bam_aux2i(p));
// Try changing values with bam_aux_update_int()
i = test_update_int(aln, "N2", 2, 'C', "N3", 3, 'C');
if (i == 0) test_update_int(aln, "N2", 1234, 'S', "N3", 3, 'C');
if (i == 0) test_update_int(aln, "N2", -1, 's', "N3", 3, 'C');
if (i == 0) test_update_int(aln, "N2", 4294967295U, 'I', "N3", 3, 'C');
if (i == 0) test_update_int(aln, "N2", -2, 'i', "N3", 3, 'C');
// Append a value with bam_aux_update_float()
if (bam_aux_update_float(aln, "F1", f1) < 0)
fail("append F1:f tag");
p = bam_aux_get(aln, "F1");
if (!p)
fail("retrieve F1 tag");
else if (*p != 'f' || bam_aux2f(p) != f1)
fail("F1 field is %c:%e, expected f:%e", *p, bam_aux2f(p), f1);
// Change a double tag to a float
if (bam_aux_update_float(aln, "F2", f2) < 0)
fail("update F2 tag");
p = bam_aux_get(aln, "F2");
if (!p)
fail("retrieve F2 tag");
else if (*p != 'f' || bam_aux2f(p) != f2)
fail("F2 field is %c:%e, expected f:%e", *p, bam_aux2f(p), f2);
// Check the next one is intact too
p = bam_aux_get(aln, "Y1");
if (!p)
fail("retrieve Y1 tag");
else if (*p != 'i' && bam_aux2i(p) != -2147483647-1)
fail("Y1 field is %"PRId64", expected -2^31", bam_aux2i(p));
// bam_aux_update_array tests
// append a new array
i = test_update_array(aln, "N4", 'c', NELE(n4v1), n4v1, "\0\0", 0, 0);
// Add a sentinel to check resizes work
if (i == 0) i = test_update_int(aln, "N5", 4242, 'S', "\0\0", 0, 0);
// alter the array tag a few times
if (i == 0)
i = test_update_array(aln, "N4", 'I', NELE(n4v2), n4v2,
"N5", 4242, 'S');
if (i == 0)
i = test_update_array(aln, "N4", 's', NELE(n4v3), n4v3,
"N5", 4242, 'S');
if (i == 0)
i = test_update_array(aln, "N4", 'f', NELE(n4v4), n4v4,
"N5", 4242, 'S');
if (i == 0)
i = test_update_array(aln, "N4", 'c', NELE(n4v5), n4v5,
"N5", 4242, 'S');
if (i == 0)
i = test_update_array(aln, "N4", 'i', NELE(n4v6), n4v6,
"N5", 4242, 'S');
if (i == 0)
i = test_update_array(aln, "N4", 'S', NELE(n4v7), n4v7,
"N5", 4242, 'S');
// Append a couple of strings
// First add and remove some data so that failure to NUL-terminate will
// be spotted
bam_aux_update_str(aln,"oo",strlen(hose) + 1,hose);
if ((p = check_bam_aux_get(aln, "oo", 'Z')) && strcmp(bam_aux2Z(p), hose) != 0)
fail("oo field is \"%s\", expected \"%s\"", bam_aux2Z(p), hose);
if (p) bam_aux_del(aln, p);
if (bam_aux_get(aln, "oo"))
fail("oo field wasn't deleted correctly");
bam_aux_update_str(aln,"Za",strlen(HELLO),HELLO);
if ((p = check_bam_aux_get(aln, "Za", 'Z')) && strcmp(bam_aux2Z(p), HELLO) != 0)
fail("Za field is \"%s\", expected \"%s\"", bam_aux2Z(p), HELLO);
bam_aux_update_str(aln,"Zb",strlen(NEW_HELLO2)+1,NEW_HELLO2);
if ((p = check_bam_aux_get(aln, "Zb", 'Z')) && strcmp(bam_aux2Z(p), NEW_HELLO2) != 0)
fail("Zb field is \"%s\", expected \"%s\"", bam_aux2Z(p), NEW_HELLO2);
if (sam_format1(header, aln, &ks) < 0)
fail("can't format record");
if (strcmp(ks.s, r1) != 0)
fail("record formatted incorrectly: \"%s\"", ks.s);
}
else fail("can't read record");
if (sam_read1(in, header, aln) >= 0) {
if (sam_format1(header, aln, &ks) < 0)
fail("can't format record r2");
if (aln->core.flag != 0x8D)
fail("r2 flag value is 0x%X, expected 0x8D", aln->core.flag);
if (strcmp(ks.s, r2) != 0)
fail("record r2 formatted incorrectly: \"%s\"", ks.s);
}
else fail("can't read record r2");
bam_destroy1(aln);
bam_hdr_destroy(header);
sam_close(in);
free(ks.s);
return 1;
}
static void set_qname(void)
{
static const char sam[] = "data:,"
"@SQ\tSN:one\tLN:1000\n"
"@SQ\tSN:two\tLN:500\n"
"r1\t0\tone\t500\t20\t8M\t*\t0\t0\tATGCATGC\tqqqqqqqq\tXA:A:k\tXi:i:37\tXf:f:" xstr(PI) "\tXd:d:" xstr(E) "\tXZ:Z:" HELLO "\tXH:H:" BEEF "\tXB:B:c,-2,0,+2\tB0:B:i,-2147483648,-1,0,1,2147483647\tB1:B:I,0,1,2147483648,4294967295\tB2:B:s,-32768,-1,0,1,32767\tB3:B:S,0,1,32768,65535\tB4:B:c,-128,-1,0,1,127\tB5:B:C,0,1,127,255\tBf:B:f,-3.14159,2.71828\tZZ:i:1000000\tF2:d:2.46801\tY1:i:-2147483648\tY2:i:-2147483647\tY3:i:-1\tY4:i:0\tY5:i:1\tY6:i:2147483647\tY7:i:2147483648\tY8:i:4294967295\n"
"r22\t0x8D\t*\t0\t0\t*\t*\t0\t0\tATGC\tqqqq\n"
"r12345678\t0x8D\t*\t0\t0\t*\t*\t0\t0\tATGC\tqqqq\n"
;
// Canonical form of the alignment records above, as output by sam_format1()
static const char r1[] = "r1\t0\tone\t500\t20\t8M\t*\t0\t0\tATGCATGC\tqqqqqqqq\tXA:A:k\tXi:i:37\tXf:f:3.14159\tXd:d:2.71828\tXZ:Z:" HELLO "\tXH:H:" BEEF "\tXB:B:c,-2,0,2\tB0:B:i,-2147483648,-1,0,1,2147483647\tB1:B:I,0,1,2147483648,4294967295\tB2:B:s,-32768,-1,0,1,32767\tB3:B:S,0,1,32768,65535\tB4:B:c,-128,-1,0,1,127\tB5:B:C,0,1,127,255\tBf:B:f,-3.14159,2.71828\tZZ:i:1000000\tF2:d:2.46801\tY1:i:-2147483648\tY2:i:-2147483647\tY3:i:-1\tY4:i:0\tY5:i:1\tY6:i:2147483647\tY7:i:2147483648\tY8:i:4294967295";
static const char r2[] = "r234\t141\t*\t0\t0\t*\t*\t0\t0\tATGC\tqqqq";
static const char r3[] = "xyz\t141\t*\t0\t0\t*\t*\t0\t0\tATGC\tqqqq";
samFile *in = sam_open(sam, "r");
bam_hdr_t *header = sam_hdr_read(in);
bam1_t *aln = bam_init1();
kstring_t ks = { 0, 0, NULL };
if (sam_read1(in, header, aln) >= 0) {
bam_set_qname(aln, "r1");
if (sam_format1(header, aln, &ks) < 0) fail("can't format record");
if (strcmp(ks.s, r1) != 0) fail("record formatted incorrectly:\nGot: \"%s\"\nExp: \"%s\"\n", ks.s, r1);
}
else fail("can't read record");
if (sam_read1(in, header, aln) >= 0) {
bam_set_qname(aln, "r234");
if (sam_format1(header, aln, &ks) < 0) fail("can't format record");
if (strcmp(ks.s, r2) != 0) fail("record formatted incorrectly:\nGot: \"%s\"\nExp: \"%s\"\n", ks.s, r2);
}
else fail("can't read record");
if (sam_read1(in, header, aln) >= 0) {
bam_set_qname(aln, "xyz");
if (sam_format1(header, aln, &ks) < 0) fail("can't format record");
if (strcmp(ks.s, r3) != 0) fail("record formatted incorrectly:\nGot: \"%s\"\nExp: \"%s\"\n", ks.s, r3);
}
else fail("can't read record");
bam_destroy1(aln);
bam_hdr_destroy(header);
sam_close(in);
free(ks.s);
}
static void iterators1(void)
{
hts_itr_destroy(sam_itr_queryi(NULL, HTS_IDX_REST, 0, 0));
hts_itr_destroy(sam_itr_queryi(NULL, HTS_IDX_NONE, 0, 0));
}
// This function uses bam_hdr_t etc as a check ensuring the legacy typedef
// and functions continue to compile successfully.
static void copy_check_alignment(const char *infname, const char *informat,
const char *outfname, const char *outmode, const char *outref)
{
samFile *in = sam_open(infname, "r");
samFile *out = sam_open(outfname, outmode);
bam1_t *aln = bam_init1();
bam_hdr_t *header = NULL;
int res;
if (!in) {
fail("couldn't open %s", infname);
goto err;
}
if (!out) {
fail("couldn't open %s with mode %s", outfname, outmode);
goto err;
}
if (!aln) {
fail("bam_init1() failed");
goto err;
}
if (outref) {
if (hts_set_opt(out, CRAM_OPT_REFERENCE, outref) < 0) {
fail("setting reference %s for %s", outref, outfname);
goto err;
}
}
header = sam_hdr_read(in);
if (!header) {
fail("reading header from %s", infname);
goto err;
}
if (sam_hdr_write(out, header) < 0) fail("writing headers to %s", outfname);
while ((res = sam_read1(in, header, aln)) >= 0) {
int mod4 = ((intptr_t) bam_get_cigar(aln)) % 4;
if (mod4 != 0)
fail("%s CIGAR not 4-byte aligned; offset is 4k+%d for \"%s\"",
informat, mod4, bam_get_qname(aln));
if (sam_write1(out, header, aln) < 0) fail("writing to %s", outfname);
}
if (res < -1) {
fail("failed to read alignment from %s", infname);
}
err:
bam_destroy1(aln);
aln = NULL;
bam_hdr_destroy(header);
header = NULL;
if (in) sam_close(in);
if (out) sam_close(out);
}
static int check_target_names(sam_hdr_t *header, int expected_n_targets,
const char **expected_targets,
const int *expected_lengths) {
int i;
// Check consistency of target_names array
if (!header->target_name) {
fail("target_name is NULL");
return -1;
}
if (!header->target_len) {
fail("target_len is NULL");
return -1;
}
if (header->n_targets != expected_n_targets) {
fail("header->n_targets (%d) != expected_n_targets (%d)",
header->n_targets, expected_n_targets);
return -1;
}
for (i = 0; i < expected_n_targets; i++) {
if (!header->target_name[i]
|| strcmp(header->target_name[i], expected_targets[i]) != 0) {
fail("header->target_name[%d] (%s) != \"%s\"",
i, header->target_name[i] ? header->target_name[i] : "NULL",
expected_targets[i]);
return -1;
}
if (header->target_len[i] != expected_lengths[i]) {
fail("header->target_len[%d] (%d) != %d",
i, header->target_len[i], expected_lengths[i]);
return -1;
}
}
return 0;
}
static void use_header_api(void) {
static const char header_text[] = "data:,"
"@HD\tVN:1.4\tGO:group\tSS:coordinate:queryname\n"
"@SQ\tSN:ref0\tLN:100\n"
"@CO\tThis line below will be updated\n"
"@SQ\tSN:ref1\tLN:5001\tM5:983dalu9ue2\n"
"@SQ\tSN:ref1.5\tLN:5001\n"
"@CO\tThis line is good\n"
"@SQ\tSN:ref2\tLN:5002\n";
static const char rg_line[] =
{ '@', 'R', 'G', '\t', 'I', 'D', ':', 'r', 'u', 'n', '1' };
static const char expected[] =
"@HD\tVN:1.5\tSO:coordinate\n"
"@CO\tThis line below will be updated\n"
"@SQ\tSN:ref1\tLN:5001\tM5:kja8u34a2q3\n"
"@CO\tThis line is good\n"
"@SQ\tSN:ref2\tLN:5002\n"
"@SQ\tSN:ref3\tLN:5003\n"
"@PG\tID:samtools\tPN:samtools\tVN:1.9\n"
"@RG\tID:run1\n"
"@RG\tID:run4\n";
static const char *expected_targets[] = { "ref1", "ref2", "ref3" };
static const int expected_lengths[] = { 5001, 5002, 5003 };
const int expected_n_targets = sizeof(expected_targets) / sizeof(char *);
const char outfname[] = "test/sam_header.tmp.sam_";
const char outmode[] = "w";
FILE *inf = NULL;
char buffer[sizeof(expected) + 1024];
samFile *in = sam_open(header_text, "r");
samFile *out = sam_open(outfname, outmode);
sam_hdr_t *header = NULL;
kstring_t ks = { 0, 0, NULL };
size_t bytes;
int r;
const char *name;
if (!in) {
fail("couldn't open file");
goto err;
}
if (!out) {
fail("couldn't open %s with mode %s", outfname, outmode);
goto err;
}
header = sam_hdr_read(in);
if (!header) {
fail("reading header from file");
goto err;
}
r = sam_hdr_remove_tag_id(header, "HD", NULL, NULL, "GO");
if (r != 1) { fail("sam_hdr_remove_tag_id"); goto err; }
r = sam_hdr_update_hd(header, "VN", "1.5");
if (r != 0) { fail("sam_hdr_update_hd"); goto err; }
r = sam_hdr_add_line(header, "SQ", "SN", "ref3", "LN", "5003", NULL);
if (r < 0) { fail("sam_hdr_add_line"); goto err; }
r = sam_hdr_update_line(header, "SQ", "SN", "ref1",
"M5", "kja8u34a2q3", NULL);
if (r != 0) { fail("sam_hdr_update_line SQ"); goto err; }
r = sam_hdr_add_pg(header, "samtools", "VN", "1.9", NULL);
if (r != 0) { fail("sam_hdr_add_pg"); goto err; }
// Test addition with no newline or trailing NUL
r = sam_hdr_add_lines(header, rg_line, sizeof(rg_line));
if (r != 0) { fail("sam_hdr_add_lines rg_line"); goto err; }
// Test header line removal
r = sam_hdr_add_line(header, "RG", "ID", "run2", NULL);
if (r < 0) { fail("sam_hdr_add_line"); goto err; }
r = sam_hdr_add_line(header, "RG", "ID", "run3", NULL);
if (r < 0) { fail("sam_hdr_add_line"); goto err; }
r = sam_hdr_add_line(header, "RG", "ID", "run4", NULL);
if (r < 0) { fail("sam_hdr_add_line"); goto err; }
r = sam_hdr_line_index(header, "RG", "run4");
if (r != 3) { fail("sam_hdr_line_index - run4~3"); goto err; }
r = sam_hdr_line_index(header, "RG", "run5");
if (r != -1) { fail("sam_hdr_line_index - run5~-1"); goto err; }
name = sam_hdr_line_name(header, "RG", 2);
if (!name || strcmp(name, "run3")) { fail("sam_hdr_line_name - 2~run3"); goto err; }
name = sam_hdr_line_name(header, "RG", 10);
if (name) { fail("sam_hdr_line_name - 10~NULL"); goto err; }
r = sam_hdr_remove_line_id(header, "RG", "ID", "run2");
if (r < 0) { fail("sam_hdr_remove_line_id"); goto err; }
r = sam_hdr_find_tag_id(header, "RG", "ID", "run3", "ID", &ks);
if (r < 0 || !ks.s || strcmp(ks.s, "run3") != 0) {
fail("sam_hdr_find_tag_id() expected \"run3\" got \"%s\"",
r == 0 && ks.s ? ks.s : "NULL");
goto err;
}
r = sam_hdr_remove_line_pos(header, "RG", 1); // Removes run3
if (r < 0) { fail("sam_hdr_remove_line_pos"); goto err; }
r = sam_hdr_remove_line_id(header, "SQ", "SN", "ref0");
if (r < 0) { fail("sam_hdr_remove_line_id"); goto err; }
r = sam_hdr_remove_line_pos(header, "SQ", 1); // Removes ref1.5
if (r < 0) { fail("sam_hdr_remove_line_pos"); goto err; }
r = sam_hdr_find_tag_id(header, "SQ", "SN", "ref1", "M5", &ks);
if (r < 0 || !ks.s || strcmp(ks.s, "kja8u34a2q3") != 0) {
fail("sam_hdr_find_tag_id() expected \"kja8u34a2q3\" got \"%s\"",
r == 0 && ks.s ? ks.s : "NULL");
goto err;
}
r = sam_hdr_line_index(header, "RG", "run4");
if (r != 1) { fail("sam_hdr_line_index - run4~1"); goto err; }
name = sam_hdr_line_name(header, "RG", 2);
if (name) { fail("sam_hdr_line_name - 2~NULL"); goto err; }
r = sam_hdr_remove_tag_hd(header, "SS");
if (r < 0) {
fail("sam_hdr_remove_tag_hd");
}
r = sam_hdr_find_hd(header, &ks);
if (r < 0 || !ks.s || strcmp(ks.s, "@HD\tVN:1.5") != 0) {
fail("sam_hdr_find_hd() expected \"@HD\tVN:1.5\" got \"%s\"",
r == 0 && ks.s ? ks.s : "NULL");
}
r = sam_hdr_find_tag_hd(header, "VN", &ks);
if (r < 0 || !ks.s || strcmp(ks.s, "1.5") != 0) {
fail("sam_hdr_find_tag_hd() expected \"1.5\" got \"%s\"",
r == 0 && ks.s ? ks.s : "NULL");
}
r = sam_hdr_update_hd(header, "SO", "coordinate");
if (r < 0) {
fail("sam_hdr_update_hd");
}
if (check_target_names(header, expected_n_targets, expected_targets,
expected_lengths) < 0) {
goto err;
}
if ((r = sam_hdr_count_lines(header, "HD")) != 1) {
fail("incorrect HD line count - expected 1, got %d", r);
goto err;
}
if ((r = sam_hdr_count_lines(header, "SQ")) != 3) {
fail("incorrect SQ line count - expected 3, got %d", r);
goto err;
}
if ((r = sam_hdr_count_lines(header, "PG")) != 1) {
fail("incorrect PG line count - expected 1, got %d", r);
goto err;
}
if ((r = sam_hdr_count_lines(header, "RG")) != 2) {
fail("incorrect RG line count - expected 2, got %d", r);
goto err;
}
if ((r = sam_hdr_count_lines(header, "CO")) != 2) {
fail("incorrect CO line count - expected 2, got %d", r);
goto err;
}
if (sam_hdr_write(out, header) < 0) {
fail("writing headers to \"%s\"", outfname);
goto err;
}
r = sam_close(out);
out = NULL;
if (r < 0) {
fail("close \"%s\"", outfname);
goto err;
}
inf = fopen(outfname, "r");
if (!inf) {
fail("Opening written header \"%s\"", outfname);
goto err;
}
bytes = fread(buffer, 1, sizeof(buffer), inf);
if (bytes != sizeof(expected) - 1 || memcmp(buffer, expected, bytes) != 0) {
fail("edited header does not match expected version");
fprintf(stderr,
"---------- Expected:\n%.*s\n"
"++++++++++ Got:\n%.*s\n"
"====================\n",
(int) sizeof(expected), expected,
(int) bytes, buffer);
goto err;
}
free(ks_release(&ks));
err:
sam_hdr_destroy(header);
header = NULL;
if (in) sam_close(in);
if (out) sam_close(out);
if (inf) fclose(inf);
free(ks_release(&ks));
}
static void test_header_pg_lines(void) {
static const char header_text[] = "data:,"
"@HD\tVN:1.5\n"
"@PG\tID:prog1\tPN:prog1\n"
"@PG\tID:prog2\tPN:prog2\tPP:prog1\n";
static const char expected[] =
"@HD\tVN:1.5\n"
"@PG\tID:prog1\tPN:prog1\n"
"@PG\tID:prog2\tPN:prog2\tPP:prog1\n"
"@PG\tID:prog3\tPN:prog3\tPP:prog2\n"
"@PG\tID:prog4\tPN:prog4\tPP:prog1\n"
"@PG\tID:prog5\tPN:prog5\tPP:prog2\n"
"@PG\tID:prog6\tPN:prog6\tPP:prog3\n"
"@PG\tID:prog6.1\tPN:prog6\tPP:prog4\n"
"@PG\tID:prog6.2\tPN:prog6\tPP:prog5\n"
"@PG\tPN:prog7\tID:my_id\tPP:prog6\n";
samFile *in = sam_open(header_text, "r");
sam_hdr_t *header = NULL;
const char *text = NULL;
enum htsLogLevel old_log_level;
int r;
if (!in) {
fail("couldn't open file");
goto err;
}
header = sam_hdr_read(in);
if (!header) {
fail("reading header from file");
goto err;
}
r = sam_hdr_add_pg(header, "prog3", NULL);
if (r != 0) { fail("sam_hdr_add_pg prog3"); goto err; }
r = sam_hdr_add_pg(header, "prog4", "PP", "prog1", NULL);
if (r != 0) { fail("sam_hdr_add_pg prog4"); goto err; }
r = sam_hdr_add_line(header, "PG", "ID",
"prog5", "PN", "prog5", "PP", "prog2", NULL);
if (r != 0) { fail("sam_hdr_add_line @PG ID:prog5"); goto err; }
r = sam_hdr_add_pg(header, "prog6", NULL);
if (r != 0) { fail("sam_hdr_add_pg prog6"); goto err; }
r = sam_hdr_add_pg(header, "prog7", "ID", "my_id", "PP", "prog6", NULL);
if (r != 0) { fail("sam_hdr_add_pg prog7"); goto err; }
text = sam_hdr_str(header);
if (!text) { fail("sam_hdr_str"); goto err; }
// These should fail
old_log_level = hts_get_log_level();
hts_set_log_level(HTS_LOG_OFF);
r = sam_hdr_add_pg(header, "prog8", "ID", "my_id", NULL);
if (r == 0) { fail("sam_hdr_add_pg prog8 (unexpected success)"); goto err; }
r = sam_hdr_add_pg(header, "prog9", "PP", "non-existent", NULL);
if (r == 0) { fail("sam_hdr_add_pg prog9 (unexpected success)"); goto err; }
hts_set_log_level(old_log_level);
// End failing tests
text = sam_hdr_str(header);
if (!text || strcmp(text, expected) != 0) {
fail("edited header does not match expected version");
fprintf(stderr,
"---------- Expected:\n%s\n"
"++++++++++ Got:\n%s\n"
"====================\n",
expected, text);
goto err;
}
err:
sam_hdr_destroy(header);
header = NULL;
if (in) sam_close(in);
return;
}
static void test_header_updates(void) {
static const char header_text[] =
"@HD\tVN:1.4\n"
"@SQ\tSN:chr1\tLN:100\n"
"@SQ\tSN:chr2\tLN:200\n"
"@SQ\tSN:chr3\tLN:300\n"
"@RG\tID:run1\n"
"@RG\tID:run2\n"
"@RG\tID:run3\n"
"@PG\tID:prog1\tPN:prog1\n";
static const char expected[] =
"@HD\tVN:1.4\n"
"@SQ\tSN:1\tLN:100\n"
"@SQ\tSN:chr2\tLN:2000\n"
"@SQ\tSN:chr3\tLN:300\n"
"@RG\tID:run1\tDS:hello\n"
"@RG\tID:aliquot2\n"
"@RG\tID:run3\n"
"@PG\tID:prog1\tPN:prog1\n";
static const char *expected_targets[] = { "1", "chr2", "chr3" };
static const int expected_lengths[] = { 100, 2000, 300 };
const int expected_n_targets = sizeof(expected_targets) / sizeof(char *);
sam_hdr_t *header = sam_hdr_parse(sizeof(header_text) - 1, header_text);
const char *hdr_str;
int r, i, old_log_level;
if (!header) {
fail("creating sam header");
goto err;
}