<p>Hi all,
Hope someone can help.</p>

<p>We can pass a region to <a href='http://samtools.sourceforge.net/'>samtools</a> view such as 1:1000-1010.</p>

<p>Is it possible to pass a list of regions to <a href='http://samtools.sourceforge.net/'>samtools</a> view and have the output into a single <a href='http://samtools.sourceforge.net/SAM1.pdf'>sam</a> file i.e. (something like)</p>

<p>$ <a href='http://samtools.sourceforge.net/'>samtools</a> view -h .bam regionlist.txt &gt; mysam.sam</p>

<p>what would the syntax for this be?</p>

<p>also, in what format does the region list file should be in?</p>

<p>thanks,
a</p>

Samtools have the `-L` option

    samtools view -b -L ROI.bed  file.bam > ROI_file.bam

BUT `-L` does not use the samtools index so the search is slooow depending on how large is your bam file. In my benchmarks querying for 10, 100 or 1000 sequences take exactly the same time so seem that bed size does not matter too much. You need to do a bit of benchmarking if you prefer to do a `-L` or to do a loop (for a bam with 23 million reads take the same time the `-L` that writting all the stuff for looping ;-)) YMMV

    $ time samtools view -b -L 100_ROI.bed file.bam > ROI.bam
    real    0m38.831s
    user    0m37.666s
    sys    0m0.556s
    
    
    $ time (samtools view -H file.bam > roi_xargs.sam; \
          cat 100_ROI.bed | perl -lane 'print "$F[0]:$F[1]-$F[2]"' | xargs -n1 -t -I{} samtools view file.bam {} >> roi_xargs.sam; \
          samtools view -bSh roi_xargs.sam > roi_xargs.bam \
      )
    real   0m7.188s
    user   0m5.080s
    sys    0m1.304s

And if you feel adventurous and your disks are using isilon or lustre you can use gnu-parallel and forget about same file concurrency and do it in parallel in a breeze. The only issue is that you would need to do the bam merge afterwards.