fasta_filename = r"C:\Genomes\Bacteria\Escherichia_coli_K12\NC_000913.ffn"

import time
import Bio
from Bio import Seq
from Bio import SeqRecord
from Bio.SeqIO import FASTA
from Bio import FormatIO
from Bio import Fasta

def method_FormatIO_SeqRecord(handle) :
    myIterator = FormatIO.FormatIO("SeqRecord", default_input_format = "fasta").readFile(handle)
    count = 0
    for myRecord in myIterator :
        count = count + 1
    return "FormatIO/SeqRecord (for record in interator)", str(myRecord.__class__), count

def method_FormatIO_SeqRecord2(handle) :
    myIterator = FormatIO.FormatIO("SeqRecord", default_input_format = "fasta").readFile(handle)
    count = 0
    while True :
        try :
            myRecord = myIterator.next()
        except StopIteration :
            break
        count = count + 1
    return "FormatIO/SeqRecord (iterator.next)", "Bio.SeqRecord.SeqRecord", count

def method_SeqIO_Fasta_FastaReader(handle) :
    myIterator = FASTA.FastaReader(handle)
    count = 0
    for myRecord in myIterator :
        count = count + 1
    return "SeqIO.FASTA.FastaReader (for record in interator)", str(myRecord.__class__), count

def method_SeqIO_Fasta_FastaReader2(handle) :
    myIterator = Bio.SeqIO.FASTA.FastaReader(handle)
    count = 0
    while True :
        myRecord = myIterator.next()
        if myRecord is None : break
        count = count + 1
    return "SeqIO.FASTA.FastaReader (iterator.next)", "Bio.SeqRecord.SeqRecord", count

def method_SeqIO_Fasta_FastaReader3(handle) :
    myIterator = Bio.SeqIO.FASTA.FastaReader(handle)
    count = 0
    while True :
        try :
            myRecord = myIterator[count]
        except IndexError :
            break
        count = count + 1
    return "SeqIO.FASTA.FastaReader (iterator[i])", str(myRecord.__class__), count

def method_Fasta_RecordParser(handle) :
    myIterator = Fasta.Iterator(handle, Fasta.RecordParser())
    count = 0
    for myRecord in myIterator :
        count = count + 1
    return "Fasta.RecordParser (for record in interator)", str(myRecord.__class__), count

def method_Fasta_SequenceParser(handle) :
    myIterator = Fasta.Iterator(handle, Fasta.SequenceParser())
    count = 0
    for myRecord in myIterator :
        count = count + 1
    return "Fasta.SequenceParser (for record in interator)", str(myRecord.__class__), count

def method_Fasta_SequenceParser2(handle) :
    myIterator = Fasta.Iterator(handle, Fasta.SequenceParser())
    count = 0
    while True :
        myRecord = myIterator.next()
        if myRecord is None : break
        count = count + 1
    return "Fasta.SequenceParser (iterator.next)", "Bio.SeqRecord.SeqRecord", count

def time_method(method, filename) :
    input_file = open(filename, 'rU')

    start = time.time()
    name, returnclass, count = method(input_file)
    end = time.time()

    input_file.close()
    return name, returnclass, count, end - start

for i in range(0,3) :
    for method in [method_FormatIO_SeqRecord,
                   method_FormatIO_SeqRecord2,
                   method_SeqIO_Fasta_FastaReader,
                   method_SeqIO_Fasta_FastaReader2,
                   method_SeqIO_Fasta_FastaReader3,
                   method_Fasta_RecordParser,
                   method_Fasta_SequenceParser,
                   method_Fasta_SequenceParser2] :
        name, returnclass, count, seconds = time_method(method, fasta_filename)
        print "%0.2fs %s" % (seconds, name)
print fasta_filename
print "%i records" % count
