Entering edit mode
10.4 years ago
Hi,
I have to design a primer from a fasta sequence. From different posts I have found the script below... (using primer3).
from Bio import Entrez
from Bio import SeqIO
Entrez.email = "alba********@.***"
handle = Entrez.efetch(db="nucleotide", rettype="fasta", retmode="text", id="294489415")
seqrecord = SeqIO.read(handle, "fasta")
def get_primers(fasta_record, start, end):
from Bio.Emboss.Applications import Primer3Commandline
from Bio.Emboss.Primer import Primer3Parser
from Bio.Application import generic_run
open("in.pr3", "w").write(str(seqrecord) + "\n")
primer_cl = Primer3Commandline()
primer_cl.set_parameter("-sequence", "in.pr3")
primer_cl.set_parameter("-outfile", "out.pr3")
primer_cl.set_parameter("-productsizerange", "350,10000")
primer_cl.set_parameter("-target", "%s,%s" % (start, end))
result, r, e = generic_run(primer_cl)
parser = Primer3Parser()
return parser.parse(open("out.pr3"))
def main(fasta_file, output_file):
output_handle = open(output_file, "w")
output_handle.write("name,forward_primer,reverse_primer\n")
parser = Fasta.RecordParser()
iterator = Fasta.Iterator(open(seqrecord), parser)
while 1:
cur_record = iterator.next()
if not(cur_record): break
primer_record = get_primers(cur_record, 100, 250)
if len(primer_record.primers) > 0:
primer = primer_record.primers[0]
output_handle.write("%s,%s,%s\n" % (cur_record.title, primer.forward_seq, primer.reverse_seq))
else:
print "No primers found for %s" % cur_record.title
This is what the program shows:
File "p3f.py", line 12, in <module>
cur_record = iterator.next()
NameError: name 'iterator' is not defined
I have tried to import Fasta from biopython, but it doesn't work:
File "p3f.py", line 1, in <module>
from Bio import Fasta
ImportError: cannot import name Fasta
How can I solve this problem?
Thank you.
This bit will not work anymore:
from Bio import Fasta
Thus the error:
ImportError: cannot import name Fasta
This is because the old
Bio.Fasta
module was deprecated and removed back in Biopython 1.55 (August 2010). Nowadays we would use theBio.SeqIO
module instead.Thanks for your answer.
Did you install biopython at all?
Did you try to import SeqIO in your python?
using:
and:
Is this your whole code?
and:
Can you link to the post on biostar where you got your code from?
I have installed Biopython 1.64
Yes, I import SeqIO (line 2 in the script) but the problem is that I don't know how can I change:
with the SeqIO function.
Here is the link: http://www.biopython.org/DIST/docs/presentations/biopython.pdf
Thanks.
That code was broken anyway, I think it should have been using the
fasta_file
variable notseqrecord
. Also using a while loop like that is very old fashioned:That would be better written as:
The SeqIO equivalent would be just:
However, SeqIO returns SeqRecord objects which are slightly different to the objects the old Fasta parser returned.