Commit e5e7d4c
CRAM 3.1 write support: full codecs, profiles, optimisation, and tests
Builds out CRAM 3.1 write support on top of cnorman's initial naive
profile, taking htsjdk to feature parity with samtools/htslib for
writing CRAM 3.1 and matching samtools on cross-implementation fidelity
across a broad range of real-world data.
CRAM 3.1 codecs and supporting infrastructure
---------------------------------------------
- Full CRAM 3.1 codec set: rANS Nx16 (Order 0/1, RLE, Stripe, Pack,
Cat), FQZComp (quality scores), Range adaptive arithmetic coder, and
the Name Tokeniser.
- New compression profile system (FAST, NORMAL, SMALL, ARCHIVE),
matching htslib's `--output-fmt-option fast/small/archive`. Each
profile picks per-DataSeries compressors and (for SMALL/ARCHIVE)
trial-compression candidate sets.
- TrialCompressor: a wrapper that tries multiple compressors per block
and keeps the smallest output. Replaces the ad-hoc tag triple-
compression path with a uniform, profile-driven mechanism.
- DataSeries content IDs aligned with htslib so CRAM dumps from the
two implementations are directly comparable.
Codec performance work
----------------------
- rANS codecs reworked to use a `byte[]` API and backwards-write,
removing per-byte stream overhead and several layers of indirection.
- GzipCodec uses Deflater/Inflater directly instead of going through
GZIPInputStream/OutputStream, avoiding gzip framing overhead per
block.
- Name Tokeniser encoder: hand-written tokenisation replaces regex,
per-type flags + STRIPE + duplicate-stream elimination + all-MATCH
fast path significantly improve both speed and ratio.
- FQZComp / Range coder / rANS encoder hot paths tightened.
- Pooled RANSNx16Decode instance in the Name Tokeniser to avoid
reallocating per call.
- Replaced ByteArrayInputStream/OutputStream with unsynchronized
CRAMByteReader/Writer to remove monitor overhead in tight loops.
- Cached SAMTag key metadata to eliminate per-record String allocation
during CRAM decode.
- Fused read-base restoration, CIGAR building, and NM/MD computation
into a single pass instead of three.
- jlibdeflate compatibility fix on the ByteBuffer path.
- Roughly 15% faster encoding overall vs the pre-optimisation state,
with read decoding gains in line.
Correctness fixes found during cross-implementation validation
--------------------------------------------------------------
- TLEN is now computed using htslib's coordinate-extent rule
(max(end1,end2) - min(start1,start2) + 1, signed by leftmost
position) for CRAM-specific compute, rather than the SAM 5'-to-5'
rule. Without this, every read with a supplementary alignment
mismatched on TLEN through any CRAM round-trip.
- CompressionHeader serialisation now uses a growable
ByteArrayOutputStream rather than a fixed 100 KB ByteBuffer for the
preservation map and tag encoding map. The TD tag dictionary alone
can exceed 100 KB for rich tag sets (PacBio/Ultima flow-space, ONT
modified-base tags) under the archive profile, where it would
previously throw BufferOverflowException.
- Slice flush now uses a dual record/bases threshold matching htslib's
`seqs_per_slice` AND `bases_per_slice` (default = readsPerSlice *
500). Without this, archive-profile encoding of long-read data
(PacBio HiFi 15 kb, ONT 30 kb+) would buffer ~1+ GB of quality
scores per slice and OOM the FQZComp encoder.
- Strip NM/MD on encode and regenerate them on decode (matching
htslib's default `store_nm=0`/`store_md=0` behaviour). Implemented
attached mate pairs to align the in-memory representation with the
spec.
- Restored CIGAR reconstruction when SEQ is `*` (CF_UNKNOWN_BASES).
- Fixed crash when reading containers that contain no slices.
- Removed unnecessary content digest tags from CRAM slice headers
(htslib doesn't write them either; htsjdk's verification on read
was overly strict).
Tools
-----
- `CramConverter`: a small command-line tool for converting between
SAM/BAM/CRAM, primarily for benchmarking and exercising profiles.
- `CramStats`: dump per-block compression statistics from a CRAM file
for debugging and ratio analysis.
Tests and CI
------------
- New hts-specs CRAM 3.0/3.1 compliance tests covering decode, index
query, and round-trip behaviour.
- FQZComp round-trip tests using the hts-specs quality data files.
- CRAI index query correctness tests; codec roundtrip property tests.
- Split CRAM31 fidelity tests into per-profile classes for parallel
execution.
- Reduced memory pressure in the unit tests to eliminate intermittent
OOM failures on CI.
- Sped up several long-running CRAM tests by caching test data,
reducing slice-size matrices, and downsampling fixtures.
- Misc: fixed a thread-safety bug in VariantContextTestProvider that
was producing non-deterministic test counts.
CHANGELOG / README updated for the 5.0.0 release.
Co-authored-by: Chris Norman <cnorman@broadinstitute.org>1 parent a9a5825 commit e5e7d4c
202 files changed
Lines changed: 12230 additions & 4328 deletions
File tree
- src
- main/java/htsjdk/samtools
- cram
- build
- compression
- fqzcomp
- nametokenisation
- tokens
- range
- rans
- rans4x8
- ransnx16
- encoding
- external
- reader
- io
- structure
- util
- zip
- test
- java/htsjdk
- beta/codecs/reads/cram
- samtools
- cram
- build
- compression
- range
- rans
- cram31
- encoding
- external
- io
- spec30
- structure
- reference
- seekablestream
- util
- variant
- bcf2
- variantcontext
- resources/htsjdk/samtools/cram
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 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 | + | |
13 | 55 | | |
14 | 56 | | |
15 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | | - | |
| 102 | + | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| |||
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
216 | 222 | | |
217 | 223 | | |
218 | 224 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
Lines changed: 9 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | | - | |
| 74 | + | |
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
| |||
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
| 92 | + | |
91 | 93 | | |
92 | 94 | | |
93 | 95 | | |
| |||
103 | 105 | | |
104 | 106 | | |
105 | 107 | | |
106 | | - | |
| 108 | + | |
| 109 | + | |
107 | 110 | | |
108 | | - | |
109 | 111 | | |
110 | | - | |
| 112 | + | |
111 | 113 | | |
112 | 114 | | |
113 | 115 | | |
| |||
131 | 133 | | |
132 | 134 | | |
133 | 135 | | |
134 | | - | |
| 136 | + | |
135 | 137 | | |
136 | 138 | | |
137 | 139 | | |
| |||
144 | 146 | | |
145 | 147 | | |
146 | 148 | | |
147 | | - | |
| 149 | + | |
148 | 150 | | |
149 | 151 | | |
150 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
374 | 374 | | |
375 | 375 | | |
376 | 376 | | |
377 | | - | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
378 | 382 | | |
379 | 383 | | |
380 | 384 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
119 | 123 | | |
120 | 124 | | |
121 | 125 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
| 80 | + | |
79 | 81 | | |
80 | 82 | | |
81 | 83 | | |
| |||
241 | 243 | | |
242 | 244 | | |
243 | 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 | + | |
244 | 270 | | |
245 | 271 | | |
246 | 272 | | |
| |||
524 | 550 | | |
525 | 551 | | |
526 | 552 | | |
527 | | - | |
528 | | - | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
529 | 561 | | |
530 | 562 | | |
531 | 563 | | |
| |||
670 | 702 | | |
671 | 703 | | |
672 | 704 | | |
| 705 | + | |
673 | 706 | | |
674 | 707 | | |
675 | 708 | | |
676 | 709 | | |
677 | 710 | | |
678 | 711 | | |
679 | | - | |
680 | 712 | | |
681 | 713 | | |
682 | 714 | | |
683 | 715 | | |
684 | | - | |
685 | | - | |
686 | | - | |
687 | | - | |
688 | | - | |
689 | | - | |
690 | 716 | | |
691 | 717 | | |
692 | 718 | | |
| |||
0 commit comments