I've got a few thousand small bam files produced against the exact same reference, and I want to merge them into one single big bam file. What is the best way to do that?

Should I do this iteratively or can I pass a long list of bam files to samtools/picard/etc in one go?

Edited, since this is now partially solved. In my terminal, the methods below works for up to 4092 files. More than that raises an error:

    samtools merge all.bam *.bam
    samtools merge all.bam `find /basedir/ -name "*myfiles*.bam"`
    samtools merge all.bam /basedir/*/???/*myfiles*.bam
<p>Bamtools handles this very cleanly.  Note that this properly constructs the header, which might matter if you have different read groups in each file.</p>

<pre><code>bamtools merge -list files.bamlist -out merged.bam
</code></pre>

<p>You can also supply a region via the <code>-region</code> flag, using the format <code>12:2232..3328</code>.</p>
