@genspectrum/dashboard-components 0.9.0 → 0.10.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.
Files changed (24) hide show
  1. package/custom-elements.json +5 -5
  2. package/dist/assets/{mutationOverTimeWorker-DuWGESoO.js.map → mutationOverTimeWorker-CvZg52rf.js.map} +1 -1
  3. package/dist/components.d.ts +1 -1
  4. package/dist/components.js +66 -36
  5. package/dist/components.js.map +1 -1
  6. package/dist/style.css +2 -2
  7. package/package.json +1 -1
  8. package/src/preact/components/confidence-interval-selector.tsx +1 -1
  9. package/src/preact/components/no-data-display.tsx +2 -2
  10. package/src/preact/mutationFilter/parseMutation.spec.ts +30 -0
  11. package/src/preact/mutationFilter/sequenceTypeFromSegment.ts +3 -2
  12. package/src/preact/mutationsOverTime/mutations-over-time-grid.tsx +2 -3
  13. package/src/preact/prevalenceOverTime/prevalence-over-time.stories.tsx +4 -4
  14. package/src/preact/prevalenceOverTime/prevalence-over-time.tsx +11 -1
  15. package/src/preact/relativeGrowthAdvantage/relative-growth-advantage.stories.tsx +91 -0
  16. package/src/preact/relativeGrowthAdvantage/relative-growth-advantage.tsx +6 -0
  17. package/src/query/queryRelativeGrowthAdvantage.ts +61 -24
  18. package/src/utils/mutations.ts +3 -3
  19. package/src/web-components/visualization/gs-prevalence-over-time.stories.ts +1 -1
  20. package/src/web-components/visualization/gs-prevalence-over-time.tsx +2 -2
  21. package/standalone-bundle/assets/{mutationOverTimeWorker-MVSt1FVw.js.map → mutationOverTimeWorker-CypX_PYM.js.map} +1 -1
  22. package/standalone-bundle/dashboard-components.js +2343 -2316
  23. package/standalone-bundle/dashboard-components.js.map +1 -1
  24. package/standalone-bundle/style.css +1 -1
package/dist/style.css CHANGED
@@ -3416,9 +3416,9 @@ input.tab:checked + .tab-content,
3416
3416
  .peer:hover ~ .peer-hover\:visible {
3417
3417
  visibility: visible;
3418
3418
  }
3419
- @container (min-width: 30rem) {
3419
+ @container (min-width: 2rem) {
3420
3420
 
3421
- .\@\[30rem\]\:visible {
3421
+ .\@\[2rem\]\:visible {
3422
3422
  visibility: visible;
3423
3423
  }
3424
3424
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genspectrum/dashboard-components",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "GenSpectrum web components for building dashboards",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-only",
@@ -20,7 +20,7 @@ export const ConfidenceIntervalSelector: FunctionComponent<ConfidenceIntervalSel
20
20
 
21
21
  const items = [
22
22
  { label: 'Confidence interval method', value: 'none', disabled: true },
23
- ...confidenceIntervalMethods.concat('none').map((method) => {
23
+ ...confidenceIntervalMethods.map((method) => {
24
24
  switch (method) {
25
25
  case 'wilson':
26
26
  return { label: 'Wilson, 95% CI', value: 'wilson' };
@@ -1,9 +1,9 @@
1
1
  import { type FunctionComponent } from 'preact';
2
2
 
3
- export const NoDataDisplay: FunctionComponent = () => {
3
+ export const NoDataDisplay: FunctionComponent<{ message?: string }> = ({ message = 'No data available.' }) => {
4
4
  return (
5
5
  <div className='h-full w-full rounded-md border-2 border-gray-100 p-2 flex items-center justify-center'>
6
- <div>No data available.</div>
6
+ <div>{message}</div>
7
7
  </div>
8
8
  );
9
9
  };
@@ -35,6 +35,16 @@ describe('parseMutation', () => {
35
35
  input: 'ins_gene1:10:ACGT',
36
36
  expected: { type: 'aminoAcidInsertions', value: new InsertionClass('gene1', 10, 'ACGT') },
37
37
  },
38
+ {
39
+ name: 'should parse amino acid insertions in all upper case',
40
+ input: 'INS_GENE1:10:ACGT',
41
+ expected: { type: 'aminoAcidInsertions', value: new InsertionClass('GENE1', 10, 'ACGT') },
42
+ },
43
+ {
44
+ name: 'should parse amino acid insertions in all lower case',
45
+ input: 'ins_gene1:10:acgt',
46
+ expected: { type: 'aminoAcidInsertions', value: new InsertionClass('gene1', 10, 'acgt') },
47
+ },
38
48
  {
39
49
  name: 'should parse amino acid insertion with LAPIS-style wildcard',
40
50
  input: 'ins_gene1:10:?AC?GT',
@@ -78,6 +88,16 @@ describe('parseMutation', () => {
78
88
  input: 'gene1:A123-',
79
89
  expected: { type: 'aminoAcidMutations', value: new DeletionClass('gene1', 'A', 123) },
80
90
  },
91
+ {
92
+ name: 'should parse amino acid deletion in all upper case',
93
+ input: 'GENE1:A123-',
94
+ expected: { type: 'aminoAcidMutations', value: new DeletionClass('GENE1', 'A', 123) },
95
+ },
96
+ {
97
+ name: 'should parse amino acid deletion in all lower case',
98
+ input: 'gene1:a123-',
99
+ expected: { type: 'aminoAcidMutations', value: new DeletionClass('gene1', 'a', 123) },
100
+ },
81
101
  {
82
102
  name: 'should parse amino acid deletion without valueAtReference',
83
103
  input: 'gene1:123-',
@@ -123,6 +143,16 @@ describe('parseMutation', () => {
123
143
  input: 'gene1:A123T',
124
144
  expected: { type: 'aminoAcidMutations', value: new SubstitutionClass('gene1', 'A', 'T', 123) },
125
145
  },
146
+ {
147
+ name: 'should parse amino acid substitution in all upper case',
148
+ input: 'GENE1:A123T',
149
+ expected: { type: 'aminoAcidMutations', value: new SubstitutionClass('GENE1', 'A', 'T', 123) },
150
+ },
151
+ {
152
+ name: 'should parse amino acid substitution in all lower case',
153
+ input: 'gene1:a123t',
154
+ expected: { type: 'aminoAcidMutations', value: new SubstitutionClass('gene1', 'a', 't', 123) },
155
+ },
126
156
  {
127
157
  name: 'should return null for substitution with segment not in reference genome',
128
158
  input: 'notInReferenceGenome:A123T',
@@ -9,11 +9,12 @@ export const sequenceTypeFromSegment = (
9
9
  return isSingleSegmented(referenceGenome) ? 'nucleotide' : undefined;
10
10
  }
11
11
 
12
- if (referenceGenome.nucleotideSequences.some((sequence) => sequence.name === possibleSegment)) {
12
+ const segment = possibleSegment.toLowerCase();
13
+ if (referenceGenome.nucleotideSequences.some((sequence) => sequence.name.toLowerCase() === segment)) {
13
14
  return 'nucleotide';
14
15
  }
15
16
 
16
- if (referenceGenome.genes.some((gene) => gene.name === possibleSegment)) {
17
+ if (referenceGenome.genes.some((gene) => gene.name.toLowerCase() === segment)) {
17
18
  return 'amino acid';
18
19
  }
19
20
  return undefined;
@@ -43,7 +43,6 @@ const MutationsOverTimeGrid: FunctionComponent<MutationsOverTimeGridProps> = ({
43
43
  gridTemplateRows: `repeat(${shownMutations.length}, 24px)`,
44
44
  gridTemplateColumns: `${MUTATION_CELL_WIDTH_REM}rem repeat(${dates.length}, minmax(0.05rem, 1fr))`,
45
45
  }}
46
- className='@container'
47
46
  >
48
47
  {shownMutations.map((mutation, rowIndex) => {
49
48
  return (
@@ -122,9 +121,9 @@ const ProportionCell: FunctionComponent<{
122
121
  backgroundColor: getColorWithingScale(value.proportion, colorScale),
123
122
  color: getTextColorForScale(value.proportion, colorScale),
124
123
  }}
125
- className={`w-full h-full text-center hover:font-bold text-xs group`}
124
+ className={`w-full h-full text-center hover:font-bold text-xs group @container`}
126
125
  >
127
- <span className='invisible @[30rem]:visible'>{formatProportion(value.proportion, 0)}</span>
126
+ <span className='invisible @[2rem]:visible'>{formatProportion(value.proportion, 0)}</span>
128
127
  </div>
129
128
  </Tooltip>
130
129
  </div>
@@ -30,7 +30,7 @@ export default {
30
30
  control: { type: 'check' },
31
31
  },
32
32
  confidenceIntervalMethods: {
33
- options: ['wilson'],
33
+ options: ['none', 'wilson'],
34
34
  control: { type: 'check' },
35
35
  },
36
36
  width: { control: 'text' },
@@ -73,7 +73,7 @@ export const TwoVariants: StoryObj<PrevalenceOverTimeProps> = {
73
73
  granularity: 'month',
74
74
  smoothingWindow: 0,
75
75
  views: ['bar', 'line', 'bubble', 'table'],
76
- confidenceIntervalMethods: ['wilson'],
76
+ confidenceIntervalMethods: ['none', 'wilson'],
77
77
  width: '100%',
78
78
  height: '700px',
79
79
  lapisDateField: 'date',
@@ -147,7 +147,7 @@ export const OneVariant: StoryObj<PrevalenceOverTimeProps> = {
147
147
  granularity: 'day',
148
148
  smoothingWindow: 7,
149
149
  views: ['bar', 'line', 'bubble', 'table'],
150
- confidenceIntervalMethods: ['wilson'],
150
+ confidenceIntervalMethods: ['none', 'wilson'],
151
151
  width: '100%',
152
152
  height: '700px',
153
153
  lapisDateField: 'date',
@@ -205,7 +205,7 @@ export const ShowsNoDataBanner: StoryObj<PrevalenceOverTimeProps> = {
205
205
  granularity: 'day',
206
206
  smoothingWindow: 7,
207
207
  views: ['bar', 'line', 'bubble', 'table'],
208
- confidenceIntervalMethods: ['wilson'],
208
+ confidenceIntervalMethods: ['none', 'wilson'],
209
209
  width: '100%',
210
210
  height: '700px',
211
211
  lapisDateField: 'date',
@@ -1,5 +1,5 @@
1
1
  import { type FunctionComponent } from 'preact';
2
- import { useContext, useState } from 'preact/hooks';
2
+ import { useContext, useEffect, useState } from 'preact/hooks';
3
3
 
4
4
  import { getPrevalenceOverTimeTableData } from './getPrevalenceOverTimeTableData';
5
5
  import PrevalenceOverTimeBarChart from './prevalence-over-time-bar-chart';
@@ -97,6 +97,16 @@ const PrevalenceOverTimeTabs: FunctionComponent<PrevalenceOverTimeTabsProps> = (
97
97
  const [confidenceIntervalMethod, setConfidenceIntervalMethod] = useState<ConfidenceIntervalMethod>(
98
98
  confidenceIntervalMethods.length > 0 ? confidenceIntervalMethods[0] : 'none',
99
99
  );
100
+
101
+ useEffect(() => {
102
+ setConfidenceIntervalMethod((confidenceIntervalMethod) => {
103
+ if (!confidenceIntervalMethods.includes(confidenceIntervalMethod)) {
104
+ return confidenceIntervalMethods.length > 0 ? confidenceIntervalMethods[0] : 'none';
105
+ }
106
+ return confidenceIntervalMethod;
107
+ });
108
+ }, [confidenceIntervalMethods]);
109
+
100
110
  const yAxisMaxConfig = { linear: yAxisMaxLinear, logarithmic: yAxisMaxLogarithmic };
101
111
 
102
112
  const getTab = (view: View) => {
@@ -1,4 +1,5 @@
1
1
  import { type StoryObj } from '@storybook/preact';
2
+ import { expect, waitFor, within } from '@storybook/test';
2
3
 
3
4
  import denominator from './__mockData__/denominatorFilter.json';
4
5
  import numerator from './__mockData__/numeratorFilter.json';
@@ -99,3 +100,93 @@ export const Primary: StoryObj<RelativeGrowthAdvantageProps> = {
99
100
  },
100
101
  },
101
102
  };
103
+
104
+ export const TooFewDataToComputeGrowthAdvantage: StoryObj<RelativeGrowthAdvantageProps> = {
105
+ ...Primary,
106
+ args: {
107
+ ...Primary.args,
108
+ numeratorFilter: {
109
+ country: 'Switzerland',
110
+ pangoLineage: 'B.1.1.7',
111
+ dateFrom: '2021-02-28',
112
+ dateTo: '2021-03-01',
113
+ },
114
+ denominatorFilter: {
115
+ country: 'Switzerland',
116
+ dateFrom: '2021-02-28',
117
+ dateTo: '2021-03-01',
118
+ },
119
+ },
120
+ parameters: {
121
+ fetchMock: {
122
+ mocks: [
123
+ {
124
+ matcher: {
125
+ name: 'numeratorFilter',
126
+ url: AGGREGATED_ENDPOINT,
127
+ body: {
128
+ country: 'Switzerland',
129
+ pangoLineage: 'B.1.1.7',
130
+ dateFrom: '2021-02-28',
131
+ dateTo: '2021-03-01',
132
+ fields: ['date'],
133
+ },
134
+ },
135
+ response: {
136
+ status: 200,
137
+ body: {
138
+ data: [
139
+ {
140
+ date: '2021-02-28',
141
+ count: 5,
142
+ },
143
+ {
144
+ date: '2021-03-01',
145
+ count: 5,
146
+ },
147
+ ],
148
+ },
149
+ },
150
+ },
151
+ {
152
+ matcher: {
153
+ name: 'denominatorFilter',
154
+ url: AGGREGATED_ENDPOINT,
155
+ body: {
156
+ country: 'Switzerland',
157
+ dateFrom: '2021-02-28',
158
+ dateTo: '2021-03-01',
159
+ fields: ['date'],
160
+ },
161
+ },
162
+ response: {
163
+ status: 200,
164
+ body: {
165
+ data: [
166
+ {
167
+ date: '2021-02-28',
168
+ count: 5,
169
+ },
170
+ {
171
+ date: '2021-03-01',
172
+ count: 7,
173
+ },
174
+ ],
175
+ },
176
+ },
177
+ },
178
+ ],
179
+ },
180
+ },
181
+ play: async ({ canvasElement }) => {
182
+ const canvas = within(canvasElement);
183
+
184
+ await waitFor(() => {
185
+ const notEnoughDataMessage = canvas.queryByText(
186
+ 'It was not possible to estimate the relative growth advantage',
187
+ { exact: false },
188
+ );
189
+ return expect(notEnoughDataMessage).toBeVisible();
190
+ });
191
+ },
192
+ };
@@ -3,6 +3,7 @@ import { useContext, useState } from 'preact/hooks';
3
3
 
4
4
  import RelativeGrowthAdvantageChart from './relative-growth-advantage-chart';
5
5
  import {
6
+ NotEnoughDataToComputeFitError,
6
7
  queryRelativeGrowthAdvantage,
7
8
  type RelativeGrowthAdvantageData,
8
9
  } from '../../query/queryRelativeGrowthAdvantage';
@@ -62,6 +63,11 @@ export const RelativeGrowthAdvantageInner: FunctionComponent<RelativeGrowthAdvan
62
63
  }
63
64
 
64
65
  if (error !== null) {
66
+ if (error instanceof NotEnoughDataToComputeFitError) {
67
+ return (
68
+ <NoDataDisplay message='It was not possible to estimate the relative growth advantage due to insufficient data in the specified filter.' />
69
+ );
70
+ }
65
71
  throw error;
66
72
  }
67
73
 
@@ -1,11 +1,18 @@
1
1
  import { FetchAggregatedOperator } from '../operator/FetchAggregatedOperator';
2
2
  import { MapOperator } from '../operator/MapOperator';
3
3
  import { RenameFieldOperator } from '../operator/RenameFieldOperator';
4
+ import { UserFacingError } from '../preact/components/error-display';
4
5
  import { type LapisFilter } from '../types';
5
6
  import { getMinMaxTemporal, TemporalCache, type YearMonthDayClass } from '../utils/temporalClass';
6
7
 
7
8
  export type RelativeGrowthAdvantageData = Awaited<ReturnType<typeof queryRelativeGrowthAdvantage>>;
8
9
 
10
+ export class NotEnoughDataToComputeFitError extends Error {
11
+ constructor() {
12
+ super('Not enough data to compute computeFit');
13
+ }
14
+ }
15
+
9
16
  export async function queryRelativeGrowthAdvantage<LapisDateField extends string>(
10
17
  numerator: LapisFilter,
11
18
  denominator: LapisFilter,
@@ -54,6 +61,37 @@ export async function queryRelativeGrowthAdvantage<LapisDateField extends string
54
61
  requestData.k.push(numeratorCounts.get(d.date) ?? 0);
55
62
  }
56
63
  });
64
+
65
+ const responseData = await computeFit(generationTime, maxDate, minDate, requestData, signal);
66
+
67
+ const transformed = {
68
+ ...responseData,
69
+ estimatedProportions: {
70
+ ...responseData.estimatedProportions,
71
+ t: responseData.estimatedProportions.t.map((t) => minDate.addDays(t)),
72
+ },
73
+ };
74
+ const observedProportions = transformed.estimatedProportions.t.map(
75
+ (t) => (numeratorCounts.get(t) ?? 0) / (denominatorCounts.get(t) ?? 0),
76
+ );
77
+
78
+ return {
79
+ ...transformed,
80
+ observedProportions,
81
+ };
82
+ }
83
+
84
+ async function computeFit(
85
+ generationTime: number,
86
+ maxDate: YearMonthDayClass,
87
+ minDate: YearMonthDayClass,
88
+ requestData: {
89
+ t: number[];
90
+ k: number[];
91
+ n: number[];
92
+ },
93
+ signal: AbortSignal | undefined,
94
+ ) {
57
95
  const requestPayload = {
58
96
  config: {
59
97
  alpha: 0.95,
@@ -66,15 +104,29 @@ export async function queryRelativeGrowthAdvantage<LapisDateField extends string
66
104
  },
67
105
  data: requestData,
68
106
  };
69
- const response = await fetch('https://cov-spectrum.org/api/v2/computed/model/chen2021Fitness', {
70
- method: 'POST',
71
- headers: {
72
- 'Content-Type': 'application/json',
73
- },
74
- body: JSON.stringify(requestPayload),
75
- signal,
76
- });
77
- const responseData = (await response.json()) as {
107
+
108
+ let response;
109
+ try {
110
+ response = await fetch('https://cov-spectrum.org/api/v2/computed/model/chen2021Fitness', {
111
+ method: 'POST',
112
+ headers: {
113
+ 'Content-Type': 'application/json',
114
+ },
115
+ body: JSON.stringify(requestPayload),
116
+ signal,
117
+ });
118
+ } catch {
119
+ throw new UserFacingError(
120
+ 'Failed to compute relative growth advantage',
121
+ 'Could not connect to the server that computes the relative growth advantage. Please try again later.',
122
+ );
123
+ }
124
+
125
+ if (!response.ok) {
126
+ throw new NotEnoughDataToComputeFitError();
127
+ }
128
+
129
+ return (await response.json()) as {
78
130
  estimatedAbsoluteNumbers: {
79
131
  t: number[];
80
132
  variantCases: number[];
@@ -109,21 +161,6 @@ export async function queryRelativeGrowthAdvantage<LapisDateField extends string
109
161
  };
110
162
  };
111
163
  };
112
- const transformed = {
113
- ...responseData,
114
- estimatedProportions: {
115
- ...responseData.estimatedProportions,
116
- t: responseData.estimatedProportions.t.map((t) => minDate.addDays(t)),
117
- },
118
- };
119
- const observedProportions = transformed.estimatedProportions.t.map(
120
- (t) => (numeratorCounts.get(t) ?? 0) / (denominatorCounts.get(t) ?? 0),
121
- );
122
-
123
- return {
124
- ...transformed,
125
- observedProportions,
126
- };
127
164
  }
128
165
 
129
166
  function toYearMonthDay(d: { date: string | null; count: number }) {
@@ -14,7 +14,7 @@ export interface MutationClass extends Mutation {
14
14
  }
15
15
 
16
16
  export const substitutionRegex =
17
- /^((?<segment>[A-Za-z0-9_-]+)(?=:):)?(?<valueAtReference>[A-Za-z])?(?<position>\d+)(?<substitutionValue>[A-Za-z.])?$/;
17
+ /^((?<segment>[A-Z0-9_-]+)(?=:):)?(?<valueAtReference>[A-Z])?(?<position>\d+)(?<substitutionValue>[A-Z.])?$/i;
18
18
 
19
19
  export interface Substitution extends Mutation {
20
20
  type: 'substitution';
@@ -68,7 +68,7 @@ export class SubstitutionClass implements MutationClass, Substitution {
68
68
  }
69
69
  }
70
70
 
71
- export const deletionRegex = /^((?<segment>[A-Za-z0-9_-]+)(?=:):)?(?<valueAtReference>[A-Za-z])?(?<position>\d+)(-)$/;
71
+ export const deletionRegex = /^((?<segment>[A-Z0-9_-]+)(?=:):)?(?<valueAtReference>[A-Z])?(?<position>\d+)(-)$/i;
72
72
 
73
73
  export interface Deletion extends Mutation {
74
74
  type: 'deletion';
@@ -119,7 +119,7 @@ export class DeletionClass implements MutationClass, Deletion {
119
119
  }
120
120
 
121
121
  export const insertionRegexp =
122
- /^ins_((?<segment>[A-Za-z0-9_-]+)(?=:):)?(?<position>\d+):(?<insertedSymbols>(([A-Za-z?]|(\.\*))+))$/i;
122
+ /^ins_((?<segment>[A-Z0-9_-]+)(?=:):)?(?<position>\d+):(?<insertedSymbols>(([A-Z?]|(\.\*))+))$/i;
123
123
 
124
124
  export interface Insertion extends Mutation {
125
125
  type: 'insertion';
@@ -46,7 +46,7 @@ const meta: Meta<Required<PrevalenceOverTimeProps>> = {
46
46
  control: { type: 'check' },
47
47
  },
48
48
  confidenceIntervalMethods: {
49
- options: ['wilson'],
49
+ options: ['none', 'wilson'],
50
50
  control: { type: 'check' },
51
51
  },
52
52
  width: { control: 'text' },
@@ -103,11 +103,11 @@ export class PrevalenceOverTimeComponent extends PreactLitAdapterWithGridJsStyle
103
103
 
104
104
  /**
105
105
  * A list of methods to calculate the confidence interval.
106
- * The option `none` is always available and disables confidence intervals.
107
106
  * Pass an empty array to disable the confidence interval selector.
107
+ * The first entry will be selected by default.
108
108
  */
109
109
  @property({ type: Array })
110
- confidenceIntervalMethods: ('wilson' | 'none')[] = ['wilson'];
110
+ confidenceIntervalMethods: ('wilson' | 'none')[] = ['none', 'wilson'];
111
111
 
112
112
  /**
113
113
  * The width of the component.