From 0b19d817a7eeb6ac8f5b44ea5edfc50cd1982db3 Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Wed, 13 Dec 2023 11:55:29 +0100 Subject: [PATCH 1/5] Fix return type of function and add 2 tests --- tests/src/chanjo2/meta/test_d4.py | 23 --------- tests/src/chanjo2/meta/test_handle_d4.py | 61 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 23 deletions(-) delete mode 100644 tests/src/chanjo2/meta/test_d4.py create mode 100644 tests/src/chanjo2/meta/test_handle_d4.py diff --git a/tests/src/chanjo2/meta/test_d4.py b/tests/src/chanjo2/meta/test_d4.py deleted file mode 100644 index 913219c5..00000000 --- a/tests/src/chanjo2/meta/test_d4.py +++ /dev/null @@ -1,23 +0,0 @@ -from chanjo2.meta.handle_d4 import predict_sex -from chanjo2.models.pydantic_models import Sex - - -def test_predict_sex_male(): - """Test the function that computes sex from coverage data from coverage file of a male sample.""" - # GIVEN a coverage of chromosome Y which is roughly half of chromosome X - # THEN the predicted sex should be Male - assert predict_sex(x_cov=12.568, y_cov=6.605) == Sex.MALE - - -def test_predict_sex_female(): - """Test the function that computes sex from coverage data from coverage file of a female sample.""" - # GIVEN a coverage of chromosome Y almost null - # THEN the predicted sex should be female - assert predict_sex(x_cov=22.81, y_cov=0.007) == Sex.FEMALE - - -def test_predict_sex_unknown(): - """Test the function that computes sex from coverage when it is not possible to establish the sex.""" - # GIVEN a coverage of chromosome X equal to 0 - # THEN the predicted sex should be unknown - assert predict_sex(x_cov=0, y_cov=6.605) == Sex.UNKNOWN diff --git a/tests/src/chanjo2/meta/test_handle_d4.py b/tests/src/chanjo2/meta/test_handle_d4.py new file mode 100644 index 00000000..4a8d6978 --- /dev/null +++ b/tests/src/chanjo2/meta/test_handle_d4.py @@ -0,0 +1,61 @@ +from chanjo2.constants import DEFAULT_COMPLETENESS_LEVELS +from decimal import Decimal +from chanjo2.meta.handle_d4 import predict_sex +from chanjo2.models.pydantic_models import Sex +from chanjo2.meta.handle_d4 import get_intervals_completeness, get_d4_file +from pyd4 import D4File + + +def test_predict_sex_male(): + """Test the function that computes sex from coverage data from coverage file of a male sample.""" + # GIVEN a coverage of chromosome Y which is roughly half of chromosome X + # THEN the predicted sex should be Male + assert predict_sex(x_cov=12.568, y_cov=6.605) == Sex.MALE + + +def test_predict_sex_female(): + """Test the function that computes sex from coverage data from coverage file of a female sample.""" + # GIVEN a coverage of chromosome Y almost null + # THEN the predicted sex should be female + assert predict_sex(x_cov=22.81, y_cov=0.007) == Sex.FEMALE + + +def test_predict_sex_unknown(): + """Test the function that computes sex from coverage when it is not possible to establish the sex.""" + # GIVEN a coverage of chromosome X equal to 0 + # THEN the predicted sex should be unknown + assert predict_sex(x_cov=0, y_cov=6.605) == Sex.UNKNOWN + + +def test_get_intervals_completeness_no_thresholds(real_coverage_path, bed_interval): + """Test the get_intervals_completeness function when no coverage thresholds are specified.""" + # GIVEN a real d4 file + d4_file: D4File = get_d4_file(real_coverage_path) + + # WHEN the get_intervals_completeness is invoked without completeness_thresholds values + completeness_stats: Optional[dict] = get_intervals_completeness( + d4_file=d4_file, intervals=[bed_interval], completeness_thresholds=None + ) + + # THEN it should return None + assert completeness_stats is None + + +def test_get_intervals_completeness(real_coverage_path, bed_interval): + """Test the function that returns coverage completeness over a d4 file for a list of intervals and a list of coverage thresholds.""" + + # GIVEN a real d4 file + d4_file: D4File = get_d4_file(real_coverage_path) + + # WHEN the get_intervals_completeness is invoked with all expected parameters + completeness_stats: Dict[int, Decimal] = get_intervals_completeness( + d4_file=d4_file, + intervals=[bed_interval], + completeness_thresholds=DEFAULT_COMPLETENESS_LEVELS, + ) + # THEN it should return a dictionary + assert isinstance(completeness_stats, dict) + + # CONTAINING stats for all the provided coverage thresholds + for level in DEFAULT_COMPLETENESS_LEVELS: + assert isinstance(completeness_stats[level], Decimal) From 530ba07c7157b8764402d7fa79033108ccec165b Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Wed, 13 Dec 2023 11:58:24 +0100 Subject: [PATCH 2/5] Updated changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 372544af..b27bbc40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Upgraded Pydantic and Fastapi libraries and their dependencies - Use app lifespan instead of deprecated startup `on_event'. - Modified code to support upgraded libraries +- Rename a test file from `test_d4.py` to `test_handle_d4.py` +- Fix return type of `get_intervals_completeness` function ## [1.2] ### Added From 17564fc82fb2a7e9f5302fcee89da936b36d8a11 Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Wed, 13 Dec 2023 12:00:17 +0100 Subject: [PATCH 3/5] Forgot to add a file --- src/chanjo2/meta/handle_d4.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chanjo2/meta/handle_d4.py b/src/chanjo2/meta/handle_d4.py index b57f75c4..6fb59e0f 100644 --- a/src/chanjo2/meta/handle_d4.py +++ b/src/chanjo2/meta/handle_d4.py @@ -76,7 +76,7 @@ def get_intervals_completeness( d4_file: D4File, intervals: List[Tuple[str, int, int]], completeness_thresholds: Optional[List[int]], -) -> Optional[List[Tuple[int, Decimal]]]: +) -> Optional[Dict[int, Decimal]]: """Compute coverage completeness over threshold values for a list of intervals.""" if not completeness_thresholds: From cc8399426bbd9d2886311a1dfafc1d50e7406a0a Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Wed, 13 Dec 2023 12:02:41 +0100 Subject: [PATCH 4/5] Updated changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b27bbc40..23a9b6d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - Upgraded Pydantic and Fastapi libraries and their dependencies - Use app lifespan instead of deprecated startup `on_event'. - Modified code to support upgraded libraries -- Rename a test file from `test_d4.py` to `test_handle_d4.py` +- Rename a test file from `test_d4.py` to `test_handle_d4.py` and add 2 new tests to it - Fix return type of `get_intervals_completeness` function ## [1.2] From 8be45a63501b2a6cdc1fef7f29efce940dff6b3d Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Wed, 13 Dec 2023 12:12:29 +0100 Subject: [PATCH 5/5] No, it is actually a float --- src/chanjo2/meta/handle_d4.py | 3 +-- tests/src/chanjo2/meta/test_handle_d4.py | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/chanjo2/meta/handle_d4.py b/src/chanjo2/meta/handle_d4.py index 6fb59e0f..4964b25a 100644 --- a/src/chanjo2/meta/handle_d4.py +++ b/src/chanjo2/meta/handle_d4.py @@ -1,5 +1,4 @@ import logging -from decimal import Decimal from statistics import mean from typing import List, Optional, Tuple, Union, Dict @@ -76,7 +75,7 @@ def get_intervals_completeness( d4_file: D4File, intervals: List[Tuple[str, int, int]], completeness_thresholds: Optional[List[int]], -) -> Optional[Dict[int, Decimal]]: +) -> Optional[Dict[int, float]]: """Compute coverage completeness over threshold values for a list of intervals.""" if not completeness_thresholds: diff --git a/tests/src/chanjo2/meta/test_handle_d4.py b/tests/src/chanjo2/meta/test_handle_d4.py index 4a8d6978..055d0d2d 100644 --- a/tests/src/chanjo2/meta/test_handle_d4.py +++ b/tests/src/chanjo2/meta/test_handle_d4.py @@ -1,5 +1,4 @@ from chanjo2.constants import DEFAULT_COMPLETENESS_LEVELS -from decimal import Decimal from chanjo2.meta.handle_d4 import predict_sex from chanjo2.models.pydantic_models import Sex from chanjo2.meta.handle_d4 import get_intervals_completeness, get_d4_file @@ -58,4 +57,6 @@ def test_get_intervals_completeness(real_coverage_path, bed_interval): # CONTAINING stats for all the provided coverage thresholds for level in DEFAULT_COMPLETENESS_LEVELS: - assert isinstance(completeness_stats[level], Decimal) + assert completeness_stats[level] == 0 or isinstance( + completeness_stats[level], float + )