Skip to content

Commit 03cfaab

Browse files
authored
Merge pull request #82 from davidkastner/submit-clustering
Adding a script for submitting clustering calculations
2 parents 6229330 + eb4c383 commit 03cfaab

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

pyqmmm/cli.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ def cli():
4242
@click.option("--translate_pdb_to_center", "-tc", is_flag=True, help="Translates PDB traj to new center.")
4343
@click.option("--xyz2pdb", "-x2p", is_flag=True, help="Converts an xyz file or traj to a PDB.")
4444
@click.option("--repo2markdown", "-r2m", is_flag=True, help="Converts python package to markdown file.")
45+
@click.option("--submit_clustering", "-sc", is_flag=True, help="Submits clustering jobs to queue.")
4546
def io(
4647
ppm2png,
4748
delete_xyz_atoms,
4849
delete_pdb_atoms,
4950
translate_pdb_to_center,
5051
xyz2pdb,
51-
repo2markdown
52+
repo2markdown,
53+
submit_clustering
5254
):
5355
"""
5456
Tools for useful manipulations of common file types.
@@ -122,6 +124,12 @@ def io(
122124
import pyqmmm.io.repo2markdown
123125
pyqmmm.io.repo2markdown.main()
124126

127+
elif submit_clustering:
128+
click.echo("Submits clustering calculations to queue")
129+
click.echo("Loading...")
130+
import pyqmmm.io.submit_clustering
131+
pyqmmm.io.submit_clustering.main()
132+
125133

126134
@cli.command()
127135
@click.option("--gbsa_submit", "-gs", is_flag=True, help="Prepares and submits a mmGBSA job.")

pyqmmm/io/submit_clustering.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
import subprocess
3+
4+
# Get the current working directory
5+
cwd = os.getcwd()
6+
7+
# Function to check if a directory contains any .out file
8+
def has_out_file(dir_path):
9+
for file in os.listdir(dir_path):
10+
if file.endswith('.out'):
11+
return True
12+
return False
13+
14+
# Iterate over all items in the cwd
15+
for item in os.listdir(cwd):
16+
item_path = os.path.join(cwd, item)
17+
18+
# Check if it's a directory and not "1_cluster"
19+
if os.path.isdir(item_path) and item != "1_cluster":
20+
# Path to 2_analysis
21+
analysis_dir = os.path.join(item_path, "2_analysis")
22+
23+
# Check if 2_analysis exists
24+
if os.path.exists(analysis_dir) and os.path.isdir(analysis_dir):
25+
# Path to 1_cluster inside 2_analysis
26+
cluster_dir = os.path.join(analysis_dir, "1_cluster")
27+
28+
# Check if 1_cluster exists
29+
if os.path.exists(cluster_dir) and os.path.isdir(cluster_dir):
30+
# Iterate over subdirectories in 1_cluster
31+
for sub_item in os.listdir(cluster_dir):
32+
sub_item_path = os.path.join(cluster_dir, sub_item)
33+
34+
# Check if it's a directory
35+
if os.path.isdir(sub_item_path):
36+
# Path to cluster.sh
37+
sh_file = os.path.join(sub_item_path, "cluster.sh")
38+
39+
# Check if cluster.sh exists
40+
if os.path.exists(sh_file):
41+
# Check for .out files
42+
if has_out_file(sub_item_path):
43+
# Prompt user
44+
response = input(f"Job might be running in {sub_item_path}. Resubmit? (y/N): ").strip().lower()
45+
if response != 'y':
46+
print(f"Skipping resubmission for {sub_item_path}")
47+
continue
48+
49+
# Submit the job
50+
try:
51+
result = subprocess.run(['sbatch', sh_file], capture_output=True, text=True, cwd=sub_item_path)
52+
if result.returncode == 0:
53+
print(f"Submitted job for {sub_item_path}: {result.stdout.strip()}")
54+
else:
55+
print(f"Error submitting job for {sub_item_path}: {result.stderr.strip()}")
56+
except Exception as e:
57+
print(f"Exception submitting job for {sub_item_path}: {str(e)}")
58+
else:
59+
print(f"cluster.sh not found in {sub_item_path}")
60+
else:
61+
print(f"1_cluster not found in {analysis_dir}")
62+
else:
63+
print(f"2_analysis not found in {item_path}")

0 commit comments

Comments
 (0)