Skip to content

feat: support for new version of loop modelling #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/drug_discovery/structures/protein.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def sequence(self) -> list[str]:

return sequences

def model_loops(self) -> None:
def model_loops(self, use_cache: bool = True) -> None:
"""model loops in protein structure"""

from deeporigin.functions.loop_modelling import model_loops
Expand All @@ -257,7 +257,7 @@ def model_loops(self) -> None:
if pdb_id is None:
raise ValueError("Currently, PDB ID is required to model loops.")

file_path = model_loops(pdb_id=pdb_id)
file_path = model_loops(pdb_id=pdb_id, use_cache=use_cache)
protein = Protein.from_file(file_path)
self.structure = protein.structure

Expand Down
46 changes: 32 additions & 14 deletions src/functions/loop_modelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
from beartype import beartype
import requests

URL = "http://loop-modelling.default.jobs.edge.deeporigin.io/model_loops"
# URL = "http://loop-modelling.default.jobs.edge.deeporigin.io/model_loops"
URL = "http://localhost:8080/model_loops"
CACHE_DIR = os.path.expanduser("~/.deeporigin/model_loops")


@beartype
def model_loops(
*,
pdb_id: str,
) -> str:
use_cache: bool = True,
) -> dict:
"""
Run system preparation on a protein-ligand complex.

Expand All @@ -29,28 +31,44 @@ def model_loops(
hash_input = f"{pdb_id}"
cache_key = hashlib.md5(hash_input.encode()).hexdigest()
cache_path = os.path.join(CACHE_DIR, cache_key)
output_pdb_path = os.path.join(cache_path, "loops_modelled.pdb")
output_json_path = os.path.join(cache_path, "data.json")
output_pdb_path = os.path.join(cache_path, "data.pdb")

# Create cache directory if it doesn't exist
os.makedirs(cache_path, exist_ok=True)

# Check if cached results exist
if os.path.exists(output_pdb_path):
return output_pdb_path
if use_cache and os.path.exists(output_json_path):
import json

with open(output_json_path, "r") as f:
return json.load(f)

# If no cached results, proceed with server call
payload = {
"pdb_id": pdb_id,
}

response = requests.post(URL, json=payload, stream=True)
try:
response = requests.post(URL, json=payload, stream=True)
except Exception as e:
raise RuntimeError(f"Failed to connect to server: {e}") from None

if response.status_code == 200:
# Create cache directory if it doesn't exist
os.makedirs(cache_path, exist_ok=True)
# Save the result to the cache
with open(output_pdb_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return output_pdb_path
import json

response_json = response.json()
# Write pdb_contents to output_pdb_path
pdb_contents = response_json.pop("pdb_contents", None)
if pdb_contents is not None:
with open(output_pdb_path, "w") as pdb_file:
pdb_file.write(pdb_contents)

response_json["pdb_file"] = output_pdb_path
# Write the modified JSON (without pdb_contents) to output_json_path
with open(output_json_path, "w") as f:
json.dump(response_json, f, indent=2)
return response_json

# If the server request fails, raise an error
raise RuntimeError(
Expand Down
Loading