strandfolio 0.1.0__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.
- strandfolio/__init__.py +5 -0
- strandfolio/__main__.py +7 -0
- strandfolio/application/__init__.py +1 -0
- strandfolio/application/alignment_session.py +414 -0
- strandfolio/application/assembly_session.py +208 -0
- strandfolio/application/digest_session.py +230 -0
- strandfolio/application/map_features.py +37 -0
- strandfolio/application/pcr_session.py +91 -0
- strandfolio/application/session.py +411 -0
- strandfolio/application/tasks.py +126 -0
- strandfolio/application/workspace.py +1082 -0
- strandfolio/data/__init__.py +9 -0
- strandfolio/data/databases/features/features.sqlite3 +0 -0
- strandfolio/data/databases/ladders/ladders.sqlite3 +0 -0
- strandfolio/data/databases/plasmids/maps/pBH4-His-SUMO-GFP-LPETGG.gb +195 -0
- strandfolio/data/examples/insert_demo_tm.json +48 -0
- strandfolio/data/examples/pReporter_demo.gb +109 -0
- strandfolio/data/icons/__init__.py +1 -0
- strandfolio/data/icons/control-check.svg +3 -0
- strandfolio/data/icons/control-chevron-up.svg +3 -0
- strandfolio/data/icons/control-chevron.svg +3 -0
- strandfolio/data/icons/control-minus-muted.svg +3 -0
- strandfolio/data/icons/control-minus.svg +3 -0
- strandfolio/data/icons/control-plus-muted.svg +3 -0
- strandfolio/data/icons/control-plus.svg +3 -0
- strandfolio/data/icons/icon_strandfolio_app.icns +0 -0
- strandfolio/data/icons/icon_strandfolio_app.ico +0 -0
- strandfolio/data/icons/icon_strandfolio_app.svg +11 -0
- strandfolio/data/icons/icon_strandfolio_gui.svg +11 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_1024x1024.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_128x128.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_16x16.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_20x20.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_24x24.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_256x256.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_32x32.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_40x40.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_48x48.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_512x512.png +0 -0
- strandfolio/data/icons/scales/icon_strandfolio_app_64x64.png +0 -0
- strandfolio/domain/__init__.py +1 -0
- strandfolio/domain/coordinates.py +103 -0
- strandfolio/domain/features.py +390 -0
- strandfolio/domain/fragment_annotations.py +205 -0
- strandfolio/domain/fragments.py +421 -0
- strandfolio/domain/primers.py +147 -0
- strandfolio/domain/provenance.py +110 -0
- strandfolio/domain/sequence.py +441 -0
- strandfolio/gui/__init__.py +1 -0
- strandfolio/gui/alignment_controller.py +235 -0
- strandfolio/gui/annotation_controller.py +201 -0
- strandfolio/gui/app.py +175 -0
- strandfolio/gui/assembly_example.py +198 -0
- strandfolio/gui/calculation_task.py +48 -0
- strandfolio/gui/controller.py +685 -0
- strandfolio/gui/digest_controller.py +1138 -0
- strandfolio/gui/figure_dialog.py +328 -0
- strandfolio/gui/import_task.py +102 -0
- strandfolio/gui/main.py +12 -0
- strandfolio/gui/panels/__init__.py +1 -0
- strandfolio/gui/panels/annotation_editor.py +239 -0
- strandfolio/gui/panels/choice_rows.py +207 -0
- strandfolio/gui/panels/dialog_layout.py +100 -0
- strandfolio/gui/panels/digest.py +875 -0
- strandfolio/gui/panels/document_dialogs.py +816 -0
- strandfolio/gui/panels/document_strip.py +385 -0
- strandfolio/gui/panels/features.py +765 -0
- strandfolio/gui/panels/fitted_label.py +32 -0
- strandfolio/gui/panels/label_review.py +211 -0
- strandfolio/gui/panels/map_features.py +84 -0
- strandfolio/gui/panels/sequence_conversion.py +446 -0
- strandfolio/gui/panels/sequence_details.py +336 -0
- strandfolio/gui/panels/sequence_text_size.py +48 -0
- strandfolio/gui/pcr_controller.py +765 -0
- strandfolio/gui/pcr_task.py +36 -0
- strandfolio/gui/sequence_export.py +300 -0
- strandfolio/gui/sequence_figure.py +212 -0
- strandfolio/gui/theme.py +342 -0
- strandfolio/gui/views/__init__.py +1 -0
- strandfolio/gui/views/alignment.py +1109 -0
- strandfolio/gui/views/alignment_matrix.py +231 -0
- strandfolio/gui/views/alignment_tracks.py +363 -0
- strandfolio/gui/views/assembly.py +725 -0
- strandfolio/gui/views/assembly_example.py +256 -0
- strandfolio/gui/views/assembly_junctions.py +92 -0
- strandfolio/gui/views/gel.py +638 -0
- strandfolio/gui/views/gel_preview.py +494 -0
- strandfolio/gui/views/history.py +464 -0
- strandfolio/gui/views/history_ancestry.py +717 -0
- strandfolio/gui/views/history_diagram.py +232 -0
- strandfolio/gui/views/multiple_alignment.py +369 -0
- strandfolio/gui/views/overview.py +312 -0
- strandfolio/gui/views/pcr.py +648 -0
- strandfolio/gui/views/pcr_result.py +451 -0
- strandfolio/gui/views/plasmid_map.py +683 -0
- strandfolio/gui/views/primer_structure_review.py +95 -0
- strandfolio/gui/views/reported_parent_map.py +90 -0
- strandfolio/gui/views/reported_parents.py +428 -0
- strandfolio/gui/views/restriction_sites.py +231 -0
- strandfolio/gui/views/sequence_tracks.py +276 -0
- strandfolio/gui/views/sequence_view.py +560 -0
- strandfolio/gui/window.py +1195 -0
- strandfolio/gui/workspace_icons.py +62 -0
- strandfolio/io/__init__.py +1 -0
- strandfolio/io/digestion.py +483 -0
- strandfolio/io/formats.py +1558 -0
- strandfolio/io/history_report.py +142 -0
- strandfolio/operations/__init__.py +1 -0
- strandfolio/operations/alignment.py +638 -0
- strandfolio/operations/conversion.py +741 -0
- strandfolio/operations/editing.py +570 -0
- strandfolio/operations/gel.py +356 -0
- strandfolio/operations/multiple_alignment.py +186 -0
- strandfolio/operations/pcr.py +713 -0
- strandfolio/operations/primer_structures.py +71 -0
- strandfolio/operations/restriction.py +585 -0
- strandfolio/rendering/__init__.py +1 -0
- strandfolio/rendering/circular.py +879 -0
- strandfolio/rendering/colors.py +36 -0
- strandfolio/rendering/gels.py +465 -0
- strandfolio/rendering/history.py +363 -0
- strandfolio/rendering/layout.py +575 -0
- strandfolio/rendering/maps.py +997 -0
- strandfolio/storage/__init__.py +1 -0
- strandfolio/storage/build_restriction_catalogue.py +258 -0
- strandfolio/storage/catalogues.py +489 -0
- strandfolio/storage/feature_catalogue.py +362 -0
- strandfolio/storage/pcr_codec.py +220 -0
- strandfolio/storage/project_backup.py +304 -0
- strandfolio/storage/project_digestions.py +343 -0
- strandfolio/storage/project_edits.py +248 -0
- strandfolio/storage/project_exports.py +544 -0
- strandfolio/storage/project_parents.py +164 -0
- strandfolio/storage/project_pcr.py +502 -0
- strandfolio/storage/project_runs.py +604 -0
- strandfolio/storage/projects.py +1401 -0
- strandfolio/storage/restriction_codec.py +353 -0
- strandfolio/storage/revision_codec.py +353 -0
- strandfolio-0.1.0.dist-info/METADATA +37 -0
- strandfolio-0.1.0.dist-info/RECORD +145 -0
- strandfolio-0.1.0.dist-info/WHEEL +4 -0
- strandfolio-0.1.0.dist-info/entry_points.txt +2 -0
- strandfolio-0.1.0.dist-info/licenses/LICENSE +28 -0
- strandfolio-0.1.0.dist-info/licenses/databases/plasmids/maps/SB2CL-LICENSE.txt +21 -0
- strandfolio-0.1.0.dist-info/licenses/docs/installation/DATA-NOTICES.md +7 -0
strandfolio/__init__.py
ADDED
strandfolio/__main__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Toolkit-independent document coordination."""
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
"""Role-bound live comparisons and exact session-retained alignment results."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable, Mapping
|
|
4
|
+
from dataclasses import dataclass, replace
|
|
5
|
+
from uuid import uuid4
|
|
6
|
+
|
|
7
|
+
from strandfolio.application.tasks import TaskInput, TaskRegistry, TaskRequest
|
|
8
|
+
from strandfolio.domain.sequence import MoleculeType, SequenceRevision
|
|
9
|
+
from strandfolio.operations.alignment import (
|
|
10
|
+
ALIGNMENT_ENGINE_VERSION,
|
|
11
|
+
PROTEIN_SUBSTITUTION_MATRIX,
|
|
12
|
+
AlignmentCanceled,
|
|
13
|
+
AlignmentResult,
|
|
14
|
+
AlignmentSettings,
|
|
15
|
+
alignPair,
|
|
16
|
+
alignmentFeatures,
|
|
17
|
+
alignmentInput,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# Each completed pair retains its own column mappings as long as the query is checked.
|
|
22
|
+
MAX_ALIGNMENT_QUERIES = 8
|
|
23
|
+
_OWNER = "alignment"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True, slots=True)
|
|
27
|
+
class AlignmentSelection:
|
|
28
|
+
"""One selected source tab, whole record or feature, and orientation."""
|
|
29
|
+
|
|
30
|
+
documentId: str
|
|
31
|
+
featureId: str | None = None
|
|
32
|
+
reverse: bool = False
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True, slots=True)
|
|
36
|
+
class RetainedAlignment:
|
|
37
|
+
"""A completed comparison and its exact immutable inputs for session inspection."""
|
|
38
|
+
|
|
39
|
+
comparisonId: str
|
|
40
|
+
result: AlignmentResult
|
|
41
|
+
referenceRevision: SequenceRevision
|
|
42
|
+
queryRevision: SequenceRevision
|
|
43
|
+
referenceName: str
|
|
44
|
+
queryName: str
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(frozen=True, slots=True)
|
|
48
|
+
class AlignmentCloseEffect:
|
|
49
|
+
"""A specific completed comparison that closing its source would remove."""
|
|
50
|
+
|
|
51
|
+
comparisonId: str
|
|
52
|
+
description: str
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass(frozen=True, slots=True)
|
|
56
|
+
class AlignmentDocument:
|
|
57
|
+
"""The current identity, revision and qualified label of an eligible source."""
|
|
58
|
+
|
|
59
|
+
binding: TaskInput
|
|
60
|
+
revision: SequenceRevision
|
|
61
|
+
name: str
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True, slots=True)
|
|
65
|
+
class AlignmentWork:
|
|
66
|
+
"""Capture one queued pair without waiting for alignment input extraction."""
|
|
67
|
+
|
|
68
|
+
request: TaskRequest
|
|
69
|
+
reference: AlignmentSelection
|
|
70
|
+
query: AlignmentSelection
|
|
71
|
+
referenceDocument: AlignmentDocument
|
|
72
|
+
queryDocument: AlignmentDocument
|
|
73
|
+
settings: AlignmentSettings
|
|
74
|
+
|
|
75
|
+
def calculate(
|
|
76
|
+
self, isCanceled: Callable[[], bool], align: Callable[..., AlignmentResult] = alignPair,
|
|
77
|
+
) -> AlignmentResult:
|
|
78
|
+
"""Resolve oriented inputs from the captured snapshots and align them."""
|
|
79
|
+
reference = alignmentInput(
|
|
80
|
+
self.referenceDocument.revision, self.reference.featureId, self.reference.reverse,
|
|
81
|
+
)
|
|
82
|
+
if isCanceled():
|
|
83
|
+
raise AlignmentCanceled()
|
|
84
|
+
query = alignmentInput(
|
|
85
|
+
self.queryDocument.revision, self.query.featureId, self.query.reverse,
|
|
86
|
+
)
|
|
87
|
+
return align(reference, query, self.settings, isCanceled=isCanceled)
|
|
88
|
+
|
|
89
|
+
def matches(self, result: AlignmentResult) -> bool:
|
|
90
|
+
"""Check that the accepted engine outcome corresponds to this exact request."""
|
|
91
|
+
isProtein = self.referenceDocument.revision.moleculeType == "protein"
|
|
92
|
+
return (
|
|
93
|
+
result.settings == self.settings
|
|
94
|
+
and (result.engineVersion,) == self.request.versions
|
|
95
|
+
and result.substitutionMatrix == (PROTEIN_SUBSTITUTION_MATRIX if isProtein else None)
|
|
96
|
+
and all(
|
|
97
|
+
(actual.recordId, actual.revisionId, actual.selection.featureId,
|
|
98
|
+
actual.reverseComplement, actual.moleculeType)
|
|
99
|
+
== (expected.revision.recordId, expected.revision.revisionId, selection.featureId,
|
|
100
|
+
selection.reverse, expected.revision.moleculeType)
|
|
101
|
+
for actual, expected, selection in (
|
|
102
|
+
(result.reference, self.referenceDocument, self.reference),
|
|
103
|
+
(result.query, self.queryDocument, self.query),
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@dataclass(slots=True)
|
|
110
|
+
class _LivePair:
|
|
111
|
+
selection: AlignmentSelection
|
|
112
|
+
work: AlignmentWork | None = None
|
|
113
|
+
comparison: RetainedAlignment | None = None
|
|
114
|
+
error: str = ""
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class AlignmentSession:
|
|
118
|
+
"""Keep independently relevant comparisons in workspace document order."""
|
|
119
|
+
|
|
120
|
+
def __init__(self, tasks: TaskRegistry) -> None:
|
|
121
|
+
self._tasks = tasks
|
|
122
|
+
self._documents: Mapping[str, AlignmentDocument] = {}
|
|
123
|
+
self._initialized = False
|
|
124
|
+
self._reference: AlignmentSelection | None = None
|
|
125
|
+
self._pairs: dict[str, _LivePair] = {}
|
|
126
|
+
self._settings = AlignmentSettings()
|
|
127
|
+
self._activeQueryId: str | None = None
|
|
128
|
+
self._retained: dict[str, RetainedAlignment] = {}
|
|
129
|
+
self._activeRetainedId: str | None = None
|
|
130
|
+
|
|
131
|
+
def synchronize(self, documents: Mapping[str, AlignmentDocument]) -> None:
|
|
132
|
+
"""Discard only the work and live results whose source binding changed."""
|
|
133
|
+
previous, self._documents = self._documents, documents
|
|
134
|
+
if self._reference is not None:
|
|
135
|
+
documentId = self._reference.documentId
|
|
136
|
+
if documentId not in documents:
|
|
137
|
+
self.setReference(None)
|
|
138
|
+
elif previous[documentId].binding != documents[documentId].binding:
|
|
139
|
+
self._reference = self._availableRegion(self._reference)
|
|
140
|
+
self._clearPairs()
|
|
141
|
+
for documentId, pair in tuple(self._pairs.items()):
|
|
142
|
+
if documentId not in documents or not self._compatibleQuery(documentId):
|
|
143
|
+
self._clearPair(pair)
|
|
144
|
+
del self._pairs[documentId]
|
|
145
|
+
elif previous[documentId].binding != documents[documentId].binding:
|
|
146
|
+
self._clearPair(pair)
|
|
147
|
+
pair.selection = self._availableRegion(pair.selection)
|
|
148
|
+
elif pair.work is not None and not self._tasks.isCurrent(pair.work.request):
|
|
149
|
+
self._clearPair(pair)
|
|
150
|
+
self._selectFirstAvailable()
|
|
151
|
+
|
|
152
|
+
def initialize(self, activeDocumentId: str | None) -> None:
|
|
153
|
+
"""Default the first eligible reference and the sole query on first entry."""
|
|
154
|
+
if self._initialized or not self._documents:
|
|
155
|
+
return
|
|
156
|
+
self._initialized = True
|
|
157
|
+
referenceId = (activeDocumentId if activeDocumentId in self._documents
|
|
158
|
+
else next(iter(self._documents)))
|
|
159
|
+
self.setReference(AlignmentSelection(referenceId))
|
|
160
|
+
compatible = [documentId for documentId in self._documents if self._compatibleQuery(documentId)]
|
|
161
|
+
if len(compatible) == 1:
|
|
162
|
+
self.setQuery(compatible[0], True)
|
|
163
|
+
|
|
164
|
+
@property
|
|
165
|
+
def reference(self) -> AlignmentSelection | None:
|
|
166
|
+
return self._reference
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def referenceMoleculeType(self) -> MoleculeType | None:
|
|
170
|
+
if self._reference is None:
|
|
171
|
+
return None
|
|
172
|
+
return self._documents[self._reference.documentId].revision.moleculeType
|
|
173
|
+
|
|
174
|
+
def _compatibleQuery(self, documentId: str) -> bool:
|
|
175
|
+
return self._reference is not None and (
|
|
176
|
+
documentId != self._reference.documentId
|
|
177
|
+
and self._documents[documentId].revision.moleculeType == self.referenceMoleculeType
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
@property
|
|
181
|
+
def queries(self) -> tuple[AlignmentSelection, ...]:
|
|
182
|
+
return tuple(pair.selection for pair in self._orderedPairs())
|
|
183
|
+
|
|
184
|
+
@property
|
|
185
|
+
def settings(self) -> AlignmentSettings:
|
|
186
|
+
return self._settings
|
|
187
|
+
|
|
188
|
+
@property
|
|
189
|
+
def activeQueryId(self) -> str | None:
|
|
190
|
+
return self._activeQueryId
|
|
191
|
+
|
|
192
|
+
@property
|
|
193
|
+
def activeResult(self) -> AlignmentResult | None:
|
|
194
|
+
retained = self.retainedComparison
|
|
195
|
+
return retained.result if retained is not None else self.resultFor(self._activeQueryId)
|
|
196
|
+
|
|
197
|
+
@property
|
|
198
|
+
def pendingQueryIds(self) -> frozenset[str]:
|
|
199
|
+
return frozenset(
|
|
200
|
+
documentId for documentId, pair in self._pairs.items() if pair.work is not None
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
@property
|
|
204
|
+
def nextWork(self) -> AlignmentWork | None:
|
|
205
|
+
return next((pair.work for pair in self._orderedPairs() if pair.work is not None), None)
|
|
206
|
+
|
|
207
|
+
@property
|
|
208
|
+
def canRun(self) -> bool:
|
|
209
|
+
return self._reference is not None and bool(self._pairs) and not self.pendingQueryIds
|
|
210
|
+
|
|
211
|
+
@property
|
|
212
|
+
def retainedComparisons(self) -> tuple[RetainedAlignment, ...]:
|
|
213
|
+
return tuple(self._retained.values())
|
|
214
|
+
|
|
215
|
+
@property
|
|
216
|
+
def retainedComparison(self) -> RetainedAlignment | None:
|
|
217
|
+
return self._retained.get(self._activeRetainedId)
|
|
218
|
+
|
|
219
|
+
def resultFor(self, queryId: str | None) -> AlignmentResult | None:
|
|
220
|
+
pair = self._pairs.get(queryId)
|
|
221
|
+
return pair.comparison.result if pair is not None and pair.comparison is not None else None
|
|
222
|
+
|
|
223
|
+
def errorFor(self, queryId: str) -> str:
|
|
224
|
+
pair = self._pairs.get(queryId)
|
|
225
|
+
return pair.error if pair is not None else ""
|
|
226
|
+
|
|
227
|
+
def setReference(self, selection: AlignmentSelection | None) -> bool:
|
|
228
|
+
"""Choose the reference and release the previous reference's entire live set."""
|
|
229
|
+
if selection == self._reference:
|
|
230
|
+
return False
|
|
231
|
+
if selection is not None:
|
|
232
|
+
self._validateSelection(selection)
|
|
233
|
+
self._initialized = True
|
|
234
|
+
self._reference = selection
|
|
235
|
+
self._clearPairs()
|
|
236
|
+
for documentId in tuple(self._pairs):
|
|
237
|
+
if not self._compatibleQuery(documentId):
|
|
238
|
+
del self._pairs[documentId]
|
|
239
|
+
return True
|
|
240
|
+
|
|
241
|
+
def setQuery(self, documentId: str, checked: bool) -> bool:
|
|
242
|
+
"""Add an eligible whole-record query or remove only that query's live pair."""
|
|
243
|
+
if checked == (documentId in self._pairs):
|
|
244
|
+
return False
|
|
245
|
+
if checked:
|
|
246
|
+
selection = AlignmentSelection(documentId)
|
|
247
|
+
self._validateSelection(selection)
|
|
248
|
+
if not self._compatibleQuery(documentId):
|
|
249
|
+
raise ValueError("Choose a reference and a different query of the same molecule type.")
|
|
250
|
+
if len(self._pairs) >= MAX_ALIGNMENT_QUERIES:
|
|
251
|
+
raise ValueError(f"Choose up to {MAX_ALIGNMENT_QUERIES} queries per alignment run.")
|
|
252
|
+
self._pairs[documentId] = _LivePair(selection)
|
|
253
|
+
else:
|
|
254
|
+
self._clearPair(self._pairs.pop(documentId))
|
|
255
|
+
self._selectFirstAvailable()
|
|
256
|
+
return True
|
|
257
|
+
|
|
258
|
+
def setQueryRegion(self, selection: AlignmentSelection) -> bool:
|
|
259
|
+
"""Update one query region or orientation and release its old work."""
|
|
260
|
+
pair = self._pairs.get(selection.documentId)
|
|
261
|
+
if pair is None or pair.selection == selection:
|
|
262
|
+
return False
|
|
263
|
+
self._validateSelection(selection)
|
|
264
|
+
self._clearPair(pair)
|
|
265
|
+
pair.selection = selection
|
|
266
|
+
self._selectFirstAvailable()
|
|
267
|
+
return True
|
|
268
|
+
|
|
269
|
+
def setSettings(self, settings: AlignmentSettings) -> bool:
|
|
270
|
+
"""Bind subsequent comparisons to new scoring settings."""
|
|
271
|
+
if settings == self._settings:
|
|
272
|
+
return False
|
|
273
|
+
self._settings = settings
|
|
274
|
+
self._clearPairs()
|
|
275
|
+
return True
|
|
276
|
+
|
|
277
|
+
def selectResult(self, queryId: str) -> bool:
|
|
278
|
+
"""Inspect a completed live result."""
|
|
279
|
+
if self.resultFor(queryId) is None:
|
|
280
|
+
return False
|
|
281
|
+
self._activeQueryId, self._activeRetainedId = queryId, None
|
|
282
|
+
return True
|
|
283
|
+
|
|
284
|
+
def retainResult(self, queryId: str | None = None) -> RetainedAlignment | None:
|
|
285
|
+
"""Keep a live result independently of later changes to its source tabs."""
|
|
286
|
+
pair = self._pairs.get(queryId if queryId is not None else self._activeQueryId)
|
|
287
|
+
if pair is None or pair.comparison is None:
|
|
288
|
+
return None
|
|
289
|
+
comparison = pair.comparison
|
|
290
|
+
self._retained[comparison.comparisonId] = comparison
|
|
291
|
+
return comparison
|
|
292
|
+
|
|
293
|
+
def selectRetained(self, comparisonId: str | None) -> bool:
|
|
294
|
+
"""Inspect a kept result or return to the currently selected live pair."""
|
|
295
|
+
if comparisonId is not None and comparisonId not in self._retained:
|
|
296
|
+
return False
|
|
297
|
+
self._activeRetainedId = comparisonId
|
|
298
|
+
return True
|
|
299
|
+
|
|
300
|
+
def beginComparisons(self) -> bool:
|
|
301
|
+
"""Queue missing comparisons, or rerun the completed set upon explicit request."""
|
|
302
|
+
reference = self._reference
|
|
303
|
+
if reference is None or not self.canRun:
|
|
304
|
+
return False
|
|
305
|
+
self._activeRetainedId = None
|
|
306
|
+
referenceDocument = self._documents[reference.documentId]
|
|
307
|
+
pairs = self._orderedPairs()
|
|
308
|
+
missing = [pair for pair in pairs if pair.comparison is None]
|
|
309
|
+
for pair in missing or pairs:
|
|
310
|
+
query = pair.selection
|
|
311
|
+
queryDocument = self._documents[query.documentId]
|
|
312
|
+
request = self._tasks.begin(
|
|
313
|
+
_OWNER, query.documentId, inputs=(
|
|
314
|
+
replace(referenceDocument.binding, role="reference"),
|
|
315
|
+
replace(queryDocument.binding, role="query"),
|
|
316
|
+
),
|
|
317
|
+
settings=(reference, query, self._settings), versions=(ALIGNMENT_ENGINE_VERSION,),
|
|
318
|
+
description=f"Align {queryDocument.name} to {referenceDocument.name}",
|
|
319
|
+
)
|
|
320
|
+
pair.work = AlignmentWork(
|
|
321
|
+
request, reference, query, referenceDocument, queryDocument, self._settings,
|
|
322
|
+
)
|
|
323
|
+
pair.comparison, pair.error = None, ""
|
|
324
|
+
self._selectFirstAvailable()
|
|
325
|
+
return True
|
|
326
|
+
|
|
327
|
+
def finish(
|
|
328
|
+
self, work: AlignmentWork, result: AlignmentResult | None, error: str | None,
|
|
329
|
+
) -> bool:
|
|
330
|
+
"""Publish one accepted completion and leave every other pair independent."""
|
|
331
|
+
pair = self._pairs.get(work.query.documentId)
|
|
332
|
+
if pair is None or pair.work is not work:
|
|
333
|
+
return False
|
|
334
|
+
pair.work = None
|
|
335
|
+
if not self._tasks.finish(work.request):
|
|
336
|
+
return False
|
|
337
|
+
if result is None:
|
|
338
|
+
pair.error = error or "Alignment did not produce a result."
|
|
339
|
+
elif not isinstance(result, AlignmentResult) or not work.matches(result):
|
|
340
|
+
pair.error = "Alignment returned a result for different inputs."
|
|
341
|
+
else:
|
|
342
|
+
pair.comparison = RetainedAlignment(
|
|
343
|
+
str(uuid4()), result, work.referenceDocument.revision, work.queryDocument.revision,
|
|
344
|
+
work.referenceDocument.name, work.queryDocument.name,
|
|
345
|
+
)
|
|
346
|
+
self._selectFirstAvailable()
|
|
347
|
+
return True
|
|
348
|
+
|
|
349
|
+
def cancelPending(self) -> bool:
|
|
350
|
+
"""Cancel running and queued comparisons and preserve completed peers."""
|
|
351
|
+
pending = self.pendingQueryIds
|
|
352
|
+
for documentId in pending:
|
|
353
|
+
pair = self._pairs[documentId]
|
|
354
|
+
self._clearPair(pair)
|
|
355
|
+
pair.error = "Alignment canceled."
|
|
356
|
+
return bool(pending)
|
|
357
|
+
|
|
358
|
+
def closeEffects(self, documentIds: frozenset[str]) -> tuple[AlignmentCloseEffect, ...]:
|
|
359
|
+
"""Return exact unkept live results that would be lost on document close."""
|
|
360
|
+
reference = self._reference
|
|
361
|
+
if reference is None:
|
|
362
|
+
return ()
|
|
363
|
+
affected = []
|
|
364
|
+
referenceCloses = reference.documentId in documentIds
|
|
365
|
+
for pair in self._orderedPairs():
|
|
366
|
+
if not referenceCloses and pair.selection.documentId not in documentIds:
|
|
367
|
+
continue
|
|
368
|
+
comparison = pair.comparison
|
|
369
|
+
if comparison is not None and comparison.comparisonId not in self._retained:
|
|
370
|
+
affected.append(AlignmentCloseEffect(
|
|
371
|
+
comparison.comparisonId, f"{comparison.queryName} to {comparison.referenceName}",
|
|
372
|
+
))
|
|
373
|
+
return tuple(affected)
|
|
374
|
+
|
|
375
|
+
def _orderedPairs(self) -> tuple[_LivePair, ...]:
|
|
376
|
+
return tuple(
|
|
377
|
+
self._pairs[documentId] for documentId in self._documents if documentId in self._pairs
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
def _selectFirstAvailable(self) -> None:
|
|
381
|
+
if self.resultFor(self._activeQueryId) is None:
|
|
382
|
+
completed = (pair for pair in self._orderedPairs() if pair.comparison is not None)
|
|
383
|
+
self._activeQueryId = next((pair.selection.documentId for pair in completed), None)
|
|
384
|
+
|
|
385
|
+
def _clearPair(self, pair: _LivePair) -> None:
|
|
386
|
+
if pair.work is not None:
|
|
387
|
+
self._tasks.cancel(_OWNER, pair.work.request.slot)
|
|
388
|
+
pair.work, pair.comparison, pair.error = None, None, ""
|
|
389
|
+
|
|
390
|
+
def _clearPairs(self) -> None:
|
|
391
|
+
for pair in self._pairs.values():
|
|
392
|
+
self._clearPair(pair)
|
|
393
|
+
self._activeQueryId = None
|
|
394
|
+
|
|
395
|
+
def _availableRegion(self, selection: AlignmentSelection) -> AlignmentSelection:
|
|
396
|
+
if self._documents[selection.documentId].revision.moleculeType == "protein":
|
|
397
|
+
return AlignmentSelection(selection.documentId)
|
|
398
|
+
if selection.featureId is None or self._hasFeature(selection):
|
|
399
|
+
return selection
|
|
400
|
+
return replace(selection, featureId=None)
|
|
401
|
+
|
|
402
|
+
def _hasFeature(self, selection: AlignmentSelection) -> bool:
|
|
403
|
+
document = self._documents[selection.documentId]
|
|
404
|
+
return any(
|
|
405
|
+
feature.featureId == selection.featureId for feature in alignmentFeatures(document.revision)
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
def _validateSelection(self, selection: AlignmentSelection) -> None:
|
|
409
|
+
if selection.documentId not in self._documents:
|
|
410
|
+
raise ValueError("Choose an open sequence record for alignment.")
|
|
411
|
+
if selection.reverse and self._documents[selection.documentId].revision.moleculeType == "protein":
|
|
412
|
+
raise ValueError("Reverse complement is available for DNA and RNA.")
|
|
413
|
+
if selection.featureId is not None and not self._hasFeature(selection):
|
|
414
|
+
raise ValueError("Choose an exact supported feature from that record.")
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"""Pin exact Assembly sources and maintain their session order."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import InitVar, dataclass, fields, is_dataclass, replace
|
|
4
|
+
from itertools import pairwise
|
|
5
|
+
from typing import Literal
|
|
6
|
+
from uuid import uuid4
|
|
7
|
+
|
|
8
|
+
from strandfolio.application.pcr_session import AssemblyPcrInput
|
|
9
|
+
from strandfolio.domain.fragments import DNAFragment
|
|
10
|
+
from strandfolio.domain.primers import PrimerBinding
|
|
11
|
+
from strandfolio.domain.provenance import OutputReference
|
|
12
|
+
from strandfolio.domain.sequence import SequenceRevision
|
|
13
|
+
from strandfolio.operations.restriction import RestrictionDigest
|
|
14
|
+
from strandfolio.storage.revision_codec import sameRevisionSnapshot
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
AssemblyOrientation = Literal["forward", "reverse"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def isAssemblySource(revision: SequenceRevision) -> bool:
|
|
21
|
+
"""A DNA revision can occupy a role if it has no declared circular topology."""
|
|
22
|
+
return (isinstance(revision, SequenceRevision) and revision.moleculeType == "DNA"
|
|
23
|
+
and revision.topology != "circular")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _sameOutputEvidence(recorded: object, offered: object) -> bool:
|
|
27
|
+
"""Compare immutable output fields, preserving IDs and the types of coordinates and flags."""
|
|
28
|
+
if recorded is offered:
|
|
29
|
+
return True
|
|
30
|
+
if type(recorded) is not type(offered):
|
|
31
|
+
return False
|
|
32
|
+
if isinstance(recorded, SequenceRevision):
|
|
33
|
+
return sameRevisionSnapshot(recorded, offered)
|
|
34
|
+
if isinstance(recorded, tuple):
|
|
35
|
+
return len(recorded) == len(offered) and all(
|
|
36
|
+
_sameOutputEvidence(a, b) for a, b in zip(recorded, offered)
|
|
37
|
+
)
|
|
38
|
+
if is_dataclass(recorded):
|
|
39
|
+
return all(_sameOutputEvidence(getattr(recorded, field.name), getattr(offered, field.name))
|
|
40
|
+
for field in fields(recorded))
|
|
41
|
+
return recorded == offered
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True, slots=True)
|
|
45
|
+
class AssemblyDigestInput:
|
|
46
|
+
"""The exact recorded fragment and the run that supplies its source evidence."""
|
|
47
|
+
|
|
48
|
+
run: RestrictionDigest
|
|
49
|
+
fragment: DNAFragment
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def revision(self) -> SequenceRevision:
|
|
53
|
+
return self.fragment.revision
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True, slots=True)
|
|
57
|
+
class AssemblySource:
|
|
58
|
+
"""Pin a revision and, for a prepared input, the exact output owned by its run.
|
|
59
|
+
|
|
60
|
+
The source picker supplies the index while enumerating a run. A single handoff
|
|
61
|
+
can omit it and resolve by output key. Both routes check the same evidence.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
label: str
|
|
65
|
+
value: SequenceRevision | AssemblyDigestInput | AssemblyPcrInput
|
|
66
|
+
recordedIndex: InitVar[int | None] = None
|
|
67
|
+
|
|
68
|
+
def __post_init__(self, recordedIndex: int | None) -> None:
|
|
69
|
+
if not isAssemblySource(self.revision):
|
|
70
|
+
raise ValueError("Choose a DNA input that is linear or has unspecified topology.")
|
|
71
|
+
value = self.value
|
|
72
|
+
if isinstance(value, SequenceRevision):
|
|
73
|
+
return
|
|
74
|
+
if isinstance(value, AssemblyDigestInput):
|
|
75
|
+
offered, outputs = value.fragment, value.run.fragments
|
|
76
|
+
parentIds = (offered.sourceRevisionId,)
|
|
77
|
+
else:
|
|
78
|
+
offered, outputs = value.product, value.run.products
|
|
79
|
+
bindings = (offered.forwardBinding, offered.reverseBinding)
|
|
80
|
+
if not all(isinstance(binding, PrimerBinding) for binding in bindings):
|
|
81
|
+
raise ValueError("The selected output has incomplete preparation evidence.")
|
|
82
|
+
parentIds = tuple(binding.templateRevisionId for binding in bindings)
|
|
83
|
+
if value.run.status != "completed":
|
|
84
|
+
raise ValueError("Choose an output from a completed preparation.")
|
|
85
|
+
reference = self.outputReference
|
|
86
|
+
if (reference is None or reference.operationId != value.run.operationId
|
|
87
|
+
or offered.operationId != value.run.operationId or offered.outputKey != reference.outputKey
|
|
88
|
+
or any(parentId != value.run.template.revisionId for parentId in parentIds)):
|
|
89
|
+
raise ValueError("The selected output does not match its recorded preparation.")
|
|
90
|
+
if recordedIndex is None:
|
|
91
|
+
recorded = next((item for item in outputs if item.outputKey == offered.outputKey), None)
|
|
92
|
+
elif 0 <= recordedIndex < len(outputs):
|
|
93
|
+
recorded = outputs[recordedIndex]
|
|
94
|
+
else:
|
|
95
|
+
recorded = None
|
|
96
|
+
if not _sameOutputEvidence(recorded, offered):
|
|
97
|
+
raise ValueError("The selected output differs from the one stored in its preparation.")
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def revision(self) -> SequenceRevision:
|
|
101
|
+
return self.value if isinstance(self.value, SequenceRevision) else self.value.revision
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def sourceRevision(self) -> SequenceRevision:
|
|
105
|
+
"""The original record pinned for this input, including the template of a recorded run."""
|
|
106
|
+
if isinstance(self.value, SequenceRevision):
|
|
107
|
+
return self.value
|
|
108
|
+
return self.value.run.template
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def sourceRevisionId(self) -> str:
|
|
112
|
+
return self.sourceRevision.revisionId
|
|
113
|
+
|
|
114
|
+
@property
|
|
115
|
+
def outputReference(self) -> OutputReference | None:
|
|
116
|
+
return self.revision.outputReference
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@dataclass(frozen=True, slots=True)
|
|
120
|
+
class AssemblyOccurrence:
|
|
121
|
+
"""A role slot whose identity follows it through plan edits."""
|
|
122
|
+
|
|
123
|
+
occurrenceId: str
|
|
124
|
+
source: AssemblySource | None = None
|
|
125
|
+
orientation: AssemblyOrientation = "forward"
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class AssemblySession:
|
|
129
|
+
"""Maintain one backbone and an ordered list of insert slots in memory."""
|
|
130
|
+
|
|
131
|
+
def __init__(self) -> None:
|
|
132
|
+
self._backbone = self._emptySlot()
|
|
133
|
+
self._inserts = [self._emptySlot()]
|
|
134
|
+
|
|
135
|
+
@staticmethod
|
|
136
|
+
def _emptySlot() -> AssemblyOccurrence:
|
|
137
|
+
return AssemblyOccurrence(uuid4().hex)
|
|
138
|
+
|
|
139
|
+
@property
|
|
140
|
+
def backbone(self) -> AssemblyOccurrence:
|
|
141
|
+
return self._backbone
|
|
142
|
+
|
|
143
|
+
@property
|
|
144
|
+
def inserts(self) -> tuple[AssemblyOccurrence, ...]:
|
|
145
|
+
return tuple(self._inserts)
|
|
146
|
+
|
|
147
|
+
@property
|
|
148
|
+
def occurrences(self) -> tuple[AssemblyOccurrence, ...]:
|
|
149
|
+
return (self._backbone, *self._inserts)
|
|
150
|
+
|
|
151
|
+
@property
|
|
152
|
+
def plannedJunctions(self) -> tuple[tuple[AssemblyOccurrence, AssemblyOccurrence], ...]:
|
|
153
|
+
"""List neighbors in the chosen circular order, including unassigned slots and closure."""
|
|
154
|
+
return tuple(pairwise((*self.occurrences, self.backbone)))
|
|
155
|
+
|
|
156
|
+
def assign(self, source: AssemblySource, occurrenceId: str | None) -> AssemblyOccurrence:
|
|
157
|
+
"""Pin a source in an existing slot, or append a new insert when ID is absent."""
|
|
158
|
+
if occurrenceId is None:
|
|
159
|
+
occurrence = replace(self._emptySlot(), source=source)
|
|
160
|
+
self._inserts.append(occurrence)
|
|
161
|
+
return occurrence
|
|
162
|
+
occurrence = AssemblyOccurrence(occurrenceId, source)
|
|
163
|
+
self._replaceOccurrence(occurrence)
|
|
164
|
+
return occurrence
|
|
165
|
+
|
|
166
|
+
def reorder(self, occurrenceId: str, offset: int) -> None:
|
|
167
|
+
"""Move an insert relative to its current position."""
|
|
168
|
+
index = self._insertIndex(occurrenceId)
|
|
169
|
+
destination = index + offset
|
|
170
|
+
if not 0 <= destination < len(self._inserts):
|
|
171
|
+
raise ValueError("The destination must be within the current insert list.")
|
|
172
|
+
self._inserts.insert(destination, self._inserts.pop(index))
|
|
173
|
+
|
|
174
|
+
def flip(self, occurrenceId: str) -> None:
|
|
175
|
+
"""Reverse a pinned orientation choice while preserving its recorded input."""
|
|
176
|
+
occurrence = next(
|
|
177
|
+
(item for item in self.occurrences if item.occurrenceId == occurrenceId), None,
|
|
178
|
+
)
|
|
179
|
+
if occurrence is None or occurrence.source is None:
|
|
180
|
+
raise ValueError("Choose a source before setting its orientation.")
|
|
181
|
+
orientation = "reverse" if occurrence.orientation == "forward" else "forward"
|
|
182
|
+
self._replaceOccurrence(replace(occurrence, orientation=orientation))
|
|
183
|
+
|
|
184
|
+
def remove(self, occurrenceId: str) -> None:
|
|
185
|
+
"""Remove an insert or release the backbone pin."""
|
|
186
|
+
if occurrenceId == self._backbone.occurrenceId:
|
|
187
|
+
self._backbone = self._emptySlot()
|
|
188
|
+
return
|
|
189
|
+
self._inserts.pop(self._insertIndex(occurrenceId))
|
|
190
|
+
if not self._inserts:
|
|
191
|
+
self._inserts.append(self._emptySlot())
|
|
192
|
+
|
|
193
|
+
def clear(self) -> None:
|
|
194
|
+
"""Release all pins and return to the two empty starting roles."""
|
|
195
|
+
self._backbone = self._emptySlot()
|
|
196
|
+
self._inserts = [self._emptySlot()]
|
|
197
|
+
|
|
198
|
+
def _replaceOccurrence(self, occurrence: AssemblyOccurrence) -> None:
|
|
199
|
+
if occurrence.occurrenceId == self._backbone.occurrenceId:
|
|
200
|
+
self._backbone = occurrence
|
|
201
|
+
else:
|
|
202
|
+
self._inserts[self._insertIndex(occurrence.occurrenceId)] = occurrence
|
|
203
|
+
|
|
204
|
+
def _insertIndex(self, occurrenceId: str) -> int:
|
|
205
|
+
for index, item in enumerate(self._inserts):
|
|
206
|
+
if item.occurrenceId == occurrenceId:
|
|
207
|
+
return index
|
|
208
|
+
raise ValueError("This Assembly insert slot is no longer available.")
|