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.
Hello! Thanks everyone for help with a similar problem! I am now only struggling to keep the name of the region with the fragment (I had a bam file of fragments and chose the ones which overlap with the regions of interest (bed file with positions and names of regions) but I need the final file to remember which fragment belongs to which region name). Is it somehow possible?
Thank you! Lucie