Skip to content

Commit

Permalink
add version check between ppanggolin file and the installed ppanggolin
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanMainguy committed Dec 21, 2023
1 parent f5eadc7 commit ff80fb1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ppanggolin/formats/readBinaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ def get_status(pangenome: Pangenome, pangenome_file: Path):
pangenome.status["geneFamilySequences"] = "inFile"
if status_group._v_attrs.NeighborsGraph:
pangenome.status["neighborsGraph"] = "inFile"

if hasattr(status_group._v_attrs, "version"):
pangenome.status["ppanggolin_version"] = str(status_group._v_attrs.version)
else:
logging.getLogger("PPanGGOLiN").error(f'The provided pangenome file {pangenome_file} does not have a version stored in its status.'
' This issue may indicate that the file is corrupted.')
pangenome.status["ppanggolin_version"] = None

if status_group._v_attrs.Partitioned:
pangenome.status["partitioned"] = "inFile"
Expand Down
12 changes: 9 additions & 3 deletions ppanggolin/pangenome.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,25 @@ def __init__(self):
}
self.parameters = {}

def add_file(self, pangenome_file: Path):
"""Links an HDF5 file to the pangenome. If needed elements will be loaded from this file,
def add_file(self, pangenome_file: Path, check_version:bool=True):
"""
Links an HDF5 file to the pangenome. If needed elements will be loaded from this file,
and anything that is computed will be saved to this file when
:func:`ppanggolin.formats.writeBinaries.writePangenome` is called.
:param pangenome_file: A string representing filepath to hdf5 pangenome file to be either used or created
:param check_version: Check ppanggolin version of the pangenome file to be compatible with the current version of ppaggolin being used.
:raises AssertionError: If the `pangenome_file` is not an instance of the Path class
"""
assert isinstance(pangenome_file, Path), "pangenome file should be a Path object type"
from ppanggolin.formats.readBinaries import get_status
from ppanggolin.utils import check_version_compatibility
# importing on call instead of importing on top to avoid cross-reference problems.

get_status(self, pangenome_file)

check_version_compatibility(self.status["ppanggolin_version"])

self.file = pangenome_file.absolute().as_posix()

""" Gene Methods"""
Expand Down
38 changes: 38 additions & 0 deletions ppanggolin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,41 @@ def flatten(dictionary, parent_key=''):

flatten(nested_dict)
return flat_dict

def get_major_version(version: str) -> int:
"""
Extracts the major version number from a version string.
:param version: A string representing the version number.
:return: The major version extracted from the input version string.
:raises ValueError: If the input version does not have the expected format.
"""
try:
major_version = int(version.split('.')[0])
except ValueError:
raise ValueError(f"Version {version} does not have the expected format.")

return major_version


def check_version_compatibility(file_version: str) -> None:
"""
Checks the compatibility of the provided pangenome file version with the current PPanGGOLiN version.
:param file_version: A string representing the version of the pangenome file.
"""
# Get the current PPanGGOLiN version
current_version = distribution('ppanggolin').version

current_version_major = get_major_version(current_version)
file_major_version = get_major_version(file_version)

# Check for compatibility issues
if file_major_version != current_version_major:
logging.getLogger("PPanGGOLiN").error('Your pangenome file has been created with a different major version '
'of PPanGGOLiN than the one installed in the system. This mismatch may lead to compatibility issues.')

if file_major_version < 2 and current_version_major >= 2:
raise ValueError(f'The provided pangenome file was created by PPanGGOLiN version {file_version}, which is '
f'incompatible with the current PPanGGOLiN version {current_version}.')

0 comments on commit ff80fb1

Please sign in to comment.