@rivm-loculus-tools/sequence-editor 0.0.0-stage → 0.1.0
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.
- package/.gitkeep +0 -0
- package/CHANGELOG.md +8 -0
- package/LICENSE +288 -0
- package/eslint.config.js +16 -0
- package/package.json +140 -3
- package/scripts/update-exports.js +9 -0
- package/src/classes/services/EditorWorkerService/EditorWorkerService.ts +163 -0
- package/src/classes/services/EditorWorkerService/index.ts +1 -0
- package/src/classes/services/SequenceEditorEventBusService/SequenceEditorEventBusService.ts +37 -0
- package/src/classes/services/SequenceEditorEventBusService/index.ts +1 -0
- package/src/classes/workers/EngineWorker/EngineWorker.ts +196 -0
- package/src/classes/workers/EngineWorker/index.ts +1 -0
- package/src/components/app/App/App.tsx +22 -0
- package/src/components/app/App/index.ts +1 -0
- package/src/components/pages/EmptyWorkspace.tsx +43 -0
- package/src/components/pages/SequenceEditorErrorPage/SequenceEditorErrorPage.tsx +16 -0
- package/src/components/pages/SequenceEditorErrorPage/index.ts +1 -0
- package/src/components/pages/SequenceEditorMainPage/SequenceEditorMainPage.tsx +76 -0
- package/src/components/pages/SequenceEditorMainPage/index.ts +1 -0
- package/src/components/ui/SequenceEditor/SequenceEditor.tsx +896 -0
- package/src/components/ui/SequenceEditor/SequenceEditorGotoPositionDialog.tsx +108 -0
- package/src/components/ui/SequenceEditor/SequenceEditorStatusBar.tsx +72 -0
- package/src/components/ui/SequenceEditor/index.ts +1 -0
- package/src/components/ui/SequenceEditorPage/SequenceEditorPage.tsx +23 -0
- package/src/components/ui/SequenceEditorPage/index.ts +1 -0
- package/src/components/ui/SequenceEditorPageDrawer/SequenceEditorPageDrawer.tsx +234 -0
- package/src/components/ui/SequenceEditorPageDrawer/SequenceEditorPageDrawerColorForm.tsx +73 -0
- package/src/components/ui/SequenceEditorPageDrawer/SequenceEditorPageDrawerKeyboardShortcuts.tsx +230 -0
- package/src/components/ui/SequenceEditorPageDrawer/index.ts +1 -0
- package/src/components/ui/SequenceEditorPageHeader/SequenceEditorPageHeader.tsx +71 -0
- package/src/components/ui/SequenceEditorPageHeader/index.ts +1 -0
- package/src/components/ui/SequenceEditorSearchBar/SequenceEditorSearchBar.tsx +35 -0
- package/src/components/ui/SequenceEditorSearchBar/index.ts +1 -0
- package/src/constants/editor.ts +15 -0
- package/src/constants/index.ts +3 -0
- package/src/constants/keyboard.ts +154 -0
- package/src/constants/palette.ts +227 -0
- package/src/models/editor.ts +6 -0
- package/src/models/index.ts +3 -0
- package/src/models/model.ts +295 -0
- package/src/models/palette.ts +68 -0
- package/src/stores/sequenceEditorStore/index.ts +1 -0
- package/src/stores/sequenceEditorStore/sequenceEditorStore.ts +103 -0
- package/src/utils/EditorUtil/EditorUtil.ts +892 -0
- package/src/utils/EditorUtil/index.ts +1 -0
- package/src/utils/EngineErrorUtil/EngineErrorUtil.ts +25 -0
- package/src/utils/PaletteUtil/PaletteUtil.ts +31 -0
- package/src/utils/PaletteUtil/index.ts +1 -0
- package/tsconfig.build.json +14 -0
- package/tsconfig.json +32 -0
- package/vite.config.ts +177 -0
- package/README.md +0 -3
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/// <reference lib="webworker" />
|
|
2
|
+
import { expose } from 'comlink';
|
|
3
|
+
import type { MoveDirection } from '@rivm-loculus-tools/sequence-editor-wasm';
|
|
4
|
+
import init, { MsaEngine } from '@rivm-loculus-tools/sequence-editor-wasm';
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
EngineMeta,
|
|
8
|
+
EngineSearchResult,
|
|
9
|
+
EngineViewportResult,
|
|
10
|
+
EngineWorkerApi,
|
|
11
|
+
ViewportBounds,
|
|
12
|
+
} from '../../../models';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* MSA engine Web Worker.
|
|
16
|
+
*
|
|
17
|
+
* This worker is the **single owner** of the `MsaEngine` WASM instance and its
|
|
18
|
+
* linear memory for the lifetime of the page. All Rust calls happen here, off
|
|
19
|
+
* the main thread, so FASTA parsing, structural mutations, and undo/redo
|
|
20
|
+
* snapshots never stall the React render loop.
|
|
21
|
+
*
|
|
22
|
+
* The class is exposed to the main thread through comlink, so every public
|
|
23
|
+
* method is proxied as an asynchronous call across the worker boundary.
|
|
24
|
+
*/
|
|
25
|
+
export class EngineWorker implements EngineWorkerApi {
|
|
26
|
+
private engine: MsaEngine | null = null;
|
|
27
|
+
private initialization: null | Promise<void> = null;
|
|
28
|
+
|
|
29
|
+
public async delete(rows: number[], column: number): Promise<void> {
|
|
30
|
+
const engine = await this.requireEngine();
|
|
31
|
+
engine.delete_at_subset(this.toRowIndices(rows), column);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public async deleteColumn(column: number): Promise<void> {
|
|
35
|
+
const engine = await this.requireEngine();
|
|
36
|
+
engine.delete_column(column);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public async exportFasta(): Promise<string> {
|
|
40
|
+
const engine = await this.requireEngine();
|
|
41
|
+
const records: string[] = [];
|
|
42
|
+
for (let row = 0; row < engine.get_sequence_count(); row += 1) {
|
|
43
|
+
const sequence: string[] = [];
|
|
44
|
+
for (let column = 0; column < engine.get_row_length(row); column += 1) {
|
|
45
|
+
sequence.push(String.fromCharCode(engine.get_base(row, column)));
|
|
46
|
+
}
|
|
47
|
+
records.push(`>${engine.get_name(row)}\n${sequence.join('')}\n`);
|
|
48
|
+
}
|
|
49
|
+
return records.join('');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Idempotently instantiates the WASM module and constructs the engine.
|
|
54
|
+
* Safe to call multiple times; concurrent callers share one initialization.
|
|
55
|
+
*/
|
|
56
|
+
public async initialize(): Promise<void> {
|
|
57
|
+
if (this.engine) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (!this.initialization) {
|
|
61
|
+
this.initialization = (async () => {
|
|
62
|
+
await init(undefined);
|
|
63
|
+
this.engine = new MsaEngine();
|
|
64
|
+
})();
|
|
65
|
+
this.initialization.catch(() => {
|
|
66
|
+
this.initialization = null;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
await this.initialization;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public async insert(rows: number[], column: number): Promise<void> {
|
|
73
|
+
const engine = await this.requireEngine();
|
|
74
|
+
engine.insert_at_subset(this.toRowIndices(rows), column);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public async load(fastaContent: string): Promise<number> {
|
|
78
|
+
const engine = await this.requireEngine();
|
|
79
|
+
return engine.load_fasta(fastaContent);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public async meta(): Promise<EngineMeta> {
|
|
83
|
+
const engine = await this.requireEngine();
|
|
84
|
+
return {
|
|
85
|
+
alignmentWidth: engine.get_alignment_width(),
|
|
86
|
+
redoDepth: engine.redo_depth(),
|
|
87
|
+
sequenceCount: engine.get_sequence_count(),
|
|
88
|
+
undoDepth: engine.undo_depth(),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public async moveHorizontal(row: number, columns: number[], direction: MoveDirection): Promise<void> {
|
|
93
|
+
const engine = await this.requireEngine();
|
|
94
|
+
engine.move_horizontal(row, this.toRowIndices(columns), direction);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public async moveRowsDown(rows: number[]): Promise<void> {
|
|
98
|
+
const engine = await this.requireEngine();
|
|
99
|
+
engine.move_rows_down(this.toRowIndices(rows));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public async moveRowsUp(rows: number[]): Promise<void> {
|
|
103
|
+
const engine = await this.requireEngine();
|
|
104
|
+
engine.move_rows_up(this.toRowIndices(rows));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public async mutate(rows: number[], column: number, base: string): Promise<void> {
|
|
108
|
+
const engine = await this.requireEngine();
|
|
109
|
+
engine.mutate_base_subset(this.toRowIndices(rows), column, base.charCodeAt(0));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public async name(row: number): Promise<string> {
|
|
113
|
+
const engine = await this.requireEngine();
|
|
114
|
+
return engine.get_name(row);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public async redo(): Promise<boolean> {
|
|
118
|
+
const engine = await this.requireEngine();
|
|
119
|
+
return engine.redo();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public async search(query: string, checkRecordNames: boolean): Promise<EngineSearchResult> {
|
|
123
|
+
const engine = await this.requireEngine();
|
|
124
|
+
return engine.search(query, checkRecordNames) as EngineSearchResult;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public async translate(bounds: ViewportBounds, readingFrame: number): Promise<EngineViewportResult> {
|
|
128
|
+
const engine = await this.requireEngine();
|
|
129
|
+
this.validateBounds(bounds);
|
|
130
|
+
const bytes = engine.translate(
|
|
131
|
+
bounds.startRow,
|
|
132
|
+
bounds.endRow,
|
|
133
|
+
bounds.startCol,
|
|
134
|
+
bounds.endCol,
|
|
135
|
+
readingFrame,
|
|
136
|
+
);
|
|
137
|
+
return this.buildViewportResult(bytes, bounds);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public async undo(): Promise<boolean> {
|
|
141
|
+
const engine = await this.requireEngine();
|
|
142
|
+
return engine.undo();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
public async viewport(bounds: ViewportBounds): Promise<EngineViewportResult> {
|
|
146
|
+
const engine = await this.requireEngine();
|
|
147
|
+
this.validateBounds(bounds);
|
|
148
|
+
const bytes = engine.update_viewport(
|
|
149
|
+
bounds.startRow,
|
|
150
|
+
bounds.endRow,
|
|
151
|
+
bounds.startCol,
|
|
152
|
+
bounds.endCol,
|
|
153
|
+
);
|
|
154
|
+
return this.buildViewportResult(bytes, bounds);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Builds a fixed-stride viewport result from the engine's returned bytes. */
|
|
158
|
+
private buildViewportResult(bytes: Uint8Array, bounds: ViewportBounds): EngineViewportResult {
|
|
159
|
+
const columns = bounds.endCol - bounds.startCol + 1;
|
|
160
|
+
const rows = columns > 0 ? Math.floor(bytes.byteLength / columns) : 0;
|
|
161
|
+
return {
|
|
162
|
+
bytes,
|
|
163
|
+
cols: columns,
|
|
164
|
+
rows,
|
|
165
|
+
startCol: bounds.startCol,
|
|
166
|
+
startRow: bounds.startRow,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Returns the initialized engine, initializing it first if necessary. */
|
|
171
|
+
private async requireEngine(): Promise<MsaEngine> {
|
|
172
|
+
await this.initialize();
|
|
173
|
+
if (!this.engine) {
|
|
174
|
+
throw new Error('Engine failed to initialize');
|
|
175
|
+
}
|
|
176
|
+
return this.engine;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Converts a plain row-index list into the typed array the WASM API expects. */
|
|
180
|
+
private toRowIndices(rows: number[]): Uint32Array {
|
|
181
|
+
return new Uint32Array(rows);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Validates that viewport/translation bounds are safe, non-negative, and ordered. */
|
|
185
|
+
private validateBounds(bounds: ViewportBounds): void {
|
|
186
|
+
const values = [bounds.startRow, bounds.endRow, bounds.startCol, bounds.endCol];
|
|
187
|
+
if (!values.every((value) => Number.isSafeInteger(value) && value >= 0)) {
|
|
188
|
+
throw new Error('Viewport bounds must be non-negative safe integers');
|
|
189
|
+
}
|
|
190
|
+
if (bounds.startRow > bounds.endRow || bounds.startCol > bounds.endCol) {
|
|
191
|
+
throw new Error('Viewport bounds must be ordered (start <= end)');
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
expose(new EngineWorker());
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './EngineWorker';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CssBaseline } from '@mui/material';
|
|
2
|
+
import { ThemeProvider } from '@mui/material/styles';
|
|
3
|
+
import { ErrorBoundary } from 'react-error-boundary';
|
|
4
|
+
import { theme } from '@rivm-loculus-tools/common-frontend/theme';
|
|
5
|
+
import { I18nWrapper } from '@rivm-loculus-tools/common-frontend/components/I18nWrapper';
|
|
6
|
+
import { PageCommonContextProvider } from '@rivm-loculus-tools/common-frontend/components/page';
|
|
7
|
+
|
|
8
|
+
import { SequenceEditorMainPage } from '../../pages/SequenceEditorMainPage';
|
|
9
|
+
import { SequenceEditorErrorPage } from '../../pages/SequenceEditorErrorPage';
|
|
10
|
+
|
|
11
|
+
export const App = () => (
|
|
12
|
+
<ErrorBoundary FallbackComponent={SequenceEditorErrorPage}>
|
|
13
|
+
<I18nWrapper>
|
|
14
|
+
<ThemeProvider theme={theme}>
|
|
15
|
+
<CssBaseline />
|
|
16
|
+
<PageCommonContextProvider>
|
|
17
|
+
<SequenceEditorMainPage />
|
|
18
|
+
</PageCommonContextProvider>
|
|
19
|
+
</ThemeProvider>
|
|
20
|
+
</I18nWrapper>
|
|
21
|
+
</ErrorBoundary>
|
|
22
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './App';
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Box,
|
|
3
|
+
Button,
|
|
4
|
+
Typography,
|
|
5
|
+
} from '@mui/material';
|
|
6
|
+
import BiotechOutlinedIcon from '@mui/icons-material/BiotechOutlined';
|
|
7
|
+
import FileUploadOutlinedIcon from '@mui/icons-material/FileUploadOutlined';
|
|
8
|
+
|
|
9
|
+
export const EmptyWorkspace = ({ onImport }: { onImport: () => void }) => (
|
|
10
|
+
<Box
|
|
11
|
+
sx={{
|
|
12
|
+
alignItems: 'center',
|
|
13
|
+
display: 'flex',
|
|
14
|
+
flex: 1,
|
|
15
|
+
flexDirection: 'column',
|
|
16
|
+
gap: 1.5,
|
|
17
|
+
justifyContent: 'center',
|
|
18
|
+
p: 3,
|
|
19
|
+
textAlign: 'center',
|
|
20
|
+
}}
|
|
21
|
+
>
|
|
22
|
+
<BiotechOutlinedIcon
|
|
23
|
+
sx={{ color: 'primary.main', fontSize: 64 }}
|
|
24
|
+
/>
|
|
25
|
+
<Typography variant={'h5'}>
|
|
26
|
+
{'No alignment loaded'}
|
|
27
|
+
</Typography>
|
|
28
|
+
<Typography
|
|
29
|
+
color={'text.secondary'}
|
|
30
|
+
sx={{ maxWidth: 460 }}
|
|
31
|
+
variant={'body2'}
|
|
32
|
+
>
|
|
33
|
+
{'Import a nucleotide FASTA file or open the bundled SARS-CoV-2 demo.'}
|
|
34
|
+
</Typography>
|
|
35
|
+
<Button
|
|
36
|
+
onClick={onImport}
|
|
37
|
+
startIcon={<FileUploadOutlinedIcon />}
|
|
38
|
+
sx={{ mt: 1 }}
|
|
39
|
+
>
|
|
40
|
+
{'Import FASTA'}
|
|
41
|
+
</Button>
|
|
42
|
+
</Box>
|
|
43
|
+
);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ErrorPageCommonProps } from '@rivm-loculus-tools/common-frontend/components/page';
|
|
2
|
+
import { ErrorPageCommon } from '@rivm-loculus-tools/common-frontend/components/page';
|
|
3
|
+
|
|
4
|
+
import { SequenceEditorPage } from '../../ui/SequenceEditorPage';
|
|
5
|
+
|
|
6
|
+
export type SequenceEditorErrorPageProps = ErrorPageCommonProps;
|
|
7
|
+
|
|
8
|
+
export const SequenceEditorErrorPage = ({ error }: SequenceEditorErrorPageProps) => {
|
|
9
|
+
return (
|
|
10
|
+
<SequenceEditorPage
|
|
11
|
+
isNoInteraction
|
|
12
|
+
>
|
|
13
|
+
<ErrorPageCommon error={error} />
|
|
14
|
+
</SequenceEditorPage>
|
|
15
|
+
);
|
|
16
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './SequenceEditorErrorPage';
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { useStore } from 'zustand';
|
|
2
|
+
import {
|
|
3
|
+
useCallback,
|
|
4
|
+
useMemo,
|
|
5
|
+
} from 'react';
|
|
6
|
+
import { FastaDataEventBusService } from '@rivm-loculus-tools/common-frontend/classes/services/FastaDataEventBusService';
|
|
7
|
+
import { ErrorUtil } from '@rivm-loculus-tools/common-frontend/utils/ErrorUtil';
|
|
8
|
+
import { UrlUtil } from '@rivm-loculus-tools/common-frontend/utils/UrlUtil';
|
|
9
|
+
import type { DialogAction } from '@gen-epix/ui-core-components/components/Dialog';
|
|
10
|
+
import { FastaDataImportDialogLoader } from '@rivm-loculus-tools/common-frontend/components/FastaDataImportDialogLoader';
|
|
11
|
+
import { BackdropSpinner } from '@gen-epix/ui-core-components/components/BackdropSpinner';
|
|
12
|
+
import { FastaNotLoaded } from '@rivm-loculus-tools/common-frontend/components/FastaNotLoaded';
|
|
13
|
+
import BiotechOutlinedIcon from '@mui/icons-material/BiotechOutlined';
|
|
14
|
+
import { LOADING_STATUS } from '@rivm-loculus-tools/common-frontend/models/fasta';
|
|
15
|
+
|
|
16
|
+
import { sequenceEditorStore } from '../../../stores/sequenceEditorStore';
|
|
17
|
+
import { SequenceEditor } from '../../ui/SequenceEditor';
|
|
18
|
+
import { SequenceEditorPage } from '../../ui/SequenceEditorPage';
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export const SequenceEditorMainPage = () => {
|
|
22
|
+
const loadingStatus = useStore(sequenceEditorStore, (state) => state.loadingStatus);
|
|
23
|
+
const setData = useStore(sequenceEditorStore, (state) => state.setData);
|
|
24
|
+
const rawFasta = useStore(sequenceEditorStore, (state) => state.rawFasta);
|
|
25
|
+
|
|
26
|
+
const onClickLoadDemoData = useCallback(async () => {
|
|
27
|
+
try {
|
|
28
|
+
const fastaRes = await fetch('/sample-data/sarscov2_demo.fasta');
|
|
29
|
+
const demoFastaText = await fastaRes.text();
|
|
30
|
+
|
|
31
|
+
await setData(demoFastaText);
|
|
32
|
+
FastaDataEventBusService.getInstance().emit('onCloseImportDialog');
|
|
33
|
+
} catch (err: unknown) {
|
|
34
|
+
FastaDataEventBusService.getInstance().emit('onCloseImportDialog');
|
|
35
|
+
const errorMessage = `Failed to load demo dataset: ${ErrorUtil.getErrorMessage(err, 'Unknown error')}`;
|
|
36
|
+
FastaDataEventBusService.getInstance().emit('onShowImportDialog', errorMessage);
|
|
37
|
+
} finally {
|
|
38
|
+
UrlUtil.clearSearchParams();
|
|
39
|
+
}
|
|
40
|
+
}, [setData]);
|
|
41
|
+
|
|
42
|
+
const extraFastaDataImportDialogLoaderActions = useMemo<DialogAction[]>(() => [
|
|
43
|
+
{
|
|
44
|
+
autoFocus: true,
|
|
45
|
+
color: 'secondary',
|
|
46
|
+
label: 'Demo (SARS-CoV-2)',
|
|
47
|
+
onClick: onClickLoadDemoData,
|
|
48
|
+
variant: 'contained',
|
|
49
|
+
},
|
|
50
|
+
], [onClickLoadDemoData]);
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<SequenceEditorPage
|
|
54
|
+
isNoInteraction={false}
|
|
55
|
+
>
|
|
56
|
+
{rawFasta && (
|
|
57
|
+
<SequenceEditor />
|
|
58
|
+
)}
|
|
59
|
+
{!rawFasta && (
|
|
60
|
+
<FastaNotLoaded
|
|
61
|
+
description={'Load a FASTA sequence file to enable the editor.'}
|
|
62
|
+
icon={<BiotechOutlinedIcon sx={{ color: 'text.disabled', fontSize: 72 }} />}
|
|
63
|
+
title={'No FASTA Sequence Loaded'}
|
|
64
|
+
/>
|
|
65
|
+
)}
|
|
66
|
+
<FastaDataImportDialogLoader
|
|
67
|
+
extraActions={extraFastaDataImportDialogLoaderActions}
|
|
68
|
+
store={sequenceEditorStore}
|
|
69
|
+
/>
|
|
70
|
+
<BackdropSpinner
|
|
71
|
+
label={`Computing phylogenetic tree`}
|
|
72
|
+
open={loadingStatus === LOADING_STATUS.LOADING}
|
|
73
|
+
/>
|
|
74
|
+
</SequenceEditorPage>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './SequenceEditorMainPage';
|