BioPython: How to set the order of qualifiers
1
0
Entering edit mode
7.9 years ago
mschmid ▴ 180

I have the following code (just a snippet):

        ...
        my_feature.qualifiers["locus_tag"] = tmpStr
        my_feature.qualifiers["length"] = this_start - last_end
        my_feature.qualifiers["flanking_gene_1"] = last_gene_name
        my_feature.qualifiers["flanking_gene_2"] = this_gene
        seq_record.features.append(my_feature)
        ...

I want the qualifiers to be outputed to the genbank file in the order above.

But the result ist the folloing (flanking_gene_1 and flanking_gene_2 swapped):

 ...  
 IGR             263255..263440
                 /locus_tag="xyz_IGR_00165"
                 /length=186
                 /flanking_gene_2="xyz_01880"
                 /flanking_gene_1="xyz_01870"
 IGR             263801..263909
                 /locus_tag="xyz_IGR_00166"
                 /length=109
                 /flanking_gene_2="xyz_01890"
                 /flanking_gene_1="xyz_01880"
 ...

How do I set the order?

Thx Michael

Genbank biopython • 1.8k views
ADD COMMENT
1
Entering edit mode
7.8 years ago
Peter 6.0k

The NCBI GenBank format doesn't explicitly document a preferred order for the key - does it actually matter in your use case?

Biopython just uses the dictionary order. You can deliberately control this by replacing my_feature.qualifiers with an ordered dictionary. e.g.

from collections import OrderedDict
...
my_feature.qualifiers = OrderedDict(my_feature.qualifiers)
my_feature.qualifiers["locus_tag"] = tmpStr
my_feature.qualifiers["length"] = this_start - last_end
my_feature.qualifiers["flanking_gene_1"] = last_gene_name
my_feature.qualifiers["flanking_gene_2"] = this_gene
seq_record.features.append(my_feature)
...

I'm assuming you are using Python 2.7 or later, this is slightly more complicated on Python 2.6

ADD COMMENT

Login before adding your answer.

Traffic: 2768 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