<p>Bedtools <code>intersect</code> will do this:</p>
<blockquote>
<p>By far, the most common question asked of two sets of genomic features is whether or not any of the features in the two sets “overlap” with one another. This is known as feature intersection. bedtools intersect allows one to screen for overlaps between two sets of genomic features. Moreover, it allows one to have fine control as to how the intersections are reported. bedtools intersect works with both BED/GFF/VCF and BAM files as input.</p>
</blockquote>
<p><a href="https://bedtools.readthedocs.io/en/latest/content/tools/intersect.html" rel="nofollow noreferrer">https://bedtools.readthedocs.io/en/latest/content/tools/intersect.html</a></p>

CC BY-SA 4.0
2022-03-03T22:59:15.857Z
2022-03-03T22:59:15.857Z
gringer
<p>You could use <a href="https://github.com/bedops/bedops" rel="noreferrer">BEDOPS</a>:</p>
<pre><code>$ gtf2bed &lt; annotations.gtf | grep -wF exon &gt; exons.bed
$ sort-bed myRegions.unsorted.bed &gt; myRegions.bed
$ bedops --element-of 100% exons.bed myRegions.bed &gt; answer.bed
</code></pre>
<p>Or as a one-liner, which is even faster:</p>
<pre><code>$ bedops --element-of 100% &lt;(gtf2bed &lt; annotations.gtf | grep -wF exon) &lt;(sort-bed myRegions.unsorted.bed) &gt; answer.bed
</code></pre>
<p>The <code>--element-of 100%</code> argument requires that the exon fall entirely within a region in the <code>myRegions</code> set. This is the most stringent requirement. The most relaxed requirement would be one base of overlap: <code>--element-of 1</code>. You can adjust to either extreme, or to values in between, depending on your criteria.</p>

CC BY-SA 4.0
2022-03-03T21:01:59.293Z
Thanks Alex !! I just additionally used `awk '{ if ($0 ~ "transcript_id") print $0; else print $0" transcript_id \"\";"; }' annotations.gtf` as it was giving error `Error: Potentially missing gene or transcript ID from GTF attributes (malformed GTF at line [1]?)`
SBDK8219
CC BY-SA 4.0
2022-03-03T20:36:30.463Z
2022-03-03T20:36:30.463Z
Alex Reynolds
<p>I have a bed file that contains a list of regions and I would like to extract only the exon regions from a GTF/GFF file that are contained in the given regions.</p>
<p>Is there a simple command or tool that I can use for that?</p>

<bed><gtf><exon><gff>
Extract only exon regions from GFF/GTF file with input bed regions
# Extract only exon regions from GFF/GTF file with input bed regions

(Asked by: username_0 on 2022-03-03)

I have a bed file that contains a list of regions and I would like to extract only the exon regions from a GTF/GFF file that are contained in the given regions.

Is there a simple command or tool that I can use for that?

**Tags:** bed, gtf, exon, gff

---

## 2 answers

### Answer by username_2 on 2022-03-03. Score: 5 | Accepted answer

You could use BEDOPS:
$ gtf2bed < annotations.gtf | grep -wF exon > exons.bed
$ sort-bed myRegions.unsorted.bed > myRegions.bed
$ bedops --element-of 100% exons.bed myRegions.bed > answer.bed

Or as a one-liner, which is even faster:
$ bedops --element-of 100% <(gtf2bed < annotations.gtf | grep -wF exon) <(sort-bed myRegions.unsorted.bed) > answer.bed

The --element-of 100% argument requires that the exon fall entirely within a region in the myRegions set. This is the most stringent requirement. The most relaxed requirement would be one base of overlap: --element-of 1. You can adjust to either extreme, or to values in between, depending on your criteria.

> **Comments:**
> **username_0**: Thanks Alex !! I just additionally used `awk '{ if ($0 ~ "transcript_id") print $0; else print $0" transcript_id \"\";"; }' annotations.gtf` as it was giving error `Error: Potentially missing gene or transcript ID from GTF attributes (malformed GTF at line [1]?)`  

---

### Answer by username_1 on 2022-03-03. Score: 2

Bedtools `intersect` will do this:

> By far, the most common question asked of two sets of genomic features is whether or not any of the features in the two sets “overlap” with one another. This is known as feature intersection. bedtools intersect allows one to screen for overlaps between two sets of genomic features. Moreover, it allows one to have fine control as to how the intersections are reported. bedtools intersect works with both BED/GFF/VCF and BAM files as input.

<https://bedtools.readthedocs.io/en/latest/content/tools/intersect.html>


---

