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