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?

asked Nov 15, 2015 by anonymous

1 Answer

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.

header=0.sam
files="0.sam 1.sam 2.sam"
output=out.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".

answered Nov 15, 2015 by Eric Z Ma (44,280 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...