|
3 | 3 |
|
4 | 4 | from cg.apps.demultiplex.sample_sheet.index import (
|
5 | 5 | Index,
|
6 |
| - get_hamming_distance_for_indexes, |
| 6 | + get_hamming_distance_index_1, |
| 7 | + get_hamming_distance_index_2, |
7 | 8 | get_reverse_complement_dna_seq,
|
8 | 9 | get_valid_indexes,
|
9 | 10 | is_padding_needed,
|
@@ -76,34 +77,120 @@ def test_get_reverse_complement_not_dna(caplog):
|
76 | 77 | get_reverse_complement_dna_seq(dna=strain)
|
77 | 78 |
|
78 | 79 |
|
79 |
| -def test_get_hamming_distance_index_1_different_lengths(): |
80 |
| - """Test that hamming distance between indexes with same prefix but different lengths is zero.""" |
81 |
| - # GIVEN two index_1 sequences with the same prefixes but different lengths |
82 |
| - sequence_1: str = "GATTACA" |
83 |
| - sequence_2: str = "GATTACAXX" |
| 80 | +@pytest.mark.parametrize( |
| 81 | + "sequence_1, sequence_2, expected_distance", |
| 82 | + [ |
| 83 | + ("GATTACA", "GATTACA", 0), |
| 84 | + ("GATTACA", "GATTACAXX", 0), |
| 85 | + ("XXXACA", "GATTACA", 6), |
| 86 | + ("XXXXXXX", "GATTACA", 7), |
| 87 | + ], |
| 88 | + ids=[ |
| 89 | + "Identical sequences", |
| 90 | + "Same initial part, different lengths", |
| 91 | + "Same final part, different lengths", |
| 92 | + "Different sequences, same length", |
| 93 | + ], |
| 94 | +) |
| 95 | +def test_get_hamming_distance_index_1(sequence_1: str, sequence_2: str, expected_distance: int): |
| 96 | + """ |
| 97 | + Test that Hamming distances are calculated correctly for different sets of index 1 sequences. |
| 98 | + This is, that the operation is commutative and aligns sequences from the left. |
| 99 | + """ |
| 100 | + # GIVEN two index_1 sequences |
84 | 101 |
|
85 | 102 | # WHEN getting the hamming distance between them in any order
|
86 | 103 |
|
87 | 104 | # THEN the distance is zero
|
88 |
| - assert get_hamming_distance_for_indexes(sequence_1=sequence_1, sequence_2=sequence_2) == 0 |
89 |
| - assert get_hamming_distance_for_indexes(sequence_1=sequence_2, sequence_2=sequence_1) == 0 |
| 105 | + assert ( |
| 106 | + get_hamming_distance_index_1(sequence_1=sequence_1, sequence_2=sequence_2) |
| 107 | + == expected_distance |
| 108 | + ) |
| 109 | + assert ( |
| 110 | + get_hamming_distance_index_1(sequence_1=sequence_2, sequence_2=sequence_1) |
| 111 | + == expected_distance |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.parametrize( |
| 116 | + "sequence_1, sequence_2, expected_distance", |
| 117 | + [ |
| 118 | + ("GATTACA", "GATTACA", 0), |
| 119 | + ("GATTACA", "XXGATTACA", 0), |
| 120 | + ("GATXX", "GATTACA", 5), |
| 121 | + ("XXXXXXX", "GATTACA", 7), |
| 122 | + ], |
| 123 | + ids=[ |
| 124 | + "Identical sequences", |
| 125 | + "Same final part, different lengths", |
| 126 | + "Same initial part, different lengths", |
| 127 | + "Different sequences, same length", |
| 128 | + ], |
| 129 | +) |
| 130 | +def test_get_hamming_distance_index_2_reverse_complement( |
| 131 | + sequence_1: str, sequence_2: str, expected_distance: int |
| 132 | +): |
| 133 | + """ |
| 134 | + Test that Hamming distances are calculated correctly for different sets of index 2 sequences |
| 135 | + with reverse complement. This is, that the operation is commutative and aligns sequences from |
| 136 | + the right. |
| 137 | + """ |
| 138 | + # GIVEN two index_2 sequences |
90 | 139 |
|
91 |
| - # WHEN getting the hamming distance between themselves |
| 140 | + # WHEN getting the hamming distance between them in any order |
92 | 141 |
|
93 | 142 | # THEN the distance is zero
|
94 |
| - assert get_hamming_distance_for_indexes(sequence_1=sequence_1, sequence_2=sequence_1) == 0 |
95 |
| - assert get_hamming_distance_for_indexes(sequence_1=sequence_2, sequence_2=sequence_2) == 0 |
| 143 | + assert ( |
| 144 | + get_hamming_distance_index_2( |
| 145 | + sequence_1=sequence_1, sequence_2=sequence_2, is_reverse_complement=True |
| 146 | + ) |
| 147 | + == expected_distance |
| 148 | + ) |
| 149 | + assert ( |
| 150 | + get_hamming_distance_index_2( |
| 151 | + sequence_1=sequence_2, sequence_2=sequence_1, is_reverse_complement=True |
| 152 | + ) |
| 153 | + == expected_distance |
| 154 | + ) |
96 | 155 |
|
97 | 156 |
|
98 |
| -def test_get_hamming_distance_index_1_different_prefixes(): |
99 |
| - """Test that hamming distance for index 1 counts different characters from the left.""" |
100 |
| - # GIVEN two index_1 sequences with different lengths differing by two characters |
101 |
| - # when aligned to the left |
102 |
| - sequence_1: str = "GATXX" |
103 |
| - sequence_2: str = "GATTACA" |
| 157 | +@pytest.mark.parametrize( |
| 158 | + "sequence_1, sequence_2, expected_distance", |
| 159 | + [ |
| 160 | + ("GATTACA", "GATTACA", 0), |
| 161 | + ("GATTACA", "GATTACAXX", 0), |
| 162 | + ("XXXACA", "GATTACA", 6), |
| 163 | + ("XXXXXXX", "GATTACA", 7), |
| 164 | + ], |
| 165 | + ids=[ |
| 166 | + "Identical sequences", |
| 167 | + "Same initial part, different lengths", |
| 168 | + "Same final part, different lengths", |
| 169 | + "Different sequences, same length", |
| 170 | + ], |
| 171 | +) |
| 172 | +def test_get_hamming_distance_index_2_no_reverse_complement( |
| 173 | + sequence_1: str, sequence_2: str, expected_distance: int |
| 174 | +): |
| 175 | + """ |
| 176 | + Test that Hamming distances are calculated correctly for different sets of index 2 sequences |
| 177 | + without reverse complement. This is, that the operation is commutative and aligns sequences |
| 178 | + from the left. |
| 179 | + """ |
| 180 | + # GIVEN two index_2 sequences |
104 | 181 |
|
105 |
| - # WHEN getting the hamming distance between them in any order |
| 182 | + # WHEN getting the hamming distance between them in any order with reverse complement |
106 | 183 |
|
107 |
| - # THEN the distance is equal to the number of different characters |
108 |
| - assert get_hamming_distance_for_indexes(sequence_1=sequence_1, sequence_2=sequence_2) == 2 |
109 |
| - assert get_hamming_distance_for_indexes(sequence_1=sequence_2, sequence_2=sequence_1) == 2 |
| 184 | + # THEN the distance is zero |
| 185 | + assert ( |
| 186 | + get_hamming_distance_index_2( |
| 187 | + sequence_1=sequence_1, sequence_2=sequence_2, is_reverse_complement=False |
| 188 | + ) |
| 189 | + == expected_distance |
| 190 | + ) |
| 191 | + assert ( |
| 192 | + get_hamming_distance_index_2( |
| 193 | + sequence_1=sequence_2, sequence_2=sequence_1, is_reverse_complement=False |
| 194 | + ) |
| 195 | + == expected_distance |
| 196 | + ) |
0 commit comments