react-msaview 8.2.0 → 8.3.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 (192) hide show
  1. package/bundle/index.js +104 -117
  2. package/bundle/index.js.map +4 -4
  3. package/dist/columnCounts.d.ts +4 -3
  4. package/dist/columnCounts.js +11 -6
  5. package/dist/columnCounts.js.map +1 -1
  6. package/dist/components/CopyButton.d.ts +1 -1
  7. package/dist/components/CopyButton.js +1 -1
  8. package/dist/components/CopyButton.js.map +1 -1
  9. package/dist/components/DragHandle.js +14 -2
  10. package/dist/components/DragHandle.js.map +1 -1
  11. package/dist/components/Loading.js +10 -11
  12. package/dist/components/Loading.js.map +1 -1
  13. package/dist/components/MSAViewer.d.ts +12 -1
  14. package/dist/components/MSAViewer.js +12 -1
  15. package/dist/components/MSAViewer.js.map +1 -1
  16. package/dist/components/SequenceTextArea.d.ts +11 -0
  17. package/dist/components/SequenceTextArea.js +40 -8
  18. package/dist/components/SequenceTextArea.js.map +1 -1
  19. package/dist/components/StatusMessage.d.ts +12 -0
  20. package/dist/components/StatusMessage.js +43 -0
  21. package/dist/components/StatusMessage.js.map +1 -0
  22. package/dist/components/Track.js +3 -3
  23. package/dist/components/Track.js.map +1 -1
  24. package/dist/components/VerticalScrollbar.js +6 -1
  25. package/dist/components/VerticalScrollbar.js.map +1 -1
  26. package/dist/components/dialogs/ExportSVGDialog.d.ts +3 -2
  27. package/dist/components/dialogs/ExportSVGDialog.js +4 -2
  28. package/dist/components/dialogs/ExportSVGDialog.js.map +1 -1
  29. package/dist/components/dialogs/FeatureDialog.js +15 -10
  30. package/dist/components/dialogs/FeatureDialog.js.map +1 -1
  31. package/dist/components/getVisibleLeaves.d.ts +1 -1
  32. package/dist/components/header/FileMenu.js +9 -0
  33. package/dist/components/header/FileMenu.js.map +1 -1
  34. package/dist/components/header/GappynessSlider.js +15 -3
  35. package/dist/components/header/GappynessSlider.js.map +1 -1
  36. package/dist/components/header/Header.js +6 -7
  37. package/dist/components/header/Header.js.map +1 -1
  38. package/dist/components/header/{HeaderStatusArea.d.ts → RowSearch.d.ts} +2 -2
  39. package/dist/components/header/RowSearch.js +15 -0
  40. package/dist/components/header/RowSearch.js.map +1 -0
  41. package/dist/components/header/revealRow.d.ts +3 -0
  42. package/dist/components/header/revealRow.js +15 -0
  43. package/dist/components/header/revealRow.js.map +1 -0
  44. package/dist/components/header/settingsMenuItems.js +57 -1
  45. package/dist/components/header/settingsMenuItems.js.map +1 -1
  46. package/dist/components/import/ImportForm.js +10 -4
  47. package/dist/components/import/ImportForm.js.map +1 -1
  48. package/dist/components/import/ImportFormExamples.js +19 -27
  49. package/dist/components/import/ImportFormExamples.js.map +1 -1
  50. package/dist/components/minimap/Minimap.js +6 -1
  51. package/dist/components/minimap/Minimap.js.map +1 -1
  52. package/dist/components/msa/AnnotationLegend.js +11 -5
  53. package/dist/components/msa/AnnotationLegend.js.map +1 -1
  54. package/dist/components/msa/MSACanvas.js +6 -1
  55. package/dist/components/msa/MSACanvas.js.map +1 -1
  56. package/dist/components/msa/MSACanvasBlock.js +3 -7
  57. package/dist/components/msa/MSACanvasBlock.js.map +1 -1
  58. package/dist/components/msa/msaKeys.d.ts +5 -0
  59. package/dist/components/msa/msaKeys.js +35 -0
  60. package/dist/components/msa/msaKeys.js.map +1 -0
  61. package/dist/components/msa/useMsaBlockMouse.d.ts +2 -4
  62. package/dist/components/msa/useMsaBlockMouse.js +18 -5
  63. package/dist/components/msa/useMsaBlockMouse.js.map +1 -1
  64. package/dist/components/rowpanels/RowPanelBlock.js +10 -14
  65. package/dist/components/rowpanels/RowPanelBlock.js.map +1 -1
  66. package/dist/components/tracks/drawTracks.d.ts +1 -0
  67. package/dist/components/tracks/drawTracks.js +14 -2
  68. package/dist/components/tracks/drawTracks.js.map +1 -1
  69. package/dist/components/tracks/useTrackHover.d.ts +1 -5
  70. package/dist/components/tracks/useTrackHover.js +5 -5
  71. package/dist/components/tracks/useTrackHover.js.map +1 -1
  72. package/dist/components/tree/ScaleBarSVG.d.ts +14 -0
  73. package/dist/components/tree/ScaleBarSVG.js +11 -0
  74. package/dist/components/tree/ScaleBarSVG.js.map +1 -0
  75. package/dist/components/tree/TreeCanvas.js +8 -26
  76. package/dist/components/tree/TreeCanvas.js.map +1 -1
  77. package/dist/components/tree/TreeCanvasBlock.js +11 -20
  78. package/dist/components/tree/TreeCanvasBlock.js.map +1 -1
  79. package/dist/components/tree/{TreeBranchMenu.d.ts → TreeMenu.d.ts} +5 -4
  80. package/dist/components/tree/TreeMenu.js +48 -0
  81. package/dist/components/tree/TreeMenu.js.map +1 -0
  82. package/dist/components/tree/TreeRuler.js +3 -3
  83. package/dist/components/tree/TreeRuler.js.map +1 -1
  84. package/dist/components/tree/renderTreeCanvas.js +19 -9
  85. package/dist/components/tree/renderTreeCanvas.js.map +1 -1
  86. package/dist/components/tree/renderTreeMouseover.d.ts +5 -0
  87. package/dist/components/tree/renderTreeMouseover.js +20 -0
  88. package/dist/components/tree/renderTreeMouseover.js.map +1 -0
  89. package/dist/components/tree/useTreeHover.d.ts +3 -8
  90. package/dist/components/tree/useTreeHover.js +8 -10
  91. package/dist/components/tree/useTreeHover.js.map +1 -1
  92. package/dist/components/useHoverAnchor.d.ts +19 -0
  93. package/dist/components/useHoverAnchor.js +18 -0
  94. package/dist/components/useHoverAnchor.js.map +1 -0
  95. package/dist/constants.d.ts +4 -1
  96. package/dist/constants.js +12 -3
  97. package/dist/constants.js.map +1 -1
  98. package/dist/expandSpec.js +3 -3
  99. package/dist/expandSpec.js.map +1 -1
  100. package/dist/exportFileName.d.ts +3 -3
  101. package/dist/exportFileName.js +4 -4
  102. package/dist/exportFileName.js.map +1 -1
  103. package/dist/hierarchy.d.ts +51 -0
  104. package/dist/hierarchy.js +125 -0
  105. package/dist/hierarchy.js.map +1 -1
  106. package/dist/index.d.ts +2 -2
  107. package/dist/model/treeModel.d.ts +12 -0
  108. package/dist/model/treeModel.js +14 -1
  109. package/dist/model/treeModel.js.map +1 -1
  110. package/dist/model.d.ts +168 -47
  111. package/dist/model.js +435 -192
  112. package/dist/model.js.map +1 -1
  113. package/dist/renderToSvg.js +2 -4
  114. package/dist/renderToSvg.js.map +1 -1
  115. package/dist/rerootTree.d.ts +33 -0
  116. package/dist/rerootTree.js +144 -0
  117. package/dist/rerootTree.js.map +1 -0
  118. package/dist/svgTestUtil.d.ts +106 -20
  119. package/dist/types.d.ts +25 -12
  120. package/dist/useMsaSvgFigure.d.ts +2 -1
  121. package/dist/useMsaSvgFigure.js +11 -4
  122. package/dist/useMsaSvgFigure.js.map +1 -1
  123. package/dist/version.d.ts +1 -1
  124. package/dist/version.js +1 -1
  125. package/dist/writeNewick.d.ts +8 -0
  126. package/dist/writeNewick.js +34 -0
  127. package/dist/writeNewick.js.map +1 -0
  128. package/package.json +3 -3
  129. package/src/columnCounts.ts +14 -6
  130. package/src/components/CopyButton.tsx +2 -2
  131. package/src/components/DragHandle.tsx +21 -1
  132. package/src/components/Loading.tsx +14 -30
  133. package/src/components/MSAViewer.tsx +26 -0
  134. package/src/components/SequenceTextArea.tsx +54 -8
  135. package/src/components/StatusMessage.tsx +70 -0
  136. package/src/components/Track.tsx +8 -4
  137. package/src/components/VerticalScrollbar.tsx +7 -0
  138. package/src/components/dialogs/ExportSVGDialog.tsx +5 -2
  139. package/src/components/dialogs/FeatureDialog.tsx +59 -38
  140. package/src/components/header/FileMenu.tsx +9 -0
  141. package/src/components/header/GappynessSlider.tsx +17 -4
  142. package/src/components/header/Header.tsx +7 -8
  143. package/src/components/header/RowSearch.tsx +34 -0
  144. package/src/components/header/revealRow.ts +17 -0
  145. package/src/components/header/settingsMenuItems.ts +66 -0
  146. package/src/components/import/ImportForm.tsx +14 -7
  147. package/src/components/import/ImportFormExamples.tsx +20 -28
  148. package/src/components/minimap/Minimap.tsx +7 -0
  149. package/src/components/msa/AnnotationLegend.tsx +12 -6
  150. package/src/components/msa/MSACanvas.tsx +8 -0
  151. package/src/components/msa/MSACanvasBlock.tsx +5 -11
  152. package/src/components/msa/msaKeys.ts +39 -0
  153. package/src/components/msa/useMsaBlockMouse.ts +23 -7
  154. package/src/components/rowpanels/RowPanelBlock.tsx +16 -23
  155. package/src/components/tracks/drawTracks.ts +15 -2
  156. package/src/components/tracks/useTrackHover.ts +5 -5
  157. package/src/components/tree/ScaleBarSVG.tsx +30 -0
  158. package/src/components/tree/TreeCanvas.tsx +9 -40
  159. package/src/components/tree/TreeCanvasBlock.tsx +19 -39
  160. package/src/components/tree/TreeMenu.tsx +129 -0
  161. package/src/components/tree/TreeRuler.tsx +7 -13
  162. package/src/components/tree/renderTreeCanvas.ts +20 -8
  163. package/src/components/tree/renderTreeMouseover.ts +41 -0
  164. package/src/components/tree/useTreeHover.ts +8 -17
  165. package/src/components/useHoverAnchor.ts +31 -0
  166. package/src/constants.ts +14 -3
  167. package/src/expandSpec.ts +3 -3
  168. package/src/exportFileName.ts +4 -3
  169. package/src/hierarchy.ts +151 -4
  170. package/src/index.ts +3 -1
  171. package/src/model/treeModel.ts +22 -0
  172. package/src/model.ts +534 -223
  173. package/src/renderToSvg.tsx +6 -9
  174. package/src/rerootTree.ts +172 -0
  175. package/src/types.ts +34 -12
  176. package/src/useMsaSvgFigure.ts +11 -4
  177. package/src/version.ts +1 -1
  178. package/src/writeNewick.ts +38 -0
  179. package/dist/components/PortalTooltip.d.ts +0 -11
  180. package/dist/components/PortalTooltip.js +0 -29
  181. package/dist/components/PortalTooltip.js.map +0 -1
  182. package/dist/components/header/HeaderStatusArea.js +0 -21
  183. package/dist/components/header/HeaderStatusArea.js.map +0 -1
  184. package/dist/components/tree/TreeBranchMenu.js +0 -29
  185. package/dist/components/tree/TreeBranchMenu.js.map +0 -1
  186. package/dist/components/tree/TreeNodeMenu.d.ts +0 -13
  187. package/dist/components/tree/TreeNodeMenu.js +0 -41
  188. package/dist/components/tree/TreeNodeMenu.js.map +0 -1
  189. package/src/components/PortalTooltip.tsx +0 -47
  190. package/src/components/header/HeaderStatusArea.tsx +0 -32
  191. package/src/components/tree/TreeBranchMenu.tsx +0 -68
  192. package/src/components/tree/TreeNodeMenu.tsx +0 -95
package/src/model.ts CHANGED
@@ -9,7 +9,13 @@ import {
9
9
  isAlive,
10
10
  types,
11
11
  } from '@jbrowse/mobx-state-tree'
12
- import { autorun, computed, transaction } from 'mobx'
12
+ import {
13
+ autorun,
14
+ compareStructural,
15
+ computed,
16
+ reaction,
17
+ transaction,
18
+ } from 'mobx'
13
19
  import {
14
20
  generateNodeIds,
15
21
  gffToAnnotations,
@@ -74,10 +80,10 @@ import {
74
80
  calcDepthToLeaf,
75
81
  clusterLayout,
76
82
  collapse,
77
- collapsedSubtreeMaxLength,
78
83
  find,
79
84
  forEachDescendant,
80
85
  hierarchy,
86
+ layoutTree,
81
87
  leafIndex,
82
88
  leaves,
83
89
  maxLength,
@@ -95,6 +101,7 @@ import { TreeModelF } from './model/treeModel.ts'
95
101
  import { calculateNeighborJoiningTree } from './neighborJoining.ts'
96
102
  import { parseAsn1 } from './parseAsn1.ts'
97
103
  import { calculatePropertyConservation } from './propertyConservation.ts'
104
+ import { midpointRoot, rerootTree } from './rerootTree.ts'
98
105
  import {
99
106
  globalColToVisibleCol,
100
107
  seqPosOfGlobalCol,
@@ -114,12 +121,14 @@ import {
114
121
  withAlpha,
115
122
  } from './util.ts'
116
123
  import { saveAs } from './vendor/fileSaver.ts'
124
+ import { writeNewick } from './writeNewick.ts'
117
125
  import { parseWuss } from './wuss.ts'
118
126
 
119
127
  import type { ColumnStats } from './columnStats.ts'
120
128
  import type { ScrollZoomAxis } from './constants.ts'
121
129
  import type { HierarchyNode } from './hierarchy.ts'
122
130
  import type { ExportSvgOptions } from './renderToSvg.tsx'
131
+ import type { TreeNode } from './rerootTree.ts'
123
132
  import type { ScaleSpec } from './scales.ts'
124
133
  import type {
125
134
  Annotation,
@@ -127,6 +136,7 @@ import type {
127
136
  BasicTrack,
128
137
  Cell,
129
138
  Clade,
139
+ CladeMark,
130
140
  ResidueMappingProblem,
131
141
  ColumnTrackSpec,
132
142
  DomainBand,
@@ -136,14 +146,17 @@ import type {
136
146
  Legend,
137
147
  LegendEntry,
138
148
  NodeWithIds,
139
- NodeWithIdsAndLength,
140
149
  Region,
141
150
  ResidueMapping,
142
151
  ResidueSegment,
143
152
  ResolvedClade,
144
153
  ResolvedEncoding,
154
+ ResolvedFeaturePanel,
145
155
  ResolvedHighlight,
156
+ TreeOrder,
157
+ TreeRoot,
146
158
  ResolvedRowPanel,
159
+ ResolvedStripPanel,
147
160
  RowFeaturesSpec,
148
161
  RowPanelSpan,
149
162
  RowPanelSpec,
@@ -202,14 +215,39 @@ const UNSCALED_FEATURE = '#d9d9d9'
202
215
  const laneOverlap = 0.1
203
216
 
204
217
  // a row panel's own width, or a default: the row height, which makes a strip
205
- // cell square, and a gene neighborhood's width for a features panel
206
- function rowPanelWidth(panel: RowPanelSpec, rowHeight: number) {
218
+ // cell square, and a gene neighborhood's width for a features panel. The row
219
+ // height is read only when used, so a panel with a width of its own does not
220
+ // recompute on a vertical zoom
221
+ function rowPanelWidth(panel: RowPanelSpec, view: { rowHeight: number }) {
207
222
  return (
208
223
  panel.width ??
209
- (panel.kind === 'features' ? defaultFeaturePanelWidth : rowHeight)
224
+ (panel.kind === 'features' ? defaultFeaturePanelWidth : view.rowHeight)
210
225
  )
211
226
  }
212
227
 
228
+ // the color each row takes from a field of the row table under a scale
229
+ function colorByRow(
230
+ rowData: Record<string, Record<string, string> | undefined>,
231
+ field: string,
232
+ colorOf: (value: string) => string | undefined,
233
+ ) {
234
+ const colors = new Map<string, string>()
235
+ for (const [name, row] of Object.entries(rowData)) {
236
+ const value = row?.[field]
237
+ const color = value === undefined ? undefined : colorOf(value)
238
+ if (color) {
239
+ colors.set(name, color)
240
+ }
241
+ }
242
+ return colors
243
+ }
244
+
245
+ type StripPanelScale = Omit<ResolvedStripPanel, 'id' | 'width' | 'offsetX'>
246
+ type FeaturePanelScale = Omit<
247
+ ResolvedFeaturePanel,
248
+ 'id' | 'width' | 'offsetX' | 'spans'
249
+ >
250
+
213
251
  // the fill and outline of every feature in a list: its own GFF `color=` first,
214
252
  // then the scale an encoding resolves over one of its fields, then the
215
253
  // accession palette
@@ -402,10 +440,21 @@ function resolveFeatureScale(
402
440
  }
403
441
  }
404
442
 
443
+ const columnTrackBytes = new WeakMap<ColumnTrackSpec, number>()
444
+
445
+ function columnTrackSize(track: ColumnTrackSpec) {
446
+ let bytes = columnTrackBytes.get(track)
447
+ if (bytes === undefined) {
448
+ bytes = JSON.stringify(track).length
449
+ columnTrackBytes.set(track, bytes)
450
+ }
451
+ return bytes
452
+ }
453
+
405
454
  // a data track over this size stays in the live model but leaves the snapshot,
406
455
  // the same rule DataModel applies to an inline document
407
456
  function columnTrackSizes(tracks?: readonly ColumnTrackSpec[]) {
408
- return (tracks ?? []).map(t => JSON.stringify(t).length)
457
+ return (tracks ?? []).map(columnTrackSize)
409
458
  }
410
459
 
411
460
  function smallColumnTracks(tracks?: ColumnTrackSpec[]) {
@@ -441,6 +490,7 @@ export const preservedOnReset = new Set([
441
490
  'treeAreaWidth',
442
491
  'treeWidth',
443
492
  'showBranchLen',
493
+ 'treeOrder',
444
494
  'drawTree',
445
495
  'drawNodeBubbles',
446
496
  'drawNodeLabels',
@@ -467,6 +517,9 @@ function trackIsOff(
467
517
  return turnedOffTracks.get(id) ?? (defaultOff || defaultOffTracks.has(id))
468
518
  }
469
519
 
520
+ // the marks that set tree state from a node rather than drawing
521
+ const seedingMarks = new Set<CladeMark>(['collapse', 'focus', 'rotate'])
522
+
470
523
  // shared empty results, so observers don't see a fresh [] as a change
471
524
  const noDomains: Annotation[] = []
472
525
  const noClades: ResolvedClade[] = []
@@ -519,8 +572,7 @@ function cladeRows(
519
572
  * Every clade that resolves, with its fill color settled, against the leaf
520
573
  * order `rowNamesSet` gives. The tree panel resolves against the displayed
521
574
  * rows and the overview against its own, so both take the tree they draw. A
522
- * `range` record names no node, so `collapse` and `focus`, which need one,
523
- * drop it.
575
+ * `range` record names no node, so the seeding marks, which need one, drop it.
524
576
  */
525
577
  function resolveClades(
526
578
  clades: Clade[],
@@ -530,7 +582,7 @@ function resolveClades(
530
582
  const index = leafIndex(root)
531
583
  return clades.flatMap(clade => {
532
584
  const resolved = cladeRows(clade, root, index, rowNamesSet)
533
- const seeding = clade.mark === 'collapse' || clade.mark === 'focus'
585
+ const seeding = seedingMarks.has(clade.mark)
534
586
  return resolved && !(seeding && resolved.nodeId === undefined)
535
587
  ? [
536
588
  {
@@ -548,20 +600,50 @@ function resolveClades(
548
600
  })
549
601
  }
550
602
 
603
+ const childOrders: Record<
604
+ TreeOrder,
605
+ ((a: HierarchyNode, b: HierarchyNode) => number) | undefined
606
+ > = {
607
+ input: undefined,
608
+ branchLength: (a, b) => (a.data.length ?? 1) - (b.data.length ?? 1),
609
+ ladderize: (a, b) => a.value! - b.value!,
610
+ ladderizeReverse: (a, b) => b.value! - a.value!,
611
+ }
612
+
551
613
  /**
552
614
  * The tree with the display transforms applied: leaf counts summed, children
553
- * sorted by branch length, `showOnly` taken as the new root, and each
554
- * `collapsed` clade folded. The overview builds the same tree without the
555
- * focus, so it shows the focused subtree inside the whole.
615
+ * put in `order` and each `rotated` node's reversed, `showOnly` taken as the
616
+ * new root, and each `collapsed` clade folded. The overview builds the same
617
+ * tree without the focus, so it shows the focused subtree inside the whole.
556
618
  */
557
619
  function buildTreeRoot(
558
620
  tree: NodeWithIds,
559
- collapsed: readonly string[],
560
- showOnly?: string,
621
+ {
622
+ collapsed,
623
+ rotated,
624
+ order,
625
+ showOnly,
626
+ }: {
627
+ collapsed: readonly string[]
628
+ rotated: readonly string[]
629
+ order: TreeOrder
630
+ showOnly?: string
631
+ },
561
632
  ) {
562
633
  let hier = hierarchy(tree, d => d.children)
563
634
  hierarchySum(hier, d => (d.children.length > 0 ? 0 : 1))
564
- sort(hier, (a, b) => (a.data.length ?? 1) - (b.data.length ?? 1))
635
+ const compare = childOrders[order]
636
+ if (compare) {
637
+ sort(hier, compare)
638
+ }
639
+ if (rotated.length > 0) {
640
+ const flip = new Set(rotated)
641
+ forEachDescendant(hier, n => {
642
+ if (flip.has(n.data.id)) {
643
+ n.children?.reverse()
644
+ }
645
+ })
646
+ }
565
647
 
566
648
  if (showOnly) {
567
649
  const res = find(hier, n => n.data.id === showOnly)
@@ -586,6 +668,53 @@ function buildTreeRoot(
586
668
  return hier
587
669
  }
588
670
 
671
+ /**
672
+ * The node whose branch an outgroup roots the tree on: the outgroup's most
673
+ * recent common ancestor, or the rest of the tips' where that ancestor is the
674
+ * root, since the outgroup then straddles the root and the branch above the
675
+ * other side is the same branch of the unrooted tree.
676
+ */
677
+ function outgroupNode(root: HierarchyNode, outgroup: string[]) {
678
+ const index = leafIndex(root)
679
+ const node = mrca(root, outgroup, index)
680
+ if (node !== root) {
681
+ return node
682
+ }
683
+ const out = new Set(outgroup)
684
+ const rest = leaves(root)
685
+ .map(n => n.data.name)
686
+ .filter(name => !out.has(name))
687
+ const other = mrca(root, rest, index)
688
+ return other === root ? undefined : other
689
+ }
690
+
691
+ /**
692
+ * `tree` rerooted as `treeRoot` asks, with fresh path ids. The ids of the tree
693
+ * as written become names on unnamed nodes, and a rerooting would carry them
694
+ * along as labels, so they come off first.
695
+ */
696
+ function rootTree(tree: NodeWithIds, treeRoot: TreeRoot) {
697
+ const plain = new Map<NodeWithIds, TreeNode>()
698
+ for (const node of preorder(tree).reverse()) {
699
+ plain.set(node, {
700
+ name: node.name === node.id ? undefined : node.name,
701
+ length: node.length,
702
+ children: node.children.map(c => plain.get(c)!),
703
+ })
704
+ }
705
+ const input = plain.get(tree)!
706
+ if (treeRoot === 'midpoint') {
707
+ return generateNodeIds(midpointRoot(input))
708
+ }
709
+ const target = outgroupNode(
710
+ hierarchy(tree, d => d.children),
711
+ treeRoot.outgroup,
712
+ )
713
+ return target
714
+ ? generateNodeIds(rerootTree(input, plain.get(target.data)!))
715
+ : tree
716
+ }
717
+
589
718
  // the inclusive tip indices a node covers, from the row-space extent
590
719
  // clusterLayout writes to xMin/xMax as tip centers
591
720
  function tipRange(node: HierarchyNode): [number, number] {
@@ -796,6 +925,22 @@ function stateModelFactory() {
796
925
  */
797
926
  collapsed: stripDefault(types.array(types.string), []),
798
927
 
928
+ /**
929
+ * #property
930
+ * tree nodes whose children draw in reverse order, which is ggtree's
931
+ * `rotate`: the same tree, with the clades on either side of the node
932
+ * swapped
933
+ */
934
+ rotated: stripDefault(types.array(types.string), []),
935
+
936
+ /**
937
+ * #property
938
+ * where the tree is rooted: `midpoint` roots it halfway along the
939
+ * longest path between two tips, `{outgroup}` on the branch above
940
+ * the tips named, and undefined keeps the file's root
941
+ */
942
+ treeRoot: types.frozen<TreeRoot | undefined>(),
943
+
799
944
  /**
800
945
  * #property
801
946
  * focus on particular subtree
@@ -890,7 +1035,8 @@ function stateModelFactory() {
890
1035
  * clades of the tree with a mark drawn over them. `mrca` names tips
891
1036
  * whose common ancestor is the clade, or `range` its first and last
892
1037
  * tip in display order, and `tips` is the leaf count the producer
893
- * measured. `mark` is `highlight`, `bracket`, `collapse` or `focus`.
1038
+ * measured. `mark` is `highlight`, `bracket`, `collapse`, `focus` or
1039
+ * `rotate`.
894
1040
  * See docs/layers.md
895
1041
  */
896
1042
  clades: stripDefault(types.array(types.frozen<Clade>()), []),
@@ -1023,9 +1169,16 @@ function stateModelFactory() {
1023
1169
  * #volatile
1024
1170
  * non-fatal load problems: an optional layer that failed to load, an
1025
1171
  * overlay that failed to parse. `error` replaces the view and is for the
1026
- * alignment itself
1172
+ * alignment itself. `warnings` adds the ones derived from the data
1027
1173
  */
1028
- warnings: [] as string[],
1174
+ loadWarnings: [] as string[],
1175
+
1176
+ /**
1177
+ * #volatile
1178
+ * derived warnings the user dismissed, which stay hidden while the data
1179
+ * behind them holds
1180
+ */
1181
+ dismissedWarnings: [] as string[],
1029
1182
 
1030
1183
  /**
1031
1184
  * #volatile
@@ -1119,18 +1272,11 @@ function stateModelFactory() {
1119
1272
  * that failed to parse. A message already on the list is not added again
1120
1273
  */
1121
1274
  addWarning(warning: string) {
1122
- if (!self.warnings.includes(warning)) {
1123
- self.warnings = [...self.warnings, warning]
1275
+ if (!self.loadWarnings.includes(warning)) {
1276
+ self.loadWarnings = [...self.loadWarnings, warning]
1124
1277
  }
1125
1278
  },
1126
1279
 
1127
- /**
1128
- * #action
1129
- */
1130
- clearWarnings() {
1131
- self.warnings = []
1132
- },
1133
-
1134
1280
  /**
1135
1281
  * #action
1136
1282
  * declare that this host restores the loaded documents itself, which
@@ -1253,8 +1399,9 @@ function stateModelFactory() {
1253
1399
  /**
1254
1400
  * #action
1255
1401
  * switch to another alignment of a multi-alignment file (Stockholm).
1256
- * Clears the collapsed node ids, the subtree in focus, the reference row
1257
- * and the scroll position, which all refer to the previous alignment
1402
+ * Clears the collapsed and rotated node ids, the subtree in focus, the
1403
+ * reference row and the scroll position, which all refer to the
1404
+ * previous alignment
1258
1405
  */
1259
1406
  setCurrentAlignment(n: number) {
1260
1407
  if (n === self.currentAlignment) {
@@ -1263,6 +1410,7 @@ function stateModelFactory() {
1263
1410
  transaction(() => {
1264
1411
  self.currentAlignment = n
1265
1412
  self.collapsed.clear()
1413
+ self.rotated.clear()
1266
1414
  this.setShowOnly(undefined)
1267
1415
  this.drawRelativeTo(undefined)
1268
1416
  self.scrollX = 0
@@ -1282,6 +1430,42 @@ function stateModelFactory() {
1282
1430
  }
1283
1431
  },
1284
1432
 
1433
+ /**
1434
+ * #action
1435
+ * reverse the order of the given node's children, or restore it
1436
+ */
1437
+ toggleRotated(node: string) {
1438
+ if (self.rotated.includes(node)) {
1439
+ self.rotated.remove(node)
1440
+ } else {
1441
+ self.rotated.push(node)
1442
+ }
1443
+ },
1444
+
1445
+ /**
1446
+ * #action
1447
+ */
1448
+ clearRotated() {
1449
+ self.rotated.clear()
1450
+ },
1451
+
1452
+ /**
1453
+ * #action
1454
+ * reroot the tree. Clears `collapsed`, `rotated` and `showOnly`, whose
1455
+ * path-derived node ids name other nodes in the rerooted tree
1456
+ */
1457
+ setTreeRoot(treeRoot?: TreeRoot) {
1458
+ if (compareStructural(self.treeRoot, treeRoot)) {
1459
+ return
1460
+ }
1461
+ transaction(() => {
1462
+ self.treeRoot = treeRoot
1463
+ self.collapsed.clear()
1464
+ self.rotated.clear()
1465
+ this.setShowOnly(undefined)
1466
+ })
1467
+ },
1468
+
1285
1469
  /**
1286
1470
  * #action
1287
1471
  * show only the subtree rooted at the given node id (pass undefined to
@@ -1458,12 +1642,13 @@ function stateModelFactory() {
1458
1642
  * from disk or pasted in becomes inline text, and DataModel drops an
1459
1643
  * inline document past `maxInlineSnapshotBytes`.
1460
1644
  *
1461
- * The header lists these, and the standalone app stops rewriting the
1462
- * address bar while the list is non-empty, so a copied link does not
1463
- * open an empty viewer unannounced. A document fetched from a URL never
1464
- * appears here, since the snapshot keeps its filehandle.
1645
+ * The header lists these, and the standalone app drops `?data=` from
1646
+ * the address bar while the list is non-empty or the encoded link would
1647
+ * pass 8,000 characters, so a copied link never opens an empty viewer
1648
+ * unannounced. A document fetched from a URL never appears here, since
1649
+ * the snapshot keeps its filehandle.
1465
1650
  *
1466
- * Empty when `hostRestoresData` is true.
1651
+ * Empty when `hostCarriesData` is true.
1467
1652
  */
1468
1653
  get unshareableData(): UnshareableData[] {
1469
1654
  if (self.hostRestoresData) {
@@ -1560,8 +1745,9 @@ function stateModelFactory() {
1560
1745
 
1561
1746
  /**
1562
1747
  * #getter
1748
+ * the tree the file or the alignment gives, before `treeRoot`
1563
1749
  */
1564
- get tree(): NodeWithIds {
1750
+ get inputTree(): NodeWithIds {
1565
1751
  const text = self.data.tree
1566
1752
  return text
1567
1753
  ? generateNodeIds(parseTreeText(text))
@@ -1573,6 +1759,19 @@ function stateModelFactory() {
1573
1759
  })
1574
1760
  },
1575
1761
 
1762
+ /**
1763
+ * #getter
1764
+ * the tree as the file or the alignment gives it, rerooted where
1765
+ * `treeRoot` asks. Every node id, and so `collapsed`, `rotated` and
1766
+ * `showOnly`, refers to this tree.
1767
+ */
1768
+ get tree(): NodeWithIds {
1769
+ const tree = this.inputTree
1770
+ return self.treeRoot && !tree.noTree
1771
+ ? rootTree(tree, self.treeRoot)
1772
+ : tree
1773
+ },
1774
+
1576
1775
  /**
1577
1776
  * #getter
1578
1777
  * Returns the list of row (sequence) names in display order.
@@ -1594,8 +1793,8 @@ function stateModelFactory() {
1594
1793
  * `clades` resolved to the rows each one covers. The tip names resolve
1595
1794
  * against `tree` rather than `root`, so a clade whose ancestor the user
1596
1795
  * collapsed keeps its rows. One leaf pass over the tree serves every
1597
- * clade. A `range` record names no node, so `collapse` and `focus`, which
1598
- * need one, drop it.
1796
+ * clade. A `range` record names no node, so `collapse`, `focus` and
1797
+ * `rotate`, which need one, drop it.
1599
1798
  */
1600
1799
  get resolvedClades(): ResolvedClade[] {
1601
1800
  if (self.clades.length === 0) {
@@ -1653,7 +1852,27 @@ function stateModelFactory() {
1653
1852
  * #getter
1654
1853
  */
1655
1854
  get root() {
1656
- return buildTreeRoot(this.tree, self.collapsed, self.showOnly)
1855
+ return buildTreeRoot(this.tree, {
1856
+ collapsed: self.collapsed,
1857
+ rotated: self.rotated,
1858
+ order: self.treeOrder,
1859
+ showOnly: self.showOnly,
1860
+ })
1861
+ },
1862
+
1863
+ /**
1864
+ * #getter
1865
+ * the tree as Newick in the order and with the root it is drawn, with
1866
+ * every clade, collapsed or out of focus
1867
+ */
1868
+ get treeNewick() {
1869
+ return writeNewick(
1870
+ buildTreeRoot(this.tree, {
1871
+ collapsed: [],
1872
+ rotated: self.rotated,
1873
+ order: self.treeOrder,
1874
+ }),
1875
+ )
1657
1876
  },
1658
1877
 
1659
1878
  /**
@@ -1677,7 +1896,11 @@ function stateModelFactory() {
1677
1896
  if (!self.showTreeOverview) {
1678
1897
  return undefined
1679
1898
  }
1680
- const root = buildTreeRoot(this.tree, self.collapsed)
1899
+ const root = buildTreeRoot(this.tree, {
1900
+ collapsed: self.collapsed,
1901
+ rotated: self.rotated,
1902
+ order: self.treeOrder,
1903
+ })
1681
1904
  const numTips = leaves(root).length
1682
1905
  clusterLayout(root, numTips, 1)
1683
1906
  const rootLen = Math.max(root.data.length || 0, 0)
@@ -1759,7 +1982,7 @@ function stateModelFactory() {
1759
1982
  * alignment, the sum of each record's width
1760
1983
  */
1761
1984
  get rowPanelsWidth() {
1762
- return sum(self.rowPanels.map(p => rowPanelWidth(p, self.rowHeight)))
1985
+ return sum(self.rowPanels.map(p => rowPanelWidth(p, self)))
1763
1986
  },
1764
1987
  /**
1765
1988
  * #getter
@@ -1806,9 +2029,10 @@ function stateModelFactory() {
1806
2029
  return []
1807
2030
  }
1808
2031
  // ragged input (a3m, hand-edited fasta) can have rows shorter than the
1809
- // alignment; the widest row defines the column count and a row that
1810
- // stops early counts as gapped for the remainder
1811
- const numCols = strs.reduce((max, str) => Math.max(max, str.length), 0)
2032
+ // alignment; the widest row of the whole alignment, collapsed rows
2033
+ // included, defines the column count as it does for numColumns, and a
2034
+ // row that stops early counts as gapped for the remainder
2035
+ const numCols = this.MSA?.getWidth() ?? 0
1812
2036
  const numRows = strs.length
1813
2037
  const threshold = Math.ceil((realAllowedGappyness / 100) * numRows)
1814
2038
  const blankCounts = new Uint32Array(numCols)
@@ -1939,7 +2163,7 @@ function stateModelFactory() {
1939
2163
  * #getter
1940
2164
  */
1941
2165
  get colStats() {
1942
- return columnCountsFromRows(this.columns2d)
2166
+ return columnCountsFromRows(this.columns2d, this.numColumns)
1943
2167
  },
1944
2168
 
1945
2169
  /**
@@ -2038,40 +2262,42 @@ function stateModelFactory() {
2038
2262
  },
2039
2263
  /**
2040
2264
  * #getter
2041
- * generates a new tree that is clustered with x,y positions
2042
- */
2043
- get hierarchy(): HierarchyNode<NodeWithIdsAndLength> {
2044
- const r = this.root
2045
- clusterLayout(r, this.totalHeight, self.treeWidth)
2046
- const max = this.rootToTipLength
2047
- const k = max ? self.treeWidth / max : 0
2048
- // the displayed root starts at x=0, so subtract its length here; `root`
2049
- // returns the cached parse, which must not be mutated
2050
- setBrLength(r, -Math.max(r.data.length || 0, 0), k)
2051
- // for each collapsed clade, record the pixel x-position of its farthest
2052
- // tip so the renderer can draw a triangle spanning the branch-length
2053
- // extent of the hidden subtree
2054
- forEachDescendant(r, node => {
2055
- if (node._children) {
2056
- node.collapsedTipXFar =
2057
- (node.len ?? 0) + collapsedSubtreeMaxLength(node) * k
2058
- }
2265
+ * `root` laid out on new nodes in row and branch-length units, read in
2266
+ * pixels through `rowHeight` and `treeWidth`. A zoom leaves it and every
2267
+ * node in it as they are, and an observer reading a node's `x` or `len`
2268
+ * observes the size it scales by.
2269
+ */
2270
+ get treeLayout() {
2271
+ return layoutTree(this.root, {
2272
+ get rowHeight() {
2273
+ return self.rowHeight
2274
+ },
2275
+ get treeWidth() {
2276
+ return self.treeWidth
2277
+ },
2059
2278
  })
2060
- return r as HierarchyNode<NodeWithIdsAndLength>
2279
+ },
2280
+
2281
+ /**
2282
+ * #getter
2283
+ * the laid-out tree, with pixel positions on every node
2284
+ */
2285
+ get hierarchy(): HierarchyNode<NodeWithIds> {
2286
+ return this.treeLayout.root
2061
2287
  },
2062
2288
 
2063
2289
  /**
2064
2290
  * #getter
2065
2291
  */
2066
2292
  get totalHeight() {
2067
- return leaves(this.root).length * self.rowHeight
2293
+ return this.leaves.length * self.rowHeight
2068
2294
  },
2069
2295
 
2070
2296
  /**
2071
2297
  * #getter
2072
2298
  */
2073
- get leaves() {
2074
- return leaves(this.hierarchy)
2299
+ get leaves(): HierarchyNode<NodeWithIds>[] {
2300
+ return this.treeLayout.leaves
2075
2301
  },
2076
2302
 
2077
2303
  /**
@@ -2080,8 +2306,7 @@ function stateModelFactory() {
2080
2306
  * the tree's own units
2081
2307
  */
2082
2308
  get rootToTipLength() {
2083
- const r = this.root
2084
- return maxLength(r) - Math.max(r.data.length || 0, 0)
2309
+ return this.treeLayout.rootToTipLength
2085
2310
  },
2086
2311
 
2087
2312
  /**
@@ -2108,7 +2333,7 @@ function stateModelFactory() {
2108
2333
  * max topological depth to a tip, used to scale cladogram x-positions
2109
2334
  */
2110
2335
  get maxDepthToLeaf() {
2111
- return calcDepthToLeaf(this.hierarchy)
2336
+ return this.treeLayout.root.depthToLeaf
2112
2337
  },
2113
2338
 
2114
2339
  /**
@@ -2343,13 +2568,6 @@ function stateModelFactory() {
2343
2568
  return self.MSA?.secondaryStructureConsensus
2344
2569
  },
2345
2570
 
2346
- /**
2347
- * #getter
2348
- */
2349
- get seqConsensus() {
2350
- return self.MSA?.seqConsensus
2351
- },
2352
-
2353
2571
  /**
2354
2572
  * #getter
2355
2573
  * the base pairs of the consensus secondary structure, as arcs, in
@@ -2494,11 +2712,13 @@ function stateModelFactory() {
2494
2712
 
2495
2713
  /**
2496
2714
  * #method
2497
- * Return a row-specific letter at a visible column, or undefined if gap.
2715
+ * Return the character a row holds at a visible column: a residue, or
2716
+ * the gap character (`-` or `.`) on a gap. Undefined for a row name the
2717
+ * alignment does not have or a column past the row's end.
2498
2718
  *
2499
2719
  * @param rowName - The name of the row
2500
2720
  * @param visibleCol - The visible column index (what the user sees on screen)
2501
- * @returns The letter at that position, or undefined if it's a gap
2721
+ * @returns The character at that position
2502
2722
  */
2503
2723
  visibleColToRowLetter(rowName: string, visibleCol: number) {
2504
2724
  return self.rowMap.get(rowName)?.[
@@ -2596,6 +2816,22 @@ function stateModelFactory() {
2596
2816
  return col ?? (seqPos === 0 ? 0 : seq.length)
2597
2817
  },
2598
2818
 
2819
+ /**
2820
+ * #method
2821
+ * the global column of a span's last residue, clamping a position past
2822
+ * the end of the row to its last residue. Undefined for a row name the
2823
+ * alignment does not have, or a row with no residues.
2824
+ *
2825
+ * @param rowName - The name of the row
2826
+ * @param seqPos - The sequence position (0-based, ungapped)
2827
+ */
2828
+ seqEndToGlobalCol(rowName: string, seqPos: number) {
2829
+ const index = self.seqPosIndex(rowName)
2830
+ return index && index.length > 0
2831
+ ? index[Math.min(seqPos, index.length - 1)]
2832
+ : undefined
2833
+ },
2834
+
2599
2835
  /**
2600
2836
  * #method
2601
2837
  * Convert a sequence position (ungapped) directly to a visible column index.
@@ -2625,10 +2861,10 @@ function stateModelFactory() {
2625
2861
  const start = Math.max(1, Math.floor(rawStart))
2626
2862
  const end = Math.ceil(rawEnd)
2627
2863
  let startGlobal = start - 1
2628
- let endGlobal = end - 1
2864
+ let endGlobal = Math.min(end, self.MSA?.getWidth() ?? end) - 1
2629
2865
  if (row !== undefined) {
2630
2866
  const rowStart = this.seqPosToGlobalCol(row, start - 1)
2631
- const rowEnd = this.seqPosToGlobalCol(row, end - 1)
2867
+ const rowEnd = this.seqEndToGlobalCol(row, end - 1)
2632
2868
  if (rowStart === undefined || rowEnd === undefined) {
2633
2869
  return undefined
2634
2870
  }
@@ -2922,6 +3158,28 @@ function stateModelFactory() {
2922
3158
  },
2923
3159
  }))
2924
3160
  .views(self => ({
3161
+ /**
3162
+ * #getter
3163
+ */
3164
+ get verticalScrollbarWidth() {
3165
+ return self.showVerticalScrollbar ? 20 : 0
3166
+ },
3167
+ /**
3168
+ * #getter
3169
+ * width of the alignment canvas: the msa area less the vertical
3170
+ * scrollbar. showHorizontalScrollbar must not read it, since that feeds
3171
+ * msaAreaHeight -> showVerticalScrollbar and would form a cycle
3172
+ */
3173
+ get msaCanvasWidth() {
3174
+ return self.msaAreaWidth - this.verticalScrollbarWidth
3175
+ },
3176
+ /**
3177
+ * #getter
3178
+ * most-negative allowed scrollX, which keeps the last column in view
3179
+ */
3180
+ get maxScrollX() {
3181
+ return Math.min(-self.totalWidth + this.msaCanvasWidth, 0)
3182
+ },
2925
3183
  /**
2926
3184
  * #getter
2927
3185
  */
@@ -2939,9 +3197,29 @@ function stateModelFactory() {
2939
3197
  },
2940
3198
  /**
2941
3199
  * #getter
3200
+ * warnings that follow from the loaded data and clear when it changes. A
3201
+ * GFF whose first column names rows of some other alignment parses
3202
+ * without error and draws nothing
3203
+ */
3204
+ get dataWarnings(): string[] {
3205
+ const { annotations, rowNamesSet } = self
3206
+ return self.dataInitialized &&
3207
+ annotations.length > 0 &&
3208
+ !annotations.some(a => rowNamesSet.has(a.id))
3209
+ ? [
3210
+ `0 of ${annotations.length} annotations name a row in this alignment`,
3211
+ ]
3212
+ : []
3213
+ },
3214
+ /**
3215
+ * #getter
3216
+ * the load warnings and the undismissed data warnings, for the header
2942
3217
  */
2943
- get maxScrollX() {
2944
- return Math.min(-self.totalWidth + (self.msaAreaWidth - 100), 0)
3218
+ get warnings(): string[] {
3219
+ return [
3220
+ ...self.loadWarnings,
3221
+ ...this.dataWarnings.filter(w => !self.dismissedWarnings.includes(w)),
3222
+ ]
2945
3223
  },
2946
3224
  /**
2947
3225
  * #getter
@@ -2968,6 +3246,14 @@ function stateModelFactory() {
2968
3246
  self.drawMsaLetters = arg
2969
3247
  },
2970
3248
 
3249
+ /**
3250
+ * #action
3251
+ */
3252
+ clearWarnings() {
3253
+ self.loadWarnings = []
3254
+ self.dismissedWarnings = self.dataWarnings
3255
+ },
3256
+
2971
3257
  /**
2972
3258
  * #action
2973
3259
  */
@@ -3028,13 +3314,43 @@ function stateModelFactory() {
3028
3314
 
3029
3315
  /**
3030
3316
  * #action
3031
- * swap in a different tree over the same alignment. Clears `collapsed`
3032
- * and `showOnly`, since path-derived node ids (node-0-0-1) from the old
3317
+ * root the tree on the branch above the given node. The node's tips are
3318
+ * a split of the unrooted tree, and the side of it that is a clade in
3319
+ * the tree as written stands for it as an outgroup, by its first and
3320
+ * last tip, so the root survives the path ids changing under it
3321
+ */
3322
+ rerootAt(nodeId: string) {
3323
+ const shown = find(
3324
+ hierarchy(self.tree, d => d.children),
3325
+ n => n.data.id === nodeId,
3326
+ )
3327
+ if (!shown?.parent) {
3328
+ return
3329
+ }
3330
+ const node = outgroupNode(
3331
+ hierarchy(self.inputTree, d => d.children),
3332
+ leaves(shown).map(n => n.data.name),
3333
+ )
3334
+ if (node) {
3335
+ const tips = leaves(node)
3336
+ self.setTreeRoot({
3337
+ outgroup: [
3338
+ ...new Set([tips[0]!.data.name, tips.at(-1)!.data.name]),
3339
+ ],
3340
+ })
3341
+ }
3342
+ },
3343
+
3344
+ /**
3345
+ * #action
3346
+ * swap in a different tree over the same alignment. Clears `collapsed`,
3347
+ * `rotated` and `showOnly`, since path-derived node ids (node-0-0-1) from the old
3033
3348
  * tree would match unrelated nodes in the new one.
3034
3349
  */
3035
3350
  replaceTree(newick: string) {
3036
3351
  transaction(() => {
3037
3352
  self.collapsed.clear()
3353
+ self.rotated.clear()
3038
3354
  self.setShowOnly(undefined)
3039
3355
  self.setTree(newick)
3040
3356
  })
@@ -3048,52 +3364,53 @@ function stateModelFactory() {
3048
3364
  self.setColWidth(defaultColWidth)
3049
3365
  self.setRowHeight(defaultRowHeight)
3050
3366
  },
3367
+ /**
3368
+ * #action
3369
+ * zoom by `scaleFactor` about the center of the alignment area
3370
+ */
3371
+ zoomAtCenter(scaleFactor: number, axis: ScrollZoomAxis = 'both') {
3372
+ this.zoomToPos(
3373
+ scaleFactor,
3374
+ self.msaCanvasWidth / 2,
3375
+ self.msaAreaHeight / 2,
3376
+ axis,
3377
+ )
3378
+ },
3051
3379
  /**
3052
3380
  * #action
3053
3381
  */
3054
3382
  zoomOutHorizontal() {
3055
- self.colWidth = Math.max(minColWidth, Math.floor(self.colWidth * 0.75))
3056
- self.scrollX = clamp(self.scrollX, self.maxScrollX, 0)
3383
+ this.zoomAtCenter(0.75, 'horizontal')
3057
3384
  },
3058
3385
  /**
3059
3386
  * #action
3060
3387
  */
3061
3388
  zoomInHorizontal() {
3062
- self.colWidth = Math.min(maxCellSize, Math.ceil(self.colWidth * 1.5))
3063
- self.scrollX = clamp(self.scrollX, self.maxScrollX, 0)
3389
+ this.zoomAtCenter(1.5, 'horizontal')
3064
3390
  },
3065
3391
  /**
3066
3392
  * #action
3067
3393
  */
3068
3394
  zoomInVertical() {
3069
- self.rowHeight = Math.min(maxCellSize, Math.ceil(self.rowHeight * 1.5))
3395
+ this.zoomAtCenter(1.5, 'vertical')
3070
3396
  },
3071
3397
  /**
3072
3398
  * #action
3073
3399
  */
3074
3400
  zoomOutVertical() {
3075
- self.rowHeight = Math.max(
3076
- minRowHeight,
3077
- Math.floor(self.rowHeight * 0.75),
3078
- )
3401
+ this.zoomAtCenter(0.75, 'vertical')
3079
3402
  },
3080
3403
  /**
3081
3404
  * #action
3082
3405
  */
3083
3406
  zoomIn() {
3084
- transaction(() => {
3085
- this.zoomInHorizontal()
3086
- this.zoomInVertical()
3087
- })
3407
+ this.zoomAtCenter(1.5)
3088
3408
  },
3089
3409
  /**
3090
3410
  * #action
3091
3411
  */
3092
3412
  zoomOut() {
3093
- transaction(() => {
3094
- this.zoomOutHorizontal()
3095
- this.zoomOutVertical()
3096
- })
3413
+ this.zoomAtCenter(0.75)
3097
3414
  },
3098
3415
  /**
3099
3416
  * #action
@@ -3256,25 +3573,11 @@ function stateModelFactory() {
3256
3573
  },
3257
3574
  }))
3258
3575
  .views(self => ({
3259
- /**
3260
- * #getter
3261
- */
3262
- get verticalScrollbarWidth() {
3263
- return self.showVerticalScrollbar ? 20 : 0
3264
- },
3265
- /**
3266
- * #getter
3267
- * width of the alignment canvas: the msa area less the vertical
3268
- * scrollbar. showHorizontalScrollbar must not read it, since that feeds
3269
- * msaAreaHeight -> showVerticalScrollbar and would form a cycle
3270
- */
3271
- get msaCanvasWidth() {
3272
- return self.msaAreaWidth - this.verticalScrollbarWidth
3273
- },
3274
3576
  /**
3275
3577
  * #method
3276
3578
  * the cell at a visible column and row index, in the coordinates a host
3277
- * writes highlights in
3579
+ * writes highlights in. On a gap `residue` is undefined and `letter` is
3580
+ * the gap character
3278
3581
  */
3279
3582
  cellAt(visibleCol: number, rowIndex?: number): Cell {
3280
3583
  const column = self.visibleColToGlobalCol(visibleCol)
@@ -3322,7 +3625,7 @@ function stateModelFactory() {
3322
3625
  }
3323
3626
  const { xStart, xEnd } = visibleColRange({
3324
3627
  offsetX: -scrollX,
3325
- blockWidth: this.msaCanvasWidth,
3628
+ blockWidth: self.msaCanvasWidth,
3326
3629
  colWidth,
3327
3630
  })
3328
3631
  const last = Math.max(0, Math.min(xEnd, numColumns) - 1)
@@ -3485,7 +3788,7 @@ function stateModelFactory() {
3485
3788
  add(field, legend)
3486
3789
  }
3487
3790
  }
3488
- for (const { legendTitle, legend } of this.resolvedRowPanels) {
3791
+ for (const { legendTitle, legend } of this.rowPanelScales) {
3489
3792
  add(legendTitle, legend)
3490
3793
  }
3491
3794
  return [...byKey.values()]
@@ -3533,7 +3836,7 @@ function stateModelFactory() {
3533
3836
  // column moves to the neighboring boundary. Bands with no visible
3534
3837
  // columns, or naming a missing row, are dropped.
3535
3838
  const start = self.seqPosToGlobalCol(name, annotation.start - 1)
3536
- const end = self.seqPosToGlobalCol(name, annotation.end - 1)
3839
+ const end = self.seqEndToGlobalCol(name, annotation.end - 1)
3537
3840
  if (start === undefined || end === undefined) {
3538
3841
  return undefined
3539
3842
  }
@@ -3740,45 +4043,30 @@ function stateModelFactory() {
3740
4043
 
3741
4044
  /**
3742
4045
  * #getter
3743
- * each row panel with its scale resolved against the values its field
3744
- * takes across the row table, giving the color per row name, the pixel
3745
- * column it draws in, and the entries its legend lists. Resolved once
3746
- * per change of that table or the panels, never per block per frame.
4046
+ * each row panel's scale resolved against the values its field takes,
4047
+ * giving the colors, labels and legend entries. Reads no cell size, so
4048
+ * a zoom reuses it and the legends built from it.
3747
4049
  */
3748
- get resolvedRowPanels(): ResolvedRowPanel[] {
3749
- const rows = Object.entries(self.rowData)
4050
+ get rowPanelScales(): (StripPanelScale | FeaturePanelScale)[] {
4051
+ const { rowData } = self
3750
4052
  const { fillPalette, featureFillEncoding, featureLabels } = this
3751
4053
  const annotations = self.filteredAnnotations
3752
- let offsetX = 0
3753
- return self.rowPanels.map((panel, index) => {
3754
- const width = rowPanelWidth(panel, self.rowHeight)
3755
- const base = { id: `rowpanel-${index}`, width, offsetX }
3756
- offsetX += width
4054
+ return self.rowPanels.map(panel => {
3757
4055
  if (panel.kind === 'features') {
3758
4056
  const color = panel.encoding?.color
3759
4057
  const encoding = color
3760
4058
  ? resolveFeatureScale(color.field, color.scale, annotations)
3761
4059
  : featureFillEncoding
3762
4060
  const label = panel.encoding?.label
3763
- const align = panel.transform?.find(t => t.type === 'align')
3764
4061
  return {
3765
- ...base,
3766
4062
  kind: panel.kind,
3767
4063
  x: panel.x,
3768
4064
  header: panel.header ?? '',
3769
4065
  field: encoding?.field,
3770
4066
  legendTitle: encoding?.field,
3771
- spans: featurePanelSpans({
3772
- panel,
3773
- width,
3774
- colWidth: self.colWidth,
3775
- domainBands: this.domainBands,
3776
- annotationsByRow: self.annotationsByRow,
3777
- shifts: align
3778
- ? this.featureAlignShifts.get(align.on)
3779
- : undefined,
3780
- }),
3781
- colors: featureColorMap(annotations, encoding, fillPalette),
4067
+ colors: color
4068
+ ? featureColorMap(annotations, encoding, fillPalette)
4069
+ : this.featureColors,
3782
4070
  labels: label
3783
4071
  ? featureLabelMap(annotations, label)
3784
4072
  : featureLabels,
@@ -3793,28 +4081,56 @@ function stateModelFactory() {
3793
4081
  }
3794
4082
  const { colorOf, legend } = resolveScale(
3795
4083
  panel.scale,
3796
- rows.map(([, row]) => row?.[panel.field]).filter(notEmpty),
4084
+ Object.values(rowData)
4085
+ .map(row => row?.[panel.field])
4086
+ .filter(notEmpty),
3797
4087
  )
3798
- const colors = new Map<string, string>()
3799
- for (const [name, row] of rows) {
3800
- const value = row?.[panel.field]
3801
- const color = value === undefined ? undefined : colorOf(value)
3802
- if (color) {
3803
- colors.set(name, color)
3804
- }
3805
- }
3806
4088
  return {
3807
- ...base,
3808
4089
  kind: panel.kind,
3809
4090
  field: panel.field,
3810
4091
  header: panel.header ?? panel.field,
3811
4092
  legendTitle: panel.legend ?? panel.field,
3812
- colors,
4093
+ colors: colorByRow(rowData, panel.field, colorOf),
3813
4094
  legend,
3814
4095
  }
3815
4096
  })
3816
4097
  },
3817
4098
 
4099
+ /**
4100
+ * #getter
4101
+ * each row panel with its scale from `rowPanelScales` and its geometry:
4102
+ * the pixel column it draws in and, for a features panel, the spans per
4103
+ * row. Resolved once per change of those inputs, never per block per
4104
+ * frame.
4105
+ */
4106
+ get resolvedRowPanels(): ResolvedRowPanel[] {
4107
+ const scales = this.rowPanelScales
4108
+ let offsetX = 0
4109
+ return self.rowPanels.map((panel, index) => {
4110
+ const width = rowPanelWidth(panel, self)
4111
+ const base = { id: `rowpanel-${index}`, width, offsetX }
4112
+ offsetX += width
4113
+ if (panel.kind === 'features') {
4114
+ const align = panel.transform?.find(t => t.type === 'align')
4115
+ return {
4116
+ ...base,
4117
+ ...(scales[index] as FeaturePanelScale),
4118
+ spans: featurePanelSpans({
4119
+ panel,
4120
+ width,
4121
+ colWidth: self.colWidth,
4122
+ domainBands: this.domainBands,
4123
+ annotationsByRow: self.annotationsByRow,
4124
+ shifts: align
4125
+ ? this.featureAlignShifts.get(align.on)
4126
+ : undefined,
4127
+ }),
4128
+ }
4129
+ }
4130
+ return { ...base, ...(scales[index] as StripPanelScale) }
4131
+ })
4132
+ },
4133
+
3818
4134
  /**
3819
4135
  * #getter
3820
4136
  * the shift an `align` transform gives each row, keyed by the feature
@@ -3857,19 +4173,9 @@ function stateModelFactory() {
3857
4173
  const encoding = this.resolvedEncodings.find(
3858
4174
  e => e.channel === 'tipLabel',
3859
4175
  )
3860
- if (!encoding) {
3861
- return undefined
3862
- }
3863
- const colors = new Map<string, string>()
3864
- for (const [name, row] of Object.entries(self.rowData)) {
3865
- const value = row?.[encoding.field]
3866
- const color =
3867
- value === undefined ? undefined : encoding.colorOf(value)
3868
- if (color) {
3869
- colors.set(name, color)
3870
- }
3871
- }
3872
- return colors
4176
+ return encoding
4177
+ ? colorByRow(self.rowData, encoding.field, encoding.colorOf)
4178
+ : undefined
3873
4179
  },
3874
4180
 
3875
4181
  /**
@@ -3885,14 +4191,14 @@ function stateModelFactory() {
3885
4191
  if (!encoding) {
3886
4192
  return undefined
3887
4193
  }
4194
+ const colors = colorByRow(
4195
+ self.rowData,
4196
+ encoding.field,
4197
+ encoding.colorOf,
4198
+ )
3888
4199
  return self.rowNames.map(name => {
3889
- const value = self.rowDataOf(name)?.[encoding.field]
3890
- const color =
3891
- value === undefined ? undefined : encoding.colorOf(value)
3892
- if (!color) {
3893
- return undefined
3894
- }
3895
- return withAlpha(color, rowTintAlpha)
4200
+ const color = colors.get(name)
4201
+ return color && withAlpha(color, rowTintAlpha)
3896
4202
  })
3897
4203
  },
3898
4204
 
@@ -3974,7 +4280,8 @@ function stateModelFactory() {
3974
4280
  */
3975
4281
  reset() {
3976
4282
  self.resetCount++
3977
- self.clearWarnings()
4283
+ self.loadWarnings = []
4284
+ self.dismissedWarnings = []
3978
4285
  self.setStatus(undefined)
3979
4286
  applySnapshot(
3980
4287
  self,
@@ -4001,6 +4308,21 @@ function stateModelFactory() {
4001
4308
  const blob = new Blob([html], { type: 'image/svg+xml' })
4002
4309
  saveAs(blob, exportFileName(self.msaFilehandle, 'svg'))
4003
4310
  },
4311
+ /**
4312
+ * #action
4313
+ * save `treeNewick` as a file
4314
+ */
4315
+ exportNewick() {
4316
+ const blob = new Blob([self.treeNewick], { type: 'text/plain' })
4317
+ saveAs(
4318
+ blob,
4319
+ exportFileName(
4320
+ self.treeFilehandle ?? self.msaFilehandle,
4321
+ 'nwk',
4322
+ 'tree',
4323
+ ),
4324
+ )
4325
+ },
4004
4326
  /**
4005
4327
  * #action
4006
4328
  * show or hide an annotation type. Only hidden types are recorded; see
@@ -4084,38 +4406,45 @@ function stateModelFactory() {
4084
4406
  self.setHighlightedColumns(self.highlightColumns)
4085
4407
  }
4086
4408
 
4087
- // The `collapse` and `focus` clade marks seed the collapsed list and
4088
- // the subtree in focus, which the tree, `hideGapsEffective` and the
4089
- // alignment all read. The tree arrives with the model for inline data
4090
- // and later for a filehandle, so the seeding waits for it and then runs
4091
- // once: expanding a seeded clade sticks, and the record collapses it
4092
- // again only on reload. `dataInitialized` is true once the MSA alone
4093
- // has loaded, when the tree is still the flat stub, so a tree
4094
- // filehandle holds the seeding until its text lands.
4095
- let cladesSeeded = false
4409
+ // The `collapse`, `focus` and `rotate` clade marks seed the collapsed
4410
+ // list, the subtree in focus and the rotated nodes, which the tree,
4411
+ // `hideGapsEffective` and the alignment all read. The tree arrives with
4412
+ // the model for inline data and later for a filehandle, so the seeding
4413
+ // waits for it and then runs once per data load, per change of
4414
+ // `clades` and per reroot, which clears the node ids the seeding
4415
+ // wrote: expanding a seeded clade sticks until one of those changes.
4416
+ // `dataInitialized` is true once the MSA alone has loaded, when the
4417
+ // tree is still the flat stub, so a tree filehandle holds the seeding
4418
+ // until its text lands.
4096
4419
  addDisposer(
4097
4420
  self,
4098
- autorun(() => {
4099
- if (
4100
- cladesSeeded ||
4101
- !self.dataInitialized ||
4102
- (self.treeFilehandle && !self.data.tree) ||
4103
- self.clades.length === 0
4104
- ) {
4105
- return
4106
- }
4107
- cladesSeeded = true
4108
- for (const { mark, nodeId } of self.resolvedClades) {
4109
- if (nodeId === undefined) {
4110
- continue
4421
+ reaction(
4422
+ () =>
4423
+ self.dataInitialized && !(self.treeFilehandle && !self.data.tree)
4424
+ ? { clades: getSnapshot(self.clades), treeRoot: self.treeRoot }
4425
+ : undefined,
4426
+ seed => {
4427
+ if (!seed?.clades.length) {
4428
+ return
4111
4429
  }
4112
- if (mark === 'collapse' && !self.collapsed.includes(nodeId)) {
4113
- self.toggleCollapsed(nodeId)
4114
- } else if (mark === 'focus') {
4115
- self.setShowOnly(nodeId)
4430
+ for (const { mark, nodeId } of self.resolvedClades) {
4431
+ if (nodeId === undefined) {
4432
+ continue
4433
+ }
4434
+ if (mark === 'collapse' && !self.collapsed.includes(nodeId)) {
4435
+ self.toggleCollapsed(nodeId)
4436
+ } else if (mark === 'focus') {
4437
+ self.setShowOnly(nodeId)
4438
+ } else if (
4439
+ mark === 'rotate' &&
4440
+ !self.rotated.includes(nodeId)
4441
+ ) {
4442
+ self.toggleRotated(nodeId)
4443
+ }
4116
4444
  }
4117
- }
4118
- }),
4445
+ },
4446
+ { fireImmediately: true, equals: compareStructural },
4447
+ ),
4119
4448
  )
4120
4449
 
4121
4450
  // zoomToRegion needs a width and resolves residues through the visible
@@ -4302,24 +4631,6 @@ function stateModelFactory() {
4302
4631
  }),
4303
4632
  )
4304
4633
 
4305
- // a GFF whose first column names rows of some other alignment parses
4306
- // without error and draws nothing
4307
- addDisposer(
4308
- self,
4309
- autorun(() => {
4310
- const { annotations, rowNamesSet } = self
4311
- if (
4312
- annotations.length > 0 &&
4313
- rowNamesSet.size > 0 &&
4314
- !annotations.some(a => rowNamesSet.has(a.id))
4315
- ) {
4316
- self.addWarning(
4317
- `0 of ${annotations.length} annotations name a row in this alignment`,
4318
- )
4319
- }
4320
- }),
4321
- )
4322
-
4323
4634
  // gffFilehandle loads into data.gff, which the autorun above parses
4324
4635
  loadOnFilehandleChange({
4325
4636
  what: 'annotations',