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/app/models/train.py
CHANGED
|
@@ -12,7 +12,7 @@ from openprotein.schemas import (
|
|
|
12
12
|
|
|
13
13
|
from .assaydata import AssayDataset
|
|
14
14
|
from .futures import Future, PagedFuture
|
|
15
|
-
from .predict import
|
|
15
|
+
from .predict import PredictionResultFuture
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
class TrainFuture(Future):
|
|
@@ -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
|
"""
|
|
@@ -157,7 +159,7 @@ class TrainFuture(Future):
|
|
|
157
159
|
|
|
158
160
|
def predict(
|
|
159
161
|
self, sequences: list[str], model_ids: list[str] | None = None
|
|
160
|
-
) ->
|
|
162
|
+
) -> PredictionResultFuture:
|
|
161
163
|
"""
|
|
162
164
|
Creates a predict job based on the training job.
|
|
163
165
|
|
|
@@ -181,7 +183,7 @@ class TrainFuture(Future):
|
|
|
181
183
|
self,
|
|
182
184
|
sequence: str,
|
|
183
185
|
model_ids: list[str] | None = None,
|
|
184
|
-
) ->
|
|
186
|
+
) -> PredictionResultFuture:
|
|
185
187
|
"""
|
|
186
188
|
Creates a new Predict job for single site mutation analysis with a trained model.
|
|
187
189
|
|
|
@@ -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)
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from openprotein.api import job as job_api
|
|
3
|
+
from openprotein.api import umap
|
|
4
|
+
from openprotein.base import APISession
|
|
5
|
+
from openprotein.schemas import UMAPEmbeddingsJob, UMAPFitJob, UMAPMetadata
|
|
6
|
+
|
|
7
|
+
from .embeddings import EmbeddingModel, EmbeddingResultFuture
|
|
8
|
+
from .futures import Future
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class UMAPModel(Future):
|
|
12
|
+
"""
|
|
13
|
+
Class providing embedding endpoint for UMAP models. \
|
|
14
|
+
Also allows retrieving embeddings of sequences used to fit the UMAP with `get`.
|
|
15
|
+
Implements a Future to allow waiting for a fit job.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
job: UMAPFitJob
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
session: APISession,
|
|
23
|
+
job: UMAPFitJob | None = None,
|
|
24
|
+
metadata: UMAPMetadata | None = None,
|
|
25
|
+
):
|
|
26
|
+
"""Initializes with either job get or umap metadata get."""
|
|
27
|
+
if metadata is None:
|
|
28
|
+
# use job to fetch metadata
|
|
29
|
+
if job is None:
|
|
30
|
+
raise ValueError("Expected umap metadata or job")
|
|
31
|
+
metadata = umap.umap_get(session, job.job_id)
|
|
32
|
+
self._metadata = metadata
|
|
33
|
+
if job is None:
|
|
34
|
+
job = UMAPFitJob.create(
|
|
35
|
+
job_api.job_get(session=session, job_id=metadata.id)
|
|
36
|
+
)
|
|
37
|
+
self._sequences = None
|
|
38
|
+
self._embeddings = None
|
|
39
|
+
# getter initializes job if not provided
|
|
40
|
+
super().__init__(session, job)
|
|
41
|
+
|
|
42
|
+
def __str__(self) -> str:
|
|
43
|
+
return str(self.metadata)
|
|
44
|
+
|
|
45
|
+
def __repr__(self) -> str:
|
|
46
|
+
return repr(self.metadata)
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def id(self):
|
|
50
|
+
return self._metadata.id
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def n_components(self):
|
|
54
|
+
return self._metadata.n_components
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def n_neighbors(self):
|
|
58
|
+
return self._metadata.n_neighbors
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def min_dist(self):
|
|
62
|
+
return self._metadata.min_dist
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def sequence_length(self):
|
|
66
|
+
return self._metadata.sequence_length
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def reduction(self):
|
|
70
|
+
return self._metadata.reduction
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def metadata(self):
|
|
74
|
+
self._refresh_metadata()
|
|
75
|
+
return self._metadata
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def sequences(self):
|
|
79
|
+
if self._sequences is not None:
|
|
80
|
+
return self._sequences
|
|
81
|
+
self._sequences = self.get_inputs()
|
|
82
|
+
return self._sequences
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def embeddings(self):
|
|
86
|
+
if self._embeddings is not None:
|
|
87
|
+
return self._embeddings
|
|
88
|
+
data = umap.embed_get_batch_result(session=self.session, job_id=self.id)
|
|
89
|
+
embeddings = [
|
|
90
|
+
(seq, umap)
|
|
91
|
+
for seq, umap in zip(self.sequences, umap.embed_batch_decode(data))
|
|
92
|
+
]
|
|
93
|
+
self._embeddings = embeddings
|
|
94
|
+
return self._embeddings
|
|
95
|
+
|
|
96
|
+
def _refresh_metadata(self):
|
|
97
|
+
if not self._metadata.is_done():
|
|
98
|
+
self._metadata = umap.umap_get(self.session, self._metadata.id)
|
|
99
|
+
|
|
100
|
+
def get_model(self) -> EmbeddingModel:
|
|
101
|
+
"""Fetch embeddings model"""
|
|
102
|
+
model = EmbeddingModel.create(session=self.session, model_id=self._metadata.id)
|
|
103
|
+
return model
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def model(self) -> EmbeddingModel:
|
|
107
|
+
return self.get_model()
|
|
108
|
+
|
|
109
|
+
def delete(self) -> bool:
|
|
110
|
+
"""
|
|
111
|
+
Delete this UMAP model.
|
|
112
|
+
"""
|
|
113
|
+
return umap.umap_delete(self.session, self.id)
|
|
114
|
+
|
|
115
|
+
def get(self, verbose: bool = False):
|
|
116
|
+
return self.embeddings
|
|
117
|
+
|
|
118
|
+
def get_inputs(self) -> list[bytes]:
|
|
119
|
+
"""
|
|
120
|
+
Get sequences used for umap job.
|
|
121
|
+
|
|
122
|
+
Returns
|
|
123
|
+
-------
|
|
124
|
+
List[bytes]: list of sequences
|
|
125
|
+
"""
|
|
126
|
+
return umap.umap_get_sequences(session=self.session, umap_id=self.id)
|
|
127
|
+
|
|
128
|
+
def embed(
|
|
129
|
+
self, sequences: list[bytes] | list[str], **kwargs
|
|
130
|
+
) -> EmbeddingResultFuture:
|
|
131
|
+
"""
|
|
132
|
+
Use this UMAP model to get reduced embeddings from input sequences.
|
|
133
|
+
|
|
134
|
+
Parameters
|
|
135
|
+
----------
|
|
136
|
+
sequences : List[bytes]
|
|
137
|
+
List of protein sequences.
|
|
138
|
+
|
|
139
|
+
Returns
|
|
140
|
+
-------
|
|
141
|
+
EmbeddingResultFuture
|
|
142
|
+
Class for further job manipulation.
|
|
143
|
+
"""
|
|
144
|
+
return EmbeddingResultFuture.create(
|
|
145
|
+
session=self.session,
|
|
146
|
+
job=umap.umap_embed_post(
|
|
147
|
+
session=self.session, umap_id=self.id, sequences=sequences, **kwargs
|
|
148
|
+
),
|
|
149
|
+
sequences=sequences,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class UMAPEmbeddingResultFuture(EmbeddingResultFuture, Future):
|
|
154
|
+
"""Future for manipulating results for embeddings-related requests."""
|
|
155
|
+
|
|
156
|
+
job: UMAPEmbeddingsJob
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""
|
|
2
|
+
Application services for OpenProtein.
|
|
3
|
+
|
|
4
|
+
isort:skip_file
|
|
5
|
+
"""
|
|
2
6
|
|
|
3
|
-
from .align import AlignAPI
|
|
4
7
|
from .assaydata import AssayDataAPI
|
|
8
|
+
from .train import TrainingAPI
|
|
5
9
|
from .design import DesignAPI
|
|
10
|
+
from .align import AlignAPI
|
|
6
11
|
from .embeddings import EmbeddingsAPI
|
|
7
12
|
from .fold import FoldAPI
|
|
8
13
|
from .job import JobsAPI
|
|
9
|
-
from .predictor import PredictorAPI
|
|
10
14
|
from .svd import SVDAPI
|
|
11
|
-
from .
|
|
15
|
+
from .umap import UMAPAPI
|
|
16
|
+
from .predictor import PredictorAPI
|
|
17
|
+
from .designer import DesignerAPI
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from openprotein.api import design
|
|
2
|
-
from openprotein.app.models import
|
|
2
|
+
from openprotein.app.models import WorkflowDesignFuture
|
|
3
3
|
from openprotein.base import APISession
|
|
4
|
-
from openprotein.schemas import
|
|
4
|
+
from openprotein.schemas import WorkflowDesign, WorkflowDesignJobCreate
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
class DesignAPI:
|
|
@@ -10,7 +10,9 @@ class DesignAPI:
|
|
|
10
10
|
def __init__(self, session: APISession):
|
|
11
11
|
self.session = session
|
|
12
12
|
|
|
13
|
-
def create_design_job(
|
|
13
|
+
def create_design_job(
|
|
14
|
+
self, design_job: WorkflowDesignJobCreate
|
|
15
|
+
) -> WorkflowDesignFuture:
|
|
14
16
|
"""
|
|
15
17
|
Start a protein design job based on your assaydata, a trained ML model and Criteria (specified here).
|
|
16
18
|
|
|
@@ -19,7 +21,7 @@ class DesignAPI:
|
|
|
19
21
|
design_job : DesignJobCreate
|
|
20
22
|
The details of the design job to be created, with the following parameters:
|
|
21
23
|
- assay_id: The ID for the assay.
|
|
22
|
-
- criteria:
|
|
24
|
+
- criteria: Criteria for evaluating the design.
|
|
23
25
|
- num_steps: The number of steps in the genetic algo. Default is 8.
|
|
24
26
|
- pop_size: The population size for the genetic algo. Default is None.
|
|
25
27
|
- n_offsprings: The number of offspring for the genetic algo. Default is None.
|
|
@@ -33,7 +35,7 @@ class DesignAPI:
|
|
|
33
35
|
DesignFuture
|
|
34
36
|
The created job as a DesignFuture instance.
|
|
35
37
|
"""
|
|
36
|
-
return
|
|
38
|
+
return WorkflowDesignFuture.create(
|
|
37
39
|
session=self.session, job=design.create_design_job(self.session, design_job)
|
|
38
40
|
)
|
|
39
41
|
|
|
@@ -43,7 +45,7 @@ class DesignAPI:
|
|
|
43
45
|
step: int | None = None,
|
|
44
46
|
page_size: int | None = None,
|
|
45
47
|
page_offset: int | None = None,
|
|
46
|
-
) ->
|
|
48
|
+
) -> WorkflowDesign:
|
|
47
49
|
"""
|
|
48
50
|
Retrieves the results of a Design job.
|
|
49
51
|
|
|
@@ -60,7 +62,7 @@ class DesignAPI:
|
|
|
60
62
|
|
|
61
63
|
Returns
|
|
62
64
|
-------
|
|
63
|
-
|
|
65
|
+
WorkflowDesignJob
|
|
64
66
|
The job object representing the Design job.
|
|
65
67
|
|
|
66
68
|
Raises
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
from openprotein.api import designer
|
|
2
|
+
from openprotein.app.models import AssayDataset, DesignFuture
|
|
3
|
+
from openprotein.base import APISession
|
|
4
|
+
from openprotein.schemas import Criteria, Criterion, DesignConstraint, Subcriterion
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class DesignerAPI:
|
|
8
|
+
"""interface for calling Designer endpoints"""
|
|
9
|
+
|
|
10
|
+
def __init__(self, session: APISession):
|
|
11
|
+
self.session = session
|
|
12
|
+
|
|
13
|
+
def list_designs(self) -> list[DesignFuture]:
|
|
14
|
+
return [
|
|
15
|
+
DesignFuture(session=self.session, metadata=m)
|
|
16
|
+
for m in designer.designs_list(session=self.session)
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
def get_design(self, design_id: str) -> DesignFuture:
|
|
20
|
+
return DesignFuture(
|
|
21
|
+
session=self.session,
|
|
22
|
+
metadata=designer.design_get(session=self.session, design_id=design_id),
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
def create_genetic_algorithm_design(
|
|
26
|
+
self,
|
|
27
|
+
assay: AssayDataset,
|
|
28
|
+
criteria: Criteria | Subcriterion | Criterion,
|
|
29
|
+
num_steps: int = 25,
|
|
30
|
+
pop_size: int = 1024,
|
|
31
|
+
n_offsprings: int = 5120,
|
|
32
|
+
crossover_prob: float = 1.0,
|
|
33
|
+
crossover_prob_pointwise: float = 0.2,
|
|
34
|
+
mutation_average_mutations_per_seq: int = 1,
|
|
35
|
+
allowed_tokens: DesignConstraint | dict[int, list[str]] = {},
|
|
36
|
+
) -> DesignFuture:
|
|
37
|
+
"""
|
|
38
|
+
Start a protein design job based on your assaydata, a trained ML model and Criteria (specified here).
|
|
39
|
+
|
|
40
|
+
Parameters
|
|
41
|
+
----------
|
|
42
|
+
design_job : DesignJobCreate
|
|
43
|
+
The details of the design job to be created, with the following parameters:
|
|
44
|
+
- assay_id: The ID for the assay.
|
|
45
|
+
- criteria: Criteria for evaluating the design.
|
|
46
|
+
- num_steps: The number of steps in the genetic algo. Default is 8.
|
|
47
|
+
- pop_size: The population size for the genetic algo. Default is None.
|
|
48
|
+
- n_offsprings: The number of offspring for the genetic algo. Default is None.
|
|
49
|
+
- crossover_prob: The crossover probability for the genetic algo. Default is None.
|
|
50
|
+
- crossover_prob_pointwise: The pointwise crossover probability for the genetic algo. Default is None.
|
|
51
|
+
- mutation_average_mutations_per_seq: The average number of mutations per sequence. Default is None.
|
|
52
|
+
- allowed_tokens: A dict of positions and allows tokens (e.g. *{1:['G','L']})* ) designating how mutations may occur. Default is None.
|
|
53
|
+
|
|
54
|
+
Returns
|
|
55
|
+
-------
|
|
56
|
+
DesignFuture
|
|
57
|
+
The created job as a DesignFuture instance.
|
|
58
|
+
"""
|
|
59
|
+
return DesignFuture.create(
|
|
60
|
+
session=self.session,
|
|
61
|
+
job=designer.designer_create_genetic_algorithm(
|
|
62
|
+
self.session,
|
|
63
|
+
assay_id=assay.id,
|
|
64
|
+
criteria=criteria,
|
|
65
|
+
num_steps=num_steps,
|
|
66
|
+
pop_size=pop_size,
|
|
67
|
+
n_offsprings=n_offsprings,
|
|
68
|
+
crossover_prob=crossover_prob,
|
|
69
|
+
crossover_prob_pointwise=crossover_prob_pointwise,
|
|
70
|
+
mutation_average_mutations_per_seq=mutation_average_mutations_per_seq,
|
|
71
|
+
allowed_tokens=allowed_tokens,
|
|
72
|
+
),
|
|
73
|
+
)
|
openprotein/app/services/job.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
|
|
1
3
|
from openprotein import config
|
|
2
4
|
from openprotein.api import job
|
|
3
5
|
from openprotein.app.models import Future
|
|
4
6
|
from openprotein.base import APISession
|
|
5
|
-
from openprotein.schemas import Job
|
|
7
|
+
from openprotein.schemas import Job, JobStatus, JobType
|
|
6
8
|
|
|
7
9
|
|
|
8
10
|
class JobsAPI:
|
|
@@ -14,9 +16,19 @@ class JobsAPI:
|
|
|
14
16
|
self.session = session
|
|
15
17
|
|
|
16
18
|
def list(
|
|
17
|
-
self,
|
|
19
|
+
self,
|
|
20
|
+
status: JobStatus | None = None,
|
|
21
|
+
job_type: JobType | None = None,
|
|
22
|
+
assay_id: str | None = None,
|
|
23
|
+
more_recent_than: datetime | str | None = None,
|
|
24
|
+
limit: int = 100,
|
|
18
25
|
) -> list[Job]:
|
|
19
26
|
"""List jobs."""
|
|
27
|
+
more_recent_than_str = (
|
|
28
|
+
more_recent_than.isoformat()
|
|
29
|
+
if isinstance(more_recent_than, datetime)
|
|
30
|
+
else more_recent_than
|
|
31
|
+
)
|
|
20
32
|
return [
|
|
21
33
|
Job.create(j)
|
|
22
34
|
for j in job.jobs_list(
|
|
@@ -24,7 +36,8 @@ class JobsAPI:
|
|
|
24
36
|
status=status,
|
|
25
37
|
job_type=job_type,
|
|
26
38
|
assay_id=assay_id,
|
|
27
|
-
more_recent_than=
|
|
39
|
+
more_recent_than=more_recent_than_str,
|
|
40
|
+
limit=limit,
|
|
28
41
|
)
|
|
29
42
|
]
|
|
30
43
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
|
|
3
3
|
from openprotein.api import predict
|
|
4
|
-
from openprotein.app.models import
|
|
4
|
+
from openprotein.app.models import TrainFuture, WorkflowPredictionResultFuture
|
|
5
5
|
from openprotein.base import APISession
|
|
6
6
|
from openprotein.errors import InvalidParameterError
|
|
7
7
|
|
|
@@ -27,7 +27,7 @@ class PredictService:
|
|
|
27
27
|
sequences: list[str],
|
|
28
28
|
train_job: TrainFuture,
|
|
29
29
|
model_ids: list[str] | None = None,
|
|
30
|
-
) ->
|
|
30
|
+
) -> WorkflowPredictionResultFuture:
|
|
31
31
|
"""
|
|
32
32
|
Creates a new Predict job for a given list of sequences and a trained model.
|
|
33
33
|
|
|
@@ -69,8 +69,11 @@ class PredictService:
|
|
|
69
69
|
# f"train job has status {train_job.status.value}, Predict requires status SUCCESS"
|
|
70
70
|
# )
|
|
71
71
|
|
|
72
|
-
return
|
|
72
|
+
return WorkflowPredictionResultFuture.create(
|
|
73
73
|
session=self.session,
|
|
74
|
+
sequences=sequences,
|
|
75
|
+
train_job=train_job,
|
|
76
|
+
model_ids=model_ids,
|
|
74
77
|
job=predict.create_predict_job(
|
|
75
78
|
session=self.session,
|
|
76
79
|
sequences=sequences,
|
|
@@ -84,7 +87,7 @@ class PredictService:
|
|
|
84
87
|
sequence: str,
|
|
85
88
|
train_job: TrainFuture,
|
|
86
89
|
model_ids: list[str] | None = None,
|
|
87
|
-
) ->
|
|
90
|
+
) -> WorkflowPredictionResultFuture:
|
|
88
91
|
"""
|
|
89
92
|
Creates a new Predict job for single site mutation analysis with a trained model.
|
|
90
93
|
|
|
@@ -128,8 +131,11 @@ class PredictService:
|
|
|
128
131
|
# f"train job has status {train_job.status.value}, Predict requires status SUCCESS"
|
|
129
132
|
# )
|
|
130
133
|
|
|
131
|
-
return
|
|
134
|
+
return WorkflowPredictionResultFuture.create(
|
|
132
135
|
session=self.session,
|
|
136
|
+
sequence=sequence,
|
|
137
|
+
train_job=train_job,
|
|
138
|
+
model_ids=model_ids,
|
|
133
139
|
job=predict.create_predict_single_site(
|
|
134
140
|
session=self.session,
|
|
135
141
|
sequence=sequence,
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
from openprotein.api import umap
|
|
2
|
+
from openprotein.app.models import AssayDataset, EmbeddingModel, SVDModel, UMAPModel
|
|
3
|
+
from openprotein.base import APISession
|
|
4
|
+
from openprotein.schemas import ReductionType
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class UMAPAPI:
|
|
8
|
+
|
|
9
|
+
def __init__(self, session: APISession):
|
|
10
|
+
self.session = session
|
|
11
|
+
|
|
12
|
+
def fit_umap(
|
|
13
|
+
self,
|
|
14
|
+
model: EmbeddingModel | SVDModel,
|
|
15
|
+
sequences: list[bytes] | None = None,
|
|
16
|
+
assay: AssayDataset | None = None,
|
|
17
|
+
n_components: int = 2,
|
|
18
|
+
reduction: ReductionType | None = None,
|
|
19
|
+
**kwargs,
|
|
20
|
+
) -> UMAPModel:
|
|
21
|
+
"""
|
|
22
|
+
Fit an UMAP on the sequences with the specified model_id and hyperparameters (n_components).
|
|
23
|
+
|
|
24
|
+
Parameters
|
|
25
|
+
----------
|
|
26
|
+
model_id : str
|
|
27
|
+
The ID of the model to fit the UMAP on.
|
|
28
|
+
sequences : list[bytes]
|
|
29
|
+
The list of sequences to use for the UMAP fitting.
|
|
30
|
+
n_components : int, optional
|
|
31
|
+
The number of components for the UMAP, by default 1024.
|
|
32
|
+
reduction : str, optional
|
|
33
|
+
The reduction method to apply to the embeddings, by default None.
|
|
34
|
+
|
|
35
|
+
Returns
|
|
36
|
+
-------
|
|
37
|
+
UMAPModel
|
|
38
|
+
The model with the UMAP fit.
|
|
39
|
+
"""
|
|
40
|
+
return model.fit_umap(
|
|
41
|
+
sequences=sequences,
|
|
42
|
+
assay=assay,
|
|
43
|
+
n_components=n_components,
|
|
44
|
+
reduction=reduction,
|
|
45
|
+
**kwargs,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
def get_umap(self, umap_id: str) -> UMAPModel:
|
|
49
|
+
"""
|
|
50
|
+
Get UMAP job results. Including UMAP dimension and sequence lengths.
|
|
51
|
+
|
|
52
|
+
Requires a successful UMAP job from fit_umap
|
|
53
|
+
|
|
54
|
+
Parameters
|
|
55
|
+
----------
|
|
56
|
+
umap_id : str
|
|
57
|
+
The ID of the UMAP job.
|
|
58
|
+
Returns
|
|
59
|
+
-------
|
|
60
|
+
UMAPModel
|
|
61
|
+
The model with the UMAP fit.
|
|
62
|
+
"""
|
|
63
|
+
metadata = umap.umap_get(self.session, umap_id)
|
|
64
|
+
return UMAPModel(session=self.session, metadata=metadata)
|
|
65
|
+
|
|
66
|
+
def __delete_umap(self, umap_id: str) -> bool:
|
|
67
|
+
"""
|
|
68
|
+
Delete UMAP model.
|
|
69
|
+
|
|
70
|
+
Parameters
|
|
71
|
+
----------
|
|
72
|
+
umap_id : str
|
|
73
|
+
The ID of the UMAP job.
|
|
74
|
+
Returns
|
|
75
|
+
-------
|
|
76
|
+
bool
|
|
77
|
+
True: successful deletion
|
|
78
|
+
|
|
79
|
+
"""
|
|
80
|
+
return umap.umap_delete(self.session, umap_id)
|
|
81
|
+
|
|
82
|
+
def list_umap(self) -> list[UMAPModel]:
|
|
83
|
+
"""
|
|
84
|
+
List UMAP models made by user.
|
|
85
|
+
|
|
86
|
+
Takes no args.
|
|
87
|
+
|
|
88
|
+
Returns
|
|
89
|
+
-------
|
|
90
|
+
list[UMAPModel]
|
|
91
|
+
UMAPModels
|
|
92
|
+
|
|
93
|
+
"""
|
|
94
|
+
return [
|
|
95
|
+
UMAPModel(session=self.session, metadata=metadata)
|
|
96
|
+
for metadata in umap.umap_list_get(self.session)
|
|
97
|
+
]
|
openprotein/auth.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from openprotein.base import APISession
|
|
2
|
+
from openprotein.errors import AuthError, HTTPError
|
|
3
|
+
|
|
4
|
+
PATH_PREFIX = "v1/login"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_auth_token(session: APISession, username: str, password: str) -> str:
|
|
8
|
+
endpoint = PATH_PREFIX + "/access-token"
|
|
9
|
+
body = {"username": username, "password": password}
|
|
10
|
+
try:
|
|
11
|
+
response = session.post(endpoint, data=body, timeout=3)
|
|
12
|
+
except HTTPError as e:
|
|
13
|
+
# if an error occured during auth, we raise an AuthError with reference to the HTTPError
|
|
14
|
+
raise AuthError(
|
|
15
|
+
f"Authentication failed. Please check your credentials and connection."
|
|
16
|
+
) from e
|
|
17
|
+
result = response.json()
|
|
18
|
+
token = result.get("access_token")
|
|
19
|
+
if token is None:
|
|
20
|
+
raise AuthError("Unable to authenticate with given credentials.")
|
|
21
|
+
return token
|