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?
Bioinformatics Stack Exchange is a question and answer site for researchers, developers, students, teachers, and end users interested in bioinformatics. It only takes a minute to sign up.
Sign up to join this communityYou 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.
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$
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