Design primers (Primer3)
0
0
Entering edit mode
9.7 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.

biopython iterator fasta python primer3 • 4.7k views
ADD COMMENT
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

Thanks for your answer.

ADD REPLY
0
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

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:

parser = Fasta.RecordParser()
iterator = Fasta.Iterator(open(seqrecord), parser)

with the SeqIO function.

Here is the link: http://www.biopython.org/DIST/docs/presentations/biopython.pdf

Thanks.

ADD REPLY
1
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

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