<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>

CC BY-SA 3.0
2018-01-07T16:51:43.500Z
thanks for the reply so what I want is I want to parse out the exon coordinates then  11868   12227   +   exon
chr1    11871   12227   +   exon
chr1    11873   12227   +   exon
chr1    12009   12057   +   exon
chr1    12178   12227   +   exon as i you can see 11868 to 12227 there are like 5 exons so i want only the first and the last exon to be taken and then i want to use it for divergent primer..so the way you suggested so i have to kind of define the exon i want to parse every time, is there a better way, because , i want to do the same for all the genes with their exons...
kcm
CC BY-SA 3.0
2018-01-07T16:56:49.087Z
You can use findOverlaps() with something and itself.
Devon Ryan
CC BY-SA 3.0
2018-01-07T16:58:32.777Z
okay will try your suggestion
kcm
CC BY-SA 3.0
2018-01-08T19:29:27.787Z
I get this error "  invalid class “GRanges” object: 'mcols(x)' is not parallel to 'x'
kcm
CC BY-SA 3.0
2018-01-08T19:38:11.020Z
That should probably be its own question, though my guess is that you have something the wrong length somewhere.
Devon Ryan
CC BY-SA 3.0
2018-01-07T15:21:07.027Z
2018-01-07T15:34:31.803Z
2018-01-07T15:34:31.803Z
Devon Ryan
<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>

CC BY-SA 3.0
2018-01-07T20:17:27.923Z
2018-01-07T20:22:49.107Z
2018-01-07T20:22:49.107Z
Alex Reynolds
<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>

CC BY-SA 3.0
2018-01-07T17:16:21.163Z
cross posted: https://www.biostars.org/p/292247/
Pierre
CC BY-SA 3.0
2018-01-07T17:17:39.250Z
yes i posted earlier in biostar so i didn't have any response so i posted here hoping to get some solution
kcm
<r><bed><bedtools><perl>
Parse out exon coordinates from bed file for each gene
# Parse out exon coordinates from bed file for each gene

(Asked by: username_0 on 2018-01-07)

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.

**Tags:** r, bed, bedtools, perl
> **Comments:**
> **username_1**: cross posted: https://www.biostars.org/p/292247/  
> **username_0**: yes i posted earlier in biostar so i didn't have any response so i posted here hoping to get some solution  

---

## 2 answers

### Answer by username_2 on 2018-01-07. Score: 5

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.

> **Comments:**
> **username_0**: thanks for the reply so what I want is I want to parse out the exon coordinates then  11868   12227   +   exon chr1    11871   12227   +   exon chr1    11873   12227   +   exon chr1    12009   12057   +   exon chr1    12178   12227   +   exon as i you can see 11868 to 12227 there are like 5 exons so i want only the first and the last exon to be taken and then i want to use it for divergent primer..so the way you suggested so i have to kind of define the exon i want to parse every time, is there a better way, because , i want to do the same for all the genes with their exons...  
> **username_2**: You can use findOverlaps() with something and itself.  
> **username_0**: okay will try your suggestion  
> **username_0**: I get this error "  invalid class “GRanges” object: 'mcols(x)' is not parallel to 'x'  
> **username_2**: That should probably be its own question, though my guess is that you have something the wrong length somewhere.  

---

### Answer by username_3 on 2018-01-07. Score: 3

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.


---

