|
a |
|
b/tests/test_blockzip.py |
|
|
1 |
#!/usr/bin/env python3 |
|
|
2 |
# -*- coding: utf-8 -*- |
|
|
3 |
import unittest |
|
|
4 |
from singlecellmultiomics.utils import BlockZip |
|
|
5 |
|
|
|
6 |
import os |
|
|
7 |
""" |
|
|
8 |
These tests check if the BlockZip module is working correctly |
|
|
9 |
""" |
|
|
10 |
|
|
|
11 |
class Test_BlockZip(unittest.TestCase): |
|
|
12 |
|
|
|
13 |
def test_read_write(self): |
|
|
14 |
zip_path = './data/test.bgzf' |
|
|
15 |
with BlockZip(zip_path,'w') as f: |
|
|
16 |
f.write('chr1',100,False,'yes') |
|
|
17 |
f.write('chr1',101,False,'yes') |
|
|
18 |
f.write('chr2',2101,True,'yes') |
|
|
19 |
f.write('chrX',0,False,'X') |
|
|
20 |
|
|
|
21 |
with BlockZip(zip_path,'r') as f: |
|
|
22 |
self.assertEqual( f[ ('chr1',100,False) ], 'yes' ) |
|
|
23 |
self.assertEqual( f[ ('chr1',0,False) ], None) |
|
|
24 |
self.assertEqual( f[ ('chrX',0,False) ], 'X' ) |
|
|
25 |
self.assertEqual( f[ ('chr2',2101,True) ], 'yes' ) |
|
|
26 |
self.assertEqual( f[ ('chr1',0,True) ], None) |
|
|
27 |
|
|
|
28 |
|
|
|
29 |
# Test verification: |
|
|
30 |
with BlockZip(zip_path,'r') as f: |
|
|
31 |
f.verify() |
|
|
32 |
|
|
|
33 |
with BlockZip(zip_path,'w') as f: |
|
|
34 |
f.write('chr1',101,False,'yes') |
|
|
35 |
f.write('chr1',100,False,'yes') |
|
|
36 |
f.write('chr2',2101,True,'yes') |
|
|
37 |
f.write('chrX',0,False,'X') |
|
|
38 |
|
|
|
39 |
with BlockZip(zip_path,'r') as bz: |
|
|
40 |
self.assertRaises(ValueError, bz.verify) |
|
|
41 |
|
|
|
42 |
|
|
|
43 |
# Test for missing index: |
|
|
44 |
os.remove(zip_path+'.idx') |
|
|
45 |
self.assertRaises(ValueError, BlockZip, './non_existing.bgzf','r') |
|
|
46 |
|
|
|
47 |
os.remove(zip_path) |
|
|
48 |
self.assertRaises(ValueError, BlockZip, './non_existing.bgzf','r') |
|
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
if __name__ == '__main__': |
|
|
54 |
unittest.main() |