Download double_diff/batch_run_foldseek.py from Jumbol/Acr_Prophage_External_Validation_Dataset: direct link, hf CLI and curl.
- Browser
- Download file 5.13 kB
-
https://huggingface.co/datasets/Jumbol/Acr_Prophage_External_Validation_Dataset/resolve/main/double_diff/batch_run_foldseek.py
- Command line
-
hf download hf://datasets/Jumbol/Acr_Prophage_External_Validation_Dataset/double_diff/batch_run_foldseek.py
-
curl -L -o batch_run_foldseek.py https://huggingface.co/datasets/Jumbol/Acr_Prophage_External_Validation_Dataset/resolve/main/double_diff/batch_run_foldseek.py
5.13 kB
| #!/usr/bin/env python3 | |
| import argparse | |
| import os | |
| import subprocess | |
| import shlex | |
| from tqdm import tqdm | |
| import multiprocessing | |
| import shutil # MODIFICATION: Import shutil for directory cleanup | |
| 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] | |
| # --- MODIFICATION START: Solve Race Condition --- | |
| # Create a unique temporary directory for this specific job | |
| # to avoid conflicts when running in parallel. | |
| # We use the PDB base name as a unique identifier for the sub-directory. | |
| job_tmp_dir = os.path.join(tmp_dir, base_name) | |
| os.makedirs(job_tmp_dir, exist_ok=True) | |
| if show_compare: | |
| output_extension = ".html" # Set extension to .html if show_compare is true | |
| format_option = "--format-mode 3" | |
| else: | |
| output_extension = ".txt" # Default extension is .txt | |
| format_option = ( | |
| "--format-output query,target,prob,alntmscore,lddt,fident,alnlen," | |
| "mismatch,gapopen,qstart,qend,tstart,tend,evalue,bits" | |
| ) | |
| # Construct the final output path with the correct extension | |
| output_path = os.path.join(output_dir, f"{base_name}{output_extension}") | |
| # --- MODIFICATION END --- | |
| safe_pdb_path = shlex.quote(pdb_path) | |
| # MODIFICATION: Use the new output_path variable | |
| safe_output_path = shlex.quote(output_path) | |
| safe_target_db = shlex.quote(target_db) | |
| # MODIFICATION: Use the unique job-specific temp directory | |
| safe_job_tmp_dir = shlex.quote(job_tmp_dir) | |
| # The format_option is now defined above, so the old if/else is removed | |
| foldseek_cmd = ( | |
| f"foldseek easy-search " | |
| # MODIFICATION: Use the new safe_output_path and safe_job_tmp_dir variables | |
| 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: | |
| # MODIFICATION: Clean up even on failure | |
| shutil.rmtree(job_tmp_dir) | |
| return f"Warning: Failed to process {pdb_file}: {e.stderr}" | |
| # --- MODIFICATION START: Clean up the unique temp directory --- | |
| # After the process is successfully finished, remove its unique temp dir | |
| try: | |
| shutil.rmtree(job_tmp_dir) | |
| except OSError as e: | |
| # Log a warning if cleanup fails, but don't crash | |
| return f"Warning: Successfully processed {pdb_file} but failed to clean up temp dir {job_tmp_dir}: {e}" | |
| # --- MODIFICATION END --- | |
| 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') # MODIFICATION: Updated help text | |
| 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'") # MODIFICATION: Updated help text | |
| 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 | |
| # MODIFICATION: Use a list to store results for better error logging | |
| results = [] | |
| with tqdm(total=len(pdb_files), desc="Progress") as pbar: | |
| def update_progress(result): | |
| # MODIFICATION: Store the result message and update the bar | |
| 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) # MODIFICATION: Ensure bar updates on critical error | |
| ) | |
| pool.close() | |
| pool.join() | |
| print(f"\nProcessing completed! Results saved to: {args.output}") | |
| # MODIFICATION: Log any warnings at the end | |
| 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() | |