-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathcram_decode.c
More file actions
3387 lines (2957 loc) · 113 KB
/
cram_decode.c
File metadata and controls
3387 lines (2957 loc) · 113 KB
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
/*
Copyright (c) 2012-2014 Genome Research Ltd.
Author: James Bonfield <jkb@sanger.ac.uk>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
Institute nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* - In-memory decoding of CRAM data structures.
* - Iterator for reading CRAM record by record.
*/
#define HTS_BUILDING_LIBRARY // Enables HTSLIB_EXPORT, see htslib/hts_defs.h
#include <config.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <stdint.h>
#include <inttypes.h>
#include "cram/cram.h"
#include "cram/os.h"
#include "htslib/hts.h"
//Whether CIGAR has just M or uses = and X to indicate match and mismatch
//#define USE_X
/* ----------------------------------------------------------------------
* CRAM compression headers
*/
/*
* Decodes the Tag Dictionary record in the preservation map
* Updates the cram compression header.
*
* Returns number of bytes decoded on success
* -1 on failure
*/
int cram_decode_TD(char *cp, const char *endp, cram_block_compression_hdr *h) {
char *op = cp;
unsigned char *dat;
cram_block *b;
int32_t blk_size = 0;
int nTL, i, sz;
if (!(b = cram_new_block(0, 0)))
return -1;
if (h->TD_blk || h->TL) {
hts_log_warning("More than one TD block found in compression header");
cram_free_block(h->TD_blk);
free(h->TL);
h->TD_blk = NULL;
h->TL = NULL;
}
/* Decode */
cp += safe_itf8_get(cp, endp, &blk_size);
if (!blk_size) {
h->nTL = 0;
cram_free_block(b);
return cp - op;
}
if (blk_size < 0 || endp - cp < blk_size) {
cram_free_block(b);
return -1;
}
BLOCK_APPEND(b, cp, blk_size);
cp += blk_size;
sz = cp - op;
// Force nul termination if missing
if (BLOCK_DATA(b)[BLOCK_SIZE(b)-1])
BLOCK_APPEND_CHAR(b, '\0');
/* Set up TL lookup table */
dat = BLOCK_DATA(b);
// Count
for (nTL = i = 0; i < BLOCK_SIZE(b); i++) {
nTL++;
while (dat[i])
i++;
}
// Copy
if (!(h->TL = calloc(nTL, sizeof(*h->TL)))) {
cram_free_block(b);
return -1;
}
for (nTL = i = 0; i < BLOCK_SIZE(b); i++) {
h->TL[nTL++] = &dat[i];
while (dat[i])
i++;
}
h->TD_blk = b;
h->nTL = nTL;
return sz;
block_err:
cram_free_block(b);
return -1;
}
/*
* Decodes a CRAM block compression header.
* Returns header ptr on success
* NULL on failure
*/
cram_block_compression_hdr *cram_decode_compression_header(cram_fd *fd,
cram_block *b) {
char *cp, *endp, *cp_copy;
cram_block_compression_hdr *hdr = calloc(1, sizeof(*hdr));
int i;
int32_t map_size = 0, map_count = 0;
if (!hdr)
return NULL;
if (b->method != RAW) {
if (cram_uncompress_block(b)) {
free(hdr);
return NULL;
}
}
cp = (char *)b->data;
endp = cp + b->uncomp_size;
if (CRAM_MAJOR_VERS(fd->version) == 1) {
int32_t i32;
cp += safe_itf8_get(cp, endp, &hdr->ref_seq_id);
/*
* LARGE_POS used in this code is purely a debugging mechanism for testing
* whether the htslib API can cope with 64-bit quantities. These are
* possible in SAM, but not *yet* in BAM or CRAM.
*
* DO NOT ENABLE LARGE_POS for anything other than debugging / testing.
*
* At some point it is expected these ifdefs will become a version check
* instead.
*/
#ifdef LARGE_POS
cp += safe_ltf8_get(cp, endp, &hdr->ref_seq_start);
cp += safe_ltf8_get(cp, endp, &hdr->ref_seq_span);
#else
cp += safe_itf8_get(cp, endp, &i32); hdr->ref_seq_start=i32;
cp += safe_itf8_get(cp, endp, &i32); hdr->ref_seq_span=i32;
#endif
cp += safe_itf8_get(cp, endp, &hdr->num_records);
cp += safe_itf8_get(cp, endp, &hdr->num_landmarks);
if (hdr->num_landmarks < 0 ||
hdr->num_landmarks >= SIZE_MAX / sizeof(int32_t) ||
endp - cp < hdr->num_landmarks) {
free(hdr);
return NULL;
}
if (!(hdr->landmark = malloc(hdr->num_landmarks * sizeof(int32_t)))) {
free(hdr);
return NULL;
}
for (i = 0; i < hdr->num_landmarks; i++) {
cp += safe_itf8_get(cp, endp, &hdr->landmark[i]);
}
}
hdr->preservation_map = kh_init(map);
memset(hdr->rec_encoding_map, 0,
CRAM_MAP_HASH * sizeof(hdr->rec_encoding_map[0]));
memset(hdr->tag_encoding_map, 0,
CRAM_MAP_HASH * sizeof(hdr->tag_encoding_map[0]));
if (!hdr->preservation_map) {
cram_free_compression_header(hdr);
return NULL;
}
/* Initialise defaults for preservation map */
hdr->read_names_included = 0;
hdr->AP_delta = 1;
memcpy(hdr->substitution_matrix, "CGTNAGTNACTNACGNACGT", 20);
/* Preservation map */
cp += safe_itf8_get(cp, endp, &map_size); cp_copy = cp;
cp += safe_itf8_get(cp, endp, &map_count);
for (i = 0; i < map_count; i++) {
pmap_t hd;
khint_t k;
int r;
if (endp - cp < 3) {
cram_free_compression_header(hdr);
return NULL;
}
cp += 2;
switch(CRAM_KEY(cp[-2],cp[-1])) {
case CRAM_KEY('M','I'): // was mapped QS included in V1.0
case CRAM_KEY('U','I'): // was unmapped QS included in V1.0
case CRAM_KEY('P','I'): // was unmapped placed in V1.0
hd.i = *cp++;
break;
case CRAM_KEY('R','N'):
hd.i = *cp++;
k = kh_put(map, hdr->preservation_map, "RN", &r);
if (-1 == r) {
cram_free_compression_header(hdr);
return NULL;
}
kh_val(hdr->preservation_map, k) = hd;
hdr->read_names_included = hd.i;
break;
case CRAM_KEY('A','P'):
hd.i = *cp++;
k = kh_put(map, hdr->preservation_map, "AP", &r);
if (-1 == r) {
cram_free_compression_header(hdr);
return NULL;
}
kh_val(hdr->preservation_map, k) = hd;
hdr->AP_delta = hd.i;
break;
case CRAM_KEY('R','R'):
hd.i = *cp++;
k = kh_put(map, hdr->preservation_map, "RR", &r);
if (-1 == r) {
cram_free_compression_header(hdr);
return NULL;
}
kh_val(hdr->preservation_map, k) = hd;
hdr->no_ref = !hd.i;
break;
case CRAM_KEY('S','M'):
if (endp - cp < 5) {
cram_free_compression_header(hdr);
return NULL;
}
hdr->substitution_matrix[0][(cp[0]>>6)&3] = 'C';
hdr->substitution_matrix[0][(cp[0]>>4)&3] = 'G';
hdr->substitution_matrix[0][(cp[0]>>2)&3] = 'T';
hdr->substitution_matrix[0][(cp[0]>>0)&3] = 'N';
hdr->substitution_matrix[1][(cp[1]>>6)&3] = 'A';
hdr->substitution_matrix[1][(cp[1]>>4)&3] = 'G';
hdr->substitution_matrix[1][(cp[1]>>2)&3] = 'T';
hdr->substitution_matrix[1][(cp[1]>>0)&3] = 'N';
hdr->substitution_matrix[2][(cp[2]>>6)&3] = 'A';
hdr->substitution_matrix[2][(cp[2]>>4)&3] = 'C';
hdr->substitution_matrix[2][(cp[2]>>2)&3] = 'T';
hdr->substitution_matrix[2][(cp[2]>>0)&3] = 'N';
hdr->substitution_matrix[3][(cp[3]>>6)&3] = 'A';
hdr->substitution_matrix[3][(cp[3]>>4)&3] = 'C';
hdr->substitution_matrix[3][(cp[3]>>2)&3] = 'G';
hdr->substitution_matrix[3][(cp[3]>>0)&3] = 'N';
hdr->substitution_matrix[4][(cp[4]>>6)&3] = 'A';
hdr->substitution_matrix[4][(cp[4]>>4)&3] = 'C';
hdr->substitution_matrix[4][(cp[4]>>2)&3] = 'G';
hdr->substitution_matrix[4][(cp[4]>>0)&3] = 'T';
hd.p = cp;
cp += 5;
k = kh_put(map, hdr->preservation_map, "SM", &r);
if (-1 == r) {
cram_free_compression_header(hdr);
return NULL;
}
kh_val(hdr->preservation_map, k) = hd;
break;
case CRAM_KEY('T','D'): {
int sz = cram_decode_TD(cp, endp, hdr); // tag dictionary
if (sz < 0) {
cram_free_compression_header(hdr);
return NULL;
}
hd.p = cp;
cp += sz;
k = kh_put(map, hdr->preservation_map, "TD", &r);
if (-1 == r) {
cram_free_compression_header(hdr);
return NULL;
}
kh_val(hdr->preservation_map, k) = hd;
break;
}
default:
hts_log_warning("Unrecognised preservation map key %c%c", cp[-2], cp[-1]);
// guess byte;
cp++;
break;
}
}
if (cp - cp_copy != map_size) {
cram_free_compression_header(hdr);
return NULL;
}
/* Record encoding map */
cp += safe_itf8_get(cp, endp, &map_size); cp_copy = cp;
cp += safe_itf8_get(cp, endp, &map_count);
for (i = 0; i < map_count; i++) {
char *key = cp;
int32_t encoding = E_NULL;
int32_t size = 0;
ptrdiff_t offset;
cram_map *m;
enum cram_DS_ID ds_id;
enum cram_external_type type;
if (endp - cp < 4) {
cram_free_compression_header(hdr);
return NULL;
}
cp += 2;
cp += safe_itf8_get(cp, endp, &encoding);
cp += safe_itf8_get(cp, endp, &size);
offset = cp - (char *)b->data;
if (encoding == E_NULL)
continue;
if (size < 0 || endp - cp < size) {
cram_free_compression_header(hdr);
return NULL;
}
//printf("%s codes for %.2s\n", cram_encoding2str(encoding), key);
/*
* For CRAM1.0 CF and BF are Byte and not Int.
* Practically speaking it makes no difference unless we have a
* 1.0 format file that stores these in EXTERNAL as only then
* does Byte vs Int matter.
*
* Neither this C code nor Java reference implementations did this,
* so we gloss over it and treat them as int.
*/
ds_id = DS_CORE;
if (key[0] == 'B' && key[1] == 'F') {
ds_id = DS_BF; type = E_INT;
} else if (key[0] == 'C' && key[1] == 'F') {
ds_id = DS_CF; type = E_INT;
} else if (key[0] == 'R' && key[1] == 'I') {
ds_id = DS_RI; type = E_INT;
} else if (key[0] == 'R' && key[1] == 'L') {
ds_id = DS_RL; type = E_INT;
} else if (key[0] == 'A' && key[1] == 'P') {
ds_id = DS_AP;
#ifdef LARGE_POS
type = E_LONG,
#else
type = E_INT;
#endif
} else if (key[0] == 'R' && key[1] == 'G') {
ds_id = DS_RG; type = E_INT;
} else if (key[0] == 'M' && key[1] == 'F') {
ds_id = DS_MF; type = E_INT;
} else if (key[0] == 'N' && key[1] == 'S') {
ds_id = DS_NS; type = E_INT;
} else if (key[0] == 'N' && key[1] == 'P') {
ds_id = DS_NP;
#ifdef LARGE_POS
type = E_LONG,
#else
type = E_INT;
#endif
} else if (key[0] == 'T' && key[1] == 'S') {
ds_id = DS_TS;
#ifdef LARGE_POS
type = E_LONG,
#else
type = E_INT;
#endif
} else if (key[0] == 'N' && key[1] == 'F') {
ds_id = DS_NF; type = E_INT;
} else if (key[0] == 'T' && key[1] == 'C') {
ds_id = DS_TC; type = E_BYTE;
} else if (key[0] == 'T' && key[1] == 'N') {
ds_id = DS_TN; type = E_INT;
} else if (key[0] == 'F' && key[1] == 'N') {
ds_id = DS_FN; type = E_INT;
} else if (key[0] == 'F' && key[1] == 'C') {
ds_id = DS_FC; type = E_BYTE;
} else if (key[0] == 'F' && key[1] == 'P') {
ds_id = DS_FP; type = E_INT;
} else if (key[0] == 'B' && key[1] == 'S') {
ds_id = DS_BS; type = E_BYTE;
} else if (key[0] == 'I' && key[1] == 'N') {
ds_id = DS_IN; type = E_BYTE_ARRAY;
} else if (key[0] == 'S' && key[1] == 'C') {
ds_id = DS_SC; type = E_BYTE_ARRAY;
} else if (key[0] == 'D' && key[1] == 'L') {
ds_id = DS_DL; type = E_INT;
} else if (key[0] == 'B' && key[1] == 'A') {
ds_id = DS_BA; type = E_BYTE;
} else if (key[0] == 'B' && key[1] == 'B') {
ds_id = DS_BB; type = E_BYTE_ARRAY;
} else if (key[0] == 'R' && key[1] == 'S') {
ds_id = DS_RS; type = E_INT;
} else if (key[0] == 'P' && key[1] == 'D') {
ds_id = DS_PD; type = E_INT;
} else if (key[0] == 'H' && key[1] == 'C') {
ds_id = DS_HC; type = E_INT;
} else if (key[0] == 'M' && key[1] == 'Q') {
ds_id = DS_MQ; type = E_INT;
} else if (key[0] == 'R' && key[1] == 'N') {
ds_id = DS_RN; type = E_BYTE_ARRAY_BLOCK;
} else if (key[0] == 'Q' && key[1] == 'S') {
ds_id = DS_QS; type = E_BYTE;
} else if (key[0] == 'Q' && key[1] == 'Q') {
ds_id = DS_QQ; type = E_BYTE_ARRAY;
} else if (key[0] == 'T' && key[1] == 'L') {
ds_id = DS_TL; type = E_INT;
} else if (key[0] == 'T' && key[1] == 'M') {
} else if (key[0] == 'T' && key[1] == 'V') {
} else {
hts_log_warning("Unrecognised key: %.2s", key);
}
if (ds_id != DS_CORE) {
if (hdr->codecs[ds_id] != NULL) {
hts_log_warning("Codec for key %.2s defined more than once",
key);
hdr->codecs[ds_id]->free(hdr->codecs[ds_id]);
}
hdr->codecs[ds_id] = cram_decoder_init(encoding, cp, size,
type, fd->version);
if (!hdr->codecs[ds_id]) {
cram_free_compression_header(hdr);
return NULL;
}
}
cp += size;
// Fill out cram_map purely for cram_dump to dump out.
m = malloc(sizeof(*m));
if (!m) {
cram_free_compression_header(hdr);
return NULL;
}
m->key = CRAM_KEY(key[0], key[1]);
m->encoding = encoding;
m->size = size;
m->offset = offset;
m->codec = NULL;
m->next = hdr->rec_encoding_map[CRAM_MAP(key[0], key[1])];
hdr->rec_encoding_map[CRAM_MAP(key[0], key[1])] = m;
}
if (cp - cp_copy != map_size) {
cram_free_compression_header(hdr);
return NULL;
}
/* Tag encoding map */
cp += safe_itf8_get(cp, endp, &map_size); cp_copy = cp;
cp += safe_itf8_get(cp, endp, &map_count);
for (i = 0; i < map_count; i++) {
int32_t encoding = E_NULL;
int32_t size = 0;
cram_map *m = malloc(sizeof(*m)); // FIXME: use pooled_alloc
uint8_t *key;
if (!m || endp - cp < 6) {
free(m);
cram_free_compression_header(hdr);
return NULL;
}
key = (uint8_t *) cp + 1;
m->key = (key[0]<<16)|(key[1]<<8)|key[2];
cp += 4; // Strictly ITF8, but this suffices
cp += safe_itf8_get(cp, endp, &encoding);
cp += safe_itf8_get(cp, endp, &size);
m->encoding = encoding;
m->size = size;
m->offset = cp - (char *)b->data;
if (size < 0 || endp - cp < size ||
!(m->codec = cram_decoder_init(encoding, cp, size,
E_BYTE_ARRAY_BLOCK, fd->version))) {
cram_free_compression_header(hdr);
free(m);
return NULL;
}
cp += size;
m->next = hdr->tag_encoding_map[CRAM_MAP(key[0],key[1])];
hdr->tag_encoding_map[CRAM_MAP(key[0],key[1])] = m;
}
if (cp - cp_copy != map_size) {
cram_free_compression_header(hdr);
return NULL;
}
return hdr;
}
/*
* Note we also need to scan through the record encoding map to
* see which data series share the same block, either external or
* CORE. For example if we need the BF data series but MQ and CF
* are also encoded in the same block then we need to add those in
* as a dependency in order to correctly decode BF.
*
* Returns 0 on success
* -1 on failure
*/
int cram_dependent_data_series(cram_fd *fd,
cram_block_compression_hdr *hdr,
cram_slice *s) {
int *block_used;
int core_used = 0;
int i;
static int i_to_id[] = {
DS_BF, DS_AP, DS_FP, DS_RL, DS_DL, DS_NF, DS_BA, DS_QS,
DS_FC, DS_FN, DS_BS, DS_IN, DS_RG, DS_MQ, DS_TL, DS_RN,
DS_NS, DS_NP, DS_TS, DS_MF, DS_CF, DS_RI, DS_RS, DS_PD,
DS_HC, DS_SC, DS_BB, DS_QQ,
};
uint32_t orig_ds;
/*
* Set the data_series bit field based on fd->required_fields
* contents.
*/
if (fd->required_fields && fd->required_fields != INT_MAX) {
s->data_series = 0;
if (fd->required_fields & SAM_QNAME)
s->data_series |= CRAM_RN;
if (fd->required_fields & SAM_FLAG)
s->data_series |= CRAM_BF;
if (fd->required_fields & SAM_RNAME)
s->data_series |= CRAM_RI | CRAM_BF;
if (fd->required_fields & SAM_POS)
s->data_series |= CRAM_AP | CRAM_BF;
if (fd->required_fields & SAM_MAPQ)
s->data_series |= CRAM_MQ;
if (fd->required_fields & SAM_CIGAR)
s->data_series |= CRAM_CIGAR;
if (fd->required_fields & SAM_RNEXT)
s->data_series |= CRAM_CF | CRAM_NF | CRAM_RI | CRAM_NS |CRAM_BF;
if (fd->required_fields & SAM_PNEXT)
s->data_series |= CRAM_CF | CRAM_NF | CRAM_AP | CRAM_NP | CRAM_BF;
if (fd->required_fields & SAM_TLEN)
s->data_series |= CRAM_CF | CRAM_NF | CRAM_AP | CRAM_TS |
CRAM_BF | CRAM_MF | CRAM_RI | CRAM_CIGAR;
if (fd->required_fields & SAM_SEQ)
s->data_series |= CRAM_SEQ;
if (!(fd->required_fields & SAM_AUX))
// No easy way to get MD/NM without other tags at present
s->decode_md = 0;
if (fd->required_fields & SAM_QUAL)
s->data_series |= CRAM_QUAL;
if (fd->required_fields & SAM_AUX)
s->data_series |= CRAM_RG | CRAM_TL | CRAM_aux;
if (fd->required_fields & SAM_RGAUX)
s->data_series |= CRAM_RG | CRAM_BF;
// Always uncompress CORE block
if (cram_uncompress_block(s->block[0]))
return -1;
} else {
s->data_series = CRAM_ALL;
for (i = 0; i < s->hdr->num_blocks; i++) {
if (cram_uncompress_block(s->block[i]))
return -1;
}
return 0;
}
block_used = calloc(s->hdr->num_blocks+1, sizeof(int));
if (!block_used)
return -1;
do {
/*
* Also set data_series based on code prerequisites. Eg if we need
* CRAM_QS then we also need to know CRAM_RL so we know how long it
* is, or if we need FC/FP then we also need FN (number of features).
*
* It's not reciprocal though. We may be needing to decode FN
* but have no need to decode FC, FP and cigar ops.
*/
if (s->data_series & CRAM_RS) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_PD) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_HC) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_QS) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_IN) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_SC) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_BS) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_DL) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_BA) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_BB) s->data_series |= CRAM_FC|CRAM_FP;
if (s->data_series & CRAM_QQ) s->data_series |= CRAM_FC|CRAM_FP;
// cram_decode_seq() needs seq[] array
if (s->data_series & (CRAM_SEQ|CRAM_CIGAR)) s->data_series |= CRAM_RL;
if (s->data_series & CRAM_FP) s->data_series |= CRAM_FC;
if (s->data_series & CRAM_FC) s->data_series |= CRAM_FN;
if (s->data_series & CRAM_aux) s->data_series |= CRAM_TL;
if (s->data_series & CRAM_MF) s->data_series |= CRAM_CF;
if (s->data_series & CRAM_MQ) s->data_series |= CRAM_BF;
if (s->data_series & CRAM_BS) s->data_series |= CRAM_RI;
if (s->data_series & (CRAM_MF |CRAM_NS |CRAM_NP |CRAM_TS |CRAM_NF))
s->data_series |= CRAM_CF;
if (!hdr->read_names_included && s->data_series & CRAM_RN)
s->data_series |= CRAM_CF | CRAM_NF;
if (s->data_series & (CRAM_BA | CRAM_QS | CRAM_BB | CRAM_QQ))
s->data_series |= CRAM_BF | CRAM_CF | CRAM_RL;
orig_ds = s->data_series;
// Find which blocks are in use.
for (i = 0; i < sizeof(i_to_id)/sizeof(*i_to_id); i++) {
int bnum1, bnum2, j;
cram_codec *c = hdr->codecs[i_to_id[i]];
if (!(s->data_series & (1<<i)))
continue;
if (!c)
continue;
bnum1 = cram_codec_to_id(c, &bnum2);
for (;;) {
switch (bnum1) {
case -2:
break;
case -1:
core_used = 1;
break;
default:
for (j = 0; j < s->hdr->num_blocks; j++) {
if (s->block[j]->content_type == EXTERNAL &&
s->block[j]->content_id == bnum1) {
block_used[j] = 1;
if (cram_uncompress_block(s->block[j])) {
free(block_used);
return -1;
}
}
}
break;
}
if (bnum2 == -2 || bnum1 == bnum2)
break;
bnum1 = bnum2; // 2nd pass
}
}
// Tags too
if ((fd->required_fields & SAM_AUX) ||
(s->data_series & CRAM_aux)) {
for (i = 0; i < CRAM_MAP_HASH; i++) {
int bnum1, bnum2, j;
cram_map *m = hdr->tag_encoding_map[i];
while (m) {
cram_codec *c = m->codec;
if (!c) {
m = m->next;
continue;
}
bnum1 = cram_codec_to_id(c, &bnum2);
for (;;) {
switch (bnum1) {
case -2:
break;
case -1:
core_used = 1;
break;
default:
for (j = 0; j < s->hdr->num_blocks; j++) {
if (s->block[j]->content_type == EXTERNAL &&
s->block[j]->content_id == bnum1) {
block_used[j] = 1;
if (cram_uncompress_block(s->block[j])) {
free(block_used);
return -1;
}
}
}
break;
}
if (bnum2 == -2 || bnum1 == bnum2)
break;
bnum1 = bnum2; // 2nd pass
}
m = m->next;
}
}
}
// We now know which blocks are in used, so repeat and find
// which other data series need to be added.
for (i = 0; i < sizeof(i_to_id)/sizeof(*i_to_id); i++) {
int bnum1, bnum2, j;
cram_codec *c = hdr->codecs[i_to_id[i]];
if (!c)
continue;
bnum1 = cram_codec_to_id(c, &bnum2);
for (;;) {
switch (bnum1) {
case -2:
break;
case -1:
if (core_used) {
//printf(" + data series %08x:\n", 1<<i);
s->data_series |= 1<<i;
}
break;
default:
for (j = 0; j < s->hdr->num_blocks; j++) {
if (s->block[j]->content_type == EXTERNAL &&
s->block[j]->content_id == bnum1) {
if (block_used[j]) {
//printf(" + data series %08x:\n", 1<<i);
s->data_series |= 1<<i;
}
}
}
break;
}
if (bnum2 == -2 || bnum1 == bnum2)
break;
bnum1 = bnum2; // 2nd pass
}
}
// Tags too
for (i = 0; i < CRAM_MAP_HASH; i++) {
int bnum1, bnum2, j;
cram_map *m = hdr->tag_encoding_map[i];
while (m) {
cram_codec *c = m->codec;
if (!c) {
m = m->next;
continue;
}
bnum1 = cram_codec_to_id(c, &bnum2);
for (;;) {
switch (bnum1) {
case -2:
break;
case -1:
//printf(" + data series %08x:\n", CRAM_aux);
s->data_series |= CRAM_aux;
break;
default:
for (j = 0; j < s->hdr->num_blocks; j++) {
if (s->block[j]->content_type == EXTERNAL &&
s->block[j]->content_id == bnum1) {
if (block_used[j]) {
//printf(" + data series %08x:\n",
// CRAM_aux);
s->data_series |= CRAM_aux;
}
}
}
break;
}
if (bnum2 == -2 || bnum1 == bnum2)
break;
bnum1 = bnum2; // 2nd pass
}
m = m->next;
}
}
} while (orig_ds != s->data_series);
free(block_used);
return 0;
}
/*
* Checks whether an external block is used solely by a single data series.
* Returns the codec type if so (EXTERNAL, BYTE_ARRAY_LEN, BYTE_ARRAY_STOP)
* or 0 if not (E_NULL).
*/
static int cram_ds_unique(cram_block_compression_hdr *hdr, cram_codec *c,
int id) {
int i, n_id = 0;
enum cram_encoding e_type = 0;
for (i = 0; i < DS_END; i++) {
cram_codec *c;
int bnum1, bnum2, old_n_id;
if (!(c = hdr->codecs[i]))
continue;
bnum1 = cram_codec_to_id(c, &bnum2);
old_n_id = n_id;
if (bnum1 == id) {
n_id++;
e_type = c->codec;
}
if (bnum2 == id) {
n_id++;
e_type = c->codec;
}
if (n_id == old_n_id+2)
n_id--; // len/val in same place counts once only.
}
return n_id == 1 ? e_type : 0;
}
/*
* Attempts to estimate the size of some blocks so we can preallocate them
* before decoding. Although decoding will automatically grow the blocks,
* it is typically more efficient to preallocate.
*/
void cram_decode_estimate_sizes(cram_block_compression_hdr *hdr, cram_slice *s,
int *qual_size, int *name_size,
int *q_id) {
int bnum1, bnum2;
cram_codec *cd;
*qual_size = 0;
*name_size = 0;
/* Qual */
cd = hdr->codecs[DS_QS];
if (cd == NULL) return;
bnum1 = cram_codec_to_id(cd, &bnum2);
if (bnum1 < 0 && bnum2 >= 0) bnum1 = bnum2;
if (cram_ds_unique(hdr, cd, bnum1)) {
cram_block *b = cram_get_block_by_id(s, bnum1);
if (b) *qual_size = b->uncomp_size;
if (q_id && cd->codec == E_EXTERNAL)
*q_id = bnum1;
}
/* Name */
cd = hdr->codecs[DS_RN];
if (cd == NULL) return;
bnum1 = cram_codec_to_id(cd, &bnum2);
if (bnum1 < 0 && bnum2 >= 0) bnum1 = bnum2;
if (cram_ds_unique(hdr, cd, bnum1)) {
cram_block *b = cram_get_block_by_id(s, bnum1);
if (b) *name_size = b->uncomp_size;
}
}
/* ----------------------------------------------------------------------
* CRAM slices
*/
/*
* Decodes a CRAM (un)mapped slice header block.
* Returns slice header ptr on success
* NULL on failure
*/
cram_block_slice_hdr *cram_decode_slice_header(cram_fd *fd, cram_block *b) {
cram_block_slice_hdr *hdr;
unsigned char *cp;
unsigned char *cp_end;
int i;
if (b->method != RAW) {
/* Spec. says slice header should be RAW, but we can future-proof
by trying to decode it if it isn't. */
if (cram_uncompress_block(b) < 0)
return NULL;
}
cp = (unsigned char *)BLOCK_DATA(b);
cp_end = cp + b->uncomp_size;
if (b->content_type != MAPPED_SLICE &&
b->content_type != UNMAPPED_SLICE)
return NULL;
if (!(hdr = calloc(1, sizeof(*hdr))))
return NULL;
hdr->content_type = b->content_type;
if (b->content_type == MAPPED_SLICE) {
cp += safe_itf8_get((char *)cp, (char *)cp_end, &hdr->ref_seq_id);
#ifdef LARGE_POS
cp += safe_ltf8_get((char *)cp, (char *)cp_end, &hdr->ref_seq_start);
cp += safe_ltf8_get((char *)cp, (char *)cp_end, &hdr->ref_seq_span);
#else
int32_t i32;
cp += safe_itf8_get((char *)cp, (char *)cp_end, &i32);
hdr->ref_seq_start = i32;
cp += safe_itf8_get((char *)cp, (char *)cp_end, &i32);
hdr->ref_seq_span = i32;
#endif
}
cp += safe_itf8_get((char *)cp, (char *)cp_end, &hdr->num_records);
hdr->record_counter = 0;
if (CRAM_MAJOR_VERS(fd->version) == 2) {
int32_t i32 = 0;
cp += safe_itf8_get((char *)cp, (char *)cp_end, &i32);
hdr->record_counter = i32;
} else if (CRAM_MAJOR_VERS(fd->version) >= 3) {
cp += safe_ltf8_get((char *)cp, (char *)cp_end, &hdr->record_counter);
}