Skip to content

Commit

Permalink
fix: Fix PMD command handle errors and output csv
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianMichelsen committed Aug 10, 2022
1 parent 9700d88 commit 84c002f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
62 changes: 62 additions & 0 deletions src/metaDMG/PMD.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#

import shlex
import subprocess
from pathlib import Path

import pandas as pd


#%%


def iterate_command(command: str):

p = subprocess.Popen(
shlex.split(command),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
for line in iter(p.stdout.readline, b""): # type: ignore
if line:
line = line.decode("utf-8")
if line.endswith("\n"):
line = line[:-1]
yield line

# waits for the process to finish and returns the returncode
yield p.wait()


def compute_PMDs(bam_file: Path, metaDMG_cpp: str):
"""Run the PMD command from metaDMG-cpp and get a DataFrame with the results.
Parameters
----------
bam_file
Alignment file to compute the PMD scores on
metaDMG_cpp
The metaDMG binary to use
"""

command = f"{metaDMG_cpp} pmd {bam_file}"

returncode = 1
reads = []
PMDs = []
for i, line in enumerate(iterate_command(command)):

# if finished, check returncode
if isinstance(line, int):
returncode = line

else:
read, PMD = line.split("\tPMD:")
reads.append(read)
PMDs.append(float(PMD))

if returncode != 0:
raise Exception(f"Error running {command}")

df = pd.DataFrame({"read": reads, "PMD": PMDs})
return df
30 changes: 22 additions & 8 deletions src/metaDMG/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,20 +618,34 @@ def mismatch_to_mapDamage(


@cli_app.command("PMD", rich_help_panel="Utils")
def compute_PMD(
config_file: Path = typer.Argument(
def PMD(
filename: Path = typer.Argument(
...,
file_okay=True,
help="Path to the config-file.",
help="Path to the config-file or a single BAM file.",
),
csv_out: Path = typer.Option(
...,
"--output",
"-o",
file_okay=True,
help="Output CSV file.",
),
metaDMG_cpp: str = typer.Option(
"./metaDMG-cpp",
"--metaDMG-cpp",
"-m",
help="The command needed to run the metaDMG-cpp program.",
),
):
"""Compute the PMD scores for the samples in the config file."""
"""Compute the PMD scores for the chosen BAM file and save to csv."""

from metaDMG.PMD import compute_PMDs

from metaDMG.utils import make_configs, run_PMD
df_PMDs = compute_PMDs(filename, metaDMG_cpp)

configs = make_configs(config_file)
for config in configs:
run_PMD(config)
csv_out.parent.mkdir(parents=True, exist_ok=True)
df_PMDs.to_csv(csv_out, index=False)


#%%
Expand Down

0 comments on commit 84c002f

Please sign in to comment.