|
| 1 | +"""Tests for the VEP utility module.""" |
| 2 | + |
| 3 | +import hail as hl |
| 4 | +import pytest |
| 5 | + |
| 6 | +from gnomad.utils.vep import ( |
| 7 | + get_loftee_end_trunc_filter_expr, |
| 8 | + update_loftee_end_trunc_filter, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class TestGetLofteeEndTruncFilterExpr: |
| 13 | + """Test the get_loftee_end_trunc_filter_expr function.""" |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def sample_csq_structs(self): |
| 17 | + """Fixture to create sample consequence structs with different LOFTEE annotations.""" |
| 18 | + return [ |
| 19 | + # Case 1: GERP_DIST < 0, 50_BP_RULE != PASS -> should be True for default. |
| 20 | + hl.Struct(lof_info="GERP_DIST:-2.5,50_BP_RULE:FAIL,OTHER:value"), |
| 21 | + # Case 2: GERP_DIST >= 0, 50_BP_RULE != PASS -> should be False for default. |
| 22 | + hl.Struct(lof_info="GERP_DIST:1.5,50_BP_RULE:FAIL,OTHER:value"), |
| 23 | + # Case 3: GERP_DIST < 0, 50_BP_RULE = PASS -> should be False for default. |
| 24 | + hl.Struct(lof_info="GERP_DIST:-1.0,50_BP_RULE:PASS,OTHER:value"), |
| 25 | + # Case 4: GERP_DIST >= 0, 50_BP_RULE = PASS -> should be False for default. |
| 26 | + hl.Struct(lof_info="GERP_DIST:0.5,50_BP_RULE:PASS,OTHER:value"), |
| 27 | + # Case 5: GERP_DIST >= 0, 50_BP_RULE != PASS -> should be False for default. |
| 28 | + hl.Struct(lof_info="GERP_DIST:0.5,50_BP_RULE:FAIL,OTHER:value"), |
| 29 | + # Case 6: Missing GERP_DIST (defaults to 0), 50_BP_RULE != PASS -> should |
| 30 | + # be False for default. |
| 31 | + hl.Struct(lof_info="50_BP_RULE:FAIL,OTHER:value"), |
| 32 | + # Case 7: GERP_DIST < 0, missing 50_BP_RULE (defaults to empty) -> should |
| 33 | + # be True for default. |
| 34 | + hl.Struct(lof_info="GERP_DIST:-1.5,OTHER:value"), |
| 35 | + # Case 8: Empty lof_info -> should be False for default. |
| 36 | + hl.Struct(lof_info=""), |
| 37 | + ] |
| 38 | + |
| 39 | + def test_default_cutoff(self, sample_csq_structs): |
| 40 | + """Test the function with default cutoff of 0.0.""" |
| 41 | + ht = hl.Table.parallelize( |
| 42 | + [{"csq": csq} for csq in sample_csq_structs], |
| 43 | + hl.tstruct(csq=hl.tstruct(lof_info=hl.tstr)), |
| 44 | + ) |
| 45 | + |
| 46 | + # Apply the function. |
| 47 | + ht = ht.annotate(end_trunc=get_loftee_end_trunc_filter_expr(ht.csq)) |
| 48 | + |
| 49 | + # Collect results |
| 50 | + results = ht.collect() |
| 51 | + |
| 52 | + # Expected results for default cutoff (0.0). |
| 53 | + expected = [True, False, False, False, False, False, True, False] |
| 54 | + |
| 55 | + assert [r.end_trunc for r in results] == expected |
| 56 | + |
| 57 | + def test_custom_cutoff_positive(self, sample_csq_structs): |
| 58 | + """Test the function with a positive cutoff.""" |
| 59 | + ht = hl.Table.parallelize( |
| 60 | + [{"csq": csq} for csq in sample_csq_structs], |
| 61 | + hl.tstruct(csq=hl.tstruct(lof_info=hl.tstr)), |
| 62 | + ) |
| 63 | + |
| 64 | + # Apply the function with cutoff 1.0. |
| 65 | + ht = ht.annotate( |
| 66 | + end_trunc=get_loftee_end_trunc_filter_expr(ht.csq, gerp_dist_cutoff=1.0) |
| 67 | + ) |
| 68 | + |
| 69 | + # Collect results |
| 70 | + results = ht.collect() |
| 71 | + |
| 72 | + # Expected results for cutoff 1.0. |
| 73 | + expected = [True, False, False, False, True, True, True, True] |
| 74 | + |
| 75 | + assert [r.end_trunc for r in results] == expected |
| 76 | + |
| 77 | + def test_custom_cutoff_negative(self, sample_csq_structs): |
| 78 | + """Test the function with a negative cutoff.""" |
| 79 | + ht = hl.Table.parallelize( |
| 80 | + [{"csq": csq} for csq in sample_csq_structs], |
| 81 | + hl.tstruct(csq=hl.tstruct(lof_info=hl.tstr)), |
| 82 | + ) |
| 83 | + |
| 84 | + # Apply the function with cutoff -1.0. |
| 85 | + ht = ht.annotate( |
| 86 | + end_trunc=get_loftee_end_trunc_filter_expr(ht.csq, gerp_dist_cutoff=-1.0) |
| 87 | + ) |
| 88 | + |
| 89 | + # Collect results |
| 90 | + results = ht.collect() |
| 91 | + |
| 92 | + # Expected results for cutoff -1.0. |
| 93 | + expected = [True, False, False, False, False, False, True, False] |
| 94 | + |
| 95 | + assert [r.end_trunc for r in results] == expected |
| 96 | + |
| 97 | + |
| 98 | +class TestUpdateLofteeEndTruncFilter: |
| 99 | + """Test the update_loftee_end_trunc_filter function.""" |
| 100 | + |
| 101 | + @pytest.fixture |
| 102 | + def sample_csq_structs_with_filters(self): |
| 103 | + """Fixture to create sample consequence structs with lof_filter and lof annotations.""" |
| 104 | + return [ |
| 105 | + # Case 1: Should add END_TRUNC filter. |
| 106 | + hl.Struct( |
| 107 | + lof_info="GERP_DIST:-2.5,50_BP_RULE:FAIL", |
| 108 | + lof_filter="SINGLE_EXON", |
| 109 | + lof="HC", |
| 110 | + ), |
| 111 | + # Case 2: Should not add END_TRUNC filter. |
| 112 | + hl.Struct( |
| 113 | + lof_info="GERP_DIST:1.5,50_BP_RULE:PASS", |
| 114 | + lof_filter="SINGLE_EXON", |
| 115 | + lof="HC", |
| 116 | + ), |
| 117 | + # Case 3: Should remove existing END_TRUNC filter. |
| 118 | + hl.Struct( |
| 119 | + lof_info="GERP_DIST:1.0,50_BP_RULE:PASS", |
| 120 | + lof_filter="SINGLE_EXON,END_TRUNC", |
| 121 | + lof="LC", |
| 122 | + ), |
| 123 | + # Case 4: Should add END_TRUNC. |
| 124 | + hl.Struct( |
| 125 | + lof_info="GERP_DIST:-1.0,50_BP_RULE:FAIL", lof_filter="", lof="HC" |
| 126 | + ), |
| 127 | + # Case 5: Missing lof_filter. |
| 128 | + hl.Struct( |
| 129 | + lof_info="GERP_DIST:-1.5,50_BP_RULE:FAIL", lof_filter=None, lof="HC" |
| 130 | + ), |
| 131 | + ] |
| 132 | + |
| 133 | + def test_update_single_struct(self, sample_csq_structs_with_filters): |
| 134 | + """Test updating a single consequence struct.""" |
| 135 | + ht = hl.Table.parallelize( |
| 136 | + [{"csq": csq} for csq in sample_csq_structs_with_filters], |
| 137 | + hl.tstruct( |
| 138 | + csq=hl.tstruct(lof_info=hl.tstr, lof_filter=hl.tstr, lof=hl.tstr) |
| 139 | + ), |
| 140 | + ) |
| 141 | + |
| 142 | + # Apply the function. |
| 143 | + ht = ht.annotate(updated_csq=update_loftee_end_trunc_filter(ht.csq)) |
| 144 | + |
| 145 | + # Collect results. |
| 146 | + results = ht.collect() |
| 147 | + |
| 148 | + # Check results. |
| 149 | + assert results[0].updated_csq.lof_filter == "END_TRUNC,SINGLE_EXON" |
| 150 | + assert results[0].updated_csq.lof == "LC" |
| 151 | + |
| 152 | + # Still LC because filter is not empty. |
| 153 | + assert results[1].updated_csq.lof_filter == "SINGLE_EXON" |
| 154 | + assert results[1].updated_csq.lof == "LC" |
| 155 | + |
| 156 | + # Still LC because filter is not empty. |
| 157 | + assert results[2].updated_csq.lof_filter == "SINGLE_EXON" |
| 158 | + assert results[2].updated_csq.lof == "LC" |
| 159 | + |
| 160 | + assert results[3].updated_csq.lof_filter == "END_TRUNC" |
| 161 | + assert results[3].updated_csq.lof == "LC" |
| 162 | + |
| 163 | + assert results[4].updated_csq.lof_filter == "END_TRUNC" |
| 164 | + assert results[4].updated_csq.lof == "LC" |
| 165 | + |
| 166 | + def test_update_array_of_structs(self, sample_csq_structs_with_filters): |
| 167 | + """Test updating an array of consequence structs.""" |
| 168 | + # Create a table with arrays of consequences. |
| 169 | + ht = hl.Table.parallelize( |
| 170 | + [ |
| 171 | + {"csqs": sample_csq_structs_with_filters[:2]}, |
| 172 | + {"csqs": sample_csq_structs_with_filters[2:]}, |
| 173 | + ], |
| 174 | + hl.tstruct( |
| 175 | + csqs=hl.tarray( |
| 176 | + hl.tstruct(lof_info=hl.tstr, lof_filter=hl.tstr, lof=hl.tstr) |
| 177 | + ) |
| 178 | + ), |
| 179 | + ) |
| 180 | + |
| 181 | + # Apply the function. |
| 182 | + ht = ht.annotate(updated_csqs=update_loftee_end_trunc_filter(ht.csqs)) |
| 183 | + |
| 184 | + # Collect results. |
| 185 | + results = ht.collect() |
| 186 | + |
| 187 | + # Check first array. |
| 188 | + first_array = results[0].updated_csqs |
| 189 | + assert first_array[0].lof_filter == "END_TRUNC,SINGLE_EXON" |
| 190 | + assert first_array[0].lof == "LC" |
| 191 | + assert first_array[1].lof_filter == "SINGLE_EXON" |
| 192 | + assert first_array[1].lof == "LC" |
| 193 | + |
| 194 | + # Check second array. |
| 195 | + second_array = results[1].updated_csqs |
| 196 | + assert second_array[0].lof_filter == "SINGLE_EXON" |
| 197 | + assert second_array[0].lof == "LC" |
| 198 | + assert second_array[1].lof_filter == "END_TRUNC" |
| 199 | + assert second_array[1].lof == "LC" |
| 200 | + assert second_array[2].lof_filter == "END_TRUNC" |
| 201 | + assert second_array[2].lof == "LC" |
| 202 | + |
| 203 | + def test_missing_lof_annotation(self): |
| 204 | + """Test updating when lof annotation is missing.""" |
| 205 | + csq_with_missing_lof = hl.Struct( |
| 206 | + lof_info="GERP_DIST:-2.5,50_BP_RULE:FAIL", |
| 207 | + lof_filter="SINGLE_EXON", |
| 208 | + lof=None, |
| 209 | + ) |
| 210 | + |
| 211 | + ht = hl.Table.parallelize( |
| 212 | + [{"csq": csq_with_missing_lof}], |
| 213 | + hl.tstruct( |
| 214 | + csq=hl.tstruct(lof_info=hl.tstr, lof_filter=hl.tstr, lof=hl.tstr) |
| 215 | + ), |
| 216 | + ) |
| 217 | + |
| 218 | + # Apply the function. |
| 219 | + ht = ht.annotate(updated_csq=update_loftee_end_trunc_filter(ht.csq)) |
| 220 | + |
| 221 | + # Collect results. |
| 222 | + results = ht.collect() |
| 223 | + |
| 224 | + # This case shouldn't happen. If lof_filter is defined, lof should be defined |
| 225 | + # too. However, we should handle it gracefully by adding END_TRUNC, but |
| 226 | + # maintaining the lof missingness status. |
| 227 | + assert results[0].updated_csq.lof_filter == "END_TRUNC,SINGLE_EXON" |
| 228 | + assert results[0].updated_csq.lof is None |
| 229 | + |
| 230 | + def test_empty_filter_handling(self): |
| 231 | + """Test handling of empty and None filters.""" |
| 232 | + test_cases = [ |
| 233 | + hl.Struct( |
| 234 | + lof_info="GERP_DIST:-2.5,50_BP_RULE:FAIL", lof_filter="", lof="HC" |
| 235 | + ), |
| 236 | + hl.Struct( |
| 237 | + lof_info="GERP_DIST:-2.5,50_BP_RULE:FAIL", lof_filter=None, lof="HC" |
| 238 | + ), |
| 239 | + ] |
| 240 | + |
| 241 | + ht = hl.Table.parallelize( |
| 242 | + [{"csq": csq} for csq in test_cases], |
| 243 | + hl.tstruct( |
| 244 | + csq=hl.tstruct(lof_info=hl.tstr, lof_filter=hl.tstr, lof=hl.tstr) |
| 245 | + ), |
| 246 | + ) |
| 247 | + |
| 248 | + # Apply the function. |
| 249 | + ht = ht.annotate(updated_csq=update_loftee_end_trunc_filter(ht.csq)) |
| 250 | + |
| 251 | + # Collect results. |
| 252 | + results = ht.collect() |
| 253 | + |
| 254 | + assert results[0].updated_csq.lof_filter == "END_TRUNC" |
| 255 | + assert results[0].updated_csq.lof == "LC" |
| 256 | + |
| 257 | + assert results[1].updated_csq.lof_filter == "END_TRUNC" |
| 258 | + assert results[1].updated_csq.lof == "LC" |
0 commit comments