Skip to content

Commit 2a2bd23

Browse files
Adding ValueErrors and corresponding tests
Now raises ValueError when input is not in the right format. Also added tests to check if an error is actually raised when input is not in the right format.
1 parent 7ba1918 commit 2a2bd23

5 files changed

Lines changed: 76 additions & 1 deletion

pelc/batch_eplet_comp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def compute_epletic_load(
5050
:return: None (if output_type is not None, the result will be saved on disk as a csv), or pandas.DataFrame
5151
(OutputType.COUNT_AND_DETAILS) or pandas.Series (OutputType.COUNT, or OutputType.ONLY_DETAILS) or
5252
tuple[pandas.DataFrame, pandas.DataFrame] (OutputType.FILTERED_TYPINGS)
53+
54+
:raises ValueError: if the number of unknown alleles is different for one donor and recipient pair or one allele is
55+
unknown whilst the other of the same locus isn't. Obviously doesn't raise anything if user
56+
inputs a homozygous like this: donor: A*01:01 A* and recipient: A*01:01 A*11:01.
5357
"""
5458
if not class_i and not class_ii:
5559
logging.error(
@@ -74,7 +78,10 @@ def compute_epletic_load(
7478
"Either the number of unknown alleles is different for one donor and recipient pair or one allele is "
7579
"unknown whilst the other of the same locus isn't."
7680
)
77-
return None
81+
raise ValueError(
82+
"Either the number of unknown alleles is different for one donor and recipient pair or one allele is "
83+
"unknown whilst the other of the same locus isn't."
84+
)
7885

7986

8087
df_a: pd.DataFrame
9.43 KB
Binary file not shown.
9.43 KB
Binary file not shown.
9.29 KB
Binary file not shown.

tests/test_only_one_chromosome.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import os
2+
import pandas as pd
3+
import pytest
4+
5+
from pelc.batch_eplet_comp import compute_epletic_load
6+
from pelc.output_type import OutputType
7+
from tests.base_loading_for_tests import base_loading
8+
9+
def test_one_chromosome() -> None:
10+
"""
11+
Test the epletic load calculation with only one chromosome. If only one chromosome is present, it has to be
12+
the first one, and it will mean it's a homozygote. Otherwise, _equal_amount_of_unknown_alleles will return False
13+
and compute_epletic_load will return None. No file will then be created.
14+
:return: None
15+
"""
16+
17+
# OK
18+
donordf, recipientdf, output_path = base_loading("pytest_only_one_chromosome_ok.xlsx", "Sheet 1")
19+
20+
for class_1 in False, True:
21+
compute_epletic_load(
22+
donordf,
23+
recipientdf,
24+
output_path,
25+
OutputType.DETAILS_AND_COUNT,
26+
class_1, # class_i
27+
True, # class_ii
28+
False, # abv_only
29+
)
30+
31+
output_df: pd.DataFrame = pd.read_csv(f"{output_path}.csv", index_col="Index")
32+
33+
assert output_df.at[8, "EpMismatches"] == "160S_DQ"
34+
assert output_df.at[8, "Eplet Load"] == 1
35+
36+
os.remove(f"{output_path}.csv")
37+
38+
# NOT OK
39+
donordf, recipientdf, output_path = base_loading("pytest_only_one_chromosome_not_ok.xlsx", "Sheet 1")
40+
41+
for class_1 in False, True:
42+
# Make sure this raises a ValueError
43+
with pytest.raises(ValueError):
44+
compute_epletic_load(
45+
donordf,
46+
recipientdf,
47+
output_path,
48+
OutputType.DETAILS_AND_COUNT,
49+
class_1, # class_i
50+
True, # class_ii
51+
False, # abv_only
52+
)
53+
54+
# NOT OK2
55+
donordf, recipientdf, output_path = base_loading("pytest_only_one_chromosome_not_ok_2.xlsx", "Sheet 1")
56+
57+
for class_1 in False, True:
58+
# Make sure this raises a ValueError
59+
with pytest.raises(ValueError):
60+
compute_epletic_load(
61+
donordf,
62+
recipientdf,
63+
output_path,
64+
OutputType.DETAILS_AND_COUNT,
65+
class_1, # class_i
66+
True, # class_ii
67+
False, # abv_only
68+
)

0 commit comments

Comments
 (0)