text: user7259: 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?
  
  user7259: 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     
  
original_text: 7259: <p>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.</p>
  
  <p>Is it possible to do this with samtools, and if so, how? Is there a better tool I could use to do this?</p>
   7259: <p>Thanks to the helpful comments I figured it out:</p>
  
  <pre><code>samtools view -b -h -L bedfile.bed originalbam.bam &gt; newbam.bam
  </code></pre>
  
  <p>-b = Output as bed file<br>
  -h = include header<br>
  -L = only output alignments overlapping the input bed file     </p>
  