This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Design primers (Primer3)

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.

biopython iterator fasta python primer3

Did you install biopython at all?

Did you try to import SeqIO in your python?

using:

from Bio import SeqIO

and:

Is this your whole code?

and:

Can you link to the post on biostar where you got your code from?

That code was broken anyway, I think it should have been using the fasta_file variable not seqrecord. Also using a while loop like that is very old fashioned:

parser = Fasta.RecordParser()
iterator = Fasta.Iterator(open(seqrecord), parser) # wrong variable name!
while 1:
    cur_record = iterator.next()
    if not(cur_record): break
    # use cur_record

That would be better written as:

parser = Fasta.RecordParser()
iterator = Fasta.Iterator(open(fasta_filename), parser)
for cur_record in iterator:
    # use cur_record

The SeqIO equivalent would be just:

for cur_record in SeqIO.parse(fasta_filename, "fasta"):
    # use cur_record

However, SeqIO returns SeqRecord objects which are slightly different to the objects the old Fasta parser returned.

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 the Bio.SeqIO module instead.

0 answers

No answers yet.

Log in to answer this question.