| 1 |
|
| 2 |
|
| 3 |
import unittest |
| 4 |
import os |
| 5 |
import shutil |
| 6 |
|
| 7 |
from utils import aug_assertions |
| 8 |
from utils import aug_process |
| 9 |
from utils import aug_path |
| 10 |
|
| 11 |
pathname = 'auxprogs/bam2wig/' |
| 12 |
referencedir = os.path.join(pathname, 'expected_results') |
| 13 |
resultdir = os.path.join(pathname, 'result_files') |
| 14 |
htmldir = os.path.join('output_html/') |
| 15 |
tmpdir = os.path.join(pathname, 'tmp') |
| 16 |
exampledir = os.path.join(pathname, 'test_files') |
| 17 |
exampletestfile = 'test.s.bam' |
| 18 |
bindir = '../../bin/' |
| 19 |
bam2wigbin = bindir + 'bam2wig' |
| 20 |
default_wd = os.getcwd() |
| 21 |
|
| 22 |
|
| 23 |
def clean(force_tmp_dir=True, force_html_dir=True, force_result_dir=True): |
| 24 |
"""Remove empty directories or if forced""" |
| 25 |
aug_path.rmtree_if_exists(resultdir, force_result_dir) |
| 26 |
aug_path.rmtree_if_exists(tmpdir, force_tmp_dir) |
| 27 |
aug_path.rmtree_if_exists(htmldir, force_html_dir) |
| 28 |
|
| 29 |
|
| 30 |
class TestBam2Wig(unittest.TestCase): |
| 31 |
|
| 32 |
opt_compare = False |
| 33 |
opt_html = False |
| 34 |
|
| 35 |
@classmethod |
| 36 |
def setUpClass(cls): |
| 37 |
os.chdir(default_wd) |
| 38 |
clean(force_tmp_dir=False, force_html_dir=True, force_result_dir=True) |
| 39 |
aug_path.mkdir_if_not_exists(resultdir) |
| 40 |
aug_path.mkdir_if_not_exists(tmpdir) |
| 41 |
|
| 42 |
inputfile = os.path.join(exampledir, exampletestfile) |
| 43 |
testfile = os.path.join(tmpdir, exampletestfile) |
| 44 |
shutil.copyfile(inputfile, testfile) |
| 45 |
|
| 46 |
os.chdir(tmpdir) |
| 47 |
aug_process.execute(None, 'samtools index ' + exampletestfile) |
| 48 |
|
| 49 |
if os.path.isfile(os.path.join(tmpdir, exampletestfile+'.bai')): |
| 50 |
assert False, 'Test file not indexed.' |
| 51 |
|
| 52 |
@classmethod |
| 53 |
def tearDownClass(cls): |
| 54 |
os.chdir(default_wd) |
| 55 |
clean(force_tmp_dir=False, force_html_dir=False, force_result_dir=False) |
| 56 |
|
| 57 |
def test_bam2wig(self): |
| 58 |
os.chdir(default_wd) |
| 59 |
testfile = os.path.join(tmpdir, exampletestfile) |
| 60 |
reffile = os.path.join(referencedir, 'test.s.wig') |
| 61 |
resfile = os.path.join(resultdir, 'test.s.wig') |
| 62 |
|
| 63 |
aug_process.execute( |
| 64 |
self, bam2wigbin + " " + testfile + ' | grep -v track', resfile) |
| 65 |
|
| 66 |
|
| 67 |
if TestBam2Wig.opt_compare: |
| 68 |
aug_assertions.assertEqualFiles( |
| 69 |
self, reffile, resfile, TestBam2Wig.opt_html, htmldir) |
| 70 |
os.remove(resfile) |
| 71 |
|
| 72 |
def test_bam2wig_region(self): |
| 73 |
os.chdir(default_wd) |
| 74 |
testfile = os.path.join(tmpdir, exampletestfile) |
| 75 |
reffile = os.path.join(referencedir, 'test.s.chr3L.wig') |
| 76 |
resfile = os.path.join(resultdir, 'test.s.chr3L.wig') |
| 77 |
|
| 78 |
aug_process.execute(self, |
| 79 |
bam2wigbin + ' -t "my_specified_track" -r chr3L ' + testfile + ' | grep -v track', |
| 80 |
resfile) |
| 81 |
|
| 82 |
|
| 83 |
if TestBam2Wig.opt_compare: |
| 84 |
aug_assertions.assertEqualFiles( |
| 85 |
self, reffile, resfile, TestBam2Wig.opt_html, htmldir) |
| 86 |
os.remove(resfile) |
| 87 |
|
| 88 |
|
| 89 |
def test_suite(): |
| 90 |
suite = unittest.TestSuite() |
| 91 |
suite.addTest(TestBam2Wig('test_bam2wig')) |
| 92 |
suite.addTest(TestBam2Wig('test_bam2wig_region')) |
| 93 |
|
| 94 |
return suite |
| 95 |
|
| 96 |
|
| 97 |
def execute(compare, html, mysql): |
| 98 |
TestBam2Wig.opt_compare = compare |
| 99 |
TestBam2Wig.opt_html = html |
| 100 |
|
| 101 |
runner = unittest.TextTestRunner(verbosity=2) |
| 102 |
result = runner.run(test_suite()) |
| 103 |
|
| 104 |
return result.wasSuccessful() |