forked from samtools/hts-specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRAMv2.1.tex
1687 lines (1328 loc) · 60.7 KB
/
CRAMv2.1.tex
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
%&pdfLaTeX
% !TEX encoding = UTF-8 Unicode
\documentclass[a4paper]{article}
\usepackage{ifxetex}
\ifxetex
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{STIXGeneral}
\else
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\fi
\usepackage{textcomp}
\usepackage{graphicx}
\usepackage{array}
\usepackage{ulem}
\usepackage{fixltx2e}
\usepackage{amssymb}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\setlength{\parindent}{0cm}
\setlength{\parskip}{0.18cm}
\usepackage[hmargin=2cm,vmargin=2.5cm,bindingoffset=0.0cm]{geometry}
\usepackage[pdfborder={0 0 0}]{hyperref}
\begin{document}
\input{CRAMv2.1.ver}
\title{CRAM format specification (version 2.1)}
\author{[email protected]}
\date{\headdate}
\maketitle
\begin{quote}\small
The master version of this document can be found at
\url{https://github.com/samtools/hts-specs}.\\
This printing is version~\commitdesc\ from that repository,
last modified on the date shown above.
\end{quote}
\begin{center}
\textit{license: Apache 2.0}
\end{center}
\vspace*{1em}
\section{\textbf{Overview}}
This specification describes the CRAM 2.1 format.
CRAM has the following major objectives:
\begin{enumerate}
\item Significantly better lossless compression than BAM
\item Full compatibility with BAM
\item Effortless transition to CRAM from using BAM files
\item Support for controlled loss of BAM data
\end{enumerate}
The first three objectives allow users to take immediate advantage of the CRAM
format while offering a smooth transition path from using BAM files. The fourth
objective supports the exploration of different lossy compression strategies and
provides a framework in which to effect these choices. Please note that the CRAM
format does not impose any rules about what data should or should not be preserved.
Instead, CRAM supports a wide range of lossless and lossy data preservation strategies
enabling users to choose which data should be preserved.
Data in CRAM is stored either as CRAM records or using one of the general purpose
compressors (gzip, bzip2). CRAM records are compressed using a number of different
encoding strategies. For example, bases are reference compressed (\emph{Hsi-Yang
Fritz, et al. (2011) Genome Res. 21:734-740}) by encoding base differences rather
than storing the bases themselves.
\section{\textbf{Data types}}
CRAM specification uses logical data types and storage data types; logical data
types are written as words (e.g. int) while physical data types are written using
single letters (e.g. i). The difference between the two is that storage data types
define how logical data types are stored in CRAM. Data in CRAM is stored either
as as bits or as bytes. Writing values as bits and bytes is described in detail
below.
\subsection{\textbf{Logical data types}}
\begin{description}
\item[Byte]\ \newline
Signed byte (8 bits).
\item[Integer]\ \newline
Signed 32-bit integer.
\item[Long]\ \newline
Signed 64-bit integer.
\item[Array]\ \newline
An array of any logical data type: \texttt{<}type\texttt{>}[ ]
\end{description}
% \begin{tabular}{ll}
% \textbf{Byte} & Signed byte (8 bits). \\
% \\
% \textbf{Integer} & Signed 32-bit integer. \\
% \\
% \textbf{Long} & Signed 64-bit integer. \\
% \\
% \textbf{Array} & An array of any logical data type: \texttt{<}type\texttt{>}[ ] \\
% \end{tabular}
\subsection{\textbf{Writing bits to a bit stream}}
A bit stream consists of a sequence of 1s and 0s. The bits are written most significant
bit first where new bits are stacked to the right and full bytes on the left are
written out. In a bit stream the last byte will be incomplete if less than 8 bits
have been written to it. In this case the bits in the last byte are shifted to
the left.
\subsubsection*{Example of writing to bit stream}
Let's consider the following example. The table below shows a sequence of write
operations:
\begin{tabular}{|l|l|l|l|l|}
\hline
\textbf{Operation order} & \textbf{Buffer state before} & \textbf{Written bits} & \textbf{Buffer state after} & \textbf{Issued bytes}\tabularnewline
\hline
1 & 0x0 & 1 & 0x1 & -\tabularnewline
\hline
2 & 0x1 & 0 & 0x2 & -\tabularnewline
\hline
3 & 0x2 & 11 & 0xB & -\tabularnewline
\hline
4 & 0xB & 0000 0111 & 0x7 & 0xB0\tabularnewline
\hline
\end{tabular}
After flushing the above bit stream the following bytes are written: 0xB0 0x70.
Please note that the last byte was 0x7 before shifting to the left and became 0x70
after that:
\texttt{> echo "obase=16; ibase=2; 00000111" \textbar{} bc\\
7\\
\\
> echo "obase=16; ibase=2; 01110000" \textbar{} bc\\
70}
And the whole bit sequence:
\texttt{> echo "obase=2; ibase=16; B070" \textbar{} bc\\
1011000001110000}
When reading the bits from the bit sequence it must be known that only 12 bits
are meaningful and the bit stream should not be read after that.
\subsubsection*{Note on writing to bit stream}
When writing to a bit stream both the value and the number of bits in the value
must be known. This is because programming languages normally operate with bytes
(8 bits) and to specify which bits are to be written requires a bit-holder, for
example an integer, and the number of bits in it. Equally, when reading a value
from a bit stream the number of bits must be known in advance. In case of prefix
codes (e.g. Huffman) all possible bit combinations are either known in advance
or it is possible to calculate how many bits will follow based on the first few
bits. Alternatively, two codes can be combined, where the first contains the number
of bits to read.
\subsection{\textbf{Writing bytes to a byte stream}}
The interpretation of byte stream is straightforward. CRAM uses little \emph{endiannes}
for bytes when applicable and defines the following storage data types:
\begin{description}
\item[Boolean (bool)]\ \newline
Boolean is written as 1-byte with 0x0 being `false' and 0x1 being `true'.
\item[Integer (int32)]\ \newline
Signed 32-bit integer, written as 4 bytes in little-endian byte order.
\item[Long (int64)]\ \newline
Signed 64-bit integer, written as 8 bytes in little-endian byte order.
\item[ITF-8 integer (itf8)]\ \newline
This is an alternative way to write an integer value. The idea is similar to UTF-8
encoding and therefore this encoding is called ITF-8 (Integer Transformation Format
- 8 bit).
The most significant bits of the first byte have special meaning and are called
`prefix'. These are 0 to 4 true bits followed by a 0. The number of 1's denote
the number of bytes the follow. To accommodate 32 bits such representation requires
5 bytes with only 4 lower bits used in the last byte 5.
\item[LTF-8 long or (ltf8)]\ \newline
See ITF-8 for more details. The only difference between ITF-8 and LTF-8 is the
number of bytes used to encode a single value. To do so 64 bits are required and
this can be done with 9 byte at most with the first byte consisting of just 1s
or 0xFF value.
\item[{Array ([ ])}]\ \newline
Array length is written first as integer (itf8), followed by the elements of the
array.
\end{description}
\subsubsection*{Encoding}
Encoding is a data type that specifies how data series have been compressed. Encodings
are defined as encoding\texttt{<}type\texttt{>} where the type is a logical data
type as opposed to a storage data type.
An encoding is written as follows. The first integer (itf8) denotes the codec id
and the second integer (itf8) the number of bytes in the following encoding-specific
values.
Subexponential encoding example:
\begin{tabular}{|l|l|l|}
\hline
\textbf{Value} & \textbf{Type} & \textbf{Name}\tabularnewline
\hline
0x7 & itf8 & codec id\tabularnewline
\hline
0x2 & itf8 & number of bytes to follow\tabularnewline
\hline
0x0 & itf8 & offset\tabularnewline
\hline
0x1 & itf8 & K parameter\tabularnewline
\hline
\end{tabular}
The first byte ``0x7'' is the codec id.
The second 4 bytes ``0x0 0x0 0x0 0xD'' denote the length of the bytes to follow
(13).
The subexponential encoding has 3 parameters: integer (itf8) K, int (itf8) offset
and boolean (bool) unary bit:
K = 0x1 = 1
offset = 0x0 = 0
\subsubsection*{Map}
A map is a collection of keys and associated values. A map with N keys is written
as follows:
\begin{tabular}{|l|l|l|l|l|l|l|l|}
\hline
size in bytes & N & key 1 & value 1 & key ... & value ... & key N & value N\tabularnewline
\hline
\end{tabular}
Both the size in bytes and the number of keys are written as integer (itf8). Keys
and values are written according to their data types and are specific to each map.
\subsection{\textbf{Strings}}
Strings are represented as byte arrays using UTF-8 format. Read names, reference
sequence names and tag values with type `Z' are stored as UTF-8.
\section{\textbf{Encodings }}
Encoding is a data structure that captures information about compression details
of a data series that are required to uncompress it. This could be a set of constants
required to initialize a specific decompression algorithm or statistical properties
of a data series or, in case of data series being stored in an external block,
the block content id.
Encoding notation is defined as the keyword `encoding' followed by its data type
in angular brackets, for example `encoding\texttt{<}byte\texttt{>}' stands for
an encoding that operates on a data series of data type `byte'.
Encodings may have parameters of different data types, for example the external
encoding has only one parameter, integer id of the external block. The following
encodings are defined:
\begin{tabular}{|l|l|>{\raggedright}p{155pt}|>{\raggedright}p{160pt}|}
\hline
\textbf{Codec} & \textbf{ID} & \textbf{Parameters} & \textbf{Comment}\tabularnewline
\hline
NULL & 0 & none & series not preserved\tabularnewline
\hline
EXTERNAL & 1 & int block content id & the block content identifier used to associate
external data blocks with data series\tabularnewline
\hline
GOLOMB & 2 & int offset, int M & Golomb coding\tabularnewline
\hline
HUFFMAN\_INT & 3 & int array, int array & coding with int values\tabularnewline
\hline
BYTE\_ARRAY\_LEN & 4 & encoding\texttt{<}int\texttt{>} array length, encoding\texttt{<}byte\texttt{>}
bytes & coding of byte arrays with array length\tabularnewline
\hline
BYTE\_ARRAY\_STOP & 5 & byte stop, int external block\linebreak{}
content id & coding of byte arrays with a stop value \tabularnewline
\hline
BETA & 6 & int offset, int number of bits & binary coding\tabularnewline
\hline
SUBEXP & 7 & int offset, int K & subexponential coding\tabularnewline
\hline
GOLOMB\_RICE & 8 & int offset, int log2m & Golomb-Rice coding\tabularnewline
\hline
GAMMA & 9 & int offset & Elias gamma coding\tabularnewline
\hline
\end{tabular}
A more detailed description of all the above coding algorithms and their parameters
can be found in the \emph{Codings }section.
\section{\textbf{File structure}}
The overall CRAM file structure is described in this section. Please refer to other
sections of this document for more detailed information.
A CRAM file starts with a fixed length file definition followed by one or more
containers. The BAM header is stored in the first container.
%%\begin{figure}[htbp]
\includegraphics[width=356pt, height=31pt, keepaspectratio=true]{img/CRAMFileFormat2-1-fig001.png}
%%\caption{This should be the caption for \texttt{img/CRAMFileFormat2-1-fig001.png}.}
%%\end{figure}
Pic.1 CRAM file starts with a file definition followed by the BAM header and other
containers.
Containers consist of one or more blocks. By convention, the BAM header is stored
in the first container within a single block. This is known as the BAM header block.
%%\begin{figure}[htbp]
\includegraphics[width=354pt, height=103pt, keepaspectratio=true]{img/CRAMFileFormat2-1-fig002.png}
%%\caption{This should be the caption for \texttt{img/CRAMFileFormat2-1-fig002.png}.}
%%\end{figure}
Pic.2 The BAM header is stored in the first container.
Each container starts with a container header followed by one or more blocks. Each
block starts with a block header. All data in CRAM is stored within blocks after
the block header.
%%\begin{figure}[htbp]
\includegraphics[width=356pt, height=154pt, keepaspectratio=true]{img/CRAMFileFormat2-1-fig003.png}
%%\caption{This should be the caption for \texttt{img/CRAMFileFormat2-1-fig003.png}.}
%%\end{figure}
Pic.3 Container and block structure. All data in CRAM files is stored in blocks.
The first block in each container is the compression header block:
%%\begin{figure}[htbp]
\includegraphics[width=354pt, height=103pt, keepaspectratio=true]{img/CRAMFileFormat2-1-fig004.png}
%%\caption{This should be the caption for \texttt{img/CRAMFileFormat2-1-fig004.png}.}
%%\end{figure}
Pic.4 Compression header is the first block in the container.
The blocks after the compression header are organised logically into slices. One
slice may contain, for example, a contiguous region of alignment data. Slices begin
with a slice header block and are followed by one or more data blocks:
%%\begin{figure}[htbp]
\includegraphics[width=374pt, height=137pt, keepaspectratio=true]{img/CRAMFileFormat2-1-fig005.png}
%%\caption{This should be the caption for \texttt{img/CRAMFileFormat2-1-fig005.png}.}
%%\end{figure}
Pic.5 Containers are logically organised into slices.
Data blocks are divided into core and external data blocks. Each slice must have
at least one core data block immediately after the slice header block. The core
data block may be followed by one or more external data blocks.
%%\begin{figure}[htbp]
\includegraphics[width=392pt, height=149pt, keepaspectratio=true]{img/CRAMFileFormat2-1-fig006.png}
%%\caption{This should be the caption for \texttt{img/CRAMFileFormat2-1-fig006.png}.}
%%\end{figure}
Pic.5 Data blocks are divided into core and external data blocks.
\section{\textbf{File definition}}
Each CRAM file starts with a fixed length (26 bytes) definition with the following
fields:
\begin{tabular}{|l|l|l|}
\hline
\textbf{Data type} & \textbf{Name} & \textbf{Value}\tabularnewline
\hline
byte[4] & format magic number & CRAM (0x43 0x52 0x41 0x4d)\tabularnewline
\hline
unsigned byte & major format number & 2 (0x2)\tabularnewline
\hline
unsigned byte & minor format number & 0 (0x0)\tabularnewline
\hline
byte[20] & file id & CRAM file identifier (e.g. file name or SHA1 checksum)\tabularnewline
\hline
\end{tabular}
\section{\textbf{Container structure}}
The file definition is followed by one or more containers with the following header
structure where the container content is stored in the `blocks' field:
\begin{tabular}{|l|>{\raggedright}p{120pt}|>{\raggedright}p{260pt}|}
\hline
\textbf{Data type} & \textbf{Name} & \textbf{Value}
\tabularnewline
\hline
int32 & length & byte size of the container data (blocks)\tabularnewline
\hline
itf8 & reference sequence id & reference sequence identifier or\linebreak{}
-1 for unmapped reads\linebreak{}
-2 for multiple reference sequences\tabularnewline
\hline
itf8 & starting position on the reference & the alignment start position or\linebreak{}
0 for unmapped reads\tabularnewline
\hline
itf8 & alignment span & the length of the alignment or\linebreak{}
0 for unmapped reads\tabularnewline
\hline
itf8 & number of records & number of records in the container\tabularnewline
\hline
itf8 & record counter & 1-based sequential index of records in the file/stream.\tabularnewline
\hline
ltf8 & bases & number of read bases\tabularnewline
\hline
itf8 & number of blocks & the number of blocks\tabularnewline
\hline
itf8[ ] & landmarks & Each integer value of this array is a byte offset into the
blocks byte array. Landmarks are used for random access indexing.\tabularnewline
\hline
byte[ ] & blocks & The blocks contained within the container.\tabularnewline
\hline
\end{tabular}
\subsection{\textbf{CRAM header in the first container}}
The first container in the CRAM file contains the BAM header in an uncompressed
block. BAM header is terminated with \textbackslash{}0 byte and any extra bytes
in the block can be used to expand the BAM header. For example when updating @SQ
records additional space may be required for the BAM header. It is recommended
to reserve 50\% more space in the CRAM header block than it is required by the
BAM header.
\section{\textbf{Block structure}}
Containers consist of one or more blocks. Block compression is applied independently
and in addition to any encodings used to compress data within the block. The block
have the following header structure with the data stored in the `block data' field:
\begin{tabular}{|l|>{\raggedright}p{120pt}|>{\raggedright}p{260pt}|}
\hline
\textbf{Data type} & \textbf{Name} & \textbf{Value}
\tabularnewline
\hline
byte & method & the block compression method: \linebreak{}
0: raw (none)*\linebreak{}
1: gzip\linebreak{}
2: bzip2\tabularnewline
\hline
byte & block content type id & the block content type identifier\tabularnewline
\hline
itf8 & block content id & the block content identifier used to associate external
data blocks with data series\tabularnewline
\hline
itf8 & size in bytes* & size of the block data after applying block compression\tabularnewline
\hline
itf8 & raw size in bytes* & size of the block data before applying block compression\tabularnewline
\hline
byte[ ] & block data & the data stored in the block:\linebreak{}
$\bullet$ bit stream of CRAM records (core data block)\linebreak{}
$\bullet$ byte stream (external data block)\linebreak{}
$\bullet$ additional fields ( header blocks)\tabularnewline
\hline
\end{tabular}
* Note on raw method: both compressed and raw sizes must be set to the same value.
\subsection{\textbf{Block content types}}
CRAM has the following block content types:
\begin{tabular}{|>{\raggedright}p{143pt}|>{\raggedright}p{45pt}|>{\raggedright}p{116pt}|>{\raggedright}p{114pt}|}
\hline
\textbf{Block content type} & \textbf{Block content type id} & \textbf{Name} & \textbf{Contents}\tabularnewline
\hline
FILE\_HEADER & 0 & BAM header block & BAM header\tabularnewline
\hline
COMPRESSION\_HEADER & 1 & Compression header block & See specific section\tabularnewline
\hline
MAPPED\_SLICE\_HEADER & 2 & Slice header block & See specific section\tabularnewline
\hline
& 3 & & reserved\tabularnewline
\hline
EXTERNAL\_DATA & 4 & external data block & data produced by external encodings\tabularnewline
\hline
CORE\_DATA & 5 & core data block & bit stream of all encodings except for external\tabularnewline
\hline
\end{tabular}
\subsection{\textbf{Block content id}}
Block content id is used to distinguish between external blocks in the same slice.
Each external encoding has an id parameter which must be one of the external block
content ids. For external blocks the content id is a positive integer. For all
other blocks content id should be 0. Consequently, all external encodings must
not use content id less than 1.
\subsubsection*{Data blocks}
Data is stored in data blocks. There are two types of data blocks: core data blocks
and external data blocks.The difference between core and external data blocks is
that core data blocks consist of data series that are compressed using bit encodings
while the external data blocks are byte compressed. One core data block and any
number of external data blocks are associated with each slice.
Writing to and reading from core and external data blocks is organised through
CRAM records. Each data series is associated with an encoding. In case of external
encoding the block content id is used to identify the block where the data series
is stored. Please note that external blocks can have multiple data series associated
with them; in this case the values from these data series will be interleaved.
\subsection{\textbf{BAM header block}}
The BAM header is stored in a single block within the first container.
The following constraints apply to the BAM header:
\begin{itemize}
\item The SQ:MD5 checksum is required unless the reference sequence has been embedded
into the file.
\item At least one RG record is required.
\item The HD:SO sort order is always POS.
\end{itemize}
\subsection{\textbf{Compression header block}}
The compression header block consists of 3 parts: preservation map, data series
encoding map and tag encoding map.
\subsubsection*{Preservation map}
The preservation map contains information about which data was preserved in the
CRAM file. It is stored as a map with byte[2] keys:
\begin{tabular}{|l|l|>{\raggedright}p{100pt}|>{\raggedright}p{220pt}|}
\hline
\textbf{Key} & \textbf{Value data type} & \textbf{Name} & \textbf{Value}\tabularnewline
\hline
RN & bool & read names included & true if read names are preserved for all reads\tabularnewline
\hline
AP & bool & AP data series delta & true if AP data series is delta, false otherwise\tabularnewline
\hline
RR & bool & reference required & true if reference sequence is required to restore
the data completely\tabularnewline
\hline
SM & byte[5] & substitution matrix & substitution matrix\tabularnewline
\hline
TD & byte[ ] & tag ids dictionary & a list of lists of tag ids, see tag encoding
section\tabularnewline
\hline
\end{tabular}
\subsubsection*{Data series encodings}
Each data series has an encoding. These encoding are stored in a map with byte[2]
keys:
\begin{tabular}{|l|l|>{\raggedright}p{100pt}|>{\raggedright}p{220pt}|}
\hline
\textbf{Key} & \textbf{Value data type} & \textbf{Name} & \textbf{Value}\tabularnewline
\hline
BF & encoding\texttt{<}int\texttt{>} & bit flags & see separate section\tabularnewline
\hline
AP & encoding\texttt{<}int\texttt{>} & in-seq positions & 0-based alignment start
delta from previous record *\tabularnewline
\hline
FP & encoding\texttt{<}int\texttt{>} & in-read positions & positions of the read
features\tabularnewline
\hline
RL & encoding\texttt{<}int\texttt{>} & read lengths & read lengths\tabularnewline
\hline
DL & encoding\texttt{<}int\texttt{>} & deletion lengths & base-pair deletion lengths\tabularnewline
\hline
NF & encoding\texttt{<}int\texttt{>} & distance to next fragment & number of records
to the next fragment*\tabularnewline
\hline
BA & encoding\texttt{<}byte\texttt{>} & bases & bases\tabularnewline
\hline
QS & encoding\texttt{<}byte\texttt{>} & quality scores & quality scores\tabularnewline
\hline
FC & encoding\texttt{<}byte\texttt{>} & read features codes & see separate section\tabularnewline
\hline
FN & encoding\texttt{<}int\texttt{>} & number of read features & number of read
features in each record\tabularnewline
\hline
BS & encoding\texttt{<}byte\texttt{>} & base substitution codes & base substitution
codes\tabularnewline
\hline
IN & encoding\texttt{<}byte[ ]\texttt{>} & insertion & inserted bases\tabularnewline
\hline
RG & encoding\texttt{<}int\texttt{>} & read groups & read groups. Special value
`-1' stands for no group.\tabularnewline
\hline
MQ & encoding\texttt{<}int\texttt{>} & mapping qualities & mapping quality scores
\tabularnewline
\hline
TL & encoding\texttt{<}int\texttt{>} & tag ids & list of tag ids, see tag encoding
section\tabularnewline
\hline
RN & encoding\texttt{<}byte[ ]\texttt{>} & read names & read names\tabularnewline
\hline
NS & encoding\texttt{<}int\texttt{>} & next fragment reference sequence id & reference
sequence ids for the next fragment \tabularnewline
\hline
NP & encoding\texttt{<}int\texttt{>} & next mate alignment start & alignment positions
for the next fragment\tabularnewline
\hline
TS & encoding\texttt{<}int\texttt{>} & template size & template sizes\tabularnewline
\hline
MF & encoding\texttt{<}int\texttt{>} & next mate bit flags & see specific section\tabularnewline
\hline
CF & encoding\texttt{<}int\texttt{>} & compression bit flags & see specific section\tabularnewline
\hline
TM & encoding\texttt{<}int\texttt{>} & test mark & a prefix expected before every
record, for debugging purposes.\tabularnewline
\hline
RI & encoding\texttt{<}int\texttt{>} & reference id & record reference id from
the BAM file header\tabularnewline
\hline
RS & encoding\texttt{<}int\texttt{>} & reference skip length & number of skipped
bases for the `N' read feature\tabularnewline
\hline
PD & encoding\texttt{<}int\texttt{>} & padding & number of padded bases\tabularnewline
\hline
HC & encoding\texttt{<}int\texttt{>} & hard clip & number of hard clipped bases\tabularnewline
\hline
SC & encoding\texttt{<}byte[ ]\texttt{>} & soft clip & soft clipped bases\tabularnewline
\hline
\end{tabular}
* The data series is reset for each slice.
\subsubsection*{Encoding tags}
The TL (tag list) data series represents combined information about the number
of tags in a record and their ids.
Let $L_{i}=\{T_{i0}, T_{i1}, \ldots, T_{ix}\}$
be sorted list of all tag ids for a record $R_{i}$, where $i$ is the sequential
record index and $T_{ij}$ denotes $j$-th tag id in the record. We recommend
alphabetical sort order. The list of unique $L_{i}$ is assigned sequential
integer numbers starting with 0. These integer numbers represent the TL data series.
The sorted list of unique $L_{i}$ is stored as the TD value in the preservation
map. Using TD, an integer from the TL data series can be mapped back into a list
of tag ids.
The TD is written as byte array consisting of $L_{i}$ values separated
with \textbackslash{}0. Each $L_{i}$ value is written as a sequence
of 3 bytes: tag id followed by tag value type. For example AMiOQz\textbackslash{}0OQz\textbackslash{}0,
where the TD consists of just two values: integer 0 for tags \{AM:i,OQ:z\} and
1 for tag \{OQ:z\}.
\subsubsection*{Encoding tag values}
The encodings used for different tags are stored in a map. The map has integer
keys composed of the two letter tag abbreviation followed by the tag type as defined
in the SAM specification, for example `OQZ' for `OQ:Z'. The three bytes form a
big endian integer and are written as ITF8. For example, 3-byte representation
of OQ:Z is \{0x4F, 0x51, 0x5A\} and these bytes are intepreted as the integer 0x004F515A.
The integer is finally written as ITF8.
\begin{tabular}{|l|l|l|>{\raggedright}p{160pt}|}
\hline
\textbf{Key} & \textbf{Value data type} & \textbf{Name} & \textbf{Value}
\tabularnewline
\hline
TAG NAME 1:TAG TYPE 1 & encoding\texttt{<}byte[ ]\texttt{>} & read tag 1 & tag values
(names and types are available in the data series code)\tabularnewline
\hline
... & & ... & ...\tabularnewline
\hline
TAG NAME N:TAG TYPE N & encoding\texttt{<}byte[ ]\texttt{>} & read tag N & ...\tabularnewline
\hline
\end{tabular}
Note that tag values are encoded as array of bytes. The routines to convert tag
values into byte array and back are the same as in BAM with the exception of value
type being captured in the tag key rather in the value.
\subsection{\textbf{Slice header block}}
The slice header block is never compressed (block method=raw). For reference mapped
reads the slice header also defines the reference sequence context of the data
blocks associated with the slice. Mapped and unmapped reads can be stored within
the same slice similarly to BAM file. Slices with unsorted reads must not contain
any other types of reads.
The slice header block contains the following fields.
\begin{tabular}{|l|l|>{\raggedright}p{200pt}|}
\hline
\textbf{Data type} & \textbf{Name} & \textbf{Value}\tabularnewline
\hline
itf8 & reference sequence id & reference sequence identifier or -1 for unmapped
or unsorted reads\tabularnewline
\hline
itf8 & alignment start & the alignment start position or -1 for unmapped or unsorted
reads\tabularnewline
\hline
itf8 & alignment span & the length of the alignment or 0 for unmapped or unsorted
reads\tabularnewline
\hline
itf8 & number of records & the number of records in the slice\tabularnewline
\hline
ltf8 & record counter & 1-based sequential index of records in the file/stream\tabularnewline
\hline
itf8 & number of blocks & the number of blocks in the slice\tabularnewline
\hline
itf8[ ] & block content ids & block content ids of the blocks in the slice\tabularnewline
\hline
itf8 & embedded reference bases block content id & block content id for the embedded
reference sequence bases or -1 for none\tabularnewline
\hline
byte[16] & reference md5 & MD5 checksum of the reference bases within the slice
boundaries or 16 \textbackslash{}0 bytes for unmapped or unsorted reads\tabularnewline
\hline
\end{tabular}
\subsection{\textbf{Core data block}}
A core data block is a bit stream (most significant bit first) consisting of one
or more CRAM records. Please note that one byte could hold more then one CRAM record
as a minimal CRAM record could be just a few bits long. The core data block has
the following fields:
\begin{tabular}{|l|>{\raggedright}p{120pt}|>{\raggedright}p{260pt}|}
\hline
\textbf{Data type} & \textbf{Name} & \textbf{Value}
\tabularnewline
\hline
bit[ ] & CRAM record 1 & The first CRAM record\tabularnewline
\hline
... & ... & ...\tabularnewline
\hline
bit[ ] & CRAM record N & The Nth CRAM record \tabularnewline
\hline
\end{tabular}
\subsection{\textbf{External data block}}
Relationship between core data block and external data blocks is shown in the following
picture:
%%\begin{figure}[htbp]
\includegraphics[width=451pt, height=350pt, keepaspectratio=true]{img/CRAMFileFormat2-1-fig007.png}
%%\caption{This should be the caption for \texttt{img/CRAMFileFormat2-1-fig007.png}.}
%%\end{figure}
Pic.3 Relationship between core data block and external data blocks.
The picture shows how a CRAM record (on the left) is partially written to core
data block while the other fields are stored in two external data blocks. The specific
encodings are presented only for demonstration purposes, the main point here is
to distinguish between bit encodings whose output is always stored in core data
block and the external encoding which simply stored the bytes into external data
blocks.
\section{\textbf{End of file marker}}
A special container is used to mark the end of a file or stream. It is optional
in version preceding 2.1 but required in later versions. The idea is to provide
an easy and a quick way to detect that a CRAM file or stream is complete. The marker
is basically an empty container with ref seq id set to -1 (unaligned) and alignment
start set to 4542278.
Here is a complete content of the EOF container explained in detail:
\begin{tabular}{|l|l|>{\raggedright}p{150pt}|>{\raggedright}p{180pt}|}
\hline
\textbf{hex bytes} & \textbf{data type} & \textbf{decimal value} & \textbf{field
name}\tabularnewline
\hline
\multicolumn{4}{|l|}{\textit{Container header}}\tabularnewline
\hline
0b 00 00 00 & integer & 11 & size of blocks data\tabularnewline
\hline
ff ff ff ff ff & itf8 & -1 & ref seq id\tabularnewline
\hline
e0 45 4f 46 & itf8 & 4542278 & alignment start\tabularnewline
\hline
00 & itf8 & 0 & alignment span\tabularnewline
\hline
00 & itf8 & 0 & nof records\tabularnewline
\hline
00 & itf8 & 0 & global record counter\tabularnewline
\hline
00 & itf8 & 0 & bases\tabularnewline
\hline
01 & itf8 & 1 & block count\tabularnewline
\hline
00 & array & 0 & landmarks\tabularnewline
\hline
\multicolumn{4}{|l|}{\textit{Compression header block}}\tabularnewline
\hline
00 & byte & 0 (RAW) & compression method\tabularnewline
\hline
01 & byte & 1 (COMPRESSION\_HEADER) & block content type\tabularnewline
\hline
00 & itf8 & 0 & block content id\tabularnewline
\hline
06 & itf8 & 6 & compressed size\tabularnewline
\hline
06 & itf8 & 6 & uncompressed size\tabularnewline
\hline
\multicolumn{4}{|l|}{\textit{Compression header}}\tabularnewline
\hline
01 & itf8 & 1 & preservation map byte size\tabularnewline
\hline
00 & itf8 & 0 & preservation map size\tabularnewline
\hline
01 & itf8 & 1 & encoding map byte size\tabularnewline
\hline
00 & itf8 & 0 & encoding map size\tabularnewline
\hline
01 & itf8 & 1 & tag encoding byte size\tabularnewline
\hline
00 & itf8 & 0 & tag encoding map size\tabularnewline
\hline
\end{tabular}
When compiled together the EOF marker is exactly 30 bytes long and in hex representation
is:
0b 00 00 00 ff ff ff ff ff e0 45 4f 46 00 00 00 00 01 00 00 01 00 06 06 01 00
01 00 01 00
\section{\textbf{Record structure}}
CRAM record is based on the SAM record but has additional features allowing for
more efficient data storage. In contrast to BAM record CRAM record uses bits as
well as bytes for data storage. This way, for example, various coding techniques
which output variable length binary codes can be used directly in CRAM. On the
other hand, data series that do not require binary coding can be stored separately
in external blocks with some other compression applied to them independently.
\subsection{\textbf{CRAM record}}
Both mapped and unmapped reads start with the following fields. Please note that
the data series type refers to the logical data type and the data series name corresponds
to the data series encoding map.
\begin{tabular}{|>{\raggedright}p{36pt}|>{\raggedright}p{70pt}|>{\raggedright}p{75pt}|>{\raggedright}p{90pt}|>{\raggedright}p{171pt}|}
\hline
& \textbf{Data series type} & \textbf{Data series name} & \textbf{Field} & \textbf{Description}\tabularnewline
\hline
1 & int & BF & CRAM bit flags & see CRAM record bit flags\tabularnewline
\hline
2 & int & CF & compression bit flags & see compression bit flags\tabularnewline
\hline
3 & int & RI & ref id & reference sequence id, not used for single reference slices,
reserved for future multiref slices. \tabularnewline
\hline
4 & int & RL & read length & the length of the read\tabularnewline
\hline
5 & int & AP & alignment start & the alignment start position *1\tabularnewline
\hline
6 & int & RG & read group & the read group identifier\tabularnewline
\hline
7 & byte & QS & quality scores & quality scores are stored depending on the value
of the `mapped QS included' field\tabularnewline
\hline
8 & byte[ ] & RN & read name & the read names (if preserved)\tabularnewline
\hline
9 & *2 & *2 & mate record & *2 (if not the last record)\tabularnewline
\hline
10 & int & TL & tag ids & tag ids *3\tabularnewline
\hline
11 & byte[ ] & - & tag values & tag values *3\tabularnewline
\hline
\end{tabular}
*1 The AP data series is delta encoded for reads mapped to a single reference slice
and normal integer value in all other cases.
*2 See \emph{mate record} section.
*3 See\emph{ tag encoding} section.
The CRAM record structure for mapped reads has the following additional fields:
\begin{tabular}{|>{\raggedright}p{36pt}|>{\raggedright}p{70pt}|>{\raggedright}p{74pt}|>{\raggedright}p{85pt}|>{\raggedright}p{177pt}|}
\hline
& \textbf{Data series type} & \textbf{Data series name} & \textbf{Field} & \textbf{Description}\tabularnewline
\hline
1 & *1 & *1 & read feature records & *1\tabularnewline
\hline
2 & byte & MQ & mapping quality & read mapping quality\tabularnewline
\hline
\end{tabular}
*1 See read feature record specification below.
The CRAM record structure for unmapped reads has the following additional fields:
\begin{tabular}{|>{\raggedright}p{8pt}|>{\raggedright}p{88pt}|>{\raggedright}p{83pt}|>{\raggedright}p{85pt}|>{\raggedright}p{178pt}|}
\hline
& \textbf{Data series type} & \textbf{Data series name} & \textbf{Field} & \textbf{Description}\tabularnewline
\hline
1 & byte[read length] & BA & bases & the read bases\tabularnewline
\hline
\end{tabular}
\subsection{\textbf{Read bases}}
CRAM format supports ACGTN bases only. All non-ACGTN read bases must be replaced
with N (unknown) base. In case of mismatching non-ACGTN read base and non-ACGTN
reference base a ReadBase read feature should be used to capture the fact that
the read base should be restored as N base.
\subsection{\textbf{CRAM record bit flags (BF data series)}}
The following flags are defined for each CRAM read record:
\begin{tabular}{|>{\raggedright}p{144pt}|>{\raggedright}p{144pt}|>{\raggedright}p{144pt}|}
\hline
\textbf{Bit flag} & \textbf{Comment} & \textbf{Description}\tabularnewline
\hline
0x1 & ! 0x40 \&\& ! 0x80 & template having multiple segments in sequencing\tabularnewline
\hline
0x2 & & each segment properly aligned according to the aligner\tabularnewline
\hline
0x4 & & segment unmapped\tabularnewline
\hline
0x8 & calculated* or stored in the mate's info & next segment in the template unmapped\tabularnewline
\hline
0x10 & & SEQ being reverse complemented\tabularnewline
\hline
0x20 & calculated* or stored in the mate's info & SEQ of the next segment in the
template being reversed\tabularnewline
\hline
0x40 & & the first segment in the template\tabularnewline
\hline
0x80 & & the last segment in the template\tabularnewline
\hline
0x100 & & secondary alignment\tabularnewline
\hline
0x200 & & not passing quality controls\tabularnewline
\hline
0x400 & & PCR or optical duplicate\tabularnewline
\hline
\end{tabular}
* For segments within the same slice.
\subsection{\textbf{Read feature records}}
Read features are used to store read details that are expressed using read coordinates
(e.g. base differences respective to the reference sequence). The read feature
records start with the number of read features followed by the read features themselves:
\begin{tabular}{|>{\raggedright}p{36pt}|>{\raggedright}p{65pt}|>{\raggedright}p{84pt}|>{\raggedright}p{90pt}|>{\raggedright}p{168pt}|}
\hline
& \textbf{Data series type} & \textbf{Data series name} & \textbf{Field} & \textbf{Description}\tabularnewline
\hline
1 & int & FN & number of read features & the number of read features\tabularnewline
\hline
2 *1 & int & FP & in-read-position & position of the read feature\tabularnewline
\hline
3 *1 & byte & FC & read feature code & *2\tabularnewline
\hline
4 *1 & *2 & *2 & read feature data & *2\tabularnewline
\hline
\end{tabular}
*1 Repeated for each read feature.
*2 See \emph{read feature codes} below.
\subsubsection*{Read feature codes}
The following codes are used to distinguish variations in read coordinates:
\begin{tabular}{|>{\raggedright}p{91pt}|>{\raggedright}p{45pt}|>{\raggedright}p{72pt}|>{\raggedright}p{66pt}|>{\raggedright}p{132pt}|}
\hline
\textbf{Feature code} & \textbf{Id} & \textbf{Data series type} & \textbf{Data
series name} & \textbf{Description}\tabularnewline
\hline
Read base & B (0x42) & byte,byte & BA,QS & A base and associated quality score\tabularnewline
\hline
Substitution & X (0x58) & byte & BS & base substitution codes, SAM operators X,
M and =\tabularnewline
\hline
Insertion & I (0x49) & byte[ ] & IN & inserted bases, SAM operator I\tabularnewline