Biopython SeqIO: remove gene annotation if not in a list from GenBank file
1
0
Entering edit mode
5.8 years ago
Jon ▴ 340

I'm trying to "slice" a genbank record based on the annotation (record.features) and running into some problems.  I have a genome genbank file, I do some filtering of gene models elsewhere which results in a list of genes that I would like to keep in the annotation, while those not in the list I'd like to remove (delete).  I'm currently trying to do it by deleting features that are not in my "keep" list, however I'm getting some errors.  Here's what I have thus far, and the corresponding error: 

from Bio import SeqIO
input = 'test.gbk'
keep = ['gene1', 'gene2', 'gene3', 'gene4']
with open(input, 'rU') as input:
    for record in SeqIO.parse(input, 'genbank'):
        for feature in record.features:
            if "locus_tag" in feature.qualifiers:
                if not feature.qualifiers['locus_tag'][0] in keep:
                    del feature[:]
        SeqIO.write(record, 'output.gbk', 'genbank')​

 However this seems to tell me I can't delete SeqRecord features, is there another way to do this?

Traceback (most recent call last):
File "<stdin>", line 6, in <module>
TypeError: 'SeqFeature' object does not support item deletion
Latest biopython SeqIO • 2.5k views
ADD COMMENT
0
Entering edit mode
5.8 years ago
Felix_Sim ▴ 250

I'm unable to test this implementation right now but maybe something along the following lines:

from Bio import SeqIO
input = 'test.gbk'
keep = ['gene1', 'gene2', 'gene3', 'gene4']
with open(input, 'rU') as input:
    for record in SeqIO.parse(input, 'genbank'):
        for index, feature in enumerate(record.features):
            if "locus_tag" in feature.qualifiers:
                if not feature.qualifiers['locus_tag'][0] in keep:
                    record.features.pop(index)
        SeqIO.write(record, 'output.gbk', 'genbank')​
ADD COMMENT

Login before adding your answer.

Traffic: 1613 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6