File size: 1,846 Bytes
44459bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Base module for model prediction endpoint query."""

from __future__ import annotations

import json
import logging
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any

from folding_studio_data_models import FoldingModel
from pydantic import BaseModel


class Query(ABC):
    """Interface to define folding job queries."""

    MODEL: FoldingModel | None = None

    @classmethod
    @abstractmethod
    def from_file(cls, path: str | Path, **kwargs) -> Query:
        """Instantiates a Query object from a file."""
        ...

    @classmethod
    @abstractmethod
    def from_directory(cls, path: str | Path, **kwargs) -> Query:
        """Instantiates a Query object from a directory."""
        ...

    @classmethod
    @abstractmethod
    def from_protein_sequence(cls, protein: str, **kwargs) -> Query:
        """Instantiates a Query object from string representation of a protein."""
        ...

    @property
    @abstractmethod
    def payload(self) -> dict[str, Any]:
        """Returns the payload to be sent in the POST request."""
        ...

    @property
    @abstractmethod
    def parameters(self) -> BaseModel:
        """Parameters of the query."""
        ...

    def save_parameters(self, output_dir: Path) -> None:
        """Writes the input parameters to a JSON file inside the output directory.

        Args:
            output_dir (Path): The directory where the inference parameters JSON file will be saved.
        """
        inference_parameters_path = output_dir / "query_parameters.json"
        output_dir.mkdir(parents=True, exist_ok=True)
        with inference_parameters_path.open("w", encoding="utf-8") as f:
            json.dump(self.parameters.model_dump(mode="json"), f, indent=4)
        logging.info(f"Input parameters written to {inference_parameters_path}")