Question: Problem Adding Qualifiers To A Genbank File With Python
gravatar for jcastrofigueroa
7.0 years ago by
Norwich, UK
jcastrofigueroa140 wrote:

Hi guys:

I have this GenBank file:

LOCUS       sctg_0006_0001        172997 bp    DNA              UNK 01-JAN-1980
DEFINITION  sctg_0006_0001  length=172997
ACCESSION   sctg_0006_0001
VERSION     sctg_0006_0001
SOURCE      .

FEATURES             Location/Qualifiers
     CDS             <3..182
     CDS             372..1145
     CDS[Many Many More]...

And as you can see it has the features and their respective location and a qualifier note. What I'm trying to do in to add a new qualifier called locus_tag to each CDS in this big file.

I have written this code, but I'm getting some problems:

from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqFeature import SeqFeature, FeatureLocation
from Bio.SeqRecord import SeqRecord

annotation_handle = open("/Users/jcastrof/Desktop/prueba/prueba_str.gbk","rU")

for record in SeqIO.parse(annotation_handle,"genbank"):

    a = len(record.features)

    for_rast = open("/Users/k/Desktop/prueba/contig_for_rast.gbk","w")

    for x in range(0, a):

        locus_tag = {"locus_tag":"%s_%s" %,x+1)}

        new_record = (SeqFeature(qualifiers = locus_tag))


        SeqIO.write(record, for_rast, "genbank")

And I've got this error:

Traceback (most recent call last):
  File "/Users/k/Desktop/", line 32, in <module>
    SeqIO.write(record, for_rast, "genbank")
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/SeqIO/", line 426, in write
    count = writer_class(fp).write_file(sequences)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/SeqIO/", line 254, in write_file
    count = self.write_records(records)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/SeqIO/", line 239, in write_records
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/SeqIO/", line 775, in write_record
    self._write_feature(feature, rec_length)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/SeqIO/", line 305, in _write_feature
    assert feature.type, feature
AssertionError: type: 
location: None
    Key: locus_tag, Value: sctg_0006_0001_1

What would you suggest? (please try to help me out :D ). Thanks!

feature biopython genbank • 4.1k views
ADD COMMENTlink modified 7.0 years ago by Brad Chapman9.5k • written 7.0 years ago by jcastrofigueroa140

I remember that using Artemis ( you can load your Genbank file and add qualifiers (such as locus_tag) to all or specifically filtered features (for example easily to all CDS). Maybe this could save you some work.

ADD REPLYlink written 7.0 years ago by skymningen330

Thanks, I'll try it. But what I need is to do this for many files.

ADD REPLYlink modified 7.0 years ago • written 7.0 years ago by jcastrofigueroa140

I think it will be fine for "many" as in 5 to 10, but if it's more around 200 you might have to get back to another solution.

ADD REPLYlink written 7.0 years ago by skymningen330
gravatar for Brad Chapman
7.0 years ago by
Brad Chapman9.5k
Boston, MA
Brad Chapman9.5k wrote:

Your code creates a new Feature with only your locus qualifier in it. The error message is because this new feature does not possess a type (like CDS), so it can't write it out to GenBank format.

It sounds from your description like you want to add a qualifier to CDS features, rather than making a new feature so want something like:

x = 0
final_features = []
for f in record.features:
    if f.type == "CDS":
        f.qualifiers["locus_tag"] = "%s_%s" %, x+1)
        x += 1

record.features = final_features
with open("/Users/k/Desktop/prueba/contig_for_rast.gbk","w") as for_rast:
    SeqIO.write(record, for_rast, "genbank")

Hope this helps

ADD COMMENTlink written 7.0 years ago by Brad Chapman9.5k

Tip: You can simplify the last two lines by just calling the write function with a filename instead of a handle.

Brad: Do you need the final_features list bit? Can't you remove that as you are editing the features in situ?

ADD REPLYlink modified 7.0 years ago • written 7.0 years ago by Peter5.8k

Peter, you're right on both accounts. For final_features, I was just trying to be explicit about the modification. I'm picking up that habit from working with immutable objects in Clojure. Your approach will work great as well and be shorter.

ADD REPLYlink written 7.0 years ago by Brad Chapman9.5k

You can (I presume) still edit your answer if you want to. The clojure influence makes sense now you've explained that ;)

ADD REPLYlink written 7.0 years ago by Peter5.8k

Yeah it worked!

ADD REPLYlink written 7.0 years ago by jcastrofigueroa140
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1314 users visited in the last hour