Thanks for the reply,
So what you are saying file size of BAM files will differ before sorting and after ?
I can understand that BAM and SAM files will have different in size.
11G input.sam
1.5G mapped.bam
974M sorted.bam
Hi All,
I wanted to create a sorted BAM file from the SAM file.
So these are the steps I took
samtools view -@ 8 -bhS input.sam -o mapped.bam
samtools sort -@ 8 mapped.bam -o sorted.bam
Once i created the mapped.bam file and the sorted.bam file I looked and the file sizes of BAM (mapped.bam & sorted.bam ) and saw a discrepancy. As I assumed these file sizes to be of same size, but in fact they were not.
1.5G mapped.bam
974M sorted.bam
My question is:
1) what I am doing wrong here ?
2) Is there a way to check the contents in these two files are the same ? (I am assuming ideally the contents should be same as it was just sorting them in order)
Thank you very much.
1) what I am doing wrong here ?
nothing is wrong. BAM are gzipped compressed and the reads are sorted on coordinate , In consequence some very similar data are close together (the DNA sequence of the reads) and it's much more efficient for compression.
Thanks for the reply,
So what you are saying file size of BAM files will differ before sorting and after ?
I can understand that BAM and SAM files will have different in size.
11G input.sam
1.5G mapped.bam
974M sorted.bam
So what you are saying file size of BAM files will differ before sorting and after ?
YES sorting changes the size of the compressed data.
$ wget -q -O - "https://en.wikipedia.org" |fold -w 1 | gzip -c | wc -c
23105
$ wget -q -O - "https://en.wikipedia.org" |fold -w 1 | sort | gzip -c | wc -c
851
Log in to answer this question.
Size of BAM file reduces after sorting with samtools
Thanks Pierre Lindenbaum
Does it make any difference if I use Picard to BAM files ?
picard is slower and doesn't work the same way than samtools when sorting on queryname (!= coordinate)
The answer about sizes has already been given so I won't repeat it.
However in answer to part 2, we locally use Biobambam's bamseqchksum tool to validate that a file operation hasn't lost data in the process, or that it's lost only the bits we know will be lost. For example it can compute checksums of all the sequences and quality strings irrespective or order and hence validate they still exist and haven't been modified.
https://manpages.debian.org/unstable/biobambam2/bamseqchksum.1.en.html
That may look complex, but just do "bamseqchksum < input.bam" and you'll get some stats.