<p>Hi all</p>

<p>This question could be considered as a follow up of this discussion.
<a href='http://www.biostars.org/post/show/3407/how-to-extract-reads-from-bam-that-overlap-with-specific-regions/#3414'>http://www.biostars.org/post/show/3407/how-to-extract-reads-from-bam-that-overlap-with-specific-regions/#3414</a>
What I need is to extract reads from <a href='http://samtools.sourceforge.net/SAM1.pdf'>bam</a> file that fall <strong>only within a given region</strong> (not overlap the given region), the region being in the form of a gff file or bed file. Overlapping reads could be extracted by several methods (as in the discussion mentioned or <a href='https://code.google.com/p/bedtools/'>BEDTools</a>). The idea is to try to be pretty sure of excluding 5´ UTRs in the process of detecting intergenic transcripts. I saw a tool in BamUtil (<a href='http://genome.sph.umich.edu/wiki/BamUtil'>http://genome.sph.umich.edu/wiki/BamUtil</a>) called "writeRegion" which would pretty much do what I want. Somehow could not get this running for my dataset. 
Was wondering if you guys might have an "R" or some other solution for this.
Thanks in advance
Abi</p>

<p>You can extract mappings of a <a href='http://samtools.sourceforge.net/SAM1.pdf'>sam</a>/bam file by reference and region with <a href='http://samtools.sourceforge.net/'>samtools</a>. For example:</p>

<pre><code>samtools view input.bam "Chr10:18000-45500" &gt; output.bam
</code></pre>

<p>That would output all reads in Chr10 between 18000-45500 bp.</p>
