<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.bwhpc.de/wiki/index.php?action=history&amp;feed=atom&amp;title=BinAC2%2FData_Compression</id>
	<title>BinAC2/Data Compression - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.bwhpc.de/wiki/index.php?action=history&amp;feed=atom&amp;title=BinAC2%2FData_Compression"/>
	<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=BinAC2/Data_Compression&amp;action=history"/>
	<updated>2026-11-03T16:12:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>https://wiki.bwhpc.de/wiki/index.php?title=BinAC2/Data_Compression&amp;diff=16565&amp;oldid=prev</id>
		<title>F Bartusch: Created page with &quot;= Compressing data =  __TOC__  Uncompressed data wastes a lot of space on the workspace file system. Most tools read compressed files directly, so there is rarely a reason to keep files uncompressed. On BinAC 2, most uncompressed data is bioinformatics data, so most file types below come from that field, but the general rules apply to all text based formats.  General rules: * Write output compressed in the first place instead of compressing it afterwards. * Run conversio...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.bwhpc.de/wiki/index.php?title=BinAC2/Data_Compression&amp;diff=16565&amp;oldid=prev"/>
		<updated>2026-11-03T05:57:56Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Compressing data =  __TOC__  Uncompressed data wastes a lot of space on the workspace file system. Most tools read compressed files directly, so there is rarely a reason to keep files uncompressed. On BinAC 2, most uncompressed data is bioinformatics data, so most file types below come from that field, but the general rules apply to all text based formats.  General rules: * Write output compressed in the first place instead of compressing it afterwards. * Run conversio...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Compressing data =&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
Uncompressed data wastes a lot of space on the workspace file system. Most tools read compressed files directly, so there is rarely a reason to keep files uncompressed. On BinAC 2, most uncompressed data is bioinformatics data, so most file types below come from that field, but the general rules apply to all text based formats.&lt;br /&gt;
&lt;br /&gt;
General rules:&lt;br /&gt;
* Write output compressed in the first place instead of compressing it afterwards.&lt;br /&gt;
* Run conversions in a batch job, not on the login nodes.&lt;br /&gt;
* Delete the uncompressed original only after the check shown for each file type succeeded.&lt;br /&gt;
* Use the default compression level. Higher levels (&amp;lt;code&amp;gt;-9&amp;lt;/code&amp;gt;) take 2 to 7 times longer and save only 0.4 to 1.6 percentage points.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Format !! Use instead !! Tool !! Size after compression !! Tested tools reading it&lt;br /&gt;
|-&lt;br /&gt;
| SAM || sorted BAM || &amp;lt;code&amp;gt;samtools sort&amp;lt;/code&amp;gt; || about 27 % || samtools&lt;br /&gt;
|-&lt;br /&gt;
| FASTQ || &amp;lt;code&amp;gt;.fastq.gz&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;pigz&amp;lt;/code&amp;gt; || about 35 % || bowtie2, Bismark&lt;br /&gt;
|-&lt;br /&gt;
| FASTA || &amp;lt;code&amp;gt;.fa.gz&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;bgzip&amp;lt;/code&amp;gt; || about 25 % || samtools faidx, bowtie2-build&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== File types ==&lt;br /&gt;
&lt;br /&gt;
=== SAM ===&lt;br /&gt;
&lt;br /&gt;
SAM is the uncompressed text form of BAM. It contains exactly the same information. Do not keep SAM files.&lt;br /&gt;
&lt;br /&gt;
==== Expected results ====&lt;br /&gt;
&lt;br /&gt;
Measured on BinAC 2 with bowtie2 2.5.5 alignments of NA12878 (2x101 bp) to GRCh38, samtools 1.24 defaults:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Data set !! SAM !! unsorted BAM !! sorted BAM&lt;br /&gt;
|-&lt;br /&gt;
| 20 M read pairs || 13.33 GB || 4.29 GB (32 %) || 3.62 GB (27 %)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Sorted BAM compresses better than unsorted BAM.&lt;br /&gt;
&lt;br /&gt;
CRAM, measured on BinAC 2 with the sorted BAM of the 20 M read pairs (3.62 GB), samtools 1.24, 8 threads, node local disk:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Format !! Size !! of BAM !! Write time !! Read time&lt;br /&gt;
|-&lt;br /&gt;
| sorted BAM || 3.62 GB || 100 % || ||&lt;br /&gt;
|-&lt;br /&gt;
| CRAM (default) || 1.47 GB || 41 % || 37 s || 17 s&lt;br /&gt;
|-&lt;br /&gt;
| CRAM &amp;lt;code&amp;gt;small&amp;lt;/code&amp;gt; profile || 1.33 GB || 37 % || 55 s || 33 s&lt;br /&gt;
|-&lt;br /&gt;
| CRAM &amp;lt;code&amp;gt;archive&amp;lt;/code&amp;gt; profile || 1.28 GB || 35 % || 81 s || 31 s&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
samtools 1.24 writes CRAM version 3.1 by default. Relative to the original SAM, a default CRAM is about 11 % of the size.&lt;br /&gt;
&lt;br /&gt;
All alignment data was preserved: flagstat and all fields of all 40 M records were identical, with one exception allowed by the SAM specification. For 22 read pairs where both mates start at the same position, the sign of the template length (TLEN) was swapped between the mates.&lt;br /&gt;
&lt;br /&gt;
==== Compress ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load bio/samtools/1.24&lt;br /&gt;
samtools sort -@ 8 -m 1G -o sample.bam sample.sam&lt;br /&gt;
samtools quickcheck sample.bam \&lt;br /&gt;
  &amp;amp;&amp;amp; samtools flagstat -@ 8 sample.sam &amp;gt; sam.flagstat \&lt;br /&gt;
  &amp;amp;&amp;amp; samtools flagstat -@ 8 sample.bam &amp;gt; bam.flagstat \&lt;br /&gt;
  &amp;amp;&amp;amp; cmp sam.flagstat bam.flagstat \&lt;br /&gt;
  &amp;amp;&amp;amp; rm sample.sam&lt;br /&gt;
samtools index sample.bam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;-m 1G&amp;lt;/code&amp;gt; is per thread, so request at least 8 GB memory for &amp;lt;code&amp;gt;-@ 8&amp;lt;/code&amp;gt;. The SAM file is only deleted if the BAM is intact and contains the same reads.&lt;br /&gt;
&lt;br /&gt;
For new analyses, avoid SAM files entirely and pipe the aligner output into &amp;lt;code&amp;gt;samtools sort&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bowtie2 -p 8 -x GRCh38 -1 sample_R1.fastq.gz -2 sample_R2.fastq.gz \&lt;br /&gt;
  | samtools sort -@ 4 -m 1G -o sample.bam -&lt;br /&gt;
samtools index sample.bam&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Optional, for long term storage, CRAM (default profile; &amp;lt;code&amp;gt;small&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;archive&amp;lt;/code&amp;gt; save another 10 to 13 % at 1.5 to 2 times the write time):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
samtools view -@ 8 -C -T GRCh38.fa -o sample.cram sample.bam \&lt;br /&gt;
  &amp;amp;&amp;amp; samtools quickcheck sample.cram \&lt;br /&gt;
  &amp;amp;&amp;amp; cmp &amp;lt;(samtools flagstat sample.bam) &amp;lt;(samtools flagstat --input-fmt-option reference=GRCh38.fa sample.cram) \&lt;br /&gt;
  &amp;amp;&amp;amp; rm sample.bam&lt;br /&gt;
samtools index sample.cram&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* A CRAM file cannot be read without the &amp;#039;&amp;#039;&amp;#039;exact&amp;#039;&amp;#039;&amp;#039; reference FASTA used to create it. Keep the reference together with the CRAM files.&lt;br /&gt;
* Not all tools read CRAM. Check your downstream tools before converting.&lt;br /&gt;
&lt;br /&gt;
==== Decompress ====&lt;br /&gt;
&lt;br /&gt;
Rarely needed. To look into a BAM file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
samtools view -h sample.bam | less&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Tools reading the compressed format ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Tool !! BAM !! CRAM&lt;br /&gt;
|-&lt;br /&gt;
| samtools || yes || yes (needs reference)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Only tools tested on BinAC 2 are listed. More will be added.&lt;br /&gt;
&lt;br /&gt;
=== FASTQ ===&lt;br /&gt;
&lt;br /&gt;
==== Expected results ====&lt;br /&gt;
&lt;br /&gt;
Measured on BinAC 2 with 8 threads on node local disk. Test data: 20 M reads of NA12878 (ERR194147, HiSeq 2000, 2x101 bp), 5.4 GB uncompressed, one file.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Method !! Size after compression !! Compression time !! Decompression time&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pigz&amp;lt;/code&amp;gt; (default level) || 35.3 % || 83 s || 17 s&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bgzip&amp;lt;/code&amp;gt; (default level) || 35.6 % || 28 s || 2 s (8 threads), 7 s (1 thread)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bgzip -l 9&amp;lt;/code&amp;gt; || 34.0 % || 206 s || 2 s&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!-- TODO: after pigz is rebuilt with zlib-ng, update pigz row: 35.0 %, 25 s, 12 s --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This data has full resolution quality scores. Data from newer sequencers with binned quality scores usually compresses better.&lt;br /&gt;
&lt;br /&gt;
Both &amp;lt;code&amp;gt;pigz&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;bgzip&amp;lt;/code&amp;gt; give the same compression ratio. Use &amp;lt;code&amp;gt;pigz&amp;lt;/code&amp;gt;: it writes a plain gzip file that every tool reading &amp;lt;code&amp;gt;.fastq.gz&amp;lt;/code&amp;gt; understands. &amp;lt;code&amp;gt;bgzip&amp;lt;/code&amp;gt; files are also valid gzip files, so if you already have them, keep them.&lt;br /&gt;
&lt;br /&gt;
==== Compress ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load system/pigz/2.8&lt;br /&gt;
pigz -p 8 -k sample.fastq \&lt;br /&gt;
  &amp;amp;&amp;amp; pigz -t sample.fastq.gz \&lt;br /&gt;
  &amp;amp;&amp;amp; rm sample.fastq&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;-k&amp;lt;/code&amp;gt; keeps the original until the integrity check (&amp;lt;code&amp;gt;-t&amp;lt;/code&amp;gt;) succeeded. &amp;lt;code&amp;gt;pigz&amp;lt;/code&amp;gt; is a parallel &amp;lt;code&amp;gt;gzip&amp;lt;/code&amp;gt;; the output is a normal &amp;lt;code&amp;gt;.gz&amp;lt;/code&amp;gt; file that &amp;lt;code&amp;gt;gzip&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;zcat&amp;lt;/code&amp;gt; can read.&lt;br /&gt;
&lt;br /&gt;
Write trimmed or filtered reads compressed directly, e.g. by giving the output file a &amp;lt;code&amp;gt;.fastq.gz&amp;lt;/code&amp;gt; name if the tool supports it.&lt;br /&gt;
&lt;br /&gt;
==== Decompress ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
zcat sample.fastq.gz | head     # look into the file without decompressing it&lt;br /&gt;
pigz -d sample.fastq.gz         # decompress (rarely needed)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a tool does not read gzip, do not decompress the file. Use process substitution instead:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
some_tool --in &amp;lt;(zcat sample.fastq.gz)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This only works if the tool reads its input once from start to end.&lt;br /&gt;
&lt;br /&gt;
==== Tools reading the compressed format ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Tool !! Gzipped FASTQ !! Notes&lt;br /&gt;
|-&lt;br /&gt;
| bowtie2 || yes || native support&lt;br /&gt;
|-&lt;br /&gt;
| Bismark || yes ||&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Only tools tested on BinAC 2 are listed. More will be added.&lt;br /&gt;
&lt;br /&gt;
=== FASTA ===&lt;br /&gt;
&lt;br /&gt;
==== Expected results ====&lt;br /&gt;
&lt;br /&gt;
Measured on BinAC 2 with 8 threads on node local disk. Test data: GRCh38 no_alt analysis set, 3.1 GB uncompressed.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Method !! Size after compression !! Compression time !! Decompression time&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pigz&amp;lt;/code&amp;gt; (default level) || 27.8 % || 74 s || 7 s&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pigz -9&amp;lt;/code&amp;gt; || 26.6 % || 335 s || 8 s&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bgzip&amp;lt;/code&amp;gt; (default level) || 25.2 % || 67 s || 1 s&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bgzip -l 9&amp;lt;/code&amp;gt; || 24.8 % || 131 s || 1 s&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;bgzip&amp;lt;/code&amp;gt; gives the smallest files and supports indexing. Use it for all FASTA files.&lt;br /&gt;
&lt;br /&gt;
==== Compress ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;bgzip&amp;lt;/code&amp;gt; writes gzip compatible files in independent blocks. Every tool that reads &amp;lt;code&amp;gt;.gz&amp;lt;/code&amp;gt; also reads bgzip files. In addition, samtools can index them, so tools can read a region without decompressing the whole file. Plain gzip files cannot be indexed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[E::fai_build3_core] Cannot index files compressed with gzip, please use bgzip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load bio/samtools/1.24&lt;br /&gt;
bgzip -@ 8 -k genome.fa \&lt;br /&gt;
  &amp;amp;&amp;amp; bgzip -t genome.fa.gz \&lt;br /&gt;
  &amp;amp;&amp;amp; samtools faidx genome.fa.gz \&lt;br /&gt;
  &amp;amp;&amp;amp; rm genome.fa&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;samtools faidx&amp;lt;/code&amp;gt; creates &amp;lt;code&amp;gt;genome.fa.gz.fai&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;genome.fa.gz.gzi&amp;lt;/code&amp;gt;. Keep both next to the &amp;lt;code&amp;gt;.fa.gz&amp;lt;/code&amp;gt; file. The index can also be created later.&lt;br /&gt;
&lt;br /&gt;
To check whether an existing &amp;lt;code&amp;gt;.gz&amp;lt;/code&amp;gt; file is bgzip or plain gzip, and to convert it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
htsfile genome.fa.gz          # &amp;quot;BGZF-compressed&amp;quot; or &amp;quot;gzip-compressed&amp;quot;&lt;br /&gt;
zcat genome.fa.gz | bgzip -@ 8 &amp;gt; genome.bgz.fa.gz &amp;amp;&amp;amp; samtools faidx genome.bgz.fa.gz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Decompress ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
samtools faidx genome.fa.gz chr1:1000000-1000100   # extract a region&lt;br /&gt;
bgzip -d genome.fa.gz                               # decompress (rarely needed)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Tools reading the compressed format ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Tool !! Compressed FASTA !! Notes&lt;br /&gt;
|-&lt;br /&gt;
| samtools faidx || bgzip only || not plain gzip&lt;br /&gt;
|-&lt;br /&gt;
| bowtie2-build || yes || since bowtie2 2.3.3&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Only tools tested on BinAC 2 are listed. More will be added.&lt;/div&gt;</summary>
		<author><name>F Bartusch</name></author>
	</entry>
</feed>