@rtemis/draw 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.
@@ -0,0 +1,905 @@
1
+ "use strict";
2
+ var RtemisDraw = (() => {
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ buildA3Option: () => buildA3Option,
25
+ builders: () => builders
26
+ });
27
+
28
+ // src/charts/a3/layout.ts
29
+ var AMINO_ACID_NAMES = Object.freeze({
30
+ A: "Alanine",
31
+ R: "Arginine",
32
+ N: "Asparagine",
33
+ D: "Aspartate",
34
+ C: "Cysteine",
35
+ Q: "Glutamine",
36
+ E: "Glutamate",
37
+ G: "Glycine",
38
+ H: "Histidine",
39
+ I: "Isoleucine",
40
+ L: "Leucine",
41
+ K: "Lysine",
42
+ M: "Methionine",
43
+ F: "Phenylalanine",
44
+ P: "Proline",
45
+ S: "Serine",
46
+ T: "Threonine",
47
+ W: "Tryptophan",
48
+ Y: "Tyrosine",
49
+ V: "Valine",
50
+ B: "Aspartic acid or Asparagine",
51
+ Z: "Glutamine or Glutamic acid",
52
+ X: "(Any)",
53
+ "*": "Termination codon"
54
+ });
55
+ var DEFAULT_FONT_SIZE = 18;
56
+ var DEFAULT_MARGIN_TOP = 24;
57
+ var DEFAULT_MARGIN_TOP_WITH_TITLE = 64;
58
+ var DEFAULT_TITLE_TOP = 32;
59
+ var DEFAULT_TITLE_BOTTOM_GAP = 14;
60
+ var DEFAULT_N_PER_ROW = 21;
61
+ var DEFAULT_RESIDUE_SPACING = 0.3;
62
+ function createA3LayoutModel(input, options = {}) {
63
+ const data = input;
64
+ const sequenceLength = data.sequence.length;
65
+ const nPerRow = normalizeNPerRow(options.nPerRow, sequenceLength);
66
+ const residueSpacing = normalizeResidueSpacing(options.residueSpacing);
67
+ const coordinates = createSequenceCoordinates(sequenceLength, nPerRow);
68
+ const residues = Array.from(data.sequence).map(
69
+ (residue, index) => Object.freeze({
70
+ position: index + 1,
71
+ residue,
72
+ residueName: AMINO_ACID_NAMES[residue] ?? residue,
73
+ x: scaleResidueXCoordinate(coordinates.xs[index] ?? 0, residueSpacing),
74
+ y: coordinates.ys[index] ?? 0
75
+ })
76
+ );
77
+ return Object.freeze({
78
+ data,
79
+ nPerRow,
80
+ residues: Object.freeze(residues),
81
+ bounds: computeBounds(residues)
82
+ });
83
+ }
84
+ function resolveA3TitleTop(options = {}) {
85
+ return options.titleTop ?? DEFAULT_TITLE_TOP;
86
+ }
87
+ function resolveA3TitleMarginTop(options = {}) {
88
+ if (options.title === void 0) {
89
+ return DEFAULT_MARGIN_TOP;
90
+ }
91
+ const fontSize = options.fontSize ?? DEFAULT_FONT_SIZE;
92
+ return Math.max(
93
+ DEFAULT_MARGIN_TOP_WITH_TITLE,
94
+ resolveA3TitleTop(options) + fontSize + DEFAULT_TITLE_BOTTOM_GAP
95
+ );
96
+ }
97
+ function createSequenceCoordinates(length, nPerRow) {
98
+ if (!Number.isInteger(length) || length <= 0) {
99
+ throw new RangeError("A3 layout length must be a positive integer.");
100
+ }
101
+ const xs = [];
102
+ const ys = [];
103
+ const xPattern = [...range(1, nPerRow), ...range(nPerRow - 1, 2)];
104
+ const yPattern = [1];
105
+ const nRows = Math.ceil((length - 1) / (nPerRow - 1));
106
+ const repeated = repeatEach(stepSequence(1, nRows, 1), nPerRow - 1, Math.max(0, length - 1));
107
+ yPattern.push(...repeated);
108
+ for (let index = 0; index < length; index += 1) {
109
+ xs.push(xPattern[index % xPattern.length] ?? 1);
110
+ ys.push(yPattern[index] ?? 1);
111
+ }
112
+ const period = 2 * nPerRow - 2;
113
+ const borderOffset = 1 - Math.sqrt(3) / 2;
114
+ for (let index = nPerRow - 1; index < length; index += period) {
115
+ ys[index] = (ys[index] ?? 0) + 0.5;
116
+ xs[index] = (xs[index] ?? 0) - borderOffset;
117
+ }
118
+ for (let index = 2 * nPerRow - 2; index < length; index += period) {
119
+ ys[index] = (ys[index] ?? 0) + 0.5;
120
+ xs[index] = (xs[index] ?? 0) + borderOffset;
121
+ }
122
+ return Object.freeze({ xs: Object.freeze(xs), ys: Object.freeze(ys) });
123
+ }
124
+ function normalizeNPerRow(value, _length) {
125
+ const resolved = value ?? DEFAULT_N_PER_ROW;
126
+ if (!Number.isInteger(resolved) || resolved <= 1) {
127
+ throw new RangeError("A3 visualization nPerRow must be an integer > 1.");
128
+ }
129
+ return resolved;
130
+ }
131
+ function normalizeResidueSpacing(value) {
132
+ const resolved = value ?? DEFAULT_RESIDUE_SPACING;
133
+ if (!Number.isFinite(resolved) || resolved <= 0) {
134
+ throw new RangeError("A3 visualization residueSpacing must be > 0.");
135
+ }
136
+ return resolved;
137
+ }
138
+ function scaleResidueXCoordinate(x, residueSpacing) {
139
+ if (residueSpacing === 1) {
140
+ return x;
141
+ }
142
+ return 1 + (x - 1) * residueSpacing;
143
+ }
144
+ function computeBounds(residues) {
145
+ const xs = residues.map((residue) => residue.x);
146
+ const ys = residues.map((residue) => residue.y);
147
+ return Object.freeze({
148
+ minX: Math.min(...xs),
149
+ maxX: Math.max(...xs),
150
+ minY: Math.min(...ys),
151
+ maxY: Math.max(...ys)
152
+ });
153
+ }
154
+ function range(start, end) {
155
+ if (start <= end) {
156
+ return Array.from({ length: end - start + 1 }, (_, index) => start + index);
157
+ }
158
+ return Array.from({ length: start - end + 1 }, (_, index) => start - index);
159
+ }
160
+ function stepSequence(start, end, step) {
161
+ const values = [];
162
+ for (let value = start; value <= end; value += step) {
163
+ values.push(value);
164
+ }
165
+ return values;
166
+ }
167
+ function repeatEach(values, repeatCount, outputLength) {
168
+ const output = [];
169
+ for (const value of values) {
170
+ for (let index = 0; index < repeatCount && output.length < outputLength; index += 1) {
171
+ output.push(value);
172
+ }
173
+ }
174
+ return output;
175
+ }
176
+
177
+ // src/charts/a3/palette.ts
178
+ var DEFAULT_REGION_PALETTE = Object.freeze([
179
+ "#0F766E",
180
+ "#2563EB",
181
+ "#D97706",
182
+ "#7C3AED",
183
+ "#DC2626",
184
+ "#0891B2"
185
+ ]);
186
+ var DEFAULT_SITE_PALETTE = Object.freeze([
187
+ "#0EA5E9",
188
+ "#14B8A6",
189
+ "#E11D48",
190
+ "#A855F7",
191
+ "#F97316",
192
+ "#16A34A"
193
+ ]);
194
+ var DEFAULT_PTM_PALETTE = Object.freeze([
195
+ "#0284C7",
196
+ "#F59E0B",
197
+ "#10B981",
198
+ "#8B5CF6",
199
+ "#EF4444",
200
+ "#14B8A6"
201
+ ]);
202
+ var DEFAULT_PROCESSING_PALETTE = Object.freeze([
203
+ "#D97706",
204
+ "#DC2626",
205
+ "#0EA5E9",
206
+ "#9333EA",
207
+ "#16A34A",
208
+ "#DB2777"
209
+ ]);
210
+ var DEFAULT_GEIST_FONT_STACK = "Geist, 'Geist Sans', 'Inter', 'Avenir Next', 'Segoe UI', sans-serif";
211
+ var DEFAULT_A3_VISUALIZATION_THEME = Object.freeze({
212
+ fontFamily: DEFAULT_GEIST_FONT_STACK,
213
+ annotationFontFamily: DEFAULT_GEIST_FONT_STACK,
214
+ legendFontFamily: DEFAULT_GEIST_FONT_STACK,
215
+ backgroundColor: "transparent",
216
+ plotBackgroundColor: "transparent",
217
+ residueFillColor: "#E7E5E4",
218
+ residueStrokeColor: "#44403C",
219
+ labelColor: "#1C1917",
220
+ positionLabelColor: "#78716C",
221
+ legendColor: "#1C1917",
222
+ gridColor: "#E7E5E4",
223
+ variantColor: "#FA6E1E",
224
+ diseaseVariantColor: "#E266AE",
225
+ regionPalette: DEFAULT_REGION_PALETTE,
226
+ sitePalette: DEFAULT_SITE_PALETTE,
227
+ ptmPalette: DEFAULT_PTM_PALETTE,
228
+ processingPalette: DEFAULT_PROCESSING_PALETTE
229
+ });
230
+ var DARK_A3_VISUALIZATION_THEME = Object.freeze({
231
+ fontFamily: DEFAULT_GEIST_FONT_STACK,
232
+ annotationFontFamily: DEFAULT_GEIST_FONT_STACK,
233
+ legendFontFamily: DEFAULT_GEIST_FONT_STACK,
234
+ backgroundColor: "transparent",
235
+ plotBackgroundColor: "transparent",
236
+ residueFillColor: "#3f3f46",
237
+ // zinc-700
238
+ residueStrokeColor: "#a1a1aa",
239
+ // zinc-400
240
+ labelColor: "#fafafa",
241
+ // zinc-50
242
+ positionLabelColor: "#a1a1aa",
243
+ // zinc-400
244
+ legendColor: "#e4e4e7",
245
+ // zinc-200
246
+ gridColor: "#3f3f46",
247
+ // zinc-700
248
+ variantColor: "#fb923c",
249
+ // orange-400 (brighter for dark bg)
250
+ diseaseVariantColor: "#f472b6",
251
+ // pink-400 (brighter for dark bg)
252
+ regionPalette: DEFAULT_REGION_PALETTE,
253
+ sitePalette: DEFAULT_SITE_PALETTE,
254
+ ptmPalette: DEFAULT_PTM_PALETTE,
255
+ processingPalette: DEFAULT_PROCESSING_PALETTE
256
+ });
257
+ function resolveA3VisualizationTheme(overrides = {}) {
258
+ const fallbackFontFamily = overrides.fontFamily ?? DEFAULT_A3_VISUALIZATION_THEME.annotationFontFamily;
259
+ return Object.freeze({
260
+ ...DEFAULT_A3_VISUALIZATION_THEME,
261
+ ...overrides,
262
+ fontFamily: fallbackFontFamily,
263
+ annotationFontFamily: overrides.annotationFontFamily ?? fallbackFontFamily,
264
+ legendFontFamily: overrides.legendFontFamily ?? fallbackFontFamily,
265
+ regionPalette: overrides.regionPalette ?? DEFAULT_REGION_PALETTE,
266
+ sitePalette: overrides.sitePalette ?? DEFAULT_SITE_PALETTE,
267
+ ptmPalette: overrides.ptmPalette ?? DEFAULT_PTM_PALETTE,
268
+ processingPalette: overrides.processingPalette ?? DEFAULT_PROCESSING_PALETTE
269
+ });
270
+ }
271
+ function paletteColorAt(palette, index) {
272
+ if (palette.length === 0) {
273
+ throw new RangeError("A3 visualization palettes cannot be empty.");
274
+ }
275
+ return palette[index % palette.length] ?? palette[0] ?? "#000000";
276
+ }
277
+ function withAlpha(color, alpha) {
278
+ const normalized = color.trim();
279
+ if (!normalized.startsWith("#")) {
280
+ return normalized;
281
+ }
282
+ const hex = normalized.slice(1);
283
+ const expanded = hex.length === 3 ? hex.split("").map((character) => `${character}${character}`).join("") : hex;
284
+ if (expanded.length !== 6) {
285
+ return normalized;
286
+ }
287
+ const red = Number.parseInt(expanded.slice(0, 2), 16);
288
+ const green = Number.parseInt(expanded.slice(2, 4), 16);
289
+ const blue = Number.parseInt(expanded.slice(4, 6), 16);
290
+ return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
291
+ }
292
+
293
+ // src/charts/a3/echarts.ts
294
+ var X_AXIS_PADDING = 0.2;
295
+ var Y_AXIS_PADDING_TOP = 0.7;
296
+ var Y_AXIS_PADDING_BOTTOM = 0.4;
297
+ var REGION_LEGEND_ICON = "path://M2 5 H22 V11 H2 Z";
298
+ var LEGEND_HEADING_REGIONS = "__legend_heading_regions__";
299
+ var LEGEND_HEADING_PTMS = "__legend_heading_ptms__";
300
+ var LEGEND_HEADING_PROCESSING = "__legend_heading_processing__";
301
+ var ANNOTATION_FAMILY_ORDER = [
302
+ "Sites",
303
+ "Regions",
304
+ "PTMs",
305
+ "Processing",
306
+ "Variants"
307
+ ];
308
+ function createA3EChartsOption(input, options = {}) {
309
+ const model = createA3LayoutModel(input, options);
310
+ const theme = resolveA3VisualizationTheme(options.theme);
311
+ const markerSize = options.markerSize ?? 28;
312
+ const fontSize = options.fontSize ?? 18;
313
+ const lineWidth = options.lineWidth ?? 2;
314
+ const showMarkers = options.showMarkers ?? true;
315
+ const showLabels = options.showLabels ?? true;
316
+ const annotationGap = options.annotationGap ?? 16;
317
+ const ptmPlacement = options.ptmPlacement ?? "radial";
318
+ const markerColor = options.markerColor ?? "transparent";
319
+ const regionOpacity = clampOpacity(options.regionOpacity);
320
+ const legendRightInset = 16;
321
+ const legendRailWidth = 196;
322
+ const legendGap = options.legendGap ?? 90;
323
+ const positionLabelGap = options.positionLabelGap ?? 16;
324
+ const enableAnimation = options.enableAnimation ?? true;
325
+ const animationDuration = options.animationDuration ?? 220;
326
+ const animationDurationUpdate = options.animationDurationUpdate ?? 140;
327
+ const titleTop = resolveA3TitleTop(options);
328
+ const titleMarginTop = resolveA3TitleMarginTop(options);
329
+ const series = [];
330
+ const annotationsByPosition = /* @__PURE__ */ new Map();
331
+ const addAnnotationHit = (position, hit) => {
332
+ const existing = annotationsByPosition.get(position);
333
+ if (existing) {
334
+ existing.push(hit);
335
+ } else {
336
+ annotationsByPosition.set(position, [hit]);
337
+ }
338
+ };
339
+ const regionLegendEntries = [];
340
+ const siteLegendEntries = [];
341
+ const ptmLegendEntries = [];
342
+ const processingLegendEntries = [];
343
+ const labelColors = buildResidueLabelColors(model, options, theme);
344
+ if (showMarkers) {
345
+ series.push({
346
+ type: "line",
347
+ name: "Primary structure",
348
+ z: 10,
349
+ data: model.residues.map((residue) => ({
350
+ value: [residue.x, residue.y],
351
+ residue: residue.residue,
352
+ residueName: residue.residueName,
353
+ position: residue.position
354
+ })),
355
+ tooltip: {
356
+ formatter: (params) => formatResidueTooltip(
357
+ params.data?.position,
358
+ params.data?.residueName,
359
+ annotationsByPosition,
360
+ theme
361
+ )
362
+ },
363
+ showSymbol: true,
364
+ symbol: "circle",
365
+ symbolSize: markerSize,
366
+ lineStyle: {
367
+ color: theme.residueStrokeColor,
368
+ width: lineWidth
369
+ },
370
+ itemStyle: {
371
+ color: markerColor,
372
+ borderColor: theme.residueStrokeColor,
373
+ borderWidth: 1
374
+ }
375
+ });
376
+ }
377
+ for (const [index, [name, entry]] of Object.entries(model.data.annotations.region).entries()) {
378
+ const color = paletteColorAt(theme.regionPalette, index);
379
+ const data = [];
380
+ for (const [start, end] of entry.index) {
381
+ for (let position = start; position <= end; position += 1) {
382
+ const residue = model.residues[position - 1];
383
+ if (residue !== void 0) {
384
+ data.push([residue.x, residue.y]);
385
+ addAnnotationHit(position, { family: "Regions", name, color });
386
+ }
387
+ }
388
+ data.push([null, null]);
389
+ }
390
+ series.push({
391
+ type: "line",
392
+ name,
393
+ z: 15,
394
+ // Purely visual: ignore pointer events so hovering the band falls through
395
+ // to the residue circle beneath, which renders the unified tooltip.
396
+ silent: true,
397
+ data,
398
+ connectNulls: false,
399
+ showSymbol: false,
400
+ itemStyle: {
401
+ color: withAlpha(color, regionOpacity)
402
+ },
403
+ lineStyle: {
404
+ color: withAlpha(color, regionOpacity),
405
+ width: markerSize,
406
+ cap: "round",
407
+ join: "round"
408
+ }
409
+ });
410
+ regionLegendEntries.push({
411
+ name,
412
+ icon: REGION_LEGEND_ICON
413
+ });
414
+ }
415
+ for (const [index, [name, entry]] of Object.entries(model.data.annotations.site).entries()) {
416
+ const color = paletteColorAt(theme.sitePalette, index);
417
+ series.push({
418
+ type: "scatter",
419
+ name,
420
+ z: 20,
421
+ // See region note: silent so the residue circle owns the tooltip.
422
+ silent: true,
423
+ data: entry.index.flatMap((position) => {
424
+ const residue = model.residues[position - 1];
425
+ if (residue === void 0) return [];
426
+ addAnnotationHit(position, { family: "Sites", name, color });
427
+ return [[residue.x, residue.y]];
428
+ }),
429
+ symbol: "circle",
430
+ symbolSize: markerSize,
431
+ itemStyle: {
432
+ color: "rgba(0, 0, 0, 0)",
433
+ borderColor: color,
434
+ borderWidth: 1.5
435
+ }
436
+ });
437
+ siteLegendEntries.push({
438
+ name,
439
+ icon: "circle",
440
+ itemStyle: {
441
+ color: "rgba(0,0,0,0)",
442
+ borderColor: color,
443
+ borderWidth: 2
444
+ }
445
+ });
446
+ }
447
+ for (const [index, [name, entry]] of Object.entries(model.data.annotations.ptm).entries()) {
448
+ const color = paletteColorAt(theme.ptmPalette, index);
449
+ const positions = extractPositionsFromFlexEntry(entry);
450
+ const ptmSymbolSize = markerSize / 4.5;
451
+ const offset = resolvePtmPixelOffset(
452
+ index,
453
+ Math.max(Object.keys(model.data.annotations.ptm).length, 1),
454
+ ptmPlacement,
455
+ markerSize,
456
+ ptmSymbolSize
457
+ );
458
+ series.push({
459
+ type: "scatter",
460
+ name,
461
+ z: 30,
462
+ silent: true,
463
+ data: positions.flatMap((position) => {
464
+ const residue = model.residues[position - 1];
465
+ if (residue === void 0) return [];
466
+ addAnnotationHit(position, { family: "PTMs", name, color });
467
+ return [[residue.x, residue.y]];
468
+ }),
469
+ symbol: "circle",
470
+ symbolSize: ptmSymbolSize,
471
+ symbolOffset: [offset.x, offset.y],
472
+ itemStyle: {
473
+ color,
474
+ opacity: 1
475
+ }
476
+ });
477
+ ptmLegendEntries.push({ name });
478
+ }
479
+ for (const [index, [name, entry]] of Object.entries(
480
+ model.data.annotations.processing
481
+ ).entries()) {
482
+ const color = paletteColorAt(theme.processingPalette, index);
483
+ const positions = extractPositionsFromFlexEntry(entry);
484
+ const offset = circularPixelOffset(
485
+ index + 1,
486
+ Math.max(Object.keys(model.data.annotations.processing).length, 1),
487
+ annotationGap
488
+ );
489
+ series.push({
490
+ type: "scatter",
491
+ name,
492
+ z: 31,
493
+ silent: true,
494
+ data: positions.flatMap((position) => {
495
+ const residue = model.residues[position - 1];
496
+ if (residue === void 0) return [];
497
+ addAnnotationHit(position, { family: "Processing", name, color });
498
+ return [[residue.x, residue.y]];
499
+ }),
500
+ symbol: "triangle",
501
+ symbolRotate: 180,
502
+ symbolSize: markerSize / 4,
503
+ symbolOffset: [offset.x, offset.y],
504
+ itemStyle: {
505
+ color
506
+ }
507
+ });
508
+ processingLegendEntries.push({ name });
509
+ }
510
+ const variantSourcesByPosition = /* @__PURE__ */ new Map();
511
+ for (const variant of model.data.annotations.variant) {
512
+ const labels = variantSourcesByPosition.get(variant.position) ?? /* @__PURE__ */ new Map();
513
+ variantSourcesByPosition.set(variant.position, labels);
514
+ const label = describeVariant(variant);
515
+ const sources = labels.get(label) ?? /* @__PURE__ */ new Set();
516
+ labels.set(label, sources);
517
+ for (const source of variantSources(variant)) sources.add(source);
518
+ }
519
+ for (const [position, labels] of variantSourcesByPosition) {
520
+ for (const [label, sources] of labels) {
521
+ addAnnotationHit(position, {
522
+ family: "Variants",
523
+ name: label,
524
+ color: theme.variantColor,
525
+ ...sources.size > 0 ? { detail: [...sources].join(", ") } : {}
526
+ });
527
+ }
528
+ }
529
+ if (regionLegendEntries.length > 0) {
530
+ series.push(buildLegendHeadingSeries(LEGEND_HEADING_REGIONS));
531
+ }
532
+ if (ptmLegendEntries.length > 0) {
533
+ series.push(buildLegendHeadingSeries(LEGEND_HEADING_PTMS));
534
+ }
535
+ if (processingLegendEntries.length > 0) {
536
+ series.push(buildLegendHeadingSeries(LEGEND_HEADING_PROCESSING));
537
+ }
538
+ if (showLabels) {
539
+ series.push({
540
+ type: "scatter",
541
+ name: "Residue labels",
542
+ z: 40,
543
+ data: model.residues.map((residue, index) => ({
544
+ value: [residue.x, residue.y],
545
+ label: {
546
+ show: true,
547
+ formatter: residue.residue,
548
+ color: labelColors[index],
549
+ fontFamily: theme.annotationFontFamily,
550
+ fontSize
551
+ }
552
+ })),
553
+ silent: true,
554
+ symbolSize: 0,
555
+ tooltip: {
556
+ show: false
557
+ }
558
+ });
559
+ }
560
+ const positionEvery = options.positionEvery === void 0 ? 10 : options.positionEvery;
561
+ if (positionEvery !== null && model.data.sequence.length > positionEvery) {
562
+ series.push({
563
+ type: "scatter",
564
+ name: "Position labels",
565
+ z: 41,
566
+ data: model.residues.filter((residue) => residue.position % positionEvery === 0).map((residue) => ({
567
+ value: [residue.x, residue.y],
568
+ label: {
569
+ show: true,
570
+ position: "top",
571
+ distance: positionLabelGap,
572
+ formatter: String(residue.position),
573
+ color: theme.positionLabelColor,
574
+ fontFamily: theme.annotationFontFamily,
575
+ fontSize: Math.max(10, fontSize - 6)
576
+ }
577
+ })),
578
+ symbolSize: 0,
579
+ tooltip: {
580
+ show: false
581
+ }
582
+ });
583
+ }
584
+ return Object.freeze({
585
+ backgroundColor: theme.backgroundColor,
586
+ animation: enableAnimation,
587
+ animationDuration,
588
+ animationDurationUpdate,
589
+ ...options.title ? {
590
+ title: {
591
+ text: options.title,
592
+ left: "5.5%",
593
+ top: titleTop,
594
+ textStyle: {
595
+ fontFamily: theme.legendFontFamily,
596
+ fontSize,
597
+ color: theme.legendColor
598
+ }
599
+ }
600
+ } : {},
601
+ grid: {
602
+ left: 24,
603
+ right: legendRightInset + legendRailWidth + legendGap,
604
+ top: titleMarginTop,
605
+ bottom: 24,
606
+ containLabel: false
607
+ },
608
+ xAxis: {
609
+ type: "value",
610
+ min: model.bounds.minX - X_AXIS_PADDING,
611
+ max: model.bounds.maxX + X_AXIS_PADDING,
612
+ show: false
613
+ },
614
+ yAxis: {
615
+ type: "value",
616
+ min: model.bounds.minY - Y_AXIS_PADDING_TOP,
617
+ max: model.bounds.maxY + Y_AXIS_PADDING_BOTTOM,
618
+ inverse: true,
619
+ show: false,
620
+ scale: true
621
+ },
622
+ legend: {
623
+ type: "plain",
624
+ orient: "vertical",
625
+ // Align the first legend item with the first sequence row. The y-axis
626
+ // spans [minY - paddingTop, maxY + paddingBottom] over a grid of height
627
+ // markerSize*(2*verticalSpan+1) px, so row 1's center sits
628
+ // paddingTop / (verticalSpan + paddingTop + paddingBottom) down.
629
+ top: (() => {
630
+ const vSpan = model.bounds.maxY - model.bounds.minY;
631
+ const gridH = markerSize * (2 * vSpan + 1);
632
+ const yRange = vSpan + Y_AXIS_PADDING_TOP + Y_AXIS_PADDING_BOTTOM;
633
+ return titleMarginTop + Math.round(gridH * Y_AXIS_PADDING_TOP / yRange);
634
+ })(),
635
+ right: legendRightInset,
636
+ bottom: 24,
637
+ width: legendRailWidth,
638
+ data: buildLegendData(
639
+ showMarkers,
640
+ siteLegendEntries,
641
+ regionLegendEntries,
642
+ ptmLegendEntries,
643
+ processingLegendEntries
644
+ ),
645
+ formatter: (name) => {
646
+ if (name === LEGEND_HEADING_REGIONS) {
647
+ return "{heading|Regions}";
648
+ }
649
+ if (name === LEGEND_HEADING_PTMS) {
650
+ return "{heading|PTMs}";
651
+ }
652
+ if (name === LEGEND_HEADING_PROCESSING) {
653
+ return "{heading|Processing}";
654
+ }
655
+ return name;
656
+ },
657
+ textStyle: {
658
+ color: theme.legendColor,
659
+ fontFamily: theme.legendFontFamily,
660
+ fontSize,
661
+ rich: {
662
+ heading: {
663
+ color: theme.legendColor,
664
+ fontFamily: theme.legendFontFamily,
665
+ fontSize: Math.max(12, fontSize - 1),
666
+ fontWeight: 600,
667
+ padding: [12, 0, 2, 0]
668
+ }
669
+ }
670
+ }
671
+ },
672
+ tooltip: {
673
+ trigger: "item",
674
+ confine: true
675
+ },
676
+ series: Object.freeze(series)
677
+ });
678
+ }
679
+ function extractPositionsFromFlexEntry(entry) {
680
+ const index = entry.index;
681
+ if (index.length === 0) return [];
682
+ if (Array.isArray(index[0])) {
683
+ const ranges = index;
684
+ const positions = [];
685
+ for (const [start, end] of ranges) {
686
+ for (let pos = start; pos <= end; pos++) {
687
+ positions.push(pos);
688
+ }
689
+ }
690
+ return positions;
691
+ }
692
+ return index;
693
+ }
694
+ function buildResidueLabelColors(model, options, theme) {
695
+ const diseaseVariants = new Set(options.diseaseVariants ?? []);
696
+ for (const [key, entry] of Object.entries(model.data.annotations.site)) {
697
+ if (key.toLowerCase().replace(/[-\s]/g, "_") === "disease_associated_variant") {
698
+ for (const position of entry.index) {
699
+ diseaseVariants.add(position);
700
+ }
701
+ }
702
+ }
703
+ const variantPositions = new Set(
704
+ model.data.annotations.variant.map((variant) => variant.position)
705
+ );
706
+ return model.residues.map((residue) => {
707
+ if (diseaseVariants.has(residue.position)) {
708
+ return theme.diseaseVariantColor;
709
+ }
710
+ if (variantPositions.has(residue.position)) {
711
+ return theme.variantColor;
712
+ }
713
+ return theme.labelColor;
714
+ });
715
+ }
716
+ function circularPixelOffset(index, count, gap) {
717
+ const angle = 2 * Math.PI * index / count;
718
+ return Object.freeze({
719
+ x: Math.sin(angle) * gap,
720
+ y: Math.cos(angle) * gap
721
+ });
722
+ }
723
+ function resolvePtmPixelOffset(index, count, placement, markerSize, ptmSymbolSize) {
724
+ const residueRadius = markerSize / 2;
725
+ const ptmRadius = ptmSymbolSize / 2;
726
+ const radialDistance = placement === "innerRadial" ? Math.max(0, residueRadius - ptmRadius) : placement === "radial" ? residueRadius : residueRadius + ptmRadius;
727
+ return circularPixelOffset(index + 1, count, radialDistance);
728
+ }
729
+ function clampOpacity(value, fallback = 0.35) {
730
+ if (value === void 0 || Number.isNaN(value)) {
731
+ return fallback;
732
+ }
733
+ return Math.min(1, Math.max(0, value));
734
+ }
735
+ function buildLegendData(includePrimaryStructure, siteLegendEntries, regionLegendEntries, ptmLegendEntries, processingLegendEntries) {
736
+ const entries = [];
737
+ if (includePrimaryStructure) {
738
+ entries.push("Primary structure");
739
+ }
740
+ entries.push(...siteLegendEntries);
741
+ if (regionLegendEntries.length > 0) {
742
+ entries.push({ name: LEGEND_HEADING_REGIONS, icon: "none" });
743
+ entries.push(...regionLegendEntries);
744
+ }
745
+ if (ptmLegendEntries.length > 0) {
746
+ entries.push({ name: LEGEND_HEADING_PTMS, icon: "none" });
747
+ entries.push(...ptmLegendEntries);
748
+ }
749
+ if (processingLegendEntries.length > 0) {
750
+ entries.push({ name: LEGEND_HEADING_PROCESSING, icon: "none" });
751
+ entries.push(...processingLegendEntries);
752
+ }
753
+ return entries;
754
+ }
755
+ function escapeHtml(value) {
756
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
757
+ }
758
+ function describeVariant(variant) {
759
+ const wild = variant.from ?? variant.wild_type ?? variant.wildType ?? variant.ref;
760
+ const mutant = variant.to ?? variant.mutant ?? variant.alt ?? variant.variant;
761
+ if (wild !== void 0 && variant.type === "deletion") {
762
+ return `${String(wild)}${variant.position}del`;
763
+ }
764
+ if (wild !== void 0 && mutant !== void 0) {
765
+ return `${String(wild)}${variant.position}${String(mutant)}`;
766
+ }
767
+ return `Variant @ ${variant.position}`;
768
+ }
769
+ function variantSources(variant) {
770
+ const { source } = variant;
771
+ if (typeof source === "string") return [source];
772
+ if (Array.isArray(source)) {
773
+ return source.filter((s) => typeof s === "string");
774
+ }
775
+ return [];
776
+ }
777
+ function formatResidueTooltip(position, residueName, annotationsByPosition, theme) {
778
+ const name = escapeHtml(residueName ?? "");
779
+ const header = position === void 0 ? name : `<span style="opacity:0.6;font-weight:400">${position}</span> ${name}`;
780
+ const headerHtml = `<div style="font-weight:600">${header}</div>`;
781
+ const wrap = (inner) => `<div style="color:${theme.legendColor};font-family:${theme.legendFontFamily}">${inner}</div>`;
782
+ const hits = position === void 0 ? void 0 : annotationsByPosition.get(position);
783
+ if (!hits || hits.length === 0) {
784
+ return wrap(headerHtml);
785
+ }
786
+ const rows = [];
787
+ for (const family of ANNOTATION_FAMILY_ORDER) {
788
+ const familyHits = hits.filter((hit) => hit.family === family);
789
+ if (familyHits.length === 0) {
790
+ continue;
791
+ }
792
+ rows.push(
793
+ `<div style="margin-top:4px;font-size:11px;opacity:0.6;text-transform:uppercase;letter-spacing:0.04em">${family}</div>`
794
+ );
795
+ for (const hit of familyHits) {
796
+ rows.push(
797
+ `<div style="display:flex;align-items:center;gap:6px"><span style="display:inline-block;width:8px;height:8px;border-radius:50%;background:${hit.color}"></span><span>${escapeHtml(hit.name)}</span>` + (hit.detail ? `<span style="opacity:0.6">${escapeHtml(hit.detail)}</span>` : "") + `</div>`
798
+ );
799
+ }
800
+ }
801
+ return wrap(headerHtml + rows.join(""));
802
+ }
803
+ function buildLegendHeadingSeries(name) {
804
+ return {
805
+ type: "scatter",
806
+ name,
807
+ data: [],
808
+ symbolSize: 0,
809
+ silent: true,
810
+ tooltip: {
811
+ show: false
812
+ }
813
+ };
814
+ }
815
+
816
+ // src/charts/a3/config.ts
817
+ var A3_CHART_CONFIG_MEMBERS = Object.freeze([
818
+ "type",
819
+ "n_per_row",
820
+ "position_every",
821
+ "ptm_placement",
822
+ "zoom",
823
+ "residue_spacing",
824
+ "marker_size",
825
+ "font_size",
826
+ "line_width",
827
+ "show_markers",
828
+ "show_labels",
829
+ "annotation_gap",
830
+ "position_label_gap",
831
+ "region_opacity",
832
+ "residue_fill",
833
+ "residue_stroke",
834
+ "label_color",
835
+ "pos_label_color",
836
+ "variant_color",
837
+ "disease_variant_color",
838
+ "title"
839
+ ]);
840
+ function buildA3Option(document, config = {}, appearance = {}) {
841
+ const dark = appearance.dark ?? false;
842
+ const base = dark ? DARK_A3_VISUALIZATION_THEME : {};
843
+ const theme = {
844
+ ...base,
845
+ ...appearance.fontFamily ? {
846
+ annotationFontFamily: appearance.fontFamily,
847
+ legendFontFamily: appearance.fontFamily
848
+ } : {},
849
+ ...colorOverride("residueStrokeColor", config.residue_stroke),
850
+ ...colorOverride("labelColor", config.label_color),
851
+ ...colorOverride("positionLabelColor", config.pos_label_color),
852
+ ...colorOverride("variantColor", config.variant_color),
853
+ ...colorOverride("diseaseVariantColor", config.disease_variant_color)
854
+ };
855
+ const options = {
856
+ nPerRow: config.n_per_row ?? 21,
857
+ residueSpacing: config.residue_spacing ?? 0.3,
858
+ showMarkers: config.show_markers ?? true,
859
+ showLabels: config.show_labels ?? true,
860
+ positionEvery: config.position_every === void 0 ? 10 : config.position_every,
861
+ markerSize: config.marker_size ?? 28,
862
+ fontSize: config.font_size ?? 18,
863
+ lineWidth: config.line_width ?? 2,
864
+ annotationGap: config.annotation_gap ?? 16,
865
+ positionLabelGap: config.position_label_gap ?? 16,
866
+ markerColor: config.residue_fill ?? (dark ? "#181818" : "#efefef"),
867
+ regionOpacity: config.region_opacity ?? 0.3,
868
+ ptmPlacement: config.ptm_placement ?? "radial",
869
+ legendGap: appearance.legendGap ?? 90,
870
+ enableAnimation: appearance.animation ?? true,
871
+ theme,
872
+ ...config.title ? { title: config.title } : {},
873
+ ...appearance.titleTop === void 0 ? {} : { titleTop: appearance.titleTop }
874
+ };
875
+ const option = createA3EChartsOption(document, options);
876
+ return config.zoom === false ? option : Object.freeze({ ...option, dataZoom: A3_ZOOM });
877
+ }
878
+ var A3_ZOOM = Object.freeze([
879
+ {
880
+ type: "inside",
881
+ xAxisIndex: 0,
882
+ zoomOnMouseWheel: "shift",
883
+ moveOnMouseMove: true,
884
+ moveOnMouseWheel: true,
885
+ preventDefaultMouseMove: false
886
+ },
887
+ {
888
+ type: "inside",
889
+ yAxisIndex: 0,
890
+ zoomOnMouseWheel: false,
891
+ moveOnMouseWheel: false,
892
+ moveOnMouseMove: false
893
+ }
894
+ ]);
895
+ function colorOverride(key, value) {
896
+ return value ? { [key]: value } : {};
897
+ }
898
+
899
+ // src/index.ts
900
+ var builders = Object.freeze({
901
+ a3: buildA3Option
902
+ });
903
+ return __toCommonJS(index_exports);
904
+ })();
905
+ if (typeof module === 'object' && module.exports) module.exports = RtemisDraw;