Skip to content

Commit

Permalink
Increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
maltekuehl committed Jul 5, 2024
1 parent e51927f commit 626f297
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
27 changes: 22 additions & 5 deletions pytximport/core/_tximport.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def tximport(
output_type: Literal["xarray", "anndata"] = "anndata",
output_format: Literal["csv", "h5ad"] = "csv",
save_path: Optional[Union[str, Path]] = None,
save_path_override: bool = False,
return_data: bool = True,
biotype_filter: Optional[List[str]] = None,
) -> Union[xr.Dataset, ad.AnnData, None]:
Expand Down Expand Up @@ -93,6 +94,7 @@ def tximport(
output_type (Literal["xarray", "anndata"], optional): The type of output. Defaults to "anndata".
output_format (Literal["csv", "h5ad"], optional): The type of output file. Defaults to "csv".
save_path (Optional[Union[str, Path]], optional): The path to save the gene-level expression. Defaults to None.
save_path_override (bool, optional): Whether to override the save path if it already exists. Defaults to False.
return_data (bool, optional): Whether to return the gene-level expression. Defaults to True.
biotype_filter (List[str], optional): Filter the transcripts by biotype, including only those provided.
Defaults to None.
Expand Down Expand Up @@ -215,7 +217,6 @@ def tximport(

transcript_data: Optional[xr.Dataset] = None
file_paths_missing_idx: List[int] = []
inferential_replicates_data: Optional[xr.DataArray] = None

# iterate through the files
for file_idx, file_path in tqdm(enumerate(file_paths), desc="Reading quantification files"):
Expand Down Expand Up @@ -370,6 +371,17 @@ def tximport(
)
result_index = "gene_id"

if save_path is not None:
if not isinstance(save_path, Path):
save_path = Path(save_path)

if save_path.suffix == ".csv" and output_format == "h5ad":
warning(
"The file extension of the `save_path` is `.csv` but the output format is `.h5ad`. "
"Changing the output format to `.csv`."
)
output_format = "csv"

Check warning on line 383 in pytximport/core/_tximport.py

View check run for this annotation

Codecov / codecov/patch

pytximport/core/_tximport.py#L383

Added line #L383 was not covered by tests

if output_format == "h5ad" and output_type != "anndata":
warning("The output format is h5ad but the output type is not anndata. Changing the output type to anndata.")
output_type = "anndata"
Expand All @@ -396,8 +408,13 @@ def tximport(
)

if save_path is not None:
if not isinstance(save_path, Path):
save_path = Path(save_path)
if save_path.exists() and not save_path_override:
raise FileExistsError(
f"The file already exists: {save_path}. Set `save_path_override` to True to override."
)

if not save_path.parent.exists():
save_path.parent.mkdir(parents=True)

Check warning on line 417 in pytximport/core/_tximport.py

View check run for this annotation

Codecov / codecov/patch

pytximport/core/_tximport.py#L417

Added line #L417 was not covered by tests

log(25, f"Saving the gene-level expression to: {save_path}.")

Expand Down Expand Up @@ -425,8 +442,8 @@ def tximport(

df_gene_data = pd.DataFrame(
data=count_data,
index=result[result_index],
columns=result.coords["file_path"].values,
index=(result[result_index] if output_type != "anndata" else result.var.index),
columns=(result.coords["file_path"].values if output_type != "anndata" else result.obs.index),
)
df_gene_data.sort_index(inplace=True)
df_gene_data.to_csv(save_path, index=True, header=True, quoting=2)
Expand Down
49 changes: 49 additions & 0 deletions test/test_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Test the cli function."""

import os
from pathlib import Path
from time import time
from typing import List

from pytximport import tximport


def test_export(
salmon_multiple_files: List[Path],
transcript_gene_mapping_path_mouse: Path,
) -> None:
"""Test saving the output of the tximport function as a csv and h5ad file."""
if not os.access(os.getcwd(), os.W_OK):
raise PermissionError("The current directory cannot be written to.")

current_time = int(time())

_ = tximport(
salmon_multiple_files,
"salmon",
transcript_gene_mapping_path_mouse,
output_format="csv",
save_path=f"./pytximport_cli_test_{int(time())}.csv",
)

# check that the output file was created
assert os.path.exists(f"./pytximport_cli_test_{current_time}.csv"), "Output file was not created."

# remove the temporary file
os.system(f"rm ./pytximport_cli_test_{current_time}.csv") # nosec

# test saving as h5ad
_ = tximport(
salmon_multiple_files,
"salmon",
transcript_gene_mapping_path_mouse,
output_type="anndata",
output_format="h5ad",
save_path=f"./pytximport_cli_test_{current_time}.h5ad",
)

# check that the output file was created
assert os.path.exists(f"./pytximport_cli_test_{current_time}.h5ad"), "AnnData output file was not created."

# remove the temporary file
os.system(f"rm ./pytximport_cli_test_{current_time}.h5ad") # nosec

0 comments on commit 626f297

Please sign in to comment.