-
Notifications
You must be signed in to change notification settings - Fork 113
/
MutationBadge.tsx
381 lines (319 loc) · 11.5 KB
/
MutationBadge.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* eslint-disable camelcase */
import React, { useMemo } from 'react'
import { get } from 'lodash'
import { parseVariant } from 'src/components/Common/parseVariant'
import styled from 'styled-components'
import type { Mutation, MutationColors } from 'src/types'
import { theme } from 'src/theme'
import { AMINOACID_COLORS, CLADE_COLORS, GENE_COLORS, NUCLEOTIDE_COLORS } from 'src/colors'
import { getClusterNames, getClusters } from 'src/io/getClusters'
import { LinkSmart } from 'src/components/Link/LinkSmart'
import { parseAminoacidMutation } from 'src/components/Common/parseAminoacidMutation'
import { parseNucleotideMutation } from 'src/components/Common/parseNucleotideMutation'
import { formatMutation } from 'src/components/Common/formatMutation'
import { AMINOACID_NAMES, GENE_NAMES, GREEK_ALPHABET, NUCELOTIDE_NAMES } from 'src/names'
import { colorHash } from 'src/helpers/colorHash'
import { rainbow } from 'src/helpers/colorRainbow'
const clusters = getClusters()
const clusterNames = getClusterNames()
const DEFAULT_COLOR = theme.gray700
const DEFAULT_TEXT_COLOR = theme.gray100
export const MutationBadgeBox = styled.span`
display: inline-block;
font-size: 0.75rem;
`
export const MutationWrapper = styled.span`
border-radius: 2px;
box-shadow: ${(props) => props.theme.shadows.lighter};
font-family: ${(props) => props.theme.font.monospace};
& > span:first-child {
padding-left: 4px;
border-top-left-radius: 3px;
border-bottom-left-radius: 3px;
}
& > span:last-child {
padding-right: 4px;
border-top-right-radius: 3px;
border-bottom-right-radius: 3px;
}
`
export const PrefixText = styled.span`
padding: 1px 5px;
color: ${(props) => props.theme.white};
background-color: ${(props) => props.theme.gray550};
`
export const LetterText = styled.span`
padding: 1px 5px;
color: ${(props) => props.theme.white};
background-color: ${(props) => props.theme.gray700};
`
export const WhoText = styled.span<{ $color: string }>`
padding: 1px 5px;
background-color: ${(props) => props.$color};
`
export const ColoredComponent = styled.span<{ $color: string }>`
background-color: ${(props) => props.$color};
`
export const ParentText = styled.span<{ $backgroundColor: string; $color: string }>`
padding-top: 1px;
padding-bottom: 1px;
padding-left: 5px;
padding-right: 1px;
color: ${(props) => props.$color};
background-color: ${(props) => props.$backgroundColor};
`
export const GeneText = styled.span<{ $color: string }>`
padding: 1px 2px;
background-color: ${(props) => props.$color};
color: ${(props) => props.theme.gray100};
font-weight: 700;
`
export const ColoredText = styled.span<{ $color: string }>`
padding: 1px 2px;
background-color: ${(props) => props.$color};
`
export const PositionText = styled.span`
padding: 1px 2px;
background-color: ${(props) => props.theme.gray300};
color: ${(props) => props.theme.gray800};
`
export const VersionText = styled.span`
padding: 1px 2px;
background-color: ${(props) => props.theme.gray400};
color: ${(props) => props.theme.gray800};
`
export function nucleotideMutationFromStringMaybe(mutation: Mutation | string): Mutation | undefined {
if (typeof mutation === 'string') {
return parseNucleotideMutation(mutation)
}
return mutation
}
export function aminoacidMutationFromStringMaybe(mutation: Mutation | string): Mutation | undefined {
if (typeof mutation === 'string') {
return parseAminoacidMutation(mutation)
}
return mutation
}
export function formatMutationMaybe(mutation: Mutation | string) {
if (typeof mutation === 'string') {
return mutation
}
return formatMutation(mutation)
}
export function formatVariantUrl(mutation: string) {
const cluster = clusters.find(({ display_name }) => display_name === mutation)
if (!cluster) {
console.warn(`Variant not recognized: ${mutation}. Known variants: ${clusterNames.join(', ')}`)
return undefined
}
return `/variants/${cluster.build_name}`
}
export interface MutationBadgeProps {
prefix?: string
mutation: Mutation
colors: MutationColors
tooltip?: string
}
export function MutationBadge({ prefix, mutation, colors, tooltip }: MutationBadgeProps) {
const { parent, parentDelimiter, gene, left, pos, right, version, note } = mutation
const parentColors = get(CLADE_COLORS, parent ?? '', { bg: DEFAULT_COLOR, fg: DEFAULT_TEXT_COLOR })
const geneColor = get(GENE_COLORS, gene ?? '', DEFAULT_COLOR)
const leftColor = get(colors, left ?? '', DEFAULT_COLOR)
const rightColor = get(colors, right ?? '', DEFAULT_COLOR)
const parentDelimiterStr = parentDelimiter ?? ''
return (
<MutationBadgeBox title={tooltip}>
<MutationWrapper>
{prefix && <PrefixText>{prefix}</PrefixText>}
{parent && (
<ParentText
$backgroundColor={parentColors.bg}
$color={parentColors.fg}
>{`${parent}${parentDelimiterStr}`}</ParentText>
)}
{gene && (
<GeneText $color={geneColor}>
{gene}
<span>{':'}</span>
</GeneText>
)}
{left && <ColoredText $color={leftColor}>{left}</ColoredText>}
{pos && <PositionText>{pos}</PositionText>}
{right && <ColoredText $color={rightColor}>{right}</ColoredText>}
{version && <VersionText>{version}</VersionText>}
</MutationWrapper>
{note && <span>{note}</span>}
</MutationBadgeBox>
)
}
export interface NucleotideMutationBadgeProps {
mutation: Mutation | string
}
export function NucleotideMutationBadge({ mutation }: NucleotideMutationBadgeProps) {
const mutationObj = nucleotideMutationFromStringMaybe(mutation)
if (!mutationObj) {
return <span>{`Invalid mutation: '${JSON.stringify(mutation)}'`}</span>
}
const { left, right, pos } = mutationObj
const wildTypeBase = get(NUCELOTIDE_NAMES, left ?? '', '')
const variantBase = get(NUCELOTIDE_NAMES, right ?? '', '')
const posStr = pos ?? ''
const tooltip = `Mutation of nucleotide ${posStr} from ${wildTypeBase} to ${variantBase}`
return <MutationBadge mutation={mutationObj} colors={NUCLEOTIDE_COLORS} tooltip={tooltip} />
}
export interface AminoacidMutationBadgeProps {
mutation: Mutation | string
}
export function AminoacidMutationBadge({ mutation }: AminoacidMutationBadgeProps) {
const mutationObj = aminoacidMutationFromStringMaybe(mutation)
if (!mutationObj) {
return <span>{`Invalid mutation: '${JSON.stringify(mutation)}'`}</span>
}
const { gene, left, pos, right } = mutationObj
const wildTypeAA = get(AMINOACID_NAMES, left ?? '', '')
const variantAA = right ? get(AMINOACID_NAMES, right, '') : 'one of several alternatives'
const geneName = gene ? get(GENE_NAMES, gene, gene) : ''
const posStr = pos ?? ''
const tooltip = `Mutation of amino acid ${posStr} in ${geneName} from ${wildTypeAA} to ${variantAA}`
return <MutationBadge mutation={mutationObj} colors={AMINOACID_COLORS} tooltip={tooltip} />
}
export const LinkUnstyled = styled(LinkSmart)`
color: unset;
text-decoration: none;
&:active,
&:focus,
&:hover {
color: unset;
text-decoration: none;
}
`
export function variantToObjectAndString(mutation: Mutation | string) {
let mutationObj: Mutation | undefined
let mutationStr: string | undefined
if (typeof mutation === 'string') {
mutationObj = parseVariant(mutation)
mutationStr = mutation
} else {
mutationObj = mutation
mutationStr = formatMutation(mutation)
}
return { mutationObj, mutationStr }
}
export interface VariantLinkBadgeProps {
name: Mutation | string
href?: string
prefix?: string
}
export function VariantLinkBadge({ name, href, prefix }: VariantLinkBadgeProps) {
const { mutationObj, mutationStr } = useMemo(() => variantToObjectAndString(name), [name])
const url = useMemo(() => href ?? formatVariantUrl(mutationStr), [href, mutationStr])
if (!mutationObj) {
return <span className="text-danger">{`VariantLinkBadge: Invalid mutation: ${JSON.stringify(name)}`}</span>
}
if (!url) {
return (
<span className="text-danger">
{
// prettier-ignore
`VariantLinkBadge: Variant not recognized: ${JSON.stringify(name)}.` +
`Known variants: ${clusterNames.join(', ')}`
}
</span>
)
}
return (
<LinkUnstyled href={url} icon={null}>
<MutationBadge prefix={prefix} mutation={mutationObj} colors={AMINOACID_COLORS} />
</LinkUnstyled>
)
}
export interface LineageLinkBadgeProps {
name: string
href?: string
prefix?: string
report?: boolean
}
export function LineageLinkBadge({ name, href, prefix, report }: LineageLinkBadgeProps) {
const url = useMemo(
// prettier-ignore
() => (href ?? (report ? `https://cov-lineages.org/global_report_${name}.html` : '')),
[href, report, name],
)
const tooltip = useMemo(() => `Pango Lineage ${name}`, [name])
return (
<LinkUnstyled href={url}>
<MutationBadgeBox title={tooltip}>
<MutationWrapper>
{prefix && <PrefixText>{prefix}</PrefixText>}
<ColoredText
$color={colorHash(name, {
reverse: false,
prefix: '',
suffix: '',
lightness: 0.75,
hue: undefined,
saturation: undefined,
})}
>
{name}
</ColoredText>
</MutationWrapper>
</MutationBadgeBox>
</LinkUnstyled>
)
}
export interface WhoBadgeProps {
name: string
href?: string
prefix?: string
}
const whoRainbow = rainbow(Object.keys(GREEK_ALPHABET).length, { rgb: true, lum: 75, sat: 75 })
export function getWhoBadgeColor(name: string): string {
const i = Object.keys(GREEK_ALPHABET).indexOf(name.toLowerCase().trim())
if (i < 0 || i > whoRainbow.length) {
return theme.gray500
}
return whoRainbow[i]
}
export function WhoBadge({ name, href, prefix }: WhoBadgeProps) {
const letter = get(GREEK_ALPHABET, name.toLowerCase().trim(), '')
const tooltip = useMemo(() => `WHO Label: ${letter} (${name})`, [letter, name])
const color = getWhoBadgeColor(name)
return (
<LinkUnstyled href={href}>
<MutationBadgeBox title={tooltip}>
<MutationWrapper>
{prefix && <PrefixText>{prefix}</PrefixText>}
{letter && <LetterText>{letter}</LetterText>}
<WhoText className="pl-1" $color={color}>
{name}
</WhoText>
</MutationWrapper>
</MutationBadgeBox>
</LinkUnstyled>
)
}
/** Shorter convenience alias for NucleotideMutationBadge */
export function NucMut({ mut }: { mut: string }) {
return <NucleotideMutationBadge mutation={mut} />
}
/** Shorter convenience alias for AminoacidMutationBadge */
export function AaMut({ mut }: { mut: string }) {
return <AminoacidMutationBadge mutation={mut} />
}
/** Shorter convenience alias for VariantLinkBadge */
export function Var({ name, href, prefix = 'Variant' }: { name: string; href?: string; prefix?: string }) {
return <VariantLinkBadge name={name} href={href} prefix={prefix} />
}
/** Shorter convenience alias for VariantLinkBadge */
export function Mut({ name, href, prefix = 'Mutation' }: { name: string; href?: string; prefix?: string }) {
return <VariantLinkBadge name={name} href={href} prefix={prefix} />
}
/** Shorter convenience alias for LineageLinkBadge */
export function Lin({ name, href, prefix = '', report }: LineageLinkBadgeProps) {
return <LineageLinkBadge name={name} href={href} prefix={prefix} report={report} />
}
/** Shorter convenience alias for WhoBadge */
export function Who({ name, href, prefix = '' }: WhoBadgeProps) {
return <WhoBadge name={name} href={href} prefix={prefix} />
}