text: user285: This is my bed file for all the exon coordinates I want to take out all the exons of a given gene. Let say I have gene in chr 1 which starts from chr1 11868   12227 so I want to parse out all the exons that comes in between 11868    12227.
  This is my small subset:
  cat exon.bed | head -10
  chr1    11868   12227   +   exon
  chr1    11871   12227   +   exon
  chr1    11873   12227   +   exon
  chr1    12009   12057   +   exon
  chr1    12178   12227   +   exon
  chr1    12594   12721   +   exon
  chr1    12612   12697   +   exon
  chr1    12612   12721   +   exon
  chr1    12612   12721   +   exon
  chr1    12974   13052   +   exon
  
  How do I parse out?
  I use mostly R and bit of shell script but I'm not sure if I can use R, may a few lines of perl or shell script can help me solve my problem.
  
  user776: Via BEDOPS bedops -n and Unix I/O streams:
  $ echo -e "chr1\t11868\t12227" | bedops -n 1 exon.bed - > answer.bed
  
  Or, if you have your genes in a BED file called genes.bed:
  $ bedops -n 1 exon.bed genes.bed > answer.bed
  
  If you have your genes in some other format, like GFF or GTF, you can use gff2bed or gtf2bed, e.g.:
  $ bedops -n 1 exon.bed <(gff2bed < genes.gff) > answer.bed
  
  Or:
  $ bedops -n 1 exon.bed <(gtf2bed < genes.gtf) > answer.bed
  
  The file answer.bed will contain exons that do not overlap a gene annotation.
  
  user77: There are multiple ways to go about it. On the command line you can make a 1 line BED file:
  chr1   11868   12227
  
  And then bedtools intersect with it.
  In R, you could load your original BED file and use GenomicRanges:
  library("GenomicRanges")
  bed = read.delim("foo.bed", header=F)  # Rename this
  # N.B., BED files use 0-based coordinates, I've switched to 1-based here
  exons = GRanges(seqnames=levels(bed$V1),
                  ranges=IRanges(start=bed$V2+1, end=bed$V3),
                  strand=bed$V4)
  # Again, I switched to 1-based coordinates...you don't have to do that
  region = GRanges(seqnames="chr1",
                   ranges=IRanges(start=11869, end=12227),
                   strand="+")
  theExonsYouWant = subsetByOverlaps(exons, region, type="within")
  
  You'll need to either specify a strand in your region or use ignore.strand=T in subsetByOverlaps(). I hope you don't have any genes that overlap on the same strand, since they'll prove problematic.
  
original_text: 285: <p>This is my bed file for all the exon coordinates I want to take out all the exons of a given gene. Let say I have gene in chr 1 which starts from <code>chr1 11868   12227</code> so I want to parse out all the exons that comes in between <code>11868    12227</code>.</p>
  
  <p>This is my small subset:</p>
  
  <pre><code>cat exon.bed | head -10
  chr1    11868   12227   +   exon
  chr1    11871   12227   +   exon
  chr1    11873   12227   +   exon
  chr1    12009   12057   +   exon
  chr1    12178   12227   +   exon
  chr1    12594   12721   +   exon
  chr1    12612   12697   +   exon
  chr1    12612   12721   +   exon
  chr1    12612   12721   +   exon
  chr1    12974   13052   +   exon
  </code></pre>
  
  <p>How do I parse out?</p>
  
  <p>I use mostly R and bit of shell script but I'm not sure if I can use R, may a few lines of perl or shell script can help me solve my problem.</p>
   776: <p>Via BEDOPS <code>bedops -n</code> and Unix I/O streams:</p>
  
  <pre><code>$ echo -e "chr1\t11868\t12227" | bedops -n 1 exon.bed - &gt; answer.bed
  </code></pre>
  
  <p>Or, if you have your genes in a BED file called <code>genes.bed</code>:</p>
  
  <pre><code>$ bedops -n 1 exon.bed genes.bed &gt; answer.bed
  </code></pre>
  
  <p>If you have your genes in some other format, like GFF or GTF, you can use <code>gff2bed</code> or <code>gtf2bed</code>, e.g.:</p>
  
  <pre><code>$ bedops -n 1 exon.bed &lt;(gff2bed &lt; genes.gff) &gt; answer.bed
  </code></pre>
  
  <p>Or:</p>
  
  <pre><code>$ bedops -n 1 exon.bed &lt;(gtf2bed &lt; genes.gtf) &gt; answer.bed
  </code></pre>
  
  <p>The file <code>answer.bed</code> will contain exons that do not overlap a gene annotation.</p>
   77: <p>There are multiple ways to go about it. On the command line you can make a 1 line BED file:</p>
  
  <pre><code>chr1   11868   12227
  </code></pre>
  
  <p>And then <code>bedtools intersect</code> with it.</p>
  
  <p>In R, you could load your original BED file and use GenomicRanges:</p>
  
  <pre class="lang-r prettyprint-override"><code>library("GenomicRanges")
  bed = read.delim("foo.bed", header=F)  # Rename this
  # N.B., BED files use 0-based coordinates, I've switched to 1-based here
  exons = GRanges(seqnames=levels(bed$V1),
                  ranges=IRanges(start=bed$V2+1, end=bed$V3),
                  strand=bed$V4)
  # Again, I switched to 1-based coordinates...you don't have to do that
  region = GRanges(seqnames="chr1",
                   ranges=IRanges(start=11869, end=12227),
                   strand="+")
  theExonsYouWant = subsetByOverlaps(exons, region, type="within")
  </code></pre>
  
  <p>You'll need to either specify a strand in your region or use <code>ignore.strand=T</code> in <code>subsetByOverlaps()</code>. I hope you don't have any genes that overlap on the same strand, since they'll prove problematic.</p>
  