|
15 | 15 | # |
16 | 16 |
|
17 | 17 | import pytest |
18 | | -import vcf |
19 | 18 |
|
20 | 19 | from variantworks.io.vcfio import VCFReader |
21 | 20 | from variantworks.types import VariantZygosity, Variant |
22 | 21 |
|
23 | 22 | from data.vcf_file_mock import mock_file_input, mock_invalid_file_input |
24 | 23 |
|
25 | 24 |
|
26 | | -class MockPyVCFReader: |
27 | | - original_pyvcf_reader_init_function = vcf.Reader.__init__ |
28 | | - |
29 | | - @staticmethod |
30 | | - def new_vcf_reader_init(self, *args, **kargs): |
31 | | - MockPyVCFReader.original_pyvcf_reader_init_function( |
32 | | - self, mock_file_input()) |
33 | | - |
34 | | - @staticmethod |
35 | | - def new_bad_vcf_reader_init(self, *args, **kargs): |
36 | | - MockPyVCFReader.original_pyvcf_reader_init_function( |
37 | | - self, mock_invalid_file_input()) |
38 | | - |
39 | | - @staticmethod |
40 | | - def get_vcf(mp, vcf_bam_list): |
41 | | - with mp.context() as m: |
42 | | - # Mock vcf.Reader.__init__() return value |
43 | | - m.setattr(vcf.Reader, "__init__", |
44 | | - MockPyVCFReader.new_vcf_reader_init) |
45 | | - vcf_loader = VCFReader(vcf_bam_list) |
46 | | - return vcf_loader |
47 | | - |
48 | | - @staticmethod |
49 | | - def get_invalid_vcf(mp, vcf_bam_list): |
50 | | - with mp.context() as m: |
51 | | - # Mock vcf.Reader.__init__() return value |
52 | | - m.setattr(vcf.Reader, "__init__", |
53 | | - MockPyVCFReader.new_bad_vcf_reader_init) |
54 | | - vcf_loader = VCFReader(vcf_bam_list) |
55 | | - return vcf_loader |
56 | | - |
57 | | - |
58 | | -def test_vcf_loader_snps(monkeypatch): |
| 25 | +def test_vcf_loader_snps(get_created_vcf_tabix_files): |
59 | 26 | """Get all variants from mocked file stream, filter SNPs, multi allele & multi samples |
60 | 27 | """ |
61 | | - vcf_bam_tuple = VCFReader.VcfBamPath( |
62 | | - vcf="/dummy/path.gz", bam="temp.bam", is_fp=False) |
63 | | - vcf_loader = MockPyVCFReader.get_vcf(monkeypatch, [vcf_bam_tuple]) |
| 28 | + vcf_file_path, tabix_file_path = get_created_vcf_tabix_files(mock_file_input()) |
| 29 | + vcf_bam_tuple = VCFReader.VcfBamPath(vcf=vcf_file_path, bam=tabix_file_path, is_fp=False) |
| 30 | + vcf_loader = VCFReader([vcf_bam_tuple]) |
64 | 31 | assert(len(vcf_loader) == 15) |
65 | 32 |
|
66 | 33 |
|
67 | | -def test_vcf_fetch_variant(monkeypatch): |
| 34 | +def test_vcf_fetch_variant(get_created_vcf_tabix_files): |
68 | 35 | """Get first variant from mocked VCF file stream. |
69 | 36 | """ |
70 | | - vcf_bam_tuple = VCFReader.VcfBamPath(vcf="/dummy/path.gz", bam="temp.bam", is_fp=False) |
71 | | - vcf_loader = MockPyVCFReader.get_vcf(monkeypatch, [vcf_bam_tuple]) |
| 37 | + vcf_file_path, tabix_file_path = get_created_vcf_tabix_files(mock_file_input()) |
| 38 | + vcf_bam_tuple = VCFReader.VcfBamPath(vcf=vcf_file_path, bam=tabix_file_path, is_fp=False) |
| 39 | + vcf_loader = VCFReader([vcf_bam_tuple]) |
72 | 40 | try: |
73 | 41 | assert (type(vcf_loader[0]) == Variant) |
74 | 42 | except IndexError: |
75 | 43 | pytest.fail("Can not retrieve first element from VCFReader") |
76 | 44 |
|
77 | 45 |
|
78 | | -def test_vcf_load_fp(monkeypatch): |
| 46 | +def test_vcf_load_fp(get_created_vcf_tabix_files): |
79 | 47 | """Get first variant from false positive mocked VCF file stream and check zygosity. |
80 | 48 | """ |
81 | | - vcf_bam_tuple = VCFReader.VcfBamPath( |
82 | | - vcf="/dummy/path.gz", bam="temp.bam", is_fp=True) |
83 | | - vcf_loader = MockPyVCFReader.get_vcf(monkeypatch, [vcf_bam_tuple]) |
| 49 | + vcf_file_path, tabix_file_path = get_created_vcf_tabix_files(mock_file_input()) |
| 50 | + vcf_bam_tuple = VCFReader.VcfBamPath(vcf=vcf_file_path, bam=tabix_file_path, is_fp=True) |
| 51 | + vcf_loader = VCFReader([vcf_bam_tuple]) |
84 | 52 | for v in vcf_loader: |
85 | 53 | assert(v.zygosity == VariantZygosity.NO_VARIANT) |
86 | 54 |
|
87 | 55 |
|
88 | | -def test_vcf_load_variant_from_multiple_files(monkeypatch): |
| 56 | +def test_vcf_load_variant_from_multiple_files(get_created_vcf_tabix_files): |
89 | 57 | """Get variants from multiple mocked VCF files. |
90 | 58 | """ |
91 | | - first_vcf_bam_tuple = VCFReader.VcfBamPath( |
92 | | - vcf="/dummy/path.gz", bam="temp.bam", is_fp=False) |
93 | | - second_vcf_bam_tuple = VCFReader.VcfBamPath( |
94 | | - vcf="/dummy/path.gz", bam="temp.bam", is_fp=False) |
95 | | - vcf_loader = MockPyVCFReader.get_vcf(monkeypatch, [first_vcf_bam_tuple]) |
96 | | - vcf_loader_2x = MockPyVCFReader.get_vcf( |
97 | | - monkeypatch, [first_vcf_bam_tuple, second_vcf_bam_tuple]) |
| 59 | + vcf_file_path, tabix_file_path = get_created_vcf_tabix_files(mock_file_input()) |
| 60 | + first_vcf_bam_tuple = VCFReader.VcfBamPath(vcf=vcf_file_path, bam=tabix_file_path, is_fp=False) |
| 61 | + second_vcf_bam_tuple = VCFReader.VcfBamPath(vcf=vcf_file_path, bam=tabix_file_path, is_fp=False) |
| 62 | + vcf_loader = VCFReader([first_vcf_bam_tuple]) |
| 63 | + vcf_loader_2x = VCFReader([first_vcf_bam_tuple, second_vcf_bam_tuple]) |
98 | 64 | assert (2 * len(vcf_loader) == len(vcf_loader_2x)) |
99 | 65 |
|
100 | 66 |
|
101 | | -def test_load_vcf_content_with_wrong_format(monkeypatch): |
| 67 | +def test_load_vcf_content_with_wrong_format(get_created_vcf_tabix_files): |
102 | 68 | """ parse vcf file with wrong format |
103 | 69 | """ |
104 | | - vcf_bam_tuple = VCFReader.VcfBamPath( |
105 | | - vcf="/dummy/path.gz", bam="temp.bam", is_fp=False) |
| 70 | + vcf_file_path, tabix_file_path = get_created_vcf_tabix_files(mock_invalid_file_input()) |
| 71 | + vcf_bam_tuple = VCFReader.VcfBamPath(vcf=vcf_file_path, bam=tabix_file_path, is_fp=False) |
106 | 72 | with pytest.raises(RuntimeError): |
107 | | - MockPyVCFReader.get_invalid_vcf(monkeypatch, [vcf_bam_tuple]) |
| 73 | + VCFReader([vcf_bam_tuple]) |
0 commit comments