1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
'''benchmark pysam BAM/SAM access with the samtools commandline tools.
samtools functions are called via the pysam interface to avoid the over-head
of starting additional processes.
'''
import pysam
import timeit
iterations = 10
def runBenchmark( test,
pysam_way,
samtools_way = None):
print test
print timeit.repeat( pysam_way, number = iterations, setup="from __main__ import pysam" )
if samtools_way:
print timeit.repeat( samtools_way, number = iterations, setup="from __main__ import pysam" )
runBenchmark( "Samfile.fetch",
'''
f = pysam.Samfile( "ex1.bam", "rb" )
results = list(f.fetch())
''',
'''
f = pysam.view( "ex1.bam" )
'''
)
runBenchmark( "Samfile.pileup",
'''
f = pysam.Samfile( "ex1.bam", "rb" )
results = list(f.pileup())
''',
'''
f = pysam.pileup( "ex1.bam" )
''')
runBenchmark( "Samfile.pileup with coverage retrieval",
'''
f = pysam.Samfile( "ex1.bam", "rb" )
results = [ x.n for x in f.pileup() ]
''' )
runBenchmark( "Samfile.pileup with full retrieval",
'''
f = pysam.Samfile( "ex1.bam", "rb" )
results = [ x.pileups for x in f.pileup() ]
''' )
runBenchmark( "Samfile.pileup - many references",
'''
f = pysam.Samfile( "manyrefs.bam", "rb" )
results = [ x.pileups for x in f.pileup() ]
''',
'''
f = pysam.pileup( "manyrefs.bam" )
'''
)
|