@genspectrum/dashboard-components 0.13.0 → 0.13.2

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.
Files changed (39) hide show
  1. package/custom-elements.json +178 -0
  2. package/dist/assets/mutationOverTimeWorker-B1-WrM4b.js.map +1 -0
  3. package/dist/components.d.ts +108 -43
  4. package/dist/components.js +564 -295
  5. package/dist/components.js.map +1 -1
  6. package/dist/style.css +3 -0
  7. package/dist/util.d.ts +59 -43
  8. package/package.json +2 -2
  9. package/src/constants.ts +6 -0
  10. package/src/lapisApi/__mockData__/wiseReferenceGenome.json +9 -0
  11. package/src/lapisApi/lapisApi.ts +17 -0
  12. package/src/lapisApi/lapisTypes.ts +7 -1
  13. package/src/operator/FetchDetailsOperator.ts +28 -0
  14. package/src/preact/components/downshift-combobox.tsx +18 -20
  15. package/src/preact/components/tabs.tsx +1 -1
  16. package/src/preact/mutationsOverTime/MutationOverTimeData.ts +9 -5
  17. package/src/preact/mutationsOverTime/mutations-over-time-grid.tsx +5 -3
  18. package/src/preact/shared/sort/sortSubstitutionsAndDeletions.ts +4 -7
  19. package/src/preact/textInput/fetchStringAutocompleteList.spec.ts +34 -0
  20. package/src/preact/textInput/fetchStringAutocompleteList.ts +16 -2
  21. package/src/preact/textInput/text-input.tsx +22 -8
  22. package/src/preact/wastewater/mutationsOverTime/__mockData__/details.json +88 -0
  23. package/src/preact/wastewater/mutationsOverTime/computeWastewaterMutationsOverTimeDataPerLocation.spec.ts +159 -0
  24. package/src/preact/wastewater/mutationsOverTime/computeWastewaterMutationsOverTimeDataPerLocation.ts +51 -0
  25. package/src/preact/wastewater/mutationsOverTime/wastewater-mutations-over-time.stories.tsx +71 -0
  26. package/src/preact/wastewater/mutationsOverTime/wastewater-mutations-over-time.tsx +151 -0
  27. package/src/query/queryMutationsOverTime.ts +6 -14
  28. package/src/query/queryWastewaterMutationsOverTime.spec.ts +94 -0
  29. package/src/query/queryWastewaterMutationsOverTime.ts +55 -0
  30. package/src/utils/map2d.ts +39 -0
  31. package/src/web-components/index.ts +1 -0
  32. package/src/web-components/wastewaterVisualization/gs-wastewater-mutations-over-time.stories.ts +82 -0
  33. package/src/web-components/wastewaterVisualization/gs-wastewater-mutations-over-time.tsx +112 -0
  34. package/src/web-components/wastewaterVisualization/index.ts +1 -0
  35. package/standalone-bundle/assets/{mutationOverTimeWorker-DEybsZ5r.js.map → mutationOverTimeWorker-Cls1J0cl.js.map} +1 -1
  36. package/standalone-bundle/dashboard-components.js +6228 -6008
  37. package/standalone-bundle/dashboard-components.js.map +1 -1
  38. package/standalone-bundle/style.css +1 -1
  39. package/dist/assets/mutationOverTimeWorker-DTv93Ere.js.map +0 -1
@@ -0,0 +1,55 @@
1
+ import z from 'zod';
2
+
3
+ import { FetchDetailsOperator } from '../operator/FetchDetailsOperator';
4
+ import { type LapisFilter } from '../types';
5
+ import { type Substitution, SubstitutionClass } from '../utils/mutations';
6
+ import { parseDateStringToTemporal, type TemporalClass, toTemporalClass } from '../utils/temporalClass';
7
+
8
+ export type WastewaterData = {
9
+ location: string;
10
+ date: TemporalClass;
11
+ nucleotideMutationFrequency: { mutation: Substitution; proportion: number | null }[];
12
+ aminoAcidMutationFrequency: { mutation: Substitution; proportion: number | null }[];
13
+ }[];
14
+
15
+ export async function queryWastewaterMutationsOverTime(
16
+ lapis: string,
17
+ lapisFilter: LapisFilter,
18
+ signal?: AbortSignal,
19
+ ): Promise<WastewaterData> {
20
+ const fetchData = new FetchDetailsOperator(lapisFilter, [
21
+ 'date',
22
+ 'location',
23
+ 'nucleotideMutationFrequency',
24
+ 'aminoAcidMutationFrequency',
25
+ ]);
26
+ const data = (await fetchData.evaluate(lapis, signal)).content;
27
+
28
+ return data.map((row) => ({
29
+ location: row.location as string,
30
+ date: toTemporalClass(parseDateStringToTemporal(row.date as string, 'day')),
31
+ nucleotideMutationFrequency:
32
+ row.nucleotideMutationFrequency !== null
33
+ ? transformMutations(JSON.parse(row.nucleotideMutationFrequency as string))
34
+ : [],
35
+ aminoAcidMutationFrequency:
36
+ row.aminoAcidMutationFrequency !== null
37
+ ? transformMutations(JSON.parse(row.aminoAcidMutationFrequency as string))
38
+ : [],
39
+ }));
40
+ }
41
+
42
+ const mutationFrequencySchema = z.record(z.number().nullable());
43
+
44
+ function transformMutations(input: unknown): { mutation: Substitution; proportion: number | null }[] {
45
+ const mutationFrequency = mutationFrequencySchema.safeParse(input);
46
+
47
+ if (!mutationFrequency.success) {
48
+ throw new Error(`Failed to parse mutation frequency: ${mutationFrequency.error.message}`);
49
+ }
50
+
51
+ return Object.entries(mutationFrequency.data).map(([key, value]) => ({
52
+ mutation: SubstitutionClass.parse(key)!,
53
+ proportion: value,
54
+ }));
55
+ }
@@ -17,6 +17,8 @@ export interface Map2d<Key1, Key2, Value> {
17
17
 
18
18
  serializeSecondAxis(key: Key2): string;
19
19
 
20
+ getContents(): Map2DContents<Key1, Key2, Value>;
21
+
20
22
  readonly keysFirstAxis: Map<string, Key1>;
21
23
  readonly keysSecondAxis: Map<string, Key2>;
22
24
  }
@@ -106,6 +108,35 @@ export class Map2dBase<Key1 extends object | string, Key2 extends object | strin
106
108
  }
107
109
  }
108
110
 
111
+ export class SortedMap2d<Key1 extends object | string, Key2 extends object | string, Value> extends Map2dBase<
112
+ Key1,
113
+ Key2,
114
+ Value
115
+ > {
116
+ constructor(
117
+ delegate: Map2d<Key1, Key2, Value>,
118
+ sortFirstAxis: (a: Key1, b: Key1) => number,
119
+ sortSecondAxis: (a: Key2, b: Key2) => number,
120
+ ) {
121
+ const contents = delegate.getContents();
122
+ const sortedFirstAxisKeys = new Map(
123
+ [...contents.keysFirstAxis.entries()].sort((a, b) => sortFirstAxis(a[1], b[1])),
124
+ );
125
+ const sortedSecondAxisKeys = new Map(
126
+ [...contents.keysSecondAxis.entries()].sort((a, b) => sortSecondAxis(a[1], b[1])),
127
+ );
128
+ super(
129
+ (key: Key1) => delegate.serializeFirstAxis(key),
130
+ (key: Key2) => delegate.serializeSecondAxis(key),
131
+ {
132
+ keysFirstAxis: sortedFirstAxisKeys,
133
+ keysSecondAxis: sortedSecondAxisKeys,
134
+ data: contents.data,
135
+ },
136
+ );
137
+ }
138
+ }
139
+
109
140
  export class Map2dView<Key1 extends object | string, Key2 extends object | string, Value>
110
141
  implements Map2d<Key1, Key2, Value>
111
142
  {
@@ -175,4 +206,12 @@ export class Map2dView<Key1 extends object | string, Key2 extends object | strin
175
206
 
176
207
  return this.baseMap.getRow(key);
177
208
  }
209
+
210
+ getContents() {
211
+ return {
212
+ keysFirstAxis: this.keysFirstAxis,
213
+ keysSecondAxis: this.keysSecondAxis,
214
+ data: this.baseMap.getContents().data,
215
+ };
216
+ }
178
217
  }
@@ -1,3 +1,4 @@
1
1
  export { App } from './app.js';
2
2
  export * from './visualization';
3
+ export * from './wastewaterVisualization';
3
4
  export * from './input';
@@ -0,0 +1,82 @@
1
+ import type { Meta, StoryObj } from '@storybook/web-components';
2
+ import { html } from 'lit';
3
+
4
+ import './gs-wastewater-mutations-over-time';
5
+ import '../app';
6
+ import { withComponentDocs } from '../../../.storybook/ComponentDocsBlock';
7
+ import { WISE_DETAILS_ENDPOINT, WISE_LAPIS_URL } from '../../constants';
8
+ import details from '../../preact/wastewater/mutationsOverTime/__mockData__/details.json';
9
+ import { type WastewaterMutationsOverTimeProps } from '../../preact/wastewater/mutationsOverTime/wastewater-mutations-over-time';
10
+
11
+ const codeExample = String.raw`
12
+ <gs-wastewater-mutations-over-time
13
+ lapisFilter='{ "dateFrom": "2024-01-01" }'
14
+ sequenceType='nucleotide'
15
+ width='100%'
16
+ height='700px'
17
+ ></gs-wastewater-mutations-over-time>`;
18
+
19
+ const meta: Meta<Required<WastewaterMutationsOverTimeProps>> = {
20
+ title: 'Wastewater visualization/Wastewater mutations over time',
21
+ component: 'gs-wastewater-mutations-over-time',
22
+ argTypes: {
23
+ lapisFilter: { control: 'object' },
24
+ sequenceType: {
25
+ options: ['nucleotide', 'amino acid'],
26
+ control: { type: 'radio' },
27
+ },
28
+ width: { control: 'text' },
29
+ height: { control: 'text' },
30
+ },
31
+ args: {
32
+ lapisFilter: { versionStatus: 'LATEST_VERSION', isRevocation: false },
33
+ sequenceType: 'nucleotide',
34
+ width: '100%',
35
+ height: '700px',
36
+ },
37
+ parameters: withComponentDocs({
38
+ componentDocs: {
39
+ opensShadowDom: true,
40
+ expectsChildren: false,
41
+ codeExample,
42
+ },
43
+ fetchMock: {},
44
+ }),
45
+ tags: ['autodocs'],
46
+ };
47
+
48
+ export default meta;
49
+
50
+ export const WastewaterMutationsOverTime: StoryObj<Required<WastewaterMutationsOverTimeProps>> = {
51
+ render: (args) => html`
52
+ <gs-app lapis="${WISE_LAPIS_URL}">
53
+ <gs-wastewater-mutations-over-time
54
+ .lapisFilter=${args.lapisFilter}
55
+ .sequenceType=${args.sequenceType}
56
+ .width=${args.width}
57
+ .height=${args.height}
58
+ ></gs-wastewater-mutations-over-time>
59
+ </gs-app>
60
+ `,
61
+ parameters: {
62
+ fetchMock: {
63
+ mocks: [
64
+ {
65
+ matcher: {
66
+ name: 'details',
67
+ url: WISE_DETAILS_ENDPOINT,
68
+ body: {
69
+ fields: ['date', 'location', 'nucleotideMutationFrequency', 'aminoAcidMutationFrequency'],
70
+ versionStatus: 'LATEST_VERSION',
71
+ isRevocation: false,
72
+ },
73
+ },
74
+ response: {
75
+ status: 200,
76
+ body: details,
77
+ },
78
+ },
79
+ ],
80
+ },
81
+ },
82
+ };
@@ -0,0 +1,112 @@
1
+ import { customElement, property } from 'lit/decorators.js';
2
+ import { type DetailedHTMLProps, type HTMLAttributes } from 'react';
3
+
4
+ import {
5
+ WastewaterMutationsOverTime,
6
+ type WastewaterMutationsOverTimeProps,
7
+ } from '../../preact/wastewater/mutationsOverTime/wastewater-mutations-over-time';
8
+ import { type Equals, type Expect } from '../../utils/typeAssertions';
9
+ import { PreactLitAdapterWithGridJsStyles } from '../PreactLitAdapterWithGridJsStyles';
10
+
11
+ /**
12
+ * ## Context
13
+ *
14
+ * This component displays mutations for Swiss wastewater data generated within the WISE consortium. It is designed
15
+ * only for this purpose and is not designed to be reused outside the WISE project.
16
+ *
17
+ * It relies on a LAPIS instance that has the fields `nucleotideMutationFrequency` and `aminoAcidMutationFrequency`.
18
+ * Those fields are expected to be JSON strings of the format `{ [mutation]: frequency | null }`
19
+ * (e.g. `{ "A123T": 0.5, "C456G": 0.7, "T789G": null }`).
20
+ *
21
+ * The component will stratify by `location`.
22
+ * Every location will be rendered in a separate tab.
23
+ * The content of the tab is a "mutations over time" grid, similar to the one used in the `gs-mutations-over-time` component.
24
+ *
25
+ * This component also assumes that the LAPIS instance has the field `date` which can be used for the time axis.
26
+ */
27
+ @customElement('gs-wastewater-mutations-over-time')
28
+ export class WastewaterMutationsOverTimeComponent extends PreactLitAdapterWithGridJsStyles {
29
+ /**
30
+ * Required.
31
+ *
32
+ * LAPIS filter to select the displayed data.
33
+ */
34
+ @property({ type: Object })
35
+ lapisFilter: Record<string, string | string[] | number | null | boolean | undefined> & {
36
+ nucleotideMutations?: string[];
37
+ aminoAcidMutations?: string[];
38
+ nucleotideInsertions?: string[];
39
+ aminoAcidInsertions?: string[];
40
+ } = {};
41
+
42
+ /**
43
+ * Required.
44
+ *
45
+ * Whether to display nucleotide or amino acid mutations.
46
+ */
47
+ @property({ type: String })
48
+ sequenceType: 'nucleotide' | 'amino acid' = 'nucleotide';
49
+
50
+ /**
51
+ * The width of the component.
52
+ *
53
+ * Visit https://genspectrum.github.io/dashboard-components/?path=/docs/components-size-of-components--docs for more information.
54
+ */
55
+ @property({ type: String })
56
+ width: string = '100%';
57
+
58
+ /**
59
+ * The height of the component.
60
+ *
61
+ * Visit https://genspectrum.github.io/dashboard-components/?path=/docs/components-size-of-components--docs for more information.
62
+ */
63
+ @property({ type: String })
64
+ height: string = '700px';
65
+
66
+ override render() {
67
+ return (
68
+ <WastewaterMutationsOverTime
69
+ lapisFilter={this.lapisFilter}
70
+ sequenceType={this.sequenceType}
71
+ width={this.width}
72
+ height={this.height}
73
+ />
74
+ );
75
+ }
76
+ }
77
+
78
+ declare global {
79
+ interface HTMLElementTagNameMap {
80
+ 'gs-wastewater-mutations-over-time': WastewaterMutationsOverTimeComponent;
81
+ }
82
+ }
83
+
84
+ declare global {
85
+ // eslint-disable-next-line @typescript-eslint/no-namespace
86
+ namespace JSX {
87
+ interface IntrinsicElements {
88
+ 'gs-wastewater-mutations-over-time': DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement>;
89
+ }
90
+ }
91
+ }
92
+
93
+ /* eslint-disable @typescript-eslint/no-unused-vars, no-unused-vars */
94
+ type LapisFilterMatches = Expect<
95
+ Equals<
96
+ typeof WastewaterMutationsOverTimeComponent.prototype.lapisFilter,
97
+ WastewaterMutationsOverTimeProps['lapisFilter']
98
+ >
99
+ >;
100
+ type SequenceTypeMatches = Expect<
101
+ Equals<
102
+ typeof WastewaterMutationsOverTimeComponent.prototype.sequenceType,
103
+ WastewaterMutationsOverTimeProps['sequenceType']
104
+ >
105
+ >;
106
+ type WidthMatches = Expect<
107
+ Equals<typeof WastewaterMutationsOverTimeComponent.prototype.width, WastewaterMutationsOverTimeProps['width']>
108
+ >;
109
+ type HeightMatches = Expect<
110
+ Equals<typeof WastewaterMutationsOverTimeComponent.prototype.height, WastewaterMutationsOverTimeProps['height']>
111
+ >;
112
+ /* eslint-enable @typescript-eslint/no-unused-vars, no-unused-vars */
@@ -0,0 +1 @@
1
+ export { WastewaterMutationsOverTimeComponent } from './gs-wastewater-mutations-over-time';