| 1 | """Benchmarking module for AlignmentFile functionality""" |
| 2 | import os |
| 3 | import subprocess |
| 4 | import pysam |
| 5 | |
| 6 | |
| 7 | from TestUtils import BAM_DATADIR |
| 8 | |
| 9 | |
| 10 | def count_number_lines_with_samtools(fn): |
| 11 | os.system("samtools view {} | wc -l > /dev/null".format(fn)) |
| 12 | return 3270 |
| 13 | |
| 14 | |
| 15 | def count_number_lines_with_samtoolspipe(fn): |
| 16 | with subprocess.Popen(["samtools", "view", fn], |
| 17 | stdin=subprocess.PIPE, |
| 18 | stdout=subprocess.PIPE) as proc: |
| 19 | return len(proc.stdout.readlines()) |
| 20 | |
| 21 | |
| 22 | def count_number_lines_with_pysam(*args, **kwargs): |
| 23 | with pysam.AlignmentFile(*args, **kwargs) as inf: |
| 24 | return len(list(inf.fetch())) |
| 25 | |
| 26 | |
| 27 | def test_count_number_lines_from_sam_with_samtools(benchmark): |
| 28 | result = benchmark(count_number_lines_with_samtools, |
| 29 | os.path.join(BAM_DATADIR, "ex2.sam")) |
| 30 | assert result == 3270 |
| 31 | |
| 32 | |
| 33 | def test_count_number_lines_from_sam_with_samtoolspipe(benchmark): |
| 34 | result = benchmark(count_number_lines_with_samtoolspipe, |
| 35 | os.path.join(BAM_DATADIR, "ex2.sam")) |
| 36 | assert result == 3270 |
| 37 | |
| 38 | |
| 39 | def test_count_number_lines_from_sam_with_pysam(benchmark): |
| 40 | result = benchmark(count_number_lines_with_pysam, |
| 41 | os.path.join(BAM_DATADIR, "ex2.sam"), "r") |
| 42 | assert result == 3270 |
| 43 | |
| 44 | |
| 45 | def test_count_number_lines_from_bam_with_samtools(benchmark): |
| 46 | result = benchmark(count_number_lines_with_samtools, |
| 47 | os.path.join(BAM_DATADIR, "ex2.bam")) |
| 48 | assert result == 3270 |
| 49 | |
| 50 | |
| 51 | def test_count_number_lines_from_bam_with_samtoolspipe(benchmark): |
| 52 | result = benchmark(count_number_lines_with_samtoolspipe, |
| 53 | os.path.join(BAM_DATADIR, "ex2.bam")) |
| 54 | assert result == 3270 |
| 55 | |
| 56 | |
| 57 | def test_count_number_lines_from_bam_with_pysam(benchmark): |
| 58 | result = benchmark(count_number_lines_with_pysam, |
| 59 | os.path.join(BAM_DATADIR, "ex2.bam")) |
| 60 | assert result == 3270 |