openprotein-python 0.5.0.dev1__py3-none-any.whl → 0.5.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openprotein/__init__.py +53 -23
- openprotein/api/__init__.py +0 -1
- openprotein/api/align.py +1 -23
- openprotein/api/deprecated/poet.py +5 -1
- openprotein/api/design.py +12 -8
- openprotein/api/designer.py +262 -0
- openprotein/api/embedding.py +37 -15
- openprotein/api/fold.py +5 -5
- openprotein/api/job.py +3 -0
- openprotein/api/predict.py +43 -9
- openprotein/api/predictor.py +110 -6
- openprotein/api/svd.py +48 -5
- openprotein/api/umap.py +247 -0
- openprotein/app/__init__.py +13 -4
- openprotein/app/models/__init__.py +19 -7
- openprotein/app/models/align/msa.py +1 -8
- openprotein/app/models/align/prompt.py +1 -6
- openprotein/app/models/deprecated/poet.py +4 -3
- openprotein/app/models/design.py +10 -7
- openprotein/app/models/designer.py +121 -0
- openprotein/app/models/embeddings/base.py +82 -14
- openprotein/app/models/embeddings/future.py +36 -7
- openprotein/app/models/embeddings/poet.py +79 -24
- openprotein/app/models/fold/alphafold2.py +11 -7
- openprotein/app/models/fold/esmfold.py +5 -2
- openprotein/app/models/futures.py +21 -6
- openprotein/app/models/predict.py +81 -11
- openprotein/app/models/predictor/predict.py +3 -3
- openprotein/app/models/predictor/predictor.py +169 -6
- openprotein/app/models/predictor/validate.py +37 -0
- openprotein/app/models/svd.py +99 -10
- openprotein/app/models/train.py +14 -11
- openprotein/app/models/umap.py +156 -0
- openprotein/app/services/__init__.py +10 -4
- openprotein/app/services/design.py +9 -7
- openprotein/app/services/designer.py +73 -0
- openprotein/app/services/job.py +16 -3
- openprotein/app/services/predict.py +11 -5
- openprotein/app/services/umap.py +97 -0
- openprotein/auth.py +21 -0
- openprotein/base.py +33 -31
- openprotein/csv.py +26 -0
- openprotein/schemas/__init__.py +23 -9
- openprotein/schemas/align.py +7 -13
- openprotein/schemas/design.py +151 -41
- openprotein/schemas/designer.py +38 -0
- openprotein/schemas/embeddings.py +3 -4
- openprotein/schemas/features.py +7 -0
- openprotein/schemas/job.py +19 -0
- openprotein/schemas/predict.py +2 -3
- openprotein/schemas/predictor.py +4 -12
- openprotein/schemas/svd.py +2 -3
- openprotein/schemas/train.py +3 -3
- openprotein/schemas/umap.py +35 -0
- {openprotein_python-0.5.0.dev1.dist-info → openprotein_python-0.5.2.dist-info}/METADATA +6 -7
- openprotein_python-0.5.2.dist-info/RECORD +88 -0
- {openprotein_python-0.5.0.dev1.dist-info → openprotein_python-0.5.2.dist-info}/WHEEL +1 -1
- openprotein_python-0.5.0.dev1.dist-info/RECORD +0 -76
- {openprotein_python-0.5.0.dev1.dist-info → openprotein_python-0.5.2.dist-info}/licenses/LICENSE.txt +0 -0
openprotein/__init__.py
CHANGED
|
@@ -1,14 +1,24 @@
|
|
|
1
|
+
"""
|
|
2
|
+
OpenProtein Python client.
|
|
3
|
+
|
|
4
|
+
A pythonic interface for interacting with our cutting-edge protein engineering platform.
|
|
5
|
+
|
|
6
|
+
isort:skip_file
|
|
7
|
+
"""
|
|
8
|
+
|
|
1
9
|
from openprotein._version import __version__
|
|
2
10
|
from openprotein.app import (
|
|
3
|
-
SVDAPI,
|
|
4
|
-
AlignAPI,
|
|
5
11
|
AssayDataAPI,
|
|
12
|
+
JobsAPI,
|
|
13
|
+
TrainingAPI,
|
|
6
14
|
DesignAPI,
|
|
15
|
+
AlignAPI,
|
|
7
16
|
EmbeddingsAPI,
|
|
8
17
|
FoldAPI,
|
|
9
|
-
|
|
18
|
+
SVDAPI,
|
|
19
|
+
UMAPAPI,
|
|
10
20
|
PredictorAPI,
|
|
11
|
-
|
|
21
|
+
DesignerAPI,
|
|
12
22
|
)
|
|
13
23
|
from openprotein.app.models import Future
|
|
14
24
|
from openprotein.base import APISession
|
|
@@ -19,15 +29,17 @@ class OpenProtein(APISession):
|
|
|
19
29
|
The base class for accessing OpenProtein API functionality.
|
|
20
30
|
"""
|
|
21
31
|
|
|
22
|
-
_embedding = None
|
|
23
|
-
_svd = None
|
|
24
|
-
_fold = None
|
|
25
|
-
_align = None
|
|
26
|
-
_jobs = None
|
|
27
32
|
_data = None
|
|
33
|
+
_jobs = None
|
|
28
34
|
_train = None
|
|
29
35
|
_design = None
|
|
36
|
+
_align = None
|
|
37
|
+
_embedding = None
|
|
38
|
+
_svd = None
|
|
39
|
+
_umap = None
|
|
40
|
+
_fold = None
|
|
30
41
|
_predictor = None
|
|
42
|
+
_designer = None
|
|
31
43
|
|
|
32
44
|
def wait(self, future: Future, *args, **kwargs):
|
|
33
45
|
return future.wait(*args, **kwargs)
|
|
@@ -37,15 +49,6 @@ class OpenProtein(APISession):
|
|
|
37
49
|
def load_job(self, job_id):
|
|
38
50
|
return self.jobs.__load(job_id=job_id)
|
|
39
51
|
|
|
40
|
-
@property
|
|
41
|
-
def jobs(self) -> JobsAPI:
|
|
42
|
-
"""
|
|
43
|
-
The jobs submodule gives access to functionality for listing jobs and checking their status.
|
|
44
|
-
"""
|
|
45
|
-
if self._jobs is None:
|
|
46
|
-
self._jobs = JobsAPI(self)
|
|
47
|
-
return self._jobs
|
|
48
|
-
|
|
49
52
|
@property
|
|
50
53
|
def data(self) -> AssayDataAPI:
|
|
51
54
|
"""
|
|
@@ -55,6 +58,15 @@ class OpenProtein(APISession):
|
|
|
55
58
|
self._data = AssayDataAPI(self)
|
|
56
59
|
return self._data
|
|
57
60
|
|
|
61
|
+
@property
|
|
62
|
+
def jobs(self) -> JobsAPI:
|
|
63
|
+
"""
|
|
64
|
+
The jobs submodule gives access to functionality for listing jobs and checking their status.
|
|
65
|
+
"""
|
|
66
|
+
if self._jobs is None:
|
|
67
|
+
self._jobs = JobsAPI(self)
|
|
68
|
+
return self._jobs
|
|
69
|
+
|
|
58
70
|
@property
|
|
59
71
|
def train(self) -> TrainingAPI:
|
|
60
72
|
"""
|
|
@@ -64,6 +76,15 @@ class OpenProtein(APISession):
|
|
|
64
76
|
self._train = TrainingAPI(self)
|
|
65
77
|
return self._train
|
|
66
78
|
|
|
79
|
+
@property
|
|
80
|
+
def design(self) -> DesignAPI:
|
|
81
|
+
"""
|
|
82
|
+
The design submodule gives access to functionality for designing new sequences using models from train.
|
|
83
|
+
"""
|
|
84
|
+
if self._design is None:
|
|
85
|
+
self._design = DesignAPI(self)
|
|
86
|
+
return self._design
|
|
87
|
+
|
|
67
88
|
@property
|
|
68
89
|
def align(self) -> AlignAPI:
|
|
69
90
|
"""
|
|
@@ -91,6 +112,15 @@ class OpenProtein(APISession):
|
|
|
91
112
|
self._svd = SVDAPI(self, self.embedding)
|
|
92
113
|
return self._svd
|
|
93
114
|
|
|
115
|
+
@property
|
|
116
|
+
def umap(self) -> UMAPAPI:
|
|
117
|
+
"""
|
|
118
|
+
The embedding submodule gives access to protein embedding models and their inference endpoints.
|
|
119
|
+
"""
|
|
120
|
+
if self._umap is None:
|
|
121
|
+
self._umap = UMAPAPI(self)
|
|
122
|
+
return self._umap
|
|
123
|
+
|
|
94
124
|
@property
|
|
95
125
|
def predictor(self) -> PredictorAPI:
|
|
96
126
|
"""
|
|
@@ -101,13 +131,13 @@ class OpenProtein(APISession):
|
|
|
101
131
|
return self._predictor
|
|
102
132
|
|
|
103
133
|
@property
|
|
104
|
-
def
|
|
134
|
+
def designer(self) -> DesignerAPI:
|
|
105
135
|
"""
|
|
106
|
-
The
|
|
136
|
+
The designer submodule gives access to functionality for designing new sequences using models from predictor train.
|
|
107
137
|
"""
|
|
108
|
-
if self.
|
|
109
|
-
self.
|
|
110
|
-
return self.
|
|
138
|
+
if self._designer is None:
|
|
139
|
+
self._designer = DesignerAPI(self)
|
|
140
|
+
return self._designer
|
|
111
141
|
|
|
112
142
|
@property
|
|
113
143
|
def fold(self) -> FoldAPI:
|
openprotein/api/__init__.py
CHANGED
openprotein/api/align.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import codecs
|
|
2
|
-
import csv
|
|
3
1
|
import io
|
|
4
2
|
import random
|
|
5
3
|
from typing import BinaryIO, Iterator
|
|
@@ -7,31 +5,11 @@ from typing import BinaryIO, Iterator
|
|
|
7
5
|
import openprotein.config as config
|
|
8
6
|
import requests
|
|
9
7
|
from openprotein.base import APISession
|
|
8
|
+
from openprotein.csv import csv_stream
|
|
10
9
|
from openprotein.errors import APIError, InvalidParameterError, MissingParameterError
|
|
11
10
|
from openprotein.schemas import Job, MSASamplingMethod, PoetInputType
|
|
12
11
|
|
|
13
12
|
|
|
14
|
-
def csv_stream(response: requests.Response) -> Iterator[list[str]]:
|
|
15
|
-
"""
|
|
16
|
-
Returns a CSV reader from a requests.Response object.
|
|
17
|
-
|
|
18
|
-
Parameters
|
|
19
|
-
----------
|
|
20
|
-
response : requests.Response
|
|
21
|
-
The response object to parse.
|
|
22
|
-
|
|
23
|
-
Returns
|
|
24
|
-
-------
|
|
25
|
-
csv.reader
|
|
26
|
-
A csv reader object for the response.
|
|
27
|
-
"""
|
|
28
|
-
raw_content = response.raw # the raw bytes stream
|
|
29
|
-
content = codecs.getreader("utf-8")(
|
|
30
|
-
raw_content
|
|
31
|
-
) # force the response to be encoded as utf-8
|
|
32
|
-
return csv.reader(content)
|
|
33
|
-
|
|
34
|
-
|
|
35
13
|
def get_align_job_inputs(
|
|
36
14
|
session: APISession,
|
|
37
15
|
job_id: str,
|
|
@@ -5,7 +5,11 @@ import requests
|
|
|
5
5
|
from openprotein import config
|
|
6
6
|
from openprotein.base import APISession
|
|
7
7
|
from openprotein.errors import APIError, InvalidParameterError, MissingParameterError
|
|
8
|
-
from openprotein.schemas import
|
|
8
|
+
from openprotein.schemas.deprecated.poet import (
|
|
9
|
+
PoetGenerateJob,
|
|
10
|
+
PoetScoreJob,
|
|
11
|
+
PoetSSPJob,
|
|
12
|
+
)
|
|
9
13
|
|
|
10
14
|
|
|
11
15
|
def poet_score_post(
|
openprotein/api/design.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
from openprotein.base import APISession
|
|
2
|
-
from openprotein.schemas import
|
|
2
|
+
from openprotein.schemas import (
|
|
3
|
+
WorkflowDesign,
|
|
4
|
+
WorkflowDesignJob,
|
|
5
|
+
WorkflowDesignJobCreate,
|
|
6
|
+
)
|
|
3
7
|
|
|
4
8
|
|
|
5
|
-
def create_design_job(session: APISession, design_job:
|
|
9
|
+
def create_design_job(session: APISession, design_job: WorkflowDesignJobCreate):
|
|
6
10
|
"""
|
|
7
11
|
Send a POST request for protein design job.
|
|
8
12
|
|
|
@@ -13,7 +17,7 @@ def create_design_job(session: APISession, design_job: DesignJobCreate):
|
|
|
13
17
|
design_job : DesignJobCreate
|
|
14
18
|
The details of the design job to be created, with the following parameters:
|
|
15
19
|
- assay_id: The ID for the assay.
|
|
16
|
-
- criteria:
|
|
20
|
+
- criteria: Criteria for evaluating the design.
|
|
17
21
|
- num_steps: The number of steps in the genetic algo. Default is 8.
|
|
18
22
|
- pop_size: The population size for the genetic algo. Default is None.
|
|
19
23
|
- n_offsprings: The number of offspring for the genetic algo. Default is None.
|
|
@@ -24,13 +28,13 @@ def create_design_job(session: APISession, design_job: DesignJobCreate):
|
|
|
24
28
|
|
|
25
29
|
Returns
|
|
26
30
|
-------
|
|
27
|
-
|
|
31
|
+
WorkflowDesignJob
|
|
28
32
|
The created job as a Job instance.
|
|
29
33
|
"""
|
|
30
34
|
params = design_job.model_dump(exclude_none=True)
|
|
31
35
|
# print(f"sending design: {params}")
|
|
32
36
|
response = session.post("v1/workflow/design/genetic-algorithm", json=params)
|
|
33
|
-
return
|
|
37
|
+
return WorkflowDesignJob.model_validate(response.json())
|
|
34
38
|
|
|
35
39
|
|
|
36
40
|
def get_design_results(
|
|
@@ -39,7 +43,7 @@ def get_design_results(
|
|
|
39
43
|
step: int | None = None,
|
|
40
44
|
page_size: int | None = None,
|
|
41
45
|
page_offset: int | None = None,
|
|
42
|
-
) ->
|
|
46
|
+
) -> WorkflowDesign:
|
|
43
47
|
"""
|
|
44
48
|
Retrieves the results of a Design job.
|
|
45
49
|
|
|
@@ -60,7 +64,7 @@ def get_design_results(
|
|
|
60
64
|
|
|
61
65
|
Returns
|
|
62
66
|
-------
|
|
63
|
-
|
|
67
|
+
WorkflowDesignJob
|
|
64
68
|
The job object representing the Design job.
|
|
65
69
|
|
|
66
70
|
Raises
|
|
@@ -79,4 +83,4 @@ def get_design_results(
|
|
|
79
83
|
|
|
80
84
|
response = session.get(endpoint, params=params)
|
|
81
85
|
|
|
82
|
-
return
|
|
86
|
+
return WorkflowDesign.model_validate(response.json())
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"""API for interacting with the designer service."""
|
|
2
|
+
|
|
3
|
+
from typing import Generator, Iterator, NamedTuple
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import pandas as pd
|
|
7
|
+
from openprotein.base import APISession
|
|
8
|
+
from openprotein.csv import csv_stream
|
|
9
|
+
from openprotein.schemas import (
|
|
10
|
+
Criteria,
|
|
11
|
+
Criterion,
|
|
12
|
+
Design,
|
|
13
|
+
DesignConstraint,
|
|
14
|
+
DesignJob,
|
|
15
|
+
Job,
|
|
16
|
+
Subcriterion,
|
|
17
|
+
)
|
|
18
|
+
from pydantic import TypeAdapter
|
|
19
|
+
|
|
20
|
+
PATH_PREFIX = "v1/designer/design"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def designs_list(session: APISession) -> list[Design]:
|
|
24
|
+
"""
|
|
25
|
+
List designs.
|
|
26
|
+
|
|
27
|
+
Parameters
|
|
28
|
+
----------
|
|
29
|
+
session : APISession
|
|
30
|
+
Session object for API communication.
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
-------
|
|
34
|
+
list[Design]
|
|
35
|
+
List of designs.
|
|
36
|
+
"""
|
|
37
|
+
endpoint = PATH_PREFIX
|
|
38
|
+
response = session.get(endpoint)
|
|
39
|
+
return TypeAdapter(list[Design]).validate_python(response.json())
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def design_get(session: APISession, design_id: str) -> Design:
|
|
43
|
+
"""
|
|
44
|
+
Get design.
|
|
45
|
+
|
|
46
|
+
Parameters
|
|
47
|
+
----------
|
|
48
|
+
session : APISession
|
|
49
|
+
Session object for API communication.
|
|
50
|
+
design_id: str
|
|
51
|
+
ID of design to get.
|
|
52
|
+
|
|
53
|
+
Returns
|
|
54
|
+
-------
|
|
55
|
+
Design
|
|
56
|
+
Design metadata.
|
|
57
|
+
"""
|
|
58
|
+
endpoint = PATH_PREFIX + f"/{design_id}"
|
|
59
|
+
response = session.get(endpoint)
|
|
60
|
+
return TypeAdapter(Design).validate_python(response.json())
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def designer_create_genetic_algorithm(
|
|
64
|
+
session: APISession,
|
|
65
|
+
assay_id: str,
|
|
66
|
+
criteria: Criteria | Subcriterion | Criterion,
|
|
67
|
+
num_steps: int = 25,
|
|
68
|
+
pop_size: int = 1024, # TODO - rename to library_size
|
|
69
|
+
n_offsprings: int = 5120,
|
|
70
|
+
crossover_prob: float = 1.0,
|
|
71
|
+
crossover_prob_pointwise: float = 0.2,
|
|
72
|
+
mutation_average_mutations_per_seq: int = 1,
|
|
73
|
+
allowed_tokens: DesignConstraint | dict[int, list[str]] = {},
|
|
74
|
+
) -> Job:
|
|
75
|
+
"""
|
|
76
|
+
Create design using genetic algorithm.
|
|
77
|
+
|
|
78
|
+
Parameters
|
|
79
|
+
----------
|
|
80
|
+
session : APISession
|
|
81
|
+
Session object for API communication.
|
|
82
|
+
assay_id : str
|
|
83
|
+
Assay ID to fit GP on.
|
|
84
|
+
criteria: list[list[DesignCriterion]]
|
|
85
|
+
List of list of design criteria, logically grouping by OR then AND.
|
|
86
|
+
num_steps: int, optional
|
|
87
|
+
The number of steps in the genetic algorithm. Default is 8.
|
|
88
|
+
pop_size: int, optional
|
|
89
|
+
The population size for the genetic algorithm. Default is 256.
|
|
90
|
+
n_offsprings: int, optional
|
|
91
|
+
The number of offspring for the genetic algorithm. Default is 5120.
|
|
92
|
+
crossover_prob: float, optional
|
|
93
|
+
The crossover probability for the genetic algorithm. Default is 1.
|
|
94
|
+
crossover_prob_pointwise: float, optional
|
|
95
|
+
The pointwise crossover probability for the genetic algorithm. Default is 0.2.
|
|
96
|
+
mutation_average_mutations_per_seq: int, optional
|
|
97
|
+
The average number of mutations per sequence. Default is 1.
|
|
98
|
+
allowed_tokens: DesignConstraint | dict[int, list[str]]
|
|
99
|
+
A dict of positions and allows tokens (e.g. *{1:['G','L']})* ) designating how mutations may occur. Defaults to empty dict.
|
|
100
|
+
|
|
101
|
+
Returns
|
|
102
|
+
-------
|
|
103
|
+
DesignJob
|
|
104
|
+
"""
|
|
105
|
+
if isinstance(criteria, Subcriterion):
|
|
106
|
+
criteria = Criteria([Criterion([criteria])])
|
|
107
|
+
elif isinstance(criteria, Criterion):
|
|
108
|
+
criteria = Criteria([criteria])
|
|
109
|
+
|
|
110
|
+
endpoint = PATH_PREFIX + "/genetic-algorithm"
|
|
111
|
+
|
|
112
|
+
body = {
|
|
113
|
+
"assay_id": assay_id,
|
|
114
|
+
"criteria": criteria.model_dump(),
|
|
115
|
+
"num_steps": num_steps,
|
|
116
|
+
"pop_size": pop_size,
|
|
117
|
+
"n_offsprings": n_offsprings,
|
|
118
|
+
"crossover_prob": crossover_prob,
|
|
119
|
+
"crossover_prob_pointwise": crossover_prob_pointwise,
|
|
120
|
+
"mutation_average_mutations_per_seq": mutation_average_mutations_per_seq,
|
|
121
|
+
"allowed_tokens": allowed_tokens,
|
|
122
|
+
}
|
|
123
|
+
response = session.post(endpoint, json=body)
|
|
124
|
+
return DesignJob.model_validate(response.json())
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def design_delete(session: APISession, design_id: str):
|
|
128
|
+
raise NotImplementedError()
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def designer_get_design_results(
|
|
132
|
+
session: APISession,
|
|
133
|
+
design_id: str,
|
|
134
|
+
step: int | None = -1,
|
|
135
|
+
) -> Iterator[list[str]]:
|
|
136
|
+
"""
|
|
137
|
+
Get csv encoded results for a design ID.
|
|
138
|
+
|
|
139
|
+
Parameters
|
|
140
|
+
----------
|
|
141
|
+
session : APISession
|
|
142
|
+
Session object for API communication.
|
|
143
|
+
design_id : str
|
|
144
|
+
Design ID to retrieve results from.
|
|
145
|
+
step: int | None, optional
|
|
146
|
+
Step of the design whose results to fetch. Defaults to -1, which refers to the last step.
|
|
147
|
+
|
|
148
|
+
Returns
|
|
149
|
+
-------
|
|
150
|
+
bytes
|
|
151
|
+
"""
|
|
152
|
+
params = {}
|
|
153
|
+
if step is not None:
|
|
154
|
+
params["step"] = step
|
|
155
|
+
endpoint = PATH_PREFIX + f"/{design_id}/results"
|
|
156
|
+
response = session.get(endpoint, params=params, stream=True)
|
|
157
|
+
return csv_stream(response)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class DesignResult(NamedTuple):
|
|
161
|
+
step: int
|
|
162
|
+
sample_index: int
|
|
163
|
+
sequence: str
|
|
164
|
+
scores: np.ndarray
|
|
165
|
+
subscores: np.ndarray
|
|
166
|
+
means: np.ndarray
|
|
167
|
+
vars: np.ndarray
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def decode_design_result(
|
|
171
|
+
row: list[str],
|
|
172
|
+
score_start_index: int,
|
|
173
|
+
subscore_start_index: int,
|
|
174
|
+
pred_start_index: int,
|
|
175
|
+
) -> DesignResult:
|
|
176
|
+
"""
|
|
177
|
+
Decode prediction scores.
|
|
178
|
+
|
|
179
|
+
Args:
|
|
180
|
+
data (bytes): raw bytes encoding the array received over the API
|
|
181
|
+
batched (bool): whether or not the result was batched. affects the retrieved csv format whether they contain additional columns and header rows.
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
mus (np.ndarray): decoded array of means
|
|
185
|
+
vars (np.ndarray): decoded array of variances
|
|
186
|
+
"""
|
|
187
|
+
scores = np.array(
|
|
188
|
+
[float(score) for score in row[score_start_index:subscore_start_index]]
|
|
189
|
+
)
|
|
190
|
+
subscores = np.array(
|
|
191
|
+
[float(subscore) for subscore in row[subscore_start_index:pred_start_index]]
|
|
192
|
+
)
|
|
193
|
+
preds = np.array([float(pred) for pred in row[pred_start_index:]])
|
|
194
|
+
result = DesignResult(
|
|
195
|
+
step=int(row[0]),
|
|
196
|
+
sample_index=int(row[1]),
|
|
197
|
+
sequence=row[2],
|
|
198
|
+
scores=scores,
|
|
199
|
+
subscores=subscores,
|
|
200
|
+
means=preds[::2],
|
|
201
|
+
vars=preds[1::2],
|
|
202
|
+
)
|
|
203
|
+
return result
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def decode_design_results_stream(
|
|
207
|
+
data: Iterator[list[str]], header: list[str] | None = None
|
|
208
|
+
) -> Generator[DesignResult, None, None]:
|
|
209
|
+
"""
|
|
210
|
+
Decode design results.
|
|
211
|
+
|
|
212
|
+
Args:
|
|
213
|
+
data: Iterator[list[str]]
|
|
214
|
+
Data in the form of an iterator of list of string-encoded values
|
|
215
|
+
header: list[str] | None, optional
|
|
216
|
+
Headers describing the data. Should be same length as each row returned from the data iterator.
|
|
217
|
+
Defaults to None, which means the first row in the iterator should be header.
|
|
218
|
+
|
|
219
|
+
Returns:
|
|
220
|
+
step: int
|
|
221
|
+
Step index of the design.
|
|
222
|
+
sample_index: int
|
|
223
|
+
Index of the sample in the overall design.
|
|
224
|
+
sequence: str
|
|
225
|
+
Output designed sequence.
|
|
226
|
+
scores: np.ndarray[float]
|
|
227
|
+
M array of scores based on provided criteria (M groups of subcriteria).
|
|
228
|
+
subscores: np.ndarray[float]
|
|
229
|
+
N array of subscores based on provided criteria (flattened N subcriteria).
|
|
230
|
+
means: np.ndarray[float]
|
|
231
|
+
K array of means for each model subscriterion.
|
|
232
|
+
vars: np.ndarray[float]
|
|
233
|
+
K array of variances for each model subscriterion.
|
|
234
|
+
vars (np.ndarray): decoded array of variances
|
|
235
|
+
"""
|
|
236
|
+
if header is None:
|
|
237
|
+
header = next(data)
|
|
238
|
+
if header[0].isnumeric():
|
|
239
|
+
raise ValueError(
|
|
240
|
+
"Expected first row in data to be header of 'step','sample_index',..."
|
|
241
|
+
)
|
|
242
|
+
score_start_index = subscore_start_index = pred_start_index = len(header)
|
|
243
|
+
# first start indices
|
|
244
|
+
for i, col_name in enumerate(header):
|
|
245
|
+
if col_name.startswith("score"):
|
|
246
|
+
score_start_index = i
|
|
247
|
+
break
|
|
248
|
+
for i, col_name in enumerate(header[score_start_index:]):
|
|
249
|
+
if col_name.endswith("score"):
|
|
250
|
+
subscore_start_index = score_start_index + i
|
|
251
|
+
break
|
|
252
|
+
for i, col_name in enumerate(header[subscore_start_index:]):
|
|
253
|
+
if col_name.endswith("y_mu"):
|
|
254
|
+
pred_start_index = subscore_start_index + i
|
|
255
|
+
break
|
|
256
|
+
for row in data:
|
|
257
|
+
yield decode_design_result(
|
|
258
|
+
row=row,
|
|
259
|
+
score_start_index=score_start_index,
|
|
260
|
+
subscore_start_index=subscore_start_index,
|
|
261
|
+
pred_start_index=pred_start_index,
|
|
262
|
+
)
|
openprotein/api/embedding.py
CHANGED
|
@@ -3,10 +3,19 @@ import random
|
|
|
3
3
|
from typing import Iterator
|
|
4
4
|
|
|
5
5
|
import numpy as np
|
|
6
|
-
from openprotein.api.align import csv_stream
|
|
7
6
|
from openprotein.base import APISession
|
|
7
|
+
from openprotein.csv import csv_stream
|
|
8
8
|
from openprotein.errors import InvalidParameterError
|
|
9
|
-
from openprotein.schemas import
|
|
9
|
+
from openprotein.schemas import (
|
|
10
|
+
AttnJob,
|
|
11
|
+
EmbeddingsJob,
|
|
12
|
+
GenerateJob,
|
|
13
|
+
JobType,
|
|
14
|
+
LogitsJob,
|
|
15
|
+
ModelMetadata,
|
|
16
|
+
ScoreJob,
|
|
17
|
+
ScoreSingleSiteJob,
|
|
18
|
+
)
|
|
10
19
|
from pydantic import TypeAdapter
|
|
11
20
|
|
|
12
21
|
PATH_PREFIX = "v1/embeddings"
|
|
@@ -36,7 +45,9 @@ def get_model(session: APISession, model_id: str) -> ModelMetadata:
|
|
|
36
45
|
return ModelMetadata(**result)
|
|
37
46
|
|
|
38
47
|
|
|
39
|
-
def get_request_sequences(
|
|
48
|
+
def get_request_sequences(
|
|
49
|
+
session: APISession, job_id: str, job_type: JobType = JobType.embeddings_embed
|
|
50
|
+
) -> list[bytes]:
|
|
40
51
|
"""
|
|
41
52
|
Get results associated with the given request ID.
|
|
42
53
|
|
|
@@ -51,13 +62,18 @@ def get_request_sequences(session: APISession, job_id: str) -> list[bytes]:
|
|
|
51
62
|
-------
|
|
52
63
|
sequences : List[bytes]
|
|
53
64
|
"""
|
|
54
|
-
|
|
65
|
+
# NOTE - allow to handle svd/embed and umap/embed directly too instead of redirect
|
|
66
|
+
path = "v1" + job_type.value
|
|
67
|
+
endpoint = path + f"/{job_id}/sequences"
|
|
55
68
|
response = session.get(endpoint)
|
|
56
69
|
return TypeAdapter(list[bytes]).validate_python(response.json())
|
|
57
70
|
|
|
58
71
|
|
|
59
72
|
def request_get_sequence_result(
|
|
60
|
-
session: APISession,
|
|
73
|
+
session: APISession,
|
|
74
|
+
job_id: str,
|
|
75
|
+
sequence: str | bytes,
|
|
76
|
+
job_type: JobType = JobType.embeddings_embed,
|
|
61
77
|
) -> bytes:
|
|
62
78
|
"""
|
|
63
79
|
Get encoded result for a sequence from the request ID.
|
|
@@ -75,9 +91,11 @@ def request_get_sequence_result(
|
|
|
75
91
|
-------
|
|
76
92
|
result : bytes
|
|
77
93
|
"""
|
|
94
|
+
# NOTE - allow to handle svd/embed and umap/embed directly too instead of redirect
|
|
95
|
+
path = "v1" + job_type.value
|
|
78
96
|
if isinstance(sequence, bytes):
|
|
79
97
|
sequence = sequence.decode()
|
|
80
|
-
endpoint =
|
|
98
|
+
endpoint = path + f"/{job_id}/{sequence}"
|
|
81
99
|
response = session.get(endpoint)
|
|
82
100
|
return response.content
|
|
83
101
|
|
|
@@ -256,7 +274,7 @@ def request_score_post(
|
|
|
256
274
|
model_id: str,
|
|
257
275
|
sequences: list[bytes] | list[str],
|
|
258
276
|
prompt_id: str | None = None,
|
|
259
|
-
) ->
|
|
277
|
+
) -> ScoreJob:
|
|
260
278
|
"""
|
|
261
279
|
POST a request for sequence scoring for the given model ID. \
|
|
262
280
|
Returns a Job object referring to this request \
|
|
@@ -284,15 +302,15 @@ def request_score_post(
|
|
|
284
302
|
if prompt_id is not None:
|
|
285
303
|
body["prompt_id"] = prompt_id
|
|
286
304
|
response = session.post(endpoint, json=body)
|
|
287
|
-
return
|
|
305
|
+
return ScoreJob.model_validate(response.json())
|
|
288
306
|
|
|
289
307
|
|
|
290
308
|
def request_score_single_site_post(
|
|
291
309
|
session: APISession,
|
|
292
310
|
model_id: str,
|
|
293
|
-
base_sequence: bytes,
|
|
311
|
+
base_sequence: bytes | str,
|
|
294
312
|
prompt_id: str | None = None,
|
|
295
|
-
) ->
|
|
313
|
+
) -> ScoreSingleSiteJob:
|
|
296
314
|
"""
|
|
297
315
|
POST a request for single site mutation scoring for the given model ID. \
|
|
298
316
|
Returns a Job object referring to this request \
|
|
@@ -314,12 +332,16 @@ def request_score_single_site_post(
|
|
|
314
332
|
endpoint = PATH_PREFIX + f"/models/{model_id}/score_single_site"
|
|
315
333
|
|
|
316
334
|
body: dict = {
|
|
317
|
-
"base_sequence":
|
|
335
|
+
"base_sequence": (
|
|
336
|
+
base_sequence.decode()
|
|
337
|
+
if isinstance(base_sequence, bytes)
|
|
338
|
+
else base_sequence
|
|
339
|
+
),
|
|
318
340
|
}
|
|
319
341
|
if prompt_id is not None:
|
|
320
342
|
body["prompt_id"] = prompt_id
|
|
321
343
|
response = session.post(endpoint, json=body)
|
|
322
|
-
return
|
|
344
|
+
return ScoreSingleSiteJob.model_validate(response.json())
|
|
323
345
|
|
|
324
346
|
|
|
325
347
|
def request_generate_post(
|
|
@@ -332,7 +354,7 @@ def request_generate_post(
|
|
|
332
354
|
max_length: int = 1000,
|
|
333
355
|
random_seed: int | None = None,
|
|
334
356
|
prompt_id: str | None = None,
|
|
335
|
-
) ->
|
|
357
|
+
) -> GenerateJob:
|
|
336
358
|
"""
|
|
337
359
|
POST a request for sequence generation for the given model ID. \
|
|
338
360
|
Returns a Job object referring to this request \
|
|
@@ -364,7 +386,7 @@ def request_generate_post(
|
|
|
364
386
|
random_seed = random.randrange(2**32)
|
|
365
387
|
|
|
366
388
|
body: dict = {
|
|
367
|
-
"
|
|
389
|
+
"n_sequences": num_samples,
|
|
368
390
|
"temperature": temperature,
|
|
369
391
|
"maxlen": max_length,
|
|
370
392
|
}
|
|
@@ -377,4 +399,4 @@ def request_generate_post(
|
|
|
377
399
|
if prompt_id is not None:
|
|
378
400
|
body["prompt_id"] = prompt_id
|
|
379
401
|
response = session.post(endpoint, json=body)
|
|
380
|
-
return
|
|
402
|
+
return GenerateJob.model_validate(response.json())
|
openprotein/api/fold.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from openprotein.api.embedding import ModelMetadata
|
|
2
2
|
from openprotein.base import APISession
|
|
3
|
-
from openprotein.schemas import
|
|
3
|
+
from openprotein.schemas import FoldJob
|
|
4
4
|
from pydantic import TypeAdapter
|
|
5
5
|
|
|
6
6
|
PATH_PREFIX = "v1/fold"
|
|
@@ -80,7 +80,7 @@ def fold_models_esmfold_post(
|
|
|
80
80
|
session: APISession,
|
|
81
81
|
sequences: list[bytes],
|
|
82
82
|
num_recycles: int | None = None,
|
|
83
|
-
) ->
|
|
83
|
+
) -> FoldJob:
|
|
84
84
|
"""
|
|
85
85
|
POST a request for structure prediction using ESMFold. Returns a Job object referring to this request
|
|
86
86
|
that can be used to retrieve results later.
|
|
@@ -108,7 +108,7 @@ def fold_models_esmfold_post(
|
|
|
108
108
|
body["num_recycles"] = num_recycles
|
|
109
109
|
|
|
110
110
|
response = session.post(endpoint, json=body)
|
|
111
|
-
return
|
|
111
|
+
return FoldJob.model_validate(response.json())
|
|
112
112
|
|
|
113
113
|
|
|
114
114
|
def fold_models_alphafold2_post(
|
|
@@ -117,7 +117,7 @@ def fold_models_alphafold2_post(
|
|
|
117
117
|
num_recycles: int | None = None,
|
|
118
118
|
num_models: int = 1,
|
|
119
119
|
num_relax: int = 0,
|
|
120
|
-
) ->
|
|
120
|
+
) -> FoldJob:
|
|
121
121
|
"""
|
|
122
122
|
POST a request for structure prediction using AlphaFold2. Returns a Job object referring to this request
|
|
123
123
|
that can be used to retrieve results later.
|
|
@@ -152,4 +152,4 @@ def fold_models_alphafold2_post(
|
|
|
152
152
|
response = session.post(endpoint, json=body)
|
|
153
153
|
# GET endpoint for AF2 expects the query sequence (first sequence) within the MSA
|
|
154
154
|
# since we don't know what the is, leave the sequence out of the future to be retrieved when calling get()
|
|
155
|
-
return
|
|
155
|
+
return FoldJob.model_validate(response.json())
|