Skip to content

Commit

Permalink
Improve logging error subprocess handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jpjarnoux committed Jun 6, 2024
1 parent eb732be commit 9e7a5e2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
3 changes: 1 addition & 2 deletions ppanggolin/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ def align_rep(faa_file: Path, tmpdir: Path, cpu: int = 1, coverage: float = 0.8,
:return: Result of alignment
"""
seqdb = create_mmseqs_db(faa_file, 'rep_sequence_db', tmpdir, db_mode=1, db_type=1)
cmd = list(map(str, ["mmseqs", "createdb", "--createdb-mode", 1, faa_file, seqdb]))
seqdb = create_mmseqs_db([faa_file], 'rep_sequence_db', tmpdir, db_mode=1, db_type=1)
logging.getLogger("PPanGGOLiN").info("Aligning cluster representatives...")
alndb = tmpdir / 'rep_alignment_db'
cmd = list(map(str, ["mmseqs", "search", seqdb, seqdb, alndb, tmpdir, "-a", "--min-seq-id", identity,
Expand Down
18 changes: 9 additions & 9 deletions ppanggolin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,13 +1187,13 @@ def get_consecutive_region_positions(region_positions: List[int], contig_gene_co

def run_subprocess(cmd: List[str], output: Path = None, msg: str = "Subprocess failed with the following error:\n"):
logging.getLogger("PPanGGOLiN").debug(" ".join(cmd))
if output is None:
result = subprocess.run(cmd, stdout=subprocess.DEVNULL, check=True)
try:
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
except subprocess.CalledProcessError as subprocess_err:
for line in subprocess_err.stdout.split("\n"):
logging.getLogger("PPanGGOLiN").error(line)
raise Exception(msg + subprocess_err.stderr)
else:
result = subprocess.run(cmd, capture_output=True, check=True)

with open(output, "w") as fout:
fout.write(result.stdout.decode('utf-8'))

if result.stderr and result.returncode != 0:
raise Exception(msg + f"{result.stderr.decode('utf-8')}")
if output is not None:
with open(output, 'w') as fout:
fout.write(result.stdout)

0 comments on commit 9e7a5e2

Please sign in to comment.