How to merge sam files on Linux?

The samtools merge can merge bam files while it can not work for sam files.

How to merge sam files on Linux?

According to the sam format specification, header lines start with @, while alignment lines do not. So you can use grep to merge sam files as follows efficiently.

Assume the header is from 0.sam, the files to be merged are 0.sam, 1.sam and 2.sam, and the merged output sam file is out.sam.

files="0.sam 1.sam 2.sam"

(grep ^@ $header; for f in $files; do grep -v ^@ $f; done) > $output

It should be much faster than the way of “convert sams to bams, merge bams by samtools merge, convert bam to sam”.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *