| |
|
|
| import argparse |
| import os |
| import subprocess |
| import shlex |
| from tqdm import tqdm |
| import multiprocessing |
| import shutil |
|
|
| def process_pdb_file(pdb_file, input_dir, output_dir, target_db, tmp_dir, show_compare=False): |
| pdb_path = os.path.join(input_dir, pdb_file) |
| base_name = os.path.splitext(pdb_file)[0] |
|
|
| |
| |
| |
| |
| job_tmp_dir = os.path.join(tmp_dir, base_name) |
| os.makedirs(job_tmp_dir, exist_ok=True) |
|
|
| if show_compare: |
| output_extension = ".html" |
| format_option = "--format-mode 3" |
| else: |
| output_extension = ".txt" |
| format_option = ( |
| "--format-output query,target,prob,alntmscore,lddt,fident,alnlen," |
| "mismatch,gapopen,qstart,qend,tstart,tend,evalue,bits" |
| ) |
| |
| |
| output_path = os.path.join(output_dir, f"{base_name}{output_extension}") |
| |
|
|
| safe_pdb_path = shlex.quote(pdb_path) |
| |
| safe_output_path = shlex.quote(output_path) |
| safe_target_db = shlex.quote(target_db) |
| |
| safe_job_tmp_dir = shlex.quote(job_tmp_dir) |
|
|
| |
| |
| foldseek_cmd = ( |
| f"foldseek easy-search " |
| |
| f"{safe_pdb_path} {safe_target_db} {safe_output_path} {safe_job_tmp_dir} " |
| f"{format_option}" |
| ) |
|
|
| try: |
| result = subprocess.run( |
| foldseek_cmd, |
| shell=True, |
| check=True, |
| stdout=subprocess.PIPE, |
| stderr=subprocess.PIPE, |
| text=True |
| ) |
| except subprocess.CalledProcessError as e: |
| |
| shutil.rmtree(job_tmp_dir) |
| return f"Warning: Failed to process {pdb_file}: {e.stderr}" |
| |
| |
| |
| try: |
| shutil.rmtree(job_tmp_dir) |
| except OSError as e: |
| |
| return f"Warning: Successfully processed {pdb_file} but failed to clean up temp dir {job_tmp_dir}: {e}" |
| |
|
|
| return f"Successfully processed {pdb_file}" |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description='Batch processing of Foldseek structure alignment') |
| parser.add_argument('-i', '--input', required=True, help='Input directory containing PDB files') |
| parser.add_argument('-o', '--output', required=True, help='Output directory for TXT/HTML results') |
| parser.add_argument('-t', '--target', required=True, help='Target database for Foldseek search') |
| parser.add_argument('-T', '--tmp', default='tmp', help='Temporary directory for intermediate files') |
| parser.add_argument('-w', '--workers', type=int, default=4, help='Number of worker processes') |
| parser.add_argument("--show_compare", action="store_true", help="If set, will output .html files using '--format-mode 3'") |
| |
| args = parser.parse_args() |
| |
| os.makedirs(args.output, exist_ok=True) |
| os.makedirs(args.tmp, exist_ok=True) |
| |
| pdb_files = [f for f in os.listdir(args.input) if f.endswith('.pdb')] |
| |
| if not pdb_files: |
| print(f"Error: No PDB files found in directory '{args.input}'") |
| return |
|
|
| |
| results = [] |
| |
| with tqdm(total=len(pdb_files), desc="Progress") as pbar: |
| def update_progress(result): |
| |
| results.append(result) |
| pbar.update(1) |
|
|
| with multiprocessing.Pool(processes=args.workers) as pool: |
| for pdb_file in pdb_files: |
| pool.apply_async( |
| process_pdb_file, |
| (pdb_file, args.input, args.output, args.target, args.tmp, args.show_compare), |
| callback=update_progress, |
| error_callback=lambda e: pbar.update(1) |
| ) |
| pool.close() |
| pool.join() |
| |
| print(f"\nProcessing completed! Results saved to: {args.output}") |
|
|
| |
| warnings = [r for r in results if r.startswith("Warning:")] |
| if warnings: |
| print("\n--- Warnings ---") |
| for w in warnings: |
| print(w) |
|
|
|
|
| if __name__ == '__main__': |
| main() |
|
|