Skip to content

Commit

Permalink
feat: add new CLI option --add-rank-lineage
Browse files Browse the repository at this point in the history
  • Loading branch information
Midnighter committed Aug 19, 2023
1 parent 512731c commit a6ef768
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/taxpasta/infrastructure/cli/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ def merge(
help="Add the taxon's entire lineage to the output. These are taxon "
"identifiers separated by semi-colons.",
),
rank_lineage: bool = typer.Option( # noqa: B008
False,
"--add-rank-lineage",
help="Add the taxon's entire rank lineage to the output. These are taxon "
"ranks separated by semi-colons.",
),
) -> None:
"""Standardise and merge two or more taxonomic profiles."""
# Perform input validation.
Expand Down Expand Up @@ -381,6 +387,14 @@ def merge(
)
raise typer.Exit(code=2)

if rank_lineage:
if taxonomy is None:
logger.critical(
"The '--add-rank-lineage' option requires a taxonomy. Please "
"provide one using the option '--taxonomy'."
)
raise typer.Exit(code=2)

# Ensure that we can write to the output directory.
try:
output.parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -433,6 +447,9 @@ def merge(

# The order of the following conditions is chosen specifically to yield a pleasant
# output format.
if rank_lineage and valid_output_format is not WideObservationTableFileFormat.BIOM:
assert taxonomy_service is not None # nosec assert_used
result = taxonomy_service.add_rank_lineage(result)

if id_lineage and valid_output_format is not WideObservationTableFileFormat.BIOM:
assert taxonomy_service is not None # nosec assert_used
Expand Down
20 changes: 20 additions & 0 deletions src/taxpasta/infrastructure/cli/standardise.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ def standardise(
help="Add the taxon's entire lineage to the output. These are taxon "
"identifiers separated by semi-colons.",
),
add_rank_lineage: bool = typer.Option( # noqa: B008
False,
"--add-rank-lineage",
help="Add the taxon's entire rank lineage to the output. These are taxon "
"ranks separated by semi-colons.",
),
) -> None:
"""Standardise a taxonomic profile."""
# Perform input validation.
Expand Down Expand Up @@ -213,6 +219,14 @@ def standardise(
)
raise typer.Exit(code=2)

if add_rank_lineage:
if taxonomy is None:
logger.critical(
"The '--add-rank-lineage' option requires a taxonomy. Please "
"provide one using the option '--taxonomy'."
)
raise typer.Exit(code=2)

# Ensure that we can write to the output directory.
try:
output.parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -240,6 +254,12 @@ def standardise(

# The order of the following conditions is chosen specifically to yield a pleasant
# output format.
if add_rank_lineage:
assert taxonomy_service is not None # nosec assert_used
result = Sample(
name=result.name,
profile=taxonomy_service.add_rank_lineage(result.profile),
)

if add_id_lineage:
assert taxonomy_service is not None # nosec assert_used
Expand Down

0 comments on commit a6ef768

Please sign in to comment.