This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Printing features of a specific type only from a Genbank file with Biopython

I'm able to get the genbank file from the website and print the features from the file, but I want to only print the features where type='exon'. Further, I want print just the Exactposition fields for those features where type='exon' heres what I am doing so far

>>> handle = Entrez.efetch(db="nucleotide", rettype="gb", retmode="text", id="NM_001135")
>>> seq_record = SeqIO.read(handle, "gb")
>>> seq_record.features
[SeqFeature(FeatureLocation(ExactPosition(0), ExactPosition(8543), strand=1), type='source'), SeqFeature(FeatureLocation(ExactPosition(0), ExactPosition(8543), strand=1), type='gene'), SeqFeature(FeatureLocation(ExactPosition(0), ExactPosition(367), strand=1), type='exon'), SeqFeature(FeatureLocation(ExactPosition(367), ExactPosition(444), strand=1), type='exon'),
...
...

And I would like the output to be

SeqFeature(FeatureLocation(ExactPosition(0), ExactPosition(367), strand=1), type='exon'), SeqFeature(FeatureLocation(ExactPosition(367), ExactPosition(444), strand=1), type='exon'),

Or more specifically, as a final result

0-367
367-444
seqio seq genbank biopython

1 answer

Code:

from Bio import Entrez
from Bio import SeqIO

Entrez.email = 'yourmail@mail.com'
qid = "NM_001135"
handle = Entrez.efetch(db="nucleotide", rettype="gb", retmode="text", id=qid)
seq_record = SeqIO.read(handle, "gb")
for feat in seq_record.features:
    if feat.type == 'exon':
        print('{}-{}'.format(feat.location.start, feat.location.end))

Output:

0-367
367-444
444-828
828-1003
1003-1131
1131-1425
1425-1803
1803-1978
1978-2106
2106-2400
2400-2640
2640-7206
7206-7365
7365-7448
7448-7593
7593-8543

Log in to answer this question.