Hi (Bio) python experts,
I have written a script in (bio)python below which parses through a gbk file, locates feature.qualifier['protein_id']
by matching it to protein_id
s which I have given it (as keys in a dictionary) and amends the feature.qualifier['product']
(given as values to corresponding keys in dictionary). When I print the changes in the end I can see that the feature.qualifier['product']
has changed but the problem is when I try saving the changed features/record as a new gbk file it goes into a never ending loop.
What I am looking for to have is a new gbk file with everything else the same as the original gbk file but only the feature.qualifier['product']
changed. Kindly suggest where I am going wrong. I would really really appreciate your help.
PS: I have used all possible forums where they have added/changed features and written new gbk file but nothing seems to solve it.
Very many thanks in advance
A
locustaglist=[]
newannotationlist=[]
LTandNAdic={}
with open ("CJM1cam_consensus_annotation_table_for_gbk_110315.txt", "rU") as file1:
for l,line in enumerate (file1):
line=line.rstrip()
if l==0:
pass
else:
CJM1camlocustag,CJM1camlocus,CJM1cam_consensuslocus,CJM1cam_annotation,CJM1cam_consensus_annotation,COG_description=line.split("\t")
modifiedlocustag="VBC:"+str(CJM1camlocustag)
locustaglist.append(modifiedlocustag)
newannotationlist.append(str(CJM1cam_consensus_annotation))
for loc in range(len(locustaglist)):
LTandNAdic[locustaglist[loc]]=newannotationlist[loc]
#print LTandNAdic
for k, v in LTandNAdic.items():
#print k, v
final_features=[]
for index,record in enumerate (SeqIO.parse("CJM1cam_CJM1locustag_consensus_annot.gbk", "genbank")):
#print record.annotations
for f, feature in enumerate(record.features):
if feature.type == "CDS":
if str(feature.qualifiers['protein_id'][0])==k:
feature.qualifiers['product'][0]=v
# print feature.qualifiers['protein_id'],feature.qualifiers['product'](#desired chnage has happened but prints an ever-ending loop)
#print feature(#again continuous loop)
final_features.append(feature)
record.features=final_features
#print record.features
with open ("CJM1cam_CJM1locustag_consensus_locus_annotation.gbk", "w") as new_gbk:
SeqIO.write(record,new_gbk,"genbank")(# continuous loop)
Many thanks for such a super quick response, Peter. I am trying to edit the script now as you have suggested. I will provide feedback if it works. Regards, A
Dear Peter,
The script now works wonderfully, thanks to your magic bit of code. Many upvotes to your answer!
Great - thanks for remembering to let us know it worked for you.