Skip to content

Commit b8627d6

Browse files
kirandharmar867-makerpeterjc
authored andcommitted
Run PSEA in a temporary directory to avoid leftover files
1 parent 6568a97 commit b8627d6

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

Bio/PDB/PSEA.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,40 @@
1818

1919
import os
2020
import subprocess
21+
import tempfile
22+
2123

2224
from Bio.PDB.Polypeptide import is_aa
2325

2426

2527
def run_psea(fname, verbose=False):
26-
"""Run PSEA and return output filename.
28+
"""Run PSEA and return output filename."""
29+
last = os.path.basename(fname)
30+
base = os.path.splitext(last)[0]
31+
cmd = ["psea", fname]
2732

28-
Note that this assumes the P-SEA binary is called "psea" and that it is
29-
on the path.
33+
curdir = os.getcwd()
3034

31-
Note that P-SEA will write an output file in the current directory using
32-
the input filename with extension ".sea".
35+
with tempfile.TemporaryDirectory() as tmpdir:
36+
os.chdir(tmpdir)
3337

34-
Note that P-SEA will not write output to the terminal while run unless
35-
verbose is set to True.
36-
"""
37-
last = fname.split("/")[-1]
38-
base = last.split(".")[0]
39-
cmd = ["psea", fname]
38+
p = subprocess.run(cmd, capture_output=True, text=True)
39+
40+
if verbose:
41+
print(p.stdout)
4042

41-
p = subprocess.run(cmd, capture_output=True, text=True)
43+
output = base + ".sea"
4244

43-
if verbose:
44-
print(p.stdout)
45+
if not p.stderr.strip() and os.path.exists(output):
46+
# move output back to original directory
47+
final_path = os.path.join(curdir, output)
48+
os.rename(output, final_path)
49+
os.chdir(curdir)
50+
return final_path
51+
else:
52+
os.chdir(curdir)
53+
raise RuntimeError(f"Error running p-sea: {p.stderr}")
4554

46-
if not p.stderr.strip() and os.path.exists(base + ".sea"):
47-
return base + ".sea"
48-
else:
49-
raise RuntimeError(f"Error running p-sea: {p.stderr}")
5055

5156

5257
def psea(pname):

0 commit comments

Comments
 (0)