Skip to content

Commit

Permalink
add unit test for [Sequence] AnthonyNolan filter (#243)
Browse files Browse the repository at this point in the history
* move MSF files into the unit tests data area

* restore the associated .pop file and .ini file for the `sequence-nopoptests.ini`
  • Loading branch information
alexlancaster committed Jan 18, 2025
1 parent e416de2 commit e15d70c
Show file tree
Hide file tree
Showing 76 changed files with 54 additions and 10 deletions.
41 changes: 35 additions & 6 deletions src/PyPop/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

"""Python population genetics statistics."""

import os
import sys
import time
from configparser import ConfigParser, NoOptionError, NoSectionError
Expand Down Expand Up @@ -624,13 +625,41 @@ def _runFilters(self):
except Exception:
sequenceFileSuffix = "_prot"
try:
anthonynolanPath = self.config.get(filterCall, "directory")
except Exception:
anthonynolanPath = Path(self.datapath) / "anthonynolan" / "msf"
if self.debug:
print(
f"LOG: Defaulting to system datapath {anthonynolanPath} for anthonynolanPath data"
path_obj = Path(self.config.get(filterCall, "directory"))

# if the path is relative, resolve it to an absolute path if it exists
if not path_obj.is_absolute():
if path_obj.exists() and path_obj.is_dir():
path_obj = path_obj.resolve()
elif os.environ.get(
"CURRENT_TEST_DIRECTORY"
) and os.environ.get("PYTEST_VERSION"):
# if we're running in a test environment, resolve paths relative to the parent of the "tests" directory
path_obj = (
Path(os.environ.get("CURRENT_TEST_DIRECTORY")).parent
/ path_obj
)
else:
sys.exit(
f"Relative path {path_obj} for AnthonyNolan sequence files does not exist or is not a directory."
)

# at this point, the path is absolute, now we need to check it exits
if path_obj.exists() and path_obj.is_dir():
anthonynolanPath = str(path_obj)
if self.debug:
print(
f"Using {anthonynolanPath} for AnthonyNolan data files"
)
else:
sys.exit(
f"Absolute path {path_obj} for Anthony Nolan sequence files does not exist or is not a directory"
)

except Exception:
sys.exit(
"Need to provide a path to the Anthony Nolan sequence files: no default"
)
try:
sequenceFilterMethod = self.config.get(
filterCall, "sequenceConsensusMethod"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,5 @@ validSampleFields=+populat
filtersToApply=Sequence

[Sequence]
;;alleleFileFormat=msf
;; alternative location of msf sequence alignment files (available
;; from ftp://ftp.ebi.ac.uk/pub/databases/imgt/mhc/hla/
directory=/home/alex/ihwg/src/data/anthonynolan/msf/
# relative location of msf sequence alignment files (available from ftp://ftp.ebi.ac.uk/pub/databases/imgt/mhc/hla/)
directory=tests/data/anthonynolan/msf/
17 changes: 17 additions & 0 deletions tests/test_Filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
from pathlib import Path

from base import (
DEFAULT_GOLD_OUTPUT_DIR,
abspath_test_data,
Expand Down Expand Up @@ -87,3 +90,17 @@ def test_Filters_CustomBinning_P_Filter():
DEFAULT_GOLD_OUTPUT_DIR / "custom-binning-P-Filter" / out_filename
)
assert filecmp_ignore_newlines(out_filename, gold_out_filename)


def test_Filters_Sequence_AnthonyNolan():
# FIXME: a bit hacky
# set an environment variable for the current test directory
current_dir = Path(__file__).parent # get the current test script directory
os.environ["CURRENT_TEST_DIRECTORY"] = str(current_dir)

exit_code = run_pypop_process(
"./tests/data/sequence-nopoptests.ini",
"./tests/data/USAFEL-UchiTelle.pop",
)
# check exit code
assert exit_code == 0

0 comments on commit e15d70c

Please sign in to comment.