Skip to content

Commit

Permalink
fix: Fix issue when metaDMG binary does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianMichelsen committed Sep 21, 2022
1 parent 3bce314 commit 3c949be
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/metaDMG/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,23 @@ def create_config(
file_okay=True,
help="Path to the (NCBI) names-mdmg.dmp.",
rich_help_panel="LCA parameters",
callback=cli_utils.path_exists_or_None,
),
nodes: Optional[Path] = typer.Option(
None,
exists=True,
file_okay=True,
help="Path to the (NCBI) nodes-mdmg.dmp.",
rich_help_panel="LCA parameters",
callback=cli_utils.path_exists_or_None,
),
acc2tax: Optional[Path] = typer.Option(
None,
exists=True,
file_okay=True,
help="Path to the (NCBI) acc2tax.gz.",
rich_help_panel="LCA parameters",
callback=cli_utils.path_exists_or_None,
),
min_similarity_score: Optional[float] = typer.Option(
None,
Expand Down Expand Up @@ -135,6 +138,7 @@ def create_config(
"-m",
help="The command needed to run the metaDMG-cpp program.",
rich_help_panel="General parameters",
callback=cli_utils.path_exists_or_None,
),
max_position: int = typer.Option(
15,
Expand Down
38 changes: 35 additions & 3 deletions src/metaDMG/cli/cli_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum
from functools import partial
from pathlib import Path
from typing import Iterable, Optional
from typing import Iterable, Optional, Union

import click
import typer
Expand Down Expand Up @@ -64,7 +64,7 @@ def is_in_range_or_None(
"""

if x is None:
return x
return None

if x < val_min or val_max < x:
raise typer.BadParameter(
Expand Down Expand Up @@ -92,14 +92,46 @@ def is_positive_int_or_None(x: Optional[int]) -> Optional[int]:
"""

if x is None:
return x
return None

if x < 0:
raise typer.BadParameter(f"x has to be positive. Got: {x}")

return x


def path_exists_or_None(x: Union[str, Path, None]) -> Union[str, Path, None]:
"""Confirms that x exists or is None
Parameters
----------
x
Value to check
Returns
-------
Confirmed value
Raises
------
typer.BadParameter
If x is outside bounds
"""

if x is None:
return None

if isinstance(x, str):
path = Path(x)
else:
path = x

if not path.is_file():
raise typer.BadParameter(f"The file {path} does not exists.")

return x


#%%
class RANKS(str, Enum):
"Ranks allowed in the LCA"
Expand Down
21 changes: 21 additions & 0 deletions src/metaDMG/fit/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ def run_LCA(config: Config, force: bool = False) -> None:
f"{config['sample']}: The alignment file is invalid."
)

if not metaDMG_cpp_is_valid(config):
raise metadamageError()

config["metaDMG_cpp"]

logger.info(f"LCA has to be computed. This can take a while, please wait.")

command_LCA = get_LCA_command(config)
Expand Down Expand Up @@ -332,6 +337,9 @@ def run_damage_no_lca(config: Config, force: bool = False) -> None:
f"{config['sample']}: The alignment file is invalid."
)

if not metaDMG_cpp_is_valid(config):
raise metadamageError()

logger.info(f"Computing damage. NOTE: NO LCA.")

command_damage = get_damage_command(config)
Expand Down Expand Up @@ -491,6 +499,19 @@ def BAM_file_is_valid(config: Config) -> bool:
return True


def metaDMG_cpp_is_valid(config: Config) -> bool:

if not Path(config["metaDMG_cpp"]).is_file():
logger.error(f"The metaDMG-cpp binary does not exist: {config['metaDMG_cpp']}.")
return False

if config["bam"].stat().st_size == 0:
logger.error(f"The metaDMG-cpp binary is of size 0: {config['metaDMG_cpp']}.")
return False

return True


#%%


Expand Down

0 comments on commit 3c949be

Please sign in to comment.