

Whole Genome Sequencing (WGS) projects generate large amounts of data. While analysis costs are significant, storage costs often become the dominant expense over time. The challenge is to preserve raw data and alignments for potential re-analysis with new tools without paying for unlimited storage. This post looks at how the CRAM format helps: in our chr22 subset it reduced alignment storage by about 45% compared with BAM, while keeping lossless compression and the ability to re-align. On the newer version, nf-germline-short-read-variant-calling supports CRAM output for lower storage cost and easier re-analysis.
Clone the repository:
# clone and run the new workflow version with cram file output support, removing intermediate filesgit clone git@github.com:nttg8100/nf-germline-short-read-variant-calling.git -b 0.7.0cd nf-germline-short-read-variant-callingmake test-e2eThe new output folder is organized with the CRAM files placed in the alignment folder:
# the output folder├── alignment│ └── sample1│ ├── sample1_merged.cram│ └── sample1_merged.cram.craiFASTQ files contain raw sequencing reads with quality scores. They are the initial output from sequencers such as Illumina instruments.
Characteristics:
Test Data Example (chr22 subset):
sample1_R1.fastq.gz: 37 MBsample1_R2.fastq.gz: 37 MBTotal per sample: 74 MBAdvantages:
Disadvantages:
BAM files contain aligned reads with genomic coordinates. They are the standard output from aligners such as BWA-MEM2.
File Content:
Real-World BAM Example (SAM text format for readability):
HISEQ1:18:H8VC6ADXX:1:2203:4067:21293 163 chr22 11016745 8 148M = 11017040 443TTAATGTATTGCTTAAAGGTTTGCTAGCATTTTGTTGAGGATTTTTGCATCAATATTAATTGGAGATATTGTAGTTTTCTTTTTTTAAAGTGTCTTTGATTTTGGTATAAAGGTAATTCTGGTGTTATAGAATGAGTTTGGAAGTATTCC@FFFFFHHHHHJJJJJJCHIJJJJJJJIJJJJIJIFIJGHJJJJJJIJIJIIIJIJJIIIJBGFHIIJJGHIJJJJIJJJHHFDACDDCCEEDDDDCDDACCDACDDEEDDCDCDEDDDDDDDDDEEDCDDDCDDEDDDDDDCDDENM:i:0 MD:Z:148 MC:Z:148M AS:i:148 XS:i:143 XA:Z:chr22,-17016043,148M,1; RG:Z:sample1_1Breakdown of this read:
HISEQ1:18:H8VC6ADXX:1:2203:4067:21293 - unique identifier163 - paired-end, proper pair alignmentchr2211016745 - genomic coordinate on chr228 - mapping quality148M - all 148 bases matched referenceNM:i:0 (0 mismatches), MD:Z:148 (148 exact matches), etc.Key Point: The complete 148-base sequence is stored in full, even though it matches the reference perfectly (NM:i:0 = zero mismatches).
Characteristics:
.bai file) for random accessTest Data Example (chr22 subset):
After BWA-MEM2 alignment:
sample1_aligned.bam: 74 MB (unsorted, same size as FASTQ)After coordinate sorting:
sample1_sorted.bam: 51 MB (31% reduction from unsorted)Advantages:
Disadvantages:
CRAM is a GA4GH standard format that uses reference-based compression to reduce file size while maintaining lossless information.
File Content and Compression Strategy:
Unlike BAM files that store full read sequences, CRAM files use reference-based compression:
How CRAM Compresses the Same Read:
Using the same read from the BAM example (NM:i:0, all 148 bases match reference):
BAM storage:
Full sequence (148 bp):TTAATGTATTGCTTAAAGGTTTGCTAGCATTTTGTTGAGGATTTTTGCATCAATATTAATTGGAGATATTGTAGTTTTCTTTTTTTAAAGTGTCTTTGATTTTGGTATAAAGGTAATTCTGGTGTTATAGAATGAGTTTGGAAGTATT(165 characters = ~165 bytes)
Quality scores (148 bp):CC@FFFFFHHHHHJJJJJJCHIJJJJJJJIJJJJIJIFIJGHJJJJJJIJIJIIIJIJJIIIJBGFHIIJJGHIJJJJIJJJHHFDACDDCCEEDDDDCDDACCDACDDEEDDCDCDEDDDDDDDDDEEDCDDDCDDEDDDDDDCDDE(165 characters = ~165 bytes)
Total per read: ~330+ bytesCRAM storage (for the same read with no mismatches):
Chromosome: chr22Position: 11016745CIGAR: 148M (all matched)Stored information: - Reference position (4 bytes) - CIGAR string: 148M (2 bytes) - Difference array: (empty - all bases match) (0 bytes) - Quality scores: (can be compressed or stored at reduced precision) (~50 bytes)
Total per read: ~60 bytes (for exact match reads)Compression efficiency: ~82% reduction for this single read.
Why this works: If a 148-base read matches the reference perfectly, CRAM simply records:
Instead of storing all 148 bases again.
Characteristics:
.crai file) like BAMTest Data Example (chr22 subset):
sample1_merged.cram: 28 MB (45% reduction from BAM)sample1_merged.cram.crai: index fileStorage Reduction Comparison:
FASTQ: 74 MB → CRAM: 28 MB = 62% savings overallBAM: 51 MB → CRAM: 28 MB = 45% savings vs BAMAdvantages:
Considerations:
| Data Type | Format | Size | Relative Size | Use Case |
|---|---|---|---|---|
| Raw reads | FASTQ.GZ | 74 MB | 100% | Initial input |
| Aligned (unsorted) | BAM | 74 MB | 100% | Temporary |
| Aligned (sorted) | BAM | 51 MB | 69% | Processing |
| Archived alignment | CRAM | 28 MB | 38% | Long-term storage |
Key Insight: In this subset, CRAM gives the best balance between storage cost and functionality.
The nf-germline-short-read-variant-calling pipeline implements a dual-output strategy:
Alignment Process: ├── BWA-MEM2 alignment ├── Sorting (samtools sort) ├── Marking duplicates (picard) ├── Base quality recalibration (GATK) │ └── SAMTOOLS_MERGE produces: ├── BAM file → downstream variant calling (fast processing) └── CRAM file → archival storage (45% savings)This approach keeps both options available:
For WGS projects at 30X coverage:
Per-Sample Costs (30X coverage):
| Format | Size | Annual Storage Cost* |
|---|---|---|
| FASTQ | 100 GB | $400 |
| BAM | 50 GB | $200 |
| CRAM | 28 GB | $112 |
*Assuming AWS S3 Standard at ~$0.023/GB/month
100-Sample Cohort Annual Costs:
| Format | Total Size | Annual Cost | Cost per Sample |
|---|---|---|---|
| FASTQ | 10 TB | $40,000 | $400 |
| BAM | 5 TB | $20,000 | $200 |
| CRAM | 2.8 TB | $11,200 | $112 |
5-Year Project Costs:
These figures assume AWS S3 Standard pricing at the time of writing and should be treated as illustrative rather than a quotation.
The pipeline implements CRAM output in the SAMTOOLS_MERGE process:
process SAMTOOLS_MERGE { // Strategy: Output BOTH BAM (for processing) and CRAM (for storage) // This gives best of both worlds: // - BAM continues to downstream processing (fast, compatible) // - CRAM saved for long-term storage (45% space savings)
input: tuple val(meta), path(bams) path ref_fasta
output: tuple val(meta), path("*_merged.bam"), emit: bam tuple val(meta), path("*_merged.bam.bai"), emit: bai tuple val(meta), path("*_merged.cram"), emit: cram // ← CRAM output tuple val(meta), path("*_merged.cram.crai"), emit: crai // ← CRAM index}The samtools command for CRAM conversion:
# Convert sorted BAM to CRAM using reference genomesamtools view \ -@ 8 \ -T reference/GRCh38.fasta \ -C \ -o sample1_merged.cram \ sample1_merged.bam
# Index the CRAM file for random accesssamtools index sample1_merged.cramKey Parameters:
-T : Reference genome FASTA file (required for CRAM encoding)-C : Output CRAM format-@ : Number of threads for parallel processingTo view or process CRAM files, modern tools handle decompression automatically:
# View CRAM file (samtools handles decompression)samtools view sample1_merged.cram | head
# Extract specific regionsamtools view sample1_merged.cram chr22:1000000-2000000
# Run variant calling directly on CRAMgatk HaplotypeCaller -I sample1_merged.cram -R reference/GRCh38.fasta -O variants.vcf
# Deep Variant also supports CRAM inputdeepvariant/run_deepvariant.py \ --input_file sample1_merged.cram \ --reference_fasta reference/GRCh38.fastaOne advantage of CRAM: you can re-analyze with new tools without keeping BAM files around. Be aware that some tools may not support CRAM files natively.
To re-align reads (e.g., with a newer reference genome or aligner):
# Extract reads from CRAM (automatically decompresses)samtools view -h -b sample1_merged.cram | samtools bam2fq \ | bwa mem -t 8 reference/GRCh38_updated.fasta - \ | samtools sort -O BAM > sample1_new_alignment.bamThe CRAM file contains all original reads with quality scores, enabling complete re-alignment.
Many modern variant callers support CRAM input directly:
# Using GATK with CRAMgatk HaplotypeCaller \ -I sample1_merged.cram \ -R reference/GRCh38.fasta \ -O variants_new_caller.vcf
# Using DeepVariant with CRAMdeepvariant/run_deepvariant.py \ --input_file sample1_merged.cram \ --reference_fasta reference/GRCh38.fastaIf a tool does not support CRAM files, you can convert CRAM back to BAM:
# Convert CRAM to BAMsamtools view -h -b sample1_merged.cram > sample1_converted.bam
# Index the BAM filesamtools index sample1_converted.bamThis approach allows you to archive alignments as CRAM (saving 45% storage) and convert to BAM only when a specific tool requires it.
Tier 1 (Hot - Frequent Access):├── BAM files (variant calling in progress)└── VCF results (active analysis) Cost: AWS S3 Standard ($0.023/GB/month)
Tier 2 (Warm - Occasional Access):├── CRAM files (re-analysis ready)└── FASTQ files (raw data backup) Cost: AWS S3 Intelligent-Tiering (~$0.0125/GB/month average)
Tier 3 (Cold - Archive):├── Backup CRAM files└── Project documentation Cost: AWS Glacier Deep Archive ($0.00099/GB/month)Fully supported (CRAM input natively):
Requires conversion to BAM:
Conversion approach for unsupported tools:
# Quick conversion when neededsamtools view -h -b sample1_merged.cram > sample1_temp.bamsamtools index sample1_temp.bam
# Use converted BAM with legacy toolslegacy_tool --input sample1_temp.bam
# Delete temporary BAM after userm sample1_temp.bam sample1_temp.bam.baiThis way, long-term storage keeps only CRAM files, and conversion happens only when a specific tool requires it.
Using the pipeline on the HG002 (Genome in a Bottle) chr22 subset:
Compression Statistics:
Original BAM: 51 MBCRAM output: 28 MBCompression ratio: 45% reductionVariant Calling Quality (No Impact):
Project: 100-sample WGS cohort (30X coverage)
Storage Timeline:Year 1: FASTQ + BAM + CRAM (full raw + processed) = 16.8 TBYear 2-5: CRAM only (after analysis complete) = 2.8 TB
Total 5-year cost:├── FASTQ retention strategy: $200,000├── BAM retention strategy: $100,000└── CRAM-only strategy: $56,000 (72% savings)CRAM is a pragmatic option for genomic data storage challenges:
By implementing dual BAM/CRAM output, the pipeline keeps fast variant calling during active analysis and economical long-term archival. The savings figures depend on the tools, the data, and the storage service used, so they should be measured for each project.



