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?


2 Answers 2


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.

  • 1
    $\begingroup$ 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]?) $\endgroup$
    – SBDK8219
    Mar 3, 2022 at 21:01

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.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.