File size: 2,130 Bytes
9b9c66d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import subprocess
import os
from typing import List, Optional
from pathlib import Path
import yaml
class USalignRunner:
def __init__(self, config_path: str = "config.yaml"):
"""
Initialize USalignRunner with parameters from config file.
Args:
config_path (str): Path to the configuration file
"""
with open(config_path, 'r',encoding="utf-8") as f:
config = yaml.safe_load(f)
self.usalign_path = Path(config['USalign']['path'])
self.default_params = {
'tmscore': config['USalign']['tmscore'],
'outfmt': config['USalign']['outfmt'],
'mol': 'protein' # Default to protein alignment
}
if not self.usalign_path.exists():
raise FileNotFoundError(f"USalign executable not found at {self.usalign_path}")
def run_alignment(
self,
target_dir: str,
pdb_list_file: str,
tmscore: Optional[float] = None,
outfmt: Optional[int] = None,
) -> tuple[int, str, str]:
tmscore = tmscore if tmscore is not None else self.default_params['tmscore']
outfmt = outfmt if outfmt is not None else self.default_params['outfmt']
# Create the command
cmd = [
str(self.usalign_path),
"-mol", self.default_params['mol'],
"-dir", str(target_dir),
pdb_list_file,
"-TMscore", str(tmscore),
"-outfmt", str(outfmt)
]
print(cmd)
# Convert command list to string
cmd_str = " ".join(cmd)
try:
# Execute the command
process = subprocess.Popen(
cmd_str,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
text=True
)
# Get output
stdout, stderr = process.communicate()
return process.returncode, stdout, stderr
except Exception as e:
return -1, "", str(e)
|