from Bio import SeqIO

sfile = open('/home/sb/bioinfo/seqwrep.fasta')
# mysel stores a SeqRecord object generated from the
# first (and only) record in the fasta file.
myseq = SeqIO.read(sfile, "fasta")
# title stores the "id" attribute of the SeqRecord object.
title = myseq.id
# seq stores the sequence converted into string and
# uppercased.
seq = str(myseq.seq).upper()
win_size = 45
i = 0
number_l = []
# This while is used to walk over the sequence.
while i<=(len(seq)-win_size):
    # Each position of number_l stores the amount of 'AAT'
    # found on each window.
    number_l.append(seq[i:i+win_size].count('AAT'))
    i += 1 # This is the same as i = i+1
# pos stores the position of the window with the highest
# amount of 'AAT'
pos=number_l.index(max(number_l))
fout = open('/home/sb/bioinfo/swforprimer3.txt','w')
fout.write(
'''PRIMER_SEQUENCE_ID=%s
SEQUENCE=%s
TARGET=%s,%s
PRIMER_OPT_SIZE=18
PRIMER_MIN_SIZE=15
PRIMER_MAX_SIZE=20
PRIMER_NUM_NS_ACCEPTED=0
PRIMER_EXPLAIN_FLAG=1
PRIMER_PRODUCT_SIZE_RANGE=%s-%s
=''' % (title,seq,pos,win_size,win_size,len(seq)))
fout.close()
# Saves the data formated as the input file needed by
# primer3.

This code is part of the book "Python for Bioinformatics", by Sebastian Bassi (sbassi@genesdigitales.com). Return to home page.