jbrowse-plugin-tview 2.2.1 → 3.0.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 (113) hide show
  1. package/README.md +349 -69
  2. package/dist/AddHighlightModel/index.js +2 -13
  3. package/dist/AddHighlightModel/index.js.map +1 -1
  4. package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.d.ts +44 -64
  5. package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js +57 -81
  6. package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js.map +1 -1
  7. package/dist/LaunchTView/TviewGetPlanRpc/index.d.ts +1 -1
  8. package/dist/LaunchTView/align.d.ts +18 -17
  9. package/dist/LaunchTView/align.js +198 -151
  10. package/dist/LaunchTView/align.js.map +1 -1
  11. package/dist/LaunchTView/alleles.d.ts +14 -0
  12. package/dist/LaunchTView/alleles.js +51 -3
  13. package/dist/LaunchTView/alleles.js.map +1 -1
  14. package/dist/LaunchTView/fetchTviewPlan.d.ts +5 -37
  15. package/dist/LaunchTView/fetchTviewPlan.js +7 -28
  16. package/dist/LaunchTView/fetchTviewPlan.js.map +1 -1
  17. package/dist/LaunchTView/index.js +45 -16
  18. package/dist/LaunchTView/index.js.map +1 -1
  19. package/dist/LaunchTView/launch.d.ts +50 -0
  20. package/dist/LaunchTView/launch.js +48 -0
  21. package/dist/LaunchTView/launch.js.map +1 -0
  22. package/dist/LaunchTView/launchView.d.ts +3 -8
  23. package/dist/LaunchTView/launchView.js +18 -12
  24. package/dist/LaunchTView/launchView.js.map +1 -1
  25. package/dist/LaunchTView/modifications.d.ts +64 -0
  26. package/dist/LaunchTView/modifications.js +131 -0
  27. package/dist/LaunchTView/modifications.js.map +1 -0
  28. package/dist/LaunchTView/readLayout.d.ts +35 -39
  29. package/dist/LaunchTView/readLayout.js +44 -24
  30. package/dist/LaunchTView/readLayout.js.map +1 -1
  31. package/dist/LaunchTView/repeatStats.d.ts +1 -1
  32. package/dist/LaunchTView/repeatStats.js +6 -6
  33. package/dist/LaunchTView/repeatStats.js.map +1 -1
  34. package/dist/LaunchTView/repeats.js +6 -4
  35. package/dist/LaunchTView/repeats.js.map +1 -1
  36. package/dist/LaunchTView/tview.d.ts +47 -24
  37. package/dist/LaunchTView/tview.js +188 -99
  38. package/dist/LaunchTView/tview.js.map +1 -1
  39. package/dist/TViewPanel/components/TViewPanel.js +9 -4
  40. package/dist/TViewPanel/components/TViewPanel.js.map +1 -1
  41. package/dist/TViewPanel/genome.d.ts +11 -0
  42. package/dist/TViewPanel/genome.js +32 -0
  43. package/dist/TViewPanel/genome.js.map +1 -0
  44. package/dist/TViewPanel/init.d.ts +38 -19
  45. package/dist/TViewPanel/init.js +66 -39
  46. package/dist/TViewPanel/init.js.map +1 -1
  47. package/dist/TViewPanel/initMenu.d.ts +28 -0
  48. package/dist/TViewPanel/initMenu.js +79 -0
  49. package/dist/TViewPanel/initMenu.js.map +1 -0
  50. package/dist/TViewPanel/model.d.ts +1178 -383
  51. package/dist/TViewPanel/model.js +298 -87
  52. package/dist/TViewPanel/model.js.map +1 -1
  53. package/dist/TViewPanel/modifications.d.ts +25 -0
  54. package/dist/TViewPanel/modifications.js +89 -0
  55. package/dist/TViewPanel/modifications.js.map +1 -0
  56. package/dist/index.d.ts +0 -1
  57. package/dist/index.js +0 -1
  58. package/dist/index.js.map +1 -1
  59. package/dist/jbrowse-plugin-tview.umd.production.min.js +125 -89
  60. package/dist/jbrowse-plugin-tview.umd.production.min.js.map +4 -4
  61. package/dist/version.d.ts +1 -1
  62. package/dist/version.js +1 -1
  63. package/package.json +21 -18
  64. package/src/AddHighlightModel/index.tsx +8 -14
  65. package/src/LaunchTView/TviewGetPlanRpc/TviewGetPlan.ts +119 -157
  66. package/src/LaunchTView/TviewGetPlanRpc/index.ts +0 -1
  67. package/src/LaunchTView/align.ts +233 -177
  68. package/src/LaunchTView/alleles.ts +70 -4
  69. package/src/LaunchTView/fetchTviewPlan.ts +11 -54
  70. package/src/LaunchTView/index.ts +74 -16
  71. package/src/LaunchTView/launch.ts +98 -0
  72. package/src/LaunchTView/launchView.ts +28 -20
  73. package/src/LaunchTView/modifications.ts +200 -0
  74. package/src/LaunchTView/readLayout.ts +65 -41
  75. package/src/LaunchTView/repeatStats.ts +7 -7
  76. package/src/LaunchTView/repeats.ts +6 -4
  77. package/src/LaunchTView/tview.ts +228 -112
  78. package/src/TViewPanel/components/TViewPanel.tsx +33 -4
  79. package/src/TViewPanel/genome.ts +41 -0
  80. package/src/TViewPanel/init.ts +104 -51
  81. package/src/TViewPanel/initMenu.ts +126 -0
  82. package/src/TViewPanel/model.ts +350 -97
  83. package/src/TViewPanel/modifications.ts +127 -0
  84. package/src/index.ts +0 -2
  85. package/src/version.ts +1 -1
  86. package/dist/AddHighlightModel/GenomeMouseoverHighlight.d.ts +0 -6
  87. package/dist/AddHighlightModel/GenomeMouseoverHighlight.js +0 -23
  88. package/dist/AddHighlightModel/GenomeMouseoverHighlight.js.map +0 -1
  89. package/dist/AddHighlightModel/HighlightComponents.d.ts +0 -7
  90. package/dist/AddHighlightModel/HighlightComponents.js +0 -11
  91. package/dist/AddHighlightModel/HighlightComponents.js.map +0 -1
  92. package/dist/LaunchTView/components/ArrayReport.d.ts +0 -10
  93. package/dist/LaunchTView/components/ArrayReport.js +0 -59
  94. package/dist/LaunchTView/components/ArrayReport.js.map +0 -1
  95. package/dist/LaunchTView/components/LaunchTViewDialog.d.ts +0 -6
  96. package/dist/LaunchTView/components/LaunchTViewDialog.js +0 -124
  97. package/dist/LaunchTView/components/LaunchTViewDialog.js.map +0 -1
  98. package/dist/LaunchTView/sampleTree.d.ts +0 -37
  99. package/dist/LaunchTView/sampleTree.js +0 -65
  100. package/dist/LaunchTView/sampleTree.js.map +0 -1
  101. package/dist/LaunchTView/useTviewMsa.d.ts +0 -18
  102. package/dist/LaunchTView/useTviewMsa.js +0 -24
  103. package/dist/LaunchTView/useTviewMsa.js.map +0 -1
  104. package/dist/TViewPanel/coords.d.ts +0 -25
  105. package/dist/TViewPanel/coords.js +0 -56
  106. package/dist/TViewPanel/coords.js.map +0 -1
  107. package/src/AddHighlightModel/GenomeMouseoverHighlight.tsx +0 -46
  108. package/src/AddHighlightModel/HighlightComponents.tsx +0 -25
  109. package/src/LaunchTView/components/ArrayReport.tsx +0 -64
  110. package/src/LaunchTView/components/LaunchTViewDialog.tsx +0 -212
  111. package/src/LaunchTView/sampleTree.ts +0 -73
  112. package/src/LaunchTView/useTviewMsa.ts +0 -39
  113. package/src/TViewPanel/coords.ts +0 -65
@@ -1,10 +1,10 @@
1
- import { newickSafe } from './sampleTree'
2
-
3
1
  /** the subset of an @jbrowse/core Feature that the layout reads */
4
2
  export interface AlignmentFeature {
5
- get: (key: string) => any
3
+ get: (key: string) => unknown
6
4
  }
7
5
 
6
+ const text = (value: unknown) => (typeof value === 'string' ? value : '')
7
+
8
8
  /**
9
9
  * Only '-' and '.' are treated as gaps by react-msaview (anything else counts
10
10
  * as a base in the conservation track and column stats), so the two of them
@@ -17,32 +17,36 @@ export interface AlignmentFeature {
17
17
  */
18
18
  export const SPANNED_GAP = '-'
19
19
  export const ABSENT = '.'
20
+ export const GAPS = /[-.]/g
20
21
 
21
22
  export interface ReadLayout {
22
23
  /**
23
- * Identity. Every per-row lookup is keyed by this — the array blocks, the
24
- * tree — so it is fixed once, here, and nothing downstream rewrites it.
24
+ * Identity, and the row's name in the alignment. Every per-row lookup is
25
+ * keyed by this — the array blocks, the row table — so it is fixed once,
26
+ * here, and nothing downstream rewrites it.
25
27
  */
26
28
  name: string
27
- /**
28
- * What the row is called on screen, when that is not just its name. Kept
29
- * apart from `name` because the copy-number label is added after the array
30
- * blocks are built and keyed: renaming in place instead left every labelled
31
- * row unable to find its own block, and the block fell back to gaps — a row
32
- * that silently loses its sequence while keeping its label.
33
- */
34
- label?: string
35
- /**
36
- * The sample the row belongs to, when several were loaded at once. Already
37
- * Newick-safe, and the same string the row's own name is prefixed with, so
38
- * the clade label and its leaves agree.
39
- */
29
+ /** the sample the row belongs to, as the caller named it */
40
30
  sample?: string
41
31
  start: number
42
32
  /** one char per reference position consumed: the aligned base, or a gap */
43
33
  refChars: string
44
34
  /** reference position -> bases inserted immediately before that position */
45
35
  insertions: Map<number, string>
36
+ /** where the row's bases came from in the read, for a row parsed from one */
37
+ query?: ReadQuery
38
+ }
39
+
40
+ /**
41
+ * The read's own sequence, SEQ as the file stores it, and enough of its CIGAR
42
+ * to find any base of the row in it: the query index of the first base at
43
+ * `start`, and how many bases the aligner inserted before each position. Kept
44
+ * as parsed, since the layout later moves and drops insertions.
45
+ */
46
+ export interface ReadQuery {
47
+ seq: string
48
+ offset: number
49
+ inserted: Map<number, number>
46
50
  }
47
51
 
48
52
  const cigarRegex = /([MIDNSHPX=])/
@@ -58,16 +62,21 @@ export function parseRead(
58
62
  name: string,
59
63
  sample?: string,
60
64
  ): ReadLayout {
61
- const seq = feature.get('seq') as string
62
- const ops = parseCigar(feature.get('CIGAR') as string)
63
- const start = feature.get('start')
65
+ const seq = text(feature.get('seq'))
66
+ const ops = parseCigar(text(feature.get('CIGAR')))
67
+ const start = Number(feature.get('start'))
64
68
  const insertions = new Map<number, string>()
69
+ const inserted = new Map<number, number>()
65
70
  let refChars = ''
66
71
  let seqOffset = 0
72
+ let offset: number | undefined
67
73
 
68
74
  for (let i = 0; i < ops.length; i += 2) {
69
75
  const len = +ops[i]!
70
76
  const op = ops[i + 1]!
77
+ if (op !== 'S' && op !== 'H' && op !== 'P') {
78
+ offset ??= seqOffset
79
+ }
71
80
  if (op === 'S') {
72
81
  seqOffset += len
73
82
  } else if (op === 'I') {
@@ -75,6 +84,7 @@ export function parseRead(
75
84
  const pos = start + refChars.length
76
85
  const prev = insertions.get(pos) ?? ''
77
86
  insertions.set(pos, prev + seq.slice(seqOffset, seqOffset + len))
87
+ inserted.set(pos, (inserted.get(pos) ?? 0) + len)
78
88
  seqOffset += len
79
89
  } else if (op === 'D') {
80
90
  refChars += SPANNED_GAP.repeat(len)
@@ -86,17 +96,33 @@ export function parseRead(
86
96
  }
87
97
  // H and P consume neither the read sequence nor the reference
88
98
  }
89
- // sanitized here, the same way getReadNames sanitizes the prefix it builds
90
- // from it, so the clade label and the leaves inside it are one string
91
99
  return {
92
100
  name,
93
- sample: sample === undefined ? undefined : newickSafe(sample),
101
+ sample,
94
102
  start,
95
103
  refChars,
96
104
  insertions,
105
+ query: { seq, offset: offset ?? seqOffset, inserted },
97
106
  }
98
107
  }
99
108
 
109
+ /**
110
+ * The query index of the first base a parsed read has at or after reference
111
+ * position `pos`, counting what it inserted at `pos` itself as coming first.
112
+ */
113
+ export function queryIndexAt(read: ReadLayout, query: ReadQuery, pos: number) {
114
+ const aligned = read.refChars
115
+ .slice(0, Math.max(0, pos - read.start))
116
+ .replaceAll(GAPS, '').length
117
+ let insertedBefore = 0
118
+ for (const [at, len] of query.inserted) {
119
+ if (at < pos) {
120
+ insertedBefore += len
121
+ }
122
+ }
123
+ return query.offset + aligned + insertedBefore
124
+ }
125
+
100
126
  /**
101
127
  * Reads are fetched by overlap, so they carry insertions on either side of the
102
128
  * region. Those get no columns and are never rendered, so dropping them keeps
@@ -120,26 +146,24 @@ function mateSuffix(flags: number | undefined) {
120
146
  }
121
147
 
122
148
  /**
123
- * MSA rows are keyed by name, so mates and duplicate names need disambiguating.
124
- * With several samples loaded at once the same read name can also arrive twice
125
- * from two files, which the sample prefix separates before the counter has to.
126
- *
127
- * The name is made Newick-safe here rather than where the tree is written,
128
- * because the tree names the same rows the FASTA does and the two have to agree
129
- * exactly. '|' separates the parts because ':' opens a branch length.
149
+ * MSA rows are keyed by name, and a FASTA defline names its row by the first
150
+ * whitespace-delimited word, dropping any later row whose word repeats. So a
151
+ * name loses its whitespace, mates are told apart by flag, and whatever still
152
+ * collides (the same read in two files, or a name in `reserved`) is numbered.
130
153
  */
131
154
  export function getReadNames(
132
155
  features: AlignmentFeature[],
133
- sampleOf?: (index: number) => string | undefined,
156
+ reserved: string[] = [],
134
157
  ) {
135
- const counts = new Map<string, number>()
136
- return features.map((f, i) => {
137
- const sample = sampleOf?.(i)
138
- const prefix = sample ? `${newickSafe(sample)}|` : ''
139
- const name = newickSafe(String(f.get('name')))
140
- const base = `${prefix}${name}${mateSuffix(f.get('flags'))}`
141
- const n = (counts.get(base) ?? 0) + 1
142
- counts.set(base, n)
143
- return n === 1 ? base : `${base}_${n}`
158
+ const taken = new Set(reserved)
159
+ return features.map(f => {
160
+ const flags = f.get('flags')
161
+ const base = `${String(f.get('name')).replaceAll(/\s+/g, '_')}${mateSuffix(typeof flags === 'number' ? flags : undefined)}`
162
+ let name = base
163
+ for (let n = 2; taken.has(name); n++) {
164
+ name = `${base}_${n}`
165
+ }
166
+ taken.add(name)
167
+ return name
144
168
  })
145
169
  }
@@ -133,14 +133,16 @@ export interface ArrayStats {
133
133
  width: number
134
134
  /** the reference's own copy count, when the reference was a row */
135
135
  referenceCopies?: number
136
- /** whether the rows were ordered and labelled by this array */
136
+ /** whether the rows were ordered by this array */
137
137
  subject: boolean
138
138
  samples: SampleArrayStats[]
139
139
  }
140
140
 
141
- function sampleOfRow(name: string) {
142
- const i = name.indexOf('|')
143
- return i < 0 ? undefined : name.slice(0, i)
141
+ function rowsOfSample(array: ArrayBlock, plan: TviewPlan, sample: string) {
142
+ return plan.reads
143
+ .filter(read => read.sample === sample)
144
+ .map(read => array.copiesByName.get(read.name))
145
+ .filter(copies => copies !== undefined)
144
146
  }
145
147
 
146
148
  function arrayStats(
@@ -160,9 +162,7 @@ function arrayStats(
160
162
  : undefined,
161
163
  subject: array === plan.subject,
162
164
  samples: samples.map(sample => {
163
- const copies = [...array.copiesByName]
164
- .filter(([name]) => sampleOfRow(name) === sample)
165
- .map(([, n]) => n)
165
+ const copies = rowsOfSample(array, plan, sample)
166
166
  const modes = alleleModes(copies)
167
167
  return {
168
168
  sample,
@@ -194,14 +194,16 @@ const RIVAL_SPAN_FRACTION = 0.5
194
194
  */
195
195
  export function mergeArrays(arrays: ReferenceArray[], gap = 0) {
196
196
  const groups: ReferenceArray[][] = []
197
+ // how far right the open group reaches, which is not its last member's end:
198
+ // sorting by start lets a long array be followed by one that ends inside it
199
+ let reach = -Infinity
197
200
  for (const array of [...arrays].sort((a, b) => a.start - b.start)) {
198
- const last = groups.at(-1)
199
- const end = last ? Math.max(...last.map(a => a.end)) : 0
200
- if (last && array.start <= end + gap) {
201
- last.push(array)
201
+ if (array.start <= reach + gap) {
202
+ groups.at(-1)!.push(array)
202
203
  } else {
203
204
  groups.push([array])
204
205
  }
206
+ reach = Math.max(reach, array.end)
205
207
  }
206
208
  return groups.map(group => {
207
209
  const span = (a: ReferenceArray) => a.end - a.start