Debian Codegraph @

tests/VariantRecord_test.py

raw symbols SCIP
1 import pysam
2 import pytest
3
4 from TestUtils import make_data_files, CBCF_DATADIR
5
6
7 def setUpModule():
8 make_data_files(CBCF_DATADIR)
9
10
11 @pytest.fixture
12 def vcf_header():
13 vcf_header = pysam.VariantHeader()
14 vcf_header.add_samples("sample1", "sample2")
15 vcf_header.contigs.add("1")
16 return vcf_header
17
18 ## segfault without coordinates
19
20 def test_ascii_annotation_can_be_added(vcf_header):
21 vcf_header.formats.add("AN", 1, "String", "An annotation")
22 record = vcf_header.new_record(
23 contig="1",
24 start=12,
25 stop=13,
26 samples=[
27 {"AN": "anno1"},
28 {"AN": "anno2"}])
29 assert str(record)[:-1].split("\t")[-2:] == ["anno1", "anno2"]
30
31
32 def test_ascii_annotation_with_variable_length_can_be_added(vcf_header):
33 vcf_header.formats.add("AN", 1, "String", "An annotation")
34 record = vcf_header.new_record(
35 contig="1",
36 start=12,
37 stop=13,
38 samples=[
39 {"AN": "anno1b"},
40 {"AN": "anno1"}])
41 assert str(record)[:-1].split("\t")[-2:] == ["anno1b", "anno1"]
42 record = vcf_header.new_record(
43 contig="1",
44 start=12,
45 stop=13,
46 samples=[
47 {"AN": "anno2"},
48 {"AN": "anno2b"}])
49 assert str(record)[:-1].split("\t")[-2:] == ["anno2", "anno2b"]
50
51
52 def test_unicode_annotation_can_be_added(vcf_header):
53 vcf_header.formats.add("AN", 1, "String", "An annotation")
54 record = vcf_header.new_record(
55 contig="1",
56 start=12,
57 stop=13,
58 samples=[
59 {"AN": "anno1"},
60 {"AN": "Friedrich-Alexander-Universit\u00E4t_Erlangen-N\u00FCrnberg"}])
61 assert str(record)[:-1].split("\t")[-2:] == [
62 "anno1",
63 "Friedrich-Alexander-Universit\u00E4t_Erlangen-N\u00FCrnberg"]
64
65 def test_set_sample_alleles(vcf_header):
66 vcf_header.formats.add('GT',1,'String',"Genotype") # id, number, type, description
67 record = vcf_header.new_record(
68 contig="1",
69 start=20,
70 stop=21,
71 alleles=('A','T')
72 )
73
74 record.samples['sample1'].alleles = ('T', 'A')
75 assert record.samples['sample1'].alleles == ('T','A')
76
77 # Empty record:
78 record.samples['sample1'].alleles = (None, )
79 assert record.samples['sample1'].alleles == tuple()
80 record.samples['sample1'].alleles = None
81 assert record.samples['sample1'].alleles == tuple()
82 record.samples['sample1'].alleles = tuple()
83 assert record.samples['sample1'].alleles == tuple()
84
85 # check error conditions:
86 with pytest.raises(ValueError, match='One or more of the supplied sample alleles are not defined'):
87 record.samples['sample1'].alleles = ('C', 'A')
88
89 with pytest.raises(ValueError, match='Use .allele_indices to set integer allele indices'):
90 record.samples['sample1'].alleles = (1, 0)
91
92
93 def test_repeated_new_record(vcf_header):
94 vcf_header.formats.add('GT', 1, 'String', "Genotype")
95 vcf_header.formats.add("AA", 1, "String", "An annotation")
96 vcf_header.formats.add("BB", 1, "String", "Another annotation")
97
98 data = {'id': 'INS_1', 'contig': '1', 'start': 10, 'stop': 15, 'alleles': ['A', 'TCGA'],
99 'samples': [{'AA': ('one'), 'GT': (0, 1), 'BB': ('two')},
100 {'GT': (1, 0), 'BB': ('three')}]}
101
102 record1 = vcf_header.new_record(**data)
103 assert '\tGT:' in str(record1) # Verify that GT is output first
104 assert record1.samples['sample1'].alleles == ('A', 'TCGA')
105 assert record1.samples['sample2'].alleles == ('TCGA', 'A')
106
107 record2 = vcf_header.new_record(**data)
108 assert '\tGT:' in str(record2) # Verify that GT is actually emitted and is output first
109 assert record2.samples['sample1'].alleles == ('A', 'TCGA')
110 assert record2.samples['sample2'].alleles == ('TCGA', 'A')