openprotein-python 0.5.0.dev0__py3-none-any.whl → 0.5.1__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/api/embedding.py +22 -10
- openprotein/api/fold.py +5 -5
- openprotein/api/predictor.py +74 -5
- openprotein/api/svd.py +46 -3
- openprotein/app/models/align/msa.py +1 -8
- openprotein/app/models/align/prompt.py +1 -6
- openprotein/app/models/design.py +9 -3
- openprotein/app/models/embeddings/base.py +29 -14
- openprotein/app/models/embeddings/future.py +25 -4
- openprotein/app/models/embeddings/poet.py +37 -22
- 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 +34 -9
- openprotein/app/models/predictor/predict.py +3 -3
- openprotein/app/models/predictor/predictor.py +60 -5
- openprotein/app/models/svd.py +50 -10
- openprotein/app/models/train.py +11 -8
- openprotein/base.py +33 -31
- openprotein/schemas/__init__.py +9 -1
- openprotein/schemas/align.py +7 -13
- openprotein/schemas/design.py +2 -2
- openprotein/schemas/job.py +13 -0
- openprotein/schemas/train.py +3 -3
- {openprotein_python-0.5.0.dev0.dist-info → openprotein_python-0.5.1.dist-info}/METADATA +3 -5
- {openprotein_python-0.5.0.dev0.dist-info → openprotein_python-0.5.1.dist-info}/RECORD +28 -28
- {openprotein_python-0.5.0.dev0.dist-info → openprotein_python-0.5.1.dist-info}/WHEEL +1 -1
- {openprotein_python-0.5.0.dev0.dist-info → openprotein_python-0.5.1.dist-info}/licenses/LICENSE.txt +0 -0
|
@@ -403,29 +403,44 @@ class PagedFuture(StreamingFuture, ABC):
|
|
|
403
403
|
|
|
404
404
|
with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
|
|
405
405
|
# submit the paged requests
|
|
406
|
-
futures =
|
|
406
|
+
futures: dict[concurrent.futures.Future, int] = {}
|
|
407
|
+
index: int = 0
|
|
407
408
|
for _ in range(num_workers * 2):
|
|
408
409
|
f = executor.submit(self.get_slice, offset, offset + step)
|
|
409
|
-
futures
|
|
410
|
+
futures[f] = index
|
|
411
|
+
index += 1
|
|
410
412
|
offset += step
|
|
411
413
|
|
|
412
414
|
# until we've retrieved all pages (known by retrieving a page with less than the requested number of records)
|
|
413
415
|
done = False
|
|
414
416
|
while not done:
|
|
415
|
-
|
|
417
|
+
results: list[list | None] = [None] * len(futures)
|
|
418
|
+
futures_next: dict[concurrent.futures.Future, int] = {}
|
|
419
|
+
index_next: int = 0
|
|
420
|
+
next_result_index = 0
|
|
416
421
|
# iterate the futures and submit new requests as needed
|
|
417
422
|
for f in concurrent.futures.as_completed(futures):
|
|
423
|
+
index = futures[f]
|
|
418
424
|
result_page = f.result()
|
|
425
|
+
results[index] = result_page
|
|
419
426
|
# check if we're done, meaning the result page is not full
|
|
420
427
|
done = done or len(result_page) < step
|
|
421
428
|
# if we aren't done, submit another request
|
|
422
429
|
if not done:
|
|
423
430
|
f = executor.submit(self.get_slice, offset, offset + step)
|
|
424
|
-
futures_next
|
|
431
|
+
futures_next[f] = index_next
|
|
432
|
+
index_next += 1
|
|
425
433
|
offset += step
|
|
426
434
|
# yield the results from this page
|
|
427
|
-
|
|
428
|
-
|
|
435
|
+
while (
|
|
436
|
+
next_result_index < len(results)
|
|
437
|
+
and results[next_result_index] is not None
|
|
438
|
+
):
|
|
439
|
+
result_page = results[next_result_index]
|
|
440
|
+
assert result_page is not None # checked above
|
|
441
|
+
for result in result_page:
|
|
442
|
+
yield result
|
|
443
|
+
next_result_index += 1
|
|
429
444
|
# update the list of futures and wait on them again
|
|
430
445
|
futures = futures_next
|
|
431
446
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
1
3
|
from openprotein.api import predict
|
|
2
4
|
from openprotein.base import APISession
|
|
3
5
|
from openprotein.schemas import (
|
|
@@ -8,6 +10,8 @@ from openprotein.schemas import (
|
|
|
8
10
|
|
|
9
11
|
from .futures import Future, PagedFuture
|
|
10
12
|
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
11
15
|
|
|
12
16
|
class PredictFuture(PagedFuture, Future):
|
|
13
17
|
"""Future Job for manipulating results"""
|
|
@@ -32,10 +36,9 @@ class PredictFuture(PagedFuture, Future):
|
|
|
32
36
|
def id(self):
|
|
33
37
|
return self.job.job_id
|
|
34
38
|
|
|
35
|
-
def _fmt_results(self,
|
|
36
|
-
results = job.result
|
|
39
|
+
def _fmt_results(self, results: list[WorkflowPredictJob.SequencePrediction]):
|
|
37
40
|
dict_results = {}
|
|
38
|
-
if results
|
|
41
|
+
if len(results) > 0:
|
|
39
42
|
properties = set(
|
|
40
43
|
list(i["properties"].keys())[0]
|
|
41
44
|
for i in results[0].model_dump()["predictions"]
|
|
@@ -53,10 +56,11 @@ class PredictFuture(PagedFuture, Future):
|
|
|
53
56
|
}
|
|
54
57
|
return dict_results
|
|
55
58
|
|
|
56
|
-
def _fmt_ssp_results(
|
|
57
|
-
results
|
|
59
|
+
def _fmt_ssp_results(
|
|
60
|
+
self, results: list[WorkflowPredictSingleSiteJob.MutantPrediction]
|
|
61
|
+
):
|
|
58
62
|
dict_results = {}
|
|
59
|
-
if results
|
|
63
|
+
if len(results) > 0:
|
|
60
64
|
properties = set(
|
|
61
65
|
list(i["properties"].keys())[0]
|
|
62
66
|
for i in results[0].model_dump()["predictions"]
|
|
@@ -109,13 +113,34 @@ class PredictFuture(PagedFuture, Future):
|
|
|
109
113
|
# else:
|
|
110
114
|
# return self._fmt_ssp_results(results)
|
|
111
115
|
|
|
112
|
-
def
|
|
113
|
-
|
|
114
|
-
|
|
116
|
+
def get_dict(self, verbose: bool = False) -> dict:
|
|
117
|
+
|
|
118
|
+
results: list = []
|
|
119
|
+
num_returned = self.page_size
|
|
120
|
+
offset = 0
|
|
121
|
+
|
|
122
|
+
while num_returned >= self.page_size:
|
|
123
|
+
try:
|
|
124
|
+
predict_job_results = self.get_results(
|
|
125
|
+
page_offset=offset, page_size=self.page_size
|
|
126
|
+
)
|
|
127
|
+
if predict_job_results.result is not None:
|
|
128
|
+
results += predict_job_results.result
|
|
129
|
+
num_returned = len(predict_job_results.result)
|
|
130
|
+
offset += num_returned
|
|
131
|
+
except Exception as exc:
|
|
132
|
+
if verbose:
|
|
133
|
+
logging.error(f"Failed to get results: {exc}")
|
|
134
|
+
|
|
135
|
+
if self.job.job_type == JobType.workflow_predict_single_site:
|
|
115
136
|
return self._fmt_ssp_results(results)
|
|
116
137
|
else:
|
|
117
138
|
return self._fmt_results(results)
|
|
118
139
|
|
|
140
|
+
def get_slice(self, start: int, end: int):
|
|
141
|
+
results = self.get_results(page_size=end - start, page_offset=start)
|
|
142
|
+
return results.result or [] # could be none
|
|
143
|
+
|
|
119
144
|
def get_results(
|
|
120
145
|
self, page_size: int | None = None, page_offset: int | None = None
|
|
121
146
|
) -> WorkflowPredictSingleSiteJob | WorkflowPredictJob:
|
|
@@ -59,9 +59,9 @@ class PredictionResultFuture(Future):
|
|
|
59
59
|
data = predictor.predictor_predict_get_sequence_result(
|
|
60
60
|
self.session, self.job.job_id, sequence
|
|
61
61
|
)
|
|
62
|
-
return predictor.
|
|
62
|
+
return predictor.decode_predict(data)
|
|
63
63
|
|
|
64
|
-
def get(self) -> tuple[np.ndarray, np.ndarray]:
|
|
64
|
+
def get(self, verbose: bool = False) -> tuple[np.ndarray, np.ndarray]:
|
|
65
65
|
"""
|
|
66
66
|
Get embedding results for specified sequence.
|
|
67
67
|
|
|
@@ -75,4 +75,4 @@ class PredictionResultFuture(Future):
|
|
|
75
75
|
data = predictor.predictor_predict_get_batched_result(
|
|
76
76
|
self.session, self.job.job_id
|
|
77
77
|
)
|
|
78
|
-
return predictor.
|
|
78
|
+
return predictor.decode_predict(data, batched=True)
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import numpy as np
|
|
1
2
|
from openprotein.api import assaydata
|
|
2
3
|
from openprotein.api import job as job_api
|
|
3
4
|
from openprotein.api import predictor, svd
|
|
4
5
|
from openprotein.base import APISession
|
|
5
6
|
from openprotein.errors import InvalidParameterError
|
|
6
|
-
from openprotein.schemas import PredictorMetadata, TrainJob
|
|
7
|
+
from openprotein.schemas import CVJob, PredictorMetadata, TrainJob
|
|
7
8
|
|
|
8
9
|
from ..assaydata import AssayDataset
|
|
9
10
|
from ..embeddings import EmbeddingModel
|
|
@@ -12,6 +13,37 @@ from ..svd import SVDModel
|
|
|
12
13
|
from .predict import PredictionResultFuture
|
|
13
14
|
|
|
14
15
|
|
|
16
|
+
class CVResultFuture(Future):
|
|
17
|
+
"""Future Job for manipulating results"""
|
|
18
|
+
|
|
19
|
+
job: CVJob
|
|
20
|
+
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
session: APISession,
|
|
24
|
+
job: CVJob,
|
|
25
|
+
):
|
|
26
|
+
super().__init__(session, job)
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def id(self):
|
|
30
|
+
return self.job.job_id
|
|
31
|
+
|
|
32
|
+
def get(self, verbose: bool = False) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
33
|
+
"""
|
|
34
|
+
Get embedding results for specified sequence.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
sequence (bytes): sequence to fetch results for
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
mu (np.ndarray): means of predictions
|
|
41
|
+
var (np.ndarray): variances of predictions
|
|
42
|
+
"""
|
|
43
|
+
data = predictor.predictor_crossvalidate_get(self.session, self.job.job_id)
|
|
44
|
+
return predictor.decode_crossvalidate(data)
|
|
45
|
+
|
|
46
|
+
|
|
15
47
|
class PredictorModel(Future):
|
|
16
48
|
"""
|
|
17
49
|
Class providing predict endpoint for fitted predictor models.
|
|
@@ -48,15 +80,15 @@ class PredictorModel(Future):
|
|
|
48
80
|
|
|
49
81
|
@property
|
|
50
82
|
def id(self):
|
|
51
|
-
return self.
|
|
83
|
+
return self._metadata.id
|
|
52
84
|
|
|
53
85
|
@property
|
|
54
86
|
def reduction(self):
|
|
55
|
-
return self.
|
|
87
|
+
return self._metadata.model_spec.features.reduction
|
|
56
88
|
|
|
57
89
|
@property
|
|
58
90
|
def sequence_length(self):
|
|
59
|
-
if (constraints := self.
|
|
91
|
+
if (constraints := self._metadata.model_spec.constraints) is not None:
|
|
60
92
|
return constraints.sequence_length
|
|
61
93
|
return None
|
|
62
94
|
|
|
@@ -68,7 +100,7 @@ class PredictorModel(Future):
|
|
|
68
100
|
|
|
69
101
|
@property
|
|
70
102
|
def training_properties(self) -> list[str]:
|
|
71
|
-
return self.
|
|
103
|
+
return self._metadata.training_dataset.properties
|
|
72
104
|
|
|
73
105
|
@property
|
|
74
106
|
def metadata(self):
|
|
@@ -125,6 +157,16 @@ class PredictorModel(Future):
|
|
|
125
157
|
),
|
|
126
158
|
)
|
|
127
159
|
|
|
160
|
+
def crossvalidate(self, n_splits: int | None = None) -> CVResultFuture:
|
|
161
|
+
return CVResultFuture.create(
|
|
162
|
+
session=self.session,
|
|
163
|
+
job=predictor.predictor_crossvalidate_post(
|
|
164
|
+
session=self.session,
|
|
165
|
+
predictor_id=self.id,
|
|
166
|
+
n_splits=n_splits,
|
|
167
|
+
),
|
|
168
|
+
)
|
|
169
|
+
|
|
128
170
|
def predict(self, sequences: list[bytes] | list[str]) -> PredictionResultFuture:
|
|
129
171
|
if self.sequence_length is not None:
|
|
130
172
|
for sequence in sequences:
|
|
@@ -139,3 +181,16 @@ class PredictorModel(Future):
|
|
|
139
181
|
session=self.session, predictor_id=self.id, sequences=sequences
|
|
140
182
|
),
|
|
141
183
|
)
|
|
184
|
+
|
|
185
|
+
def single_site(self, sequence: bytes | str) -> PredictionResultFuture:
|
|
186
|
+
if self.sequence_length is not None:
|
|
187
|
+
if len(sequence) != self.sequence_length:
|
|
188
|
+
raise InvalidParameterError(
|
|
189
|
+
f"Expected sequence to predict to be of length {self.sequence_length}"
|
|
190
|
+
)
|
|
191
|
+
return PredictionResultFuture.create(
|
|
192
|
+
session=self.session,
|
|
193
|
+
job=predictor.predictor_predict_single_site_post(
|
|
194
|
+
session=self.session, predictor_id=self.id, base_sequence=sequence
|
|
195
|
+
),
|
|
196
|
+
)
|
openprotein/app/models/svd.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
from typing import TYPE_CHECKING
|
|
2
2
|
|
|
3
|
+
import numpy as np
|
|
3
4
|
from openprotein.api import assaydata
|
|
4
5
|
from openprotein.api import job as job_api
|
|
5
6
|
from openprotein.api import predictor, svd
|
|
6
7
|
from openprotein.base import APISession
|
|
7
8
|
from openprotein.errors import InvalidParameterError
|
|
8
|
-
from openprotein.schemas import FeatureType, FitJob, SVDMetadata
|
|
9
|
+
from openprotein.schemas import FeatureType, FitJob, SVDEmbeddingsJob, SVDMetadata
|
|
9
10
|
|
|
10
11
|
from .assaydata import AssayDataset, AssayMetadata
|
|
11
12
|
from .embeddings import EmbeddingModel, EmbeddingResultFuture
|
|
@@ -50,19 +51,19 @@ class SVDModel(Future):
|
|
|
50
51
|
|
|
51
52
|
@property
|
|
52
53
|
def id(self):
|
|
53
|
-
return self.
|
|
54
|
+
return self._metadata.id
|
|
54
55
|
|
|
55
56
|
@property
|
|
56
57
|
def n_components(self):
|
|
57
|
-
return self.
|
|
58
|
+
return self._metadata.n_components
|
|
58
59
|
|
|
59
60
|
@property
|
|
60
61
|
def sequence_length(self):
|
|
61
|
-
return self.
|
|
62
|
+
return self._metadata.sequence_length
|
|
62
63
|
|
|
63
64
|
@property
|
|
64
65
|
def reduction(self):
|
|
65
|
-
return self.
|
|
66
|
+
return self._metadata.reduction
|
|
66
67
|
|
|
67
68
|
@property
|
|
68
69
|
def metadata(self):
|
|
@@ -102,7 +103,9 @@ class SVDModel(Future):
|
|
|
102
103
|
"""
|
|
103
104
|
return svd.svd_get_sequences(session=self.session, svd_id=self.id)
|
|
104
105
|
|
|
105
|
-
def embed(
|
|
106
|
+
def embed(
|
|
107
|
+
self, sequences: list[bytes] | list[str], **kwargs
|
|
108
|
+
) -> EmbeddingResultFuture:
|
|
106
109
|
"""
|
|
107
110
|
Use this SVD model to get reduced embeddings from input sequences.
|
|
108
111
|
|
|
@@ -118,7 +121,10 @@ class SVDModel(Future):
|
|
|
118
121
|
"""
|
|
119
122
|
return EmbeddingResultFuture.create(
|
|
120
123
|
session=self.session,
|
|
121
|
-
job=svd.svd_embed_post(
|
|
124
|
+
job=svd.svd_embed_post(
|
|
125
|
+
session=self.session, svd_id=self.id, sequences=sequences, **kwargs
|
|
126
|
+
),
|
|
127
|
+
sequences=sequences,
|
|
122
128
|
)
|
|
123
129
|
|
|
124
130
|
def fit_gp(
|
|
@@ -147,9 +153,14 @@ class SVDModel(Future):
|
|
|
147
153
|
from .predictor import PredictorModel
|
|
148
154
|
|
|
149
155
|
model_id = self.id
|
|
156
|
+
# get assay if str
|
|
157
|
+
assay = (
|
|
158
|
+
assaydata.get_assay_metadata(session=self.session, assay_id=assay)
|
|
159
|
+
if isinstance(assay, str)
|
|
160
|
+
else assay
|
|
161
|
+
)
|
|
150
162
|
# extract assay_id
|
|
151
|
-
if isinstance(assay,
|
|
152
|
-
assay = assaydata.get_assay_metadata(session=self.session, assay_id=assay)
|
|
163
|
+
assay_id = assay.assay_id if isinstance(assay, AssayMetadata) else assay.id
|
|
153
164
|
if (
|
|
154
165
|
self.sequence_length is not None
|
|
155
166
|
and assay.sequence_length != self.sequence_length
|
|
@@ -157,7 +168,17 @@ class SVDModel(Future):
|
|
|
157
168
|
raise InvalidParameterError(
|
|
158
169
|
f"Expected dataset to be of sequence length {self.sequence_length} due to svd fitted constraints"
|
|
159
170
|
)
|
|
160
|
-
|
|
171
|
+
if len(properties) == 0:
|
|
172
|
+
raise InvalidParameterError("Expected (at-least) 1 property to train")
|
|
173
|
+
if not set(properties) <= set(assay.measurement_names):
|
|
174
|
+
raise InvalidParameterError(
|
|
175
|
+
f"Expected all provided properties to be a subset of assay's measurements: {assay.measurement_names}"
|
|
176
|
+
)
|
|
177
|
+
# TODO - support multitask
|
|
178
|
+
if len(properties) > 1:
|
|
179
|
+
raise InvalidParameterError(
|
|
180
|
+
"Training a multitask GP is not yet supported (i.e. number of properties should only be 1 for now)"
|
|
181
|
+
)
|
|
161
182
|
job = predictor.predictor_fit_gp_post(
|
|
162
183
|
session=self.session,
|
|
163
184
|
assay_id=assay_id,
|
|
@@ -169,3 +190,22 @@ class SVDModel(Future):
|
|
|
169
190
|
**kwargs,
|
|
170
191
|
)
|
|
171
192
|
return PredictorModel.create(session=self.session, job=job)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
class SVDEmbeddingResultFuture(EmbeddingResultFuture, Future):
|
|
196
|
+
"""Future for manipulating results for embeddings-related requests."""
|
|
197
|
+
|
|
198
|
+
job: SVDEmbeddingsJob
|
|
199
|
+
|
|
200
|
+
def get_item(self, sequence: bytes) -> np.ndarray:
|
|
201
|
+
"""
|
|
202
|
+
Get embedding results for specified sequence.
|
|
203
|
+
|
|
204
|
+
Args:
|
|
205
|
+
sequence (bytes): sequence to fetch results for
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
np.ndarray: embeddings
|
|
209
|
+
"""
|
|
210
|
+
data = svd.embed_get_sequence_result(self.session, self.job.job_id, sequence)
|
|
211
|
+
return svd.embed_decode(data)
|
openprotein/app/models/train.py
CHANGED
|
@@ -128,7 +128,7 @@ class TrainFuture(Future):
|
|
|
128
128
|
CVFuture
|
|
129
129
|
The cross-validation job associated with this training job.
|
|
130
130
|
"""
|
|
131
|
-
|
|
131
|
+
cv_future = CVFuture.create(
|
|
132
132
|
session=self.session,
|
|
133
133
|
job=train.crossvalidate(
|
|
134
134
|
session=self.session,
|
|
@@ -136,6 +136,8 @@ class TrainFuture(Future):
|
|
|
136
136
|
),
|
|
137
137
|
train_job_id=self.id,
|
|
138
138
|
)
|
|
139
|
+
self.crossvalidation = cv_future
|
|
140
|
+
return cv_future
|
|
139
141
|
|
|
140
142
|
def list_models(self):
|
|
141
143
|
"""
|
|
@@ -233,7 +235,7 @@ class CVFuture(PagedFuture, Future):
|
|
|
233
235
|
self,
|
|
234
236
|
session: APISession,
|
|
235
237
|
job: WorkflowCVJob,
|
|
236
|
-
train_job_id: str,
|
|
238
|
+
train_job_id: str | None = None,
|
|
237
239
|
page_size: int = 1000,
|
|
238
240
|
):
|
|
239
241
|
"""
|
|
@@ -249,6 +251,11 @@ class CVFuture(PagedFuture, Future):
|
|
|
249
251
|
The Job object for this cross-validation job.
|
|
250
252
|
"""
|
|
251
253
|
super().__init__(session=session, job=job, page_size=page_size)
|
|
254
|
+
if train_job_id is None:
|
|
255
|
+
assert (
|
|
256
|
+
job.prerequisite_job_id is not None
|
|
257
|
+
), "expected prerequisite train job id"
|
|
258
|
+
train_job_id = job.prerequisite_job_id
|
|
252
259
|
self.train_job_id = train_job_id
|
|
253
260
|
|
|
254
261
|
def __str__(self) -> str:
|
|
@@ -261,12 +268,8 @@ class CVFuture(PagedFuture, Future):
|
|
|
261
268
|
def id(self):
|
|
262
269
|
return self.job.job_id
|
|
263
270
|
|
|
264
|
-
def _fmt_results(self, results: WorkflowCVJob):
|
|
265
|
-
return
|
|
266
|
-
[i.model_dump() for i in results.result]
|
|
267
|
-
if results.result is not None
|
|
268
|
-
else []
|
|
269
|
-
)
|
|
271
|
+
def _fmt_results(self, results: WorkflowCVJob) -> list[WorkflowCVItem]:
|
|
272
|
+
return results.result if results.result is not None else []
|
|
270
273
|
|
|
271
274
|
def get_slice(self, start: int, end: int):
|
|
272
275
|
results = self.get_crossvalidation(page_size=end - start, page_offset=start)
|
openprotein/base.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import openprotein.config as config
|
|
2
|
-
|
|
3
|
-
import requests
|
|
4
|
-
from urllib.parse import urljoin
|
|
5
1
|
from typing import Union
|
|
2
|
+
from urllib.parse import urljoin
|
|
6
3
|
|
|
4
|
+
import requests
|
|
7
5
|
from requests.adapters import HTTPAdapter
|
|
8
6
|
from requests.packages.urllib3.util.retry import Retry
|
|
9
|
-
|
|
7
|
+
|
|
8
|
+
import openprotein.config as config
|
|
9
|
+
from openprotein.errors import APIError, AuthError, HTTPError
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
class BearerAuth(requests.auth.AuthBase):
|
|
@@ -38,23 +38,28 @@ class APISession(requests.Session):
|
|
|
38
38
|
>>> session = APISession("username", "password")
|
|
39
39
|
"""
|
|
40
40
|
|
|
41
|
-
def __init__(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
def __init__(
|
|
42
|
+
self,
|
|
43
|
+
username: str,
|
|
44
|
+
password: str,
|
|
45
|
+
backend: str = "https://api.openprotein.ai/api/",
|
|
46
|
+
timeout: int = 180,
|
|
47
|
+
):
|
|
45
48
|
super().__init__()
|
|
46
49
|
self.backend = backend
|
|
47
50
|
self.verify = True
|
|
48
51
|
self.timeout = timeout
|
|
49
52
|
|
|
50
53
|
# Custom retry strategies
|
|
51
|
-
#auto retry for pesky connection reset errors and others
|
|
54
|
+
# auto retry for pesky connection reset errors and others
|
|
52
55
|
# 503 will catch if BE is refreshing
|
|
53
|
-
retry = Retry(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
retry = Retry(
|
|
57
|
+
total=4,
|
|
58
|
+
backoff_factor=3, # 0,1,4,13s
|
|
59
|
+
status_forcelist=[500, 502, 503, 504, 101, 104],
|
|
60
|
+
)
|
|
56
61
|
adapter = HTTPAdapter(max_retries=retry)
|
|
57
|
-
self.mount(
|
|
62
|
+
self.mount("https://", adapter)
|
|
58
63
|
self.login(username, password)
|
|
59
64
|
|
|
60
65
|
def post(self, url, data=None, json=None, **kwargs):
|
|
@@ -68,30 +73,27 @@ class APISession(requests.Session):
|
|
|
68
73
|
:rtype: requests.Response
|
|
69
74
|
"""
|
|
70
75
|
timeout = self.timeout
|
|
71
|
-
if
|
|
72
|
-
timeout = kwargs.pop(
|
|
73
|
-
|
|
74
|
-
return self.request(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def login(self, username:str, password:str):
|
|
82
|
-
"""
|
|
76
|
+
if "timeout" in kwargs:
|
|
77
|
+
timeout = kwargs.pop("timeout")
|
|
78
|
+
|
|
79
|
+
return self.request(
|
|
80
|
+
"POST", url, data=data, json=json, timeout=timeout, **kwargs
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
def login(self, username: str, password: str):
|
|
84
|
+
"""
|
|
83
85
|
Authenticate connection to OpenProtein with your credentials.
|
|
84
|
-
|
|
86
|
+
|
|
85
87
|
Parameters
|
|
86
88
|
-----------
|
|
87
89
|
username: str
|
|
88
|
-
username
|
|
90
|
+
username
|
|
89
91
|
password: str
|
|
90
92
|
password
|
|
91
93
|
"""
|
|
92
94
|
self.auth = self._get_auth_token(username, password)
|
|
93
95
|
|
|
94
|
-
def _get_auth_token(self, username:str, password:str):
|
|
96
|
+
def _get_auth_token(self, username: str, password: str):
|
|
95
97
|
endpoint = "v1/login/access-token"
|
|
96
98
|
url = urljoin(self.backend, endpoint)
|
|
97
99
|
try:
|
|
@@ -106,9 +108,9 @@ class APISession(requests.Session):
|
|
|
106
108
|
|
|
107
109
|
result = response.json()
|
|
108
110
|
token = result.get("access_token")
|
|
109
|
-
if token is None:
|
|
111
|
+
if token is None:
|
|
110
112
|
raise AuthError("Unable to authenticate with given credentials.")
|
|
111
|
-
return BearerAuth(token)
|
|
113
|
+
return BearerAuth(token)
|
|
112
114
|
|
|
113
115
|
def request(
|
|
114
116
|
self, method: Union[str, bytes], url: Union[str, bytes], *args, **kwargs
|
openprotein/schemas/__init__.py
CHANGED
|
@@ -16,7 +16,15 @@ from .predict import (
|
|
|
16
16
|
PredictJob as WorkflowPredictJob,
|
|
17
17
|
PredictSingleSiteJob as WorkflowPredictSingleSiteJob,
|
|
18
18
|
)
|
|
19
|
-
from .design import
|
|
19
|
+
from .design import (
|
|
20
|
+
ModelCriterion,
|
|
21
|
+
Criterion,
|
|
22
|
+
DesignJobCreate,
|
|
23
|
+
DesignMetadata,
|
|
24
|
+
DesignResults,
|
|
25
|
+
DesignStep,
|
|
26
|
+
DesignJob,
|
|
27
|
+
)
|
|
20
28
|
from .deprecated.poet import (
|
|
21
29
|
PoetScoreJob,
|
|
22
30
|
PoetSSPJob,
|
openprotein/schemas/align.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from enum import Enum
|
|
2
2
|
from typing import Literal
|
|
3
3
|
|
|
4
|
-
from pydantic import BaseModel, Field
|
|
4
|
+
from pydantic import BaseModel, Field
|
|
5
5
|
|
|
6
6
|
from .job import Job, JobType
|
|
7
7
|
|
|
@@ -34,22 +34,16 @@ class PromptPostParams(BaseModel):
|
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
class MSAJob(Job):
|
|
37
|
-
msa_id: str | None = None
|
|
38
37
|
job_type: Literal[JobType.align_align]
|
|
39
38
|
|
|
40
|
-
@
|
|
41
|
-
def
|
|
42
|
-
|
|
43
|
-
values["msa_id"] = values.get("job_id")
|
|
44
|
-
return values
|
|
39
|
+
@property
|
|
40
|
+
def msa_id(self):
|
|
41
|
+
return self.msa_id
|
|
45
42
|
|
|
46
43
|
|
|
47
44
|
class PromptJob(MSAJob, Job):
|
|
48
|
-
prompt_id: str | None = None
|
|
49
45
|
job_type: Literal[JobType.align_prompt]
|
|
50
46
|
|
|
51
|
-
@
|
|
52
|
-
def
|
|
53
|
-
|
|
54
|
-
values["prompt_id"] = values.get("job_id")
|
|
55
|
-
return values
|
|
47
|
+
@property
|
|
48
|
+
def prompt_id(self):
|
|
49
|
+
return self.job_id
|
openprotein/schemas/design.py
CHANGED
|
@@ -24,8 +24,8 @@ class DesignStep(BaseModel):
|
|
|
24
24
|
sequence: str
|
|
25
25
|
# scores: List[int]
|
|
26
26
|
# subscores_metadata: List[List[DesignSubscore]]
|
|
27
|
-
scores: list[float]
|
|
28
|
-
|
|
27
|
+
# scores: list[float]
|
|
28
|
+
scores: list[list[DesignSubscore]] = Field(..., alias="subscores_metadata")
|
|
29
29
|
# umap1: float
|
|
30
30
|
# umap2: float
|
|
31
31
|
|
openprotein/schemas/job.py
CHANGED
|
@@ -102,6 +102,19 @@ class Job(BaseModel):
|
|
|
102
102
|
raise ValueError(f"Error parsing job from obj: {obj}: {e}")
|
|
103
103
|
return job # type: ignore - static checker cannot know runtime type
|
|
104
104
|
|
|
105
|
+
# hide extra allowed fields
|
|
106
|
+
def __repr_args__(self):
|
|
107
|
+
for k, v in self.__dict__.items():
|
|
108
|
+
field = self.model_fields.get(k)
|
|
109
|
+
if field and field.repr:
|
|
110
|
+
yield k, v
|
|
111
|
+
|
|
112
|
+
yield from (
|
|
113
|
+
(k, getattr(self, k))
|
|
114
|
+
for k, v in self.model_computed_fields.items()
|
|
115
|
+
if v.repr
|
|
116
|
+
)
|
|
117
|
+
|
|
105
118
|
# allows to carry over subclassed job fields when factory creating
|
|
106
119
|
model_config = ConfigDict(extra="allow")
|
|
107
120
|
|
openprotein/schemas/train.py
CHANGED
|
@@ -16,9 +16,9 @@ class CVItem(BaseModel):
|
|
|
16
16
|
|
|
17
17
|
class CVJob(Job):
|
|
18
18
|
job_type: Literal[JobType.workflow_crossvalidate]
|
|
19
|
-
num_rows: int
|
|
20
|
-
page_size: int
|
|
21
|
-
page_offset: int
|
|
19
|
+
num_rows: int | None = None
|
|
20
|
+
page_size: int | None = None
|
|
21
|
+
page_offset: int | None = None
|
|
22
22
|
result: list[CVItem] | None = None
|
|
23
23
|
|
|
24
24
|
|
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: openprotein_python
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.1
|
|
4
4
|
Summary: OpenProtein Python interface.
|
|
5
5
|
Author-email: Mark Gee <markgee@ne47.bio>
|
|
6
|
-
License
|
|
7
|
-
License-File: LICENSE.txt
|
|
6
|
+
License: MIT
|
|
8
7
|
Classifier: Development Status :: 4 - Beta
|
|
9
8
|
Classifier: Programming Language :: Python :: 3
|
|
10
|
-
Requires-Python:
|
|
9
|
+
Requires-Python: >=3.10
|
|
11
10
|
Requires-Dist: numpy<3,>=2.1.1
|
|
12
11
|
Requires-Dist: pandas<3,>=2.1.4
|
|
13
12
|
Requires-Dist: pydantic<3,>=2.5
|
|
14
13
|
Requires-Dist: requests<3,>=2.32.3
|
|
15
14
|
Requires-Dist: tqdm<5,>=4.66.5
|
|
16
15
|
Provides-Extra: dev
|
|
17
|
-
Requires-Dist: hatchling<2,>=1.25.0; extra == 'dev'
|
|
18
16
|
Requires-Dist: ipykernel<7,>=6.29.5; extra == 'dev'
|
|
19
17
|
Requires-Dist: matplotlib<4,>=3.9.2; extra == 'dev'
|
|
20
18
|
Requires-Dist: pandas-stubs<3,>=2.1.4.240909; extra == 'dev'
|