silterra commited on
Commit
0303515
·
1 Parent(s): 25e2e8a

Add ability to upload a YAML file with other arguments (all passed to inference.py).

Browse files
Files changed (3) hide show
  1. main.py +18 -1
  2. requirements.txt +2 -1
  3. run_utils.py +37 -11
main.py CHANGED
@@ -6,7 +6,9 @@ import gradio as gr
6
  import run_utils
7
 
8
 
9
- def run_wrapper(protein_file, ligand_file, *args, **kwargs) -> str:
 
 
10
  return run_utils.run_cli_command(
11
  protein_file.name, ligand_file.name, *args, **kwargs
12
  )
@@ -18,6 +20,7 @@ def run():
18
  inputs=[
19
  gr.File(label="Protein PDB", file_types=[".pdb"]),
20
  gr.File(label="Ligand SDF", file_types=[".sdf"]),
 
21
  gr.Number(
22
  label="Samples Per Complex",
23
  value=1,
@@ -29,6 +32,20 @@ def run():
29
  gr.Checkbox(label="Save Visualisation", value=True),
30
  ],
31
  outputs=gr.File(label="Result"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  )
33
 
34
  iface.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
6
  import run_utils
7
 
8
 
9
+ def run_wrapper(protein_file, ligand_file, other_args_file, *args, **kwargs) -> str:
10
+ if other_args_file is not None:
11
+ kwargs["other_arg_file"] = other_args_file.name
12
  return run_utils.run_cli_command(
13
  protein_file.name, ligand_file.name, *args, **kwargs
14
  )
 
20
  inputs=[
21
  gr.File(label="Protein PDB", file_types=[".pdb"]),
22
  gr.File(label="Ligand SDF", file_types=[".sdf"]),
23
+ gr.File(label="Other arguments (Optional, YML)", file_types=[".yml", ".yaml"], value=None),
24
  gr.Number(
25
  label="Samples Per Complex",
26
  value=1,
 
32
  gr.Checkbox(label="Save Visualisation", value=True),
33
  ],
34
  outputs=gr.File(label="Result"),
35
+ title="DiffDock-Pocket",
36
+ description="""
37
+ Run [DiffDock-Pocket](https://github.com/plainerman/DiffDock-Pocket) for a single protein and ligand.
38
+ We have provided the most important inputs as UI elements.
39
+ Additional values can be included in "Other arguments", and will be passed
40
+ to [inference.py](https://github.com/plainerman/DiffDock-Pocket/blob/main/inference.py).
41
+ Must be a YAML file without any nesting.
42
+ For example, to specify a pocket of (0.0, 1.0, 2.0), the YAML file should contain:
43
+ ```
44
+ pocket_center_x: 0.0
45
+ pocket_center_y: 1.0
46
+ pocket_center_z: 2.0
47
+ ```
48
+ """
49
  )
50
 
51
  iface.launch(server_name="0.0.0.0", server_port=7860, share=False)
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  gradio==3.50
2
- requests==2.31.0
 
 
1
  gradio==3.50
2
+ requests==2.31.0
3
+ pyyaml==6.0.1
run_utils.py CHANGED
@@ -6,6 +6,9 @@ import tempfile
6
  import uuid
7
 
8
  import logging
 
 
 
9
 
10
 
11
  def set_env_variables():
@@ -37,12 +40,30 @@ def configure_logging(level=None):
37
  )
38
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def run_cli_command(
41
  protein_path: str,
42
  ligand: str,
43
  samples_per_complex: int,
44
  keep_local_structures: bool,
45
  save_visualisation: bool,
 
46
  work_dir=None,
47
  ):
48
  if work_dir is None:
@@ -50,19 +71,22 @@ def run_cli_command(
50
  "DiffDock-Pocket-Dir", os.path.join(os.environ["HOME"], "DiffDock-Pocket")
51
  )
52
 
 
 
 
 
 
 
 
 
 
 
 
53
  command = [
54
  "python3",
55
- "inference.py",
56
- f"--protein_path={protein_path}",
57
- f"--ligand={ligand}",
58
- f"--samples_per_complex={samples_per_complex}",
59
- ]
60
-
61
- # Adding boolean arguments only if they are True
62
- if keep_local_structures:
63
- command.append("--keep_local_structures")
64
- if save_visualisation:
65
- command.append("--save_visualisation")
66
 
67
  with tempfile.TemporaryDirectory() as temp_dir:
68
  temp_dir_path = temp_dir
@@ -110,6 +134,7 @@ if __name__ == "__main__":
110
  os.environ["DiffDock-Pocket-Dir"] = work_dir
111
  protein_path = os.path.join(work_dir, "example_data", "3dpf_protein.pdb")
112
  ligand = os.path.join(work_dir, "example_data", "3dpf_ligand.sdf")
 
113
 
114
  run_cli_command(
115
  protein_path,
@@ -117,4 +142,5 @@ if __name__ == "__main__":
117
  samples_per_complex=1,
118
  keep_local_structures=True,
119
  save_visualisation=True,
 
120
  )
 
6
  import uuid
7
 
8
  import logging
9
+ from typing import List
10
+
11
+ import yaml
12
 
13
 
14
  def set_env_variables():
 
40
  )
41
 
42
 
43
+ def kwargs_to_cli_args(**kwargs) -> List[str]:
44
+ """
45
+ Converts keyword arguments to a CLI argument string.
46
+ Boolean kwargs are added as flags if True, and omitted if False.
47
+ """
48
+ cli_args = []
49
+ for key, value in kwargs.items():
50
+ if isinstance(value, bool):
51
+ if value:
52
+ cli_args.append(f"--{key}")
53
+ else:
54
+ if value is not None:
55
+ cli_args.append(f"--{key}={value}")
56
+
57
+ return cli_args
58
+
59
+
60
  def run_cli_command(
61
  protein_path: str,
62
  ligand: str,
63
  samples_per_complex: int,
64
  keep_local_structures: bool,
65
  save_visualisation: bool,
66
+ other_arg_file: str,
67
  work_dir=None,
68
  ):
69
  if work_dir is None:
 
71
  "DiffDock-Pocket-Dir", os.path.join(os.environ["HOME"], "DiffDock-Pocket")
72
  )
73
 
74
+ all_arg_dict = {}
75
+ if other_arg_file:
76
+ all_arg_dict = yaml.safe_load(open(other_arg_file, "r"))
77
+ logging.debug(f"YAML dict: {all_arg_dict}")
78
+
79
+ ui_args = ["protein_path", "ligand", "samples_per_complex",
80
+ "keep_local_structures", "save_visualisation"]
81
+ for ui_arg in ui_args:
82
+ my_str = f"all_arg_dict['{ui_arg}'] = {ui_arg}"
83
+ exec(my_str)
84
+
85
  command = [
86
  "python3",
87
+ "inference.py"]
88
+
89
+ command += kwargs_to_cli_args(**all_arg_dict)
 
 
 
 
 
 
 
 
90
 
91
  with tempfile.TemporaryDirectory() as temp_dir:
92
  temp_dir_path = temp_dir
 
134
  os.environ["DiffDock-Pocket-Dir"] = work_dir
135
  protein_path = os.path.join(work_dir, "example_data", "3dpf_protein.pdb")
136
  ligand = os.path.join(work_dir, "example_data", "3dpf_ligand.sdf")
137
+ other_arg_file = os.path.join(work_dir, "example_data", "example_args.yml")
138
 
139
  run_cli_command(
140
  protein_path,
 
142
  samples_per_complex=1,
143
  keep_local_structures=True,
144
  save_visualisation=True,
145
+ other_arg_file=other_arg_file
146
  )