I have a large BAM file and I want to filter it by just what's defined in a bed file, and write it to a new file.

Is it possible to do this with samtools, and if so, how? Is there a better tool I could use to do this?

  • 1
    $\begingroup$ Did you look at the options for samtools view? $\endgroup$
    – swbarnes2
    Feb 20 '20 at 19:22
  • $\begingroup$ ´samtools view` has filtering options for BED files as reference. Type samtools view and check available flags. $\endgroup$
    – ATpoint
    Feb 20 '20 at 20:49
  • $\begingroup$ Hi, i.think your question needs more specific details $\endgroup$
    – M__
    Feb 20 '20 at 22:08

Thanks to the helpful comments I figured it out:

samtools view -b -h -L bedfile.bed originalbam.bam > newbam.bam

-b = Output as bed file
-h = include header
-L = only output alignments overlapping the input bed file


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.