|
18 | 18 |
|
19 | 19 | import os |
20 | 20 | import subprocess |
| 21 | +import tempfile |
| 22 | + |
21 | 23 |
|
22 | 24 | from Bio.PDB.Polypeptide import is_aa |
23 | 25 |
|
24 | 26 |
|
25 | 27 | 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] |
27 | 32 |
|
28 | | - Note that this assumes the P-SEA binary is called "psea" and that it is |
29 | | - on the path. |
| 33 | + curdir = os.getcwd() |
30 | 34 |
|
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) |
33 | 37 |
|
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) |
40 | 42 |
|
41 | | - p = subprocess.run(cmd, capture_output=True, text=True) |
| 43 | + output = base + ".sea" |
42 | 44 |
|
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}") |
45 | 54 |
|
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}") |
50 | 55 |
|
51 | 56 |
|
52 | 57 | def psea(pname): |
|
0 commit comments