@rtemis/draw 0.0.0-stage → 0.1.1

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