jbrowse-plugin-tview 2.2.3 → 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.
- package/README.md +317 -105
- package/dist/AddHighlightModel/index.js +2 -13
- package/dist/AddHighlightModel/index.js.map +1 -1
- package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.d.ts +44 -64
- package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js +57 -81
- package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js.map +1 -1
- package/dist/LaunchTView/TviewGetPlanRpc/index.d.ts +1 -1
- package/dist/LaunchTView/align.d.ts +11 -17
- package/dist/LaunchTView/align.js +190 -150
- package/dist/LaunchTView/align.js.map +1 -1
- package/dist/LaunchTView/alleles.d.ts +14 -0
- package/dist/LaunchTView/alleles.js +47 -0
- package/dist/LaunchTView/alleles.js.map +1 -1
- package/dist/LaunchTView/fetchTviewPlan.d.ts +5 -37
- package/dist/LaunchTView/fetchTviewPlan.js +7 -28
- package/dist/LaunchTView/fetchTviewPlan.js.map +1 -1
- package/dist/LaunchTView/index.js +45 -16
- package/dist/LaunchTView/index.js.map +1 -1
- package/dist/LaunchTView/launch.d.ts +50 -0
- package/dist/LaunchTView/launch.js +48 -0
- package/dist/LaunchTView/launch.js.map +1 -0
- package/dist/LaunchTView/launchView.d.ts +3 -8
- package/dist/LaunchTView/launchView.js +18 -12
- package/dist/LaunchTView/launchView.js.map +1 -1
- package/dist/LaunchTView/modifications.d.ts +64 -0
- package/dist/LaunchTView/modifications.js +131 -0
- package/dist/LaunchTView/modifications.js.map +1 -0
- package/dist/LaunchTView/readLayout.d.ts +35 -39
- package/dist/LaunchTView/readLayout.js +44 -24
- package/dist/LaunchTView/readLayout.js.map +1 -1
- package/dist/LaunchTView/repeatStats.d.ts +1 -1
- package/dist/LaunchTView/repeatStats.js +6 -21
- package/dist/LaunchTView/repeatStats.js.map +1 -1
- package/dist/LaunchTView/tview.d.ts +34 -23
- package/dist/LaunchTView/tview.js +127 -78
- package/dist/LaunchTView/tview.js.map +1 -1
- package/dist/TViewPanel/components/TViewPanel.js +9 -4
- package/dist/TViewPanel/components/TViewPanel.js.map +1 -1
- package/dist/TViewPanel/genome.d.ts +11 -0
- package/dist/TViewPanel/genome.js +32 -0
- package/dist/TViewPanel/genome.js.map +1 -0
- package/dist/TViewPanel/init.d.ts +38 -19
- package/dist/TViewPanel/init.js +66 -39
- package/dist/TViewPanel/init.js.map +1 -1
- package/dist/TViewPanel/initMenu.d.ts +28 -0
- package/dist/TViewPanel/initMenu.js +79 -0
- package/dist/TViewPanel/initMenu.js.map +1 -0
- package/dist/TViewPanel/model.d.ts +1057 -334
- package/dist/TViewPanel/model.js +298 -87
- package/dist/TViewPanel/model.js.map +1 -1
- package/dist/TViewPanel/modifications.d.ts +25 -0
- package/dist/TViewPanel/modifications.js +89 -0
- package/dist/TViewPanel/modifications.js.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/jbrowse-plugin-tview.umd.production.min.js +125 -88
- package/dist/jbrowse-plugin-tview.umd.production.min.js.map +4 -4
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +8 -8
- package/src/AddHighlightModel/index.tsx +8 -14
- package/src/LaunchTView/TviewGetPlanRpc/TviewGetPlan.ts +119 -157
- package/src/LaunchTView/TviewGetPlanRpc/index.ts +0 -1
- package/src/LaunchTView/align.ts +225 -176
- package/src/LaunchTView/alleles.ts +66 -0
- package/src/LaunchTView/fetchTviewPlan.ts +11 -54
- package/src/LaunchTView/index.ts +74 -16
- package/src/LaunchTView/launch.ts +98 -0
- package/src/LaunchTView/launchView.ts +28 -20
- package/src/LaunchTView/modifications.ts +200 -0
- package/src/LaunchTView/readLayout.ts +65 -41
- package/src/LaunchTView/repeatStats.ts +7 -23
- package/src/LaunchTView/tview.ts +158 -91
- package/src/TViewPanel/components/TViewPanel.tsx +33 -4
- package/src/TViewPanel/genome.ts +41 -0
- package/src/TViewPanel/init.ts +104 -51
- package/src/TViewPanel/initMenu.ts +126 -0
- package/src/TViewPanel/model.ts +350 -97
- package/src/TViewPanel/modifications.ts +127 -0
- package/src/index.ts +0 -2
- package/src/version.ts +1 -1
- package/dist/AddHighlightModel/GenomeMouseoverHighlight.d.ts +0 -6
- package/dist/AddHighlightModel/GenomeMouseoverHighlight.js +0 -23
- package/dist/AddHighlightModel/GenomeMouseoverHighlight.js.map +0 -1
- package/dist/AddHighlightModel/HighlightComponents.d.ts +0 -7
- package/dist/AddHighlightModel/HighlightComponents.js +0 -11
- package/dist/AddHighlightModel/HighlightComponents.js.map +0 -1
- package/dist/LaunchTView/components/ArrayReport.d.ts +0 -10
- package/dist/LaunchTView/components/ArrayReport.js +0 -59
- package/dist/LaunchTView/components/ArrayReport.js.map +0 -1
- package/dist/LaunchTView/components/LaunchTViewDialog.d.ts +0 -6
- package/dist/LaunchTView/components/LaunchTViewDialog.js +0 -124
- package/dist/LaunchTView/components/LaunchTViewDialog.js.map +0 -1
- package/dist/LaunchTView/sampleTree.d.ts +0 -37
- package/dist/LaunchTView/sampleTree.js +0 -65
- package/dist/LaunchTView/sampleTree.js.map +0 -1
- package/dist/LaunchTView/useTviewMsa.d.ts +0 -18
- package/dist/LaunchTView/useTviewMsa.js +0 -24
- package/dist/LaunchTView/useTviewMsa.js.map +0 -1
- package/dist/TViewPanel/coords.d.ts +0 -25
- package/dist/TViewPanel/coords.js +0 -56
- package/dist/TViewPanel/coords.js.map +0 -1
- package/src/AddHighlightModel/GenomeMouseoverHighlight.tsx +0 -46
- package/src/AddHighlightModel/HighlightComponents.tsx +0 -25
- package/src/LaunchTView/components/ArrayReport.tsx +0 -64
- package/src/LaunchTView/components/LaunchTViewDialog.tsx +0 -212
- package/src/LaunchTView/sampleTree.ts +0 -73
- package/src/LaunchTView/useTviewMsa.ts +0 -39
- package/src/TViewPanel/coords.ts +0 -65
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
* back counts, which is what makes it testable without the network.
|
|
9
9
|
*/
|
|
10
10
|
import { ABSENT, SPANNED_GAP } from './readLayout'
|
|
11
|
-
import { newickSafe } from './sampleTree'
|
|
12
11
|
import { renderRow } from './tview'
|
|
13
12
|
|
|
14
13
|
import type { ArrayBlock } from './alleles'
|
|
@@ -134,31 +133,16 @@ export interface ArrayStats {
|
|
|
134
133
|
width: number
|
|
135
134
|
/** the reference's own copy count, when the reference was a row */
|
|
136
135
|
referenceCopies?: number
|
|
137
|
-
/** whether the rows were ordered
|
|
136
|
+
/** whether the rows were ordered by this array */
|
|
138
137
|
subject: boolean
|
|
139
138
|
samples: SampleArrayStats[]
|
|
140
139
|
}
|
|
141
140
|
|
|
142
|
-
function
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Rows are prefixed with the Newick-safe form of their sample's name, so a
|
|
149
|
-
* caller naming the samples it wants reported has to be met there rather than
|
|
150
|
-
* taken at its word. A track called `HG004 (mother)` otherwise matched no row
|
|
151
|
-
* and came back with nought spanning reads and no alleles — which is what a
|
|
152
|
-
* locus nobody sequenced looks like, not what a misspelt name should.
|
|
153
|
-
*
|
|
154
|
-
* The name the caller gave is what gets reported back; only the matching is
|
|
155
|
-
* sanitized.
|
|
156
|
-
*/
|
|
157
|
-
function rowsOfSample(copiesByName: Map<string, number>, sample: string) {
|
|
158
|
-
const key = newickSafe(sample)
|
|
159
|
-
return [...copiesByName]
|
|
160
|
-
.filter(([name]) => sampleOfRow(name) === key)
|
|
161
|
-
.map(([, n]) => n)
|
|
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)
|
|
162
146
|
}
|
|
163
147
|
|
|
164
148
|
function arrayStats(
|
|
@@ -178,7 +162,7 @@ function arrayStats(
|
|
|
178
162
|
: undefined,
|
|
179
163
|
subject: array === plan.subject,
|
|
180
164
|
samples: samples.map(sample => {
|
|
181
|
-
const copies = rowsOfSample(array
|
|
165
|
+
const copies = rowsOfSample(array, plan, sample)
|
|
182
166
|
const modes = alleleModes(copies)
|
|
183
167
|
return {
|
|
184
168
|
sample,
|
package/src/LaunchTView/tview.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { alignInsertionSite } from './align'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
buildArrayBlocks,
|
|
4
|
+
normalizeInsertions,
|
|
5
|
+
reanchorInsertions,
|
|
6
|
+
} from './alleles'
|
|
3
7
|
import {
|
|
4
8
|
ABSENT,
|
|
5
9
|
SPANNED_GAP,
|
|
@@ -9,10 +13,10 @@ import {
|
|
|
9
13
|
parseRead,
|
|
10
14
|
} from './readLayout'
|
|
11
15
|
import { findReferenceArrays, mergeArrays } from './repeats'
|
|
12
|
-
import { newickSafe } from './sampleTree'
|
|
13
16
|
|
|
14
17
|
import type { ArrayBlock } from './alleles'
|
|
15
18
|
import type { AlignmentFeature, ReadLayout } from './readLayout'
|
|
19
|
+
import type { Highlight, RowDataInput } from 'react-msaview'
|
|
16
20
|
|
|
17
21
|
export type { AlignmentFeature, ReadLayout } from './readLayout'
|
|
18
22
|
export { clipInsertions, parseCigar, parseRead } from './readLayout'
|
|
@@ -168,6 +172,26 @@ function arrayContaining(arrays: ArrayBlock[], pos: number) {
|
|
|
168
172
|
return arrays.find(a => a.start < pos && pos < a.end)
|
|
169
173
|
}
|
|
170
174
|
|
|
175
|
+
/**
|
|
176
|
+
* The reference positions a row is walked over, from inclusive to exclusive. A
|
|
177
|
+
* trailing insertion is keyed one past the last position the read spans, so
|
|
178
|
+
* that slot is in it.
|
|
179
|
+
*/
|
|
180
|
+
export function rowSpan(read: ReadLayout, layout: ColumnLayout) {
|
|
181
|
+
const { start, end, arrays } = layout
|
|
182
|
+
const lastPos = read.start + read.refChars.length
|
|
183
|
+
const from = Math.max(start, read.start)
|
|
184
|
+
const to = Math.min(end, lastPos + 1)
|
|
185
|
+
// a row that begins or ends inside an array does not span it, so it gets no
|
|
186
|
+
// block; snapping past the block keeps the walk from emitting part of one
|
|
187
|
+
const startsInside = arrayContaining(arrays, from)
|
|
188
|
+
const endsInside = arrayContaining(arrays, to)
|
|
189
|
+
return {
|
|
190
|
+
from: startsInside ? Math.min(end, startsInside.end) : from,
|
|
191
|
+
to: endsInside ? endsInside.start : to,
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
171
195
|
/**
|
|
172
196
|
* Lays a single row out across the whole region. Rows are typically a tiny
|
|
173
197
|
* fraction of the region, so the columns on either side are filled in one shot
|
|
@@ -177,22 +201,10 @@ function arrayContaining(arrays: ArrayBlock[], pos: number) {
|
|
|
177
201
|
* `refChars` between them.
|
|
178
202
|
*/
|
|
179
203
|
export function renderRow(read: ReadLayout, layout: ColumnLayout) {
|
|
180
|
-
const { start,
|
|
204
|
+
const { start, insWidths, offsets, totalColumns } = layout
|
|
181
205
|
const { arrayByStart, breaks } = layout
|
|
182
|
-
// a trailing insertion is keyed one past the last position the read spans
|
|
183
206
|
const lastPos = read.start + read.refChars.length
|
|
184
|
-
|
|
185
|
-
let to = Math.min(end, lastPos + 1)
|
|
186
|
-
// a row that begins or ends inside an array does not span it, so it gets no
|
|
187
|
-
// block; snapping past the block keeps the walk from emitting part of one
|
|
188
|
-
const startsInside = arrayContaining(arrays, from)
|
|
189
|
-
if (startsInside) {
|
|
190
|
-
from = Math.min(end, startsInside.end)
|
|
191
|
-
}
|
|
192
|
-
const endsInside = arrayContaining(arrays, to)
|
|
193
|
-
if (endsInside) {
|
|
194
|
-
to = endsInside.start
|
|
195
|
-
}
|
|
207
|
+
const { from, to } = rowSpan(read, layout)
|
|
196
208
|
if (to <= from) {
|
|
197
209
|
return ABSENT.repeat(totalColumns)
|
|
198
210
|
}
|
|
@@ -224,81 +236,40 @@ export function renderRow(read: ReadLayout, layout: ColumnLayout) {
|
|
|
224
236
|
continue
|
|
225
237
|
}
|
|
226
238
|
const refChar = pos < lastPos ? read.refChars[pos - read.start]! : ABSENT
|
|
227
|
-
// a read that is not really here pads the insertion the same way it pads
|
|
228
|
-
// the reference column, so an intron reads as one absence
|
|
229
|
-
const pad = refChar === ABSENT ? ABSENT : SPANNED_GAP
|
|
230
239
|
const ins = read.insertions.get(pos) ?? ''
|
|
240
|
+
// insertion columns sit between pos - 1 and pos, so a read that is absent
|
|
241
|
+
// on either side, an intron or its own first base, does not span them
|
|
242
|
+
const spans =
|
|
243
|
+
ins !== '' ||
|
|
244
|
+
(refChar !== ABSENT &&
|
|
245
|
+
pos > read.start &&
|
|
246
|
+
read.refChars[pos - read.start - 1] !== ABSENT)
|
|
247
|
+
const pad = spans ? SPANNED_GAP : ABSENT
|
|
231
248
|
ret += ins + pad.repeat(insWidths.get(pos)! - ins.length) + refChar
|
|
232
249
|
pos++
|
|
233
250
|
}
|
|
234
251
|
return ret + ABSENT.repeat(totalColumns - offsets[to - start]!)
|
|
235
252
|
}
|
|
236
253
|
|
|
237
|
-
// A defline is truncated at its first space (msa-parsers' FastaMSA), so the
|
|
238
|
-
// count is joined on. `labelsAlignRight` puts it flush against the sequence.
|
|
239
|
-
const COPY_COUNT_SEPARATOR = '|'
|
|
240
|
-
|
|
241
254
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
255
|
+
* The reference row's name, and the sample its row table entry gives it. Not
|
|
256
|
+
* the sequence's own name: react-msaview prints a row's residues as
|
|
257
|
+
* `<row>:<index>`, and `chr4:25` would read as a genome coordinate when it is an
|
|
258
|
+
* index into the window.
|
|
245
259
|
*/
|
|
246
|
-
export
|
|
247
|
-
return read.label ?? read.name
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Rows ordered longest-allele-first and labelled with their copy count.
|
|
252
|
-
*
|
|
253
|
-
* Copy number is the measurement at a tandem array, and reading it off where a
|
|
254
|
-
* row runs out is the thing the unit-per-block layout exists to avoid, so it is
|
|
255
|
-
* stated in the label. Ordering by it puts the alleles in a ladder, which is
|
|
256
|
-
* what makes one row's array comparable to the next at a glance. Rows that do
|
|
257
|
-
* not span the array keep genomic order, below the ones that do; the reference
|
|
258
|
-
* stays on top whatever its allele, since it is what the columns are named
|
|
259
|
-
* after rather than one of the observations.
|
|
260
|
-
*/
|
|
261
|
-
export function labelAndOrderByCopies(
|
|
262
|
-
reads: ReadLayout[],
|
|
263
|
-
array: ArrayBlock,
|
|
264
|
-
referenceName?: string,
|
|
265
|
-
) {
|
|
266
|
-
const rank = (read: ReadLayout) => (read.name === referenceName ? 1 : 0)
|
|
267
|
-
return reads
|
|
268
|
-
.map(read => ({ read, copies: array.copiesByName.get(read.name) }))
|
|
269
|
-
.sort((a, b) => {
|
|
270
|
-
const byRef = rank(b.read) - rank(a.read)
|
|
271
|
-
if (byRef) {
|
|
272
|
-
return byRef
|
|
273
|
-
}
|
|
274
|
-
if (a.copies === undefined || b.copies === undefined) {
|
|
275
|
-
return a.copies === b.copies
|
|
276
|
-
? byStart(a.read, b.read)
|
|
277
|
-
: a.copies === undefined
|
|
278
|
-
? 1
|
|
279
|
-
: -1
|
|
280
|
-
}
|
|
281
|
-
return b.copies - a.copies || byStart(a.read, b.read)
|
|
282
|
-
})
|
|
283
|
-
.map(({ read, copies }) =>
|
|
284
|
-
copies === undefined
|
|
285
|
-
? read
|
|
286
|
-
: { ...read, label: `${read.name}${COPY_COUNT_SEPARATOR}n=${copies}` },
|
|
287
|
-
)
|
|
288
|
-
}
|
|
260
|
+
export const REFERENCE = 'reference'
|
|
289
261
|
|
|
290
262
|
export interface TviewPlan {
|
|
291
263
|
reads: ReadLayout[]
|
|
264
|
+
/** the row each feature became, parallel to the features planned */
|
|
265
|
+
featureNames: string[]
|
|
292
266
|
layout: ColumnLayout
|
|
293
|
-
insertionWidths: [number, number][]
|
|
294
|
-
/** [start, end, width] per array block, to rebuild the column mapping */
|
|
295
|
-
arraySpans: [number, number, number][]
|
|
296
267
|
region: { refName: string; start: number; end: number }
|
|
297
268
|
/** rows x columns, i.e. how big the alignment renderTviewMsa builds will be */
|
|
298
269
|
cellCount: number
|
|
299
270
|
/** tandem arrays the reference carries here, left to right */
|
|
300
271
|
arrays: ArrayBlock[]
|
|
301
|
-
/** the array the rows are ordered
|
|
272
|
+
/** the array the rows are ordered by, when there is one */
|
|
302
273
|
subject?: ArrayBlock
|
|
303
274
|
/** name of the reference row, when the reference sequence was available */
|
|
304
275
|
referenceName?: string
|
|
@@ -336,7 +307,7 @@ export function planTviewMsa({
|
|
|
336
307
|
sequence,
|
|
337
308
|
sampleOf,
|
|
338
309
|
}: PlanArgs): TviewPlan {
|
|
339
|
-
const names = getReadNames(features,
|
|
310
|
+
const names = getReadNames(features, [REFERENCE])
|
|
340
311
|
const parsed = features
|
|
341
312
|
.map((f, i) => parseRead(f, names[i]!, sampleOf?.(i)))
|
|
342
313
|
.sort(byStart)
|
|
@@ -344,10 +315,11 @@ export function planTviewMsa({
|
|
|
344
315
|
// The reference is laid out as a row like any other, which is what lets it be
|
|
345
316
|
// one of the alleles an array block squares up rather than a thing the block
|
|
346
317
|
// has to be compared against afterwards.
|
|
347
|
-
const referenceName = sequence ?
|
|
318
|
+
const referenceName = sequence ? REFERENCE : undefined
|
|
348
319
|
const referenceRead: ReadLayout | undefined = sequence
|
|
349
320
|
? {
|
|
350
|
-
name:
|
|
321
|
+
name: REFERENCE,
|
|
322
|
+
sample: REFERENCE,
|
|
351
323
|
start,
|
|
352
324
|
refChars: sequence.toUpperCase(),
|
|
353
325
|
insertions: new Map(),
|
|
@@ -368,7 +340,11 @@ export function planTviewMsa({
|
|
|
368
340
|
const intervals = sequence
|
|
369
341
|
? mergeArrays(findReferenceArrays(sequence, start))
|
|
370
342
|
: []
|
|
371
|
-
const anchored =
|
|
343
|
+
const anchored = normalizeInsertions(
|
|
344
|
+
reanchorInsertions(clipped, intervals),
|
|
345
|
+
intervals,
|
|
346
|
+
start,
|
|
347
|
+
)
|
|
372
348
|
const arrays = buildArrayBlocks(anchored, intervals)
|
|
373
349
|
const ownedByArray = new Set<number>()
|
|
374
350
|
for (const array of arrays) {
|
|
@@ -387,39 +363,130 @@ export function planTviewMsa({
|
|
|
387
363
|
best && alleleSpread(best) >= alleleSpread(array) ? best : array,
|
|
388
364
|
undefined,
|
|
389
365
|
)
|
|
390
|
-
const
|
|
391
|
-
? labelAndOrderByCopies(aligned, subject, referenceName)
|
|
392
|
-
: aligned
|
|
393
|
-
|
|
394
|
-
const insWidths = maxInsertionWidths(reads, ownedByArray)
|
|
366
|
+
const insWidths = maxInsertionWidths(aligned, ownedByArray)
|
|
395
367
|
const layout = buildColumnLayout(start, end, insWidths, arrays)
|
|
396
368
|
|
|
397
369
|
return {
|
|
398
|
-
reads,
|
|
370
|
+
reads: aligned,
|
|
371
|
+
featureNames: names,
|
|
399
372
|
layout,
|
|
400
|
-
insertionWidths: [...insWidths.entries()].sort((a, b) => a[0] - b[0]),
|
|
401
|
-
arraySpans: arrays.map(a => [a.start, a.end, a.width]),
|
|
402
373
|
region: { refName, start, end },
|
|
403
|
-
cellCount:
|
|
374
|
+
cellCount: aligned.length * layout.totalColumns,
|
|
404
375
|
arrays,
|
|
405
376
|
subject,
|
|
406
377
|
referenceName,
|
|
407
378
|
samples: [
|
|
408
|
-
...new Set(
|
|
379
|
+
...new Set(
|
|
380
|
+
features.map((_, i) => sampleOf?.(i)).filter(s => s !== undefined),
|
|
381
|
+
),
|
|
409
382
|
],
|
|
410
383
|
}
|
|
411
384
|
}
|
|
412
385
|
|
|
413
|
-
export function
|
|
414
|
-
return reads.map(r =>
|
|
386
|
+
export function renderRows({ reads, layout }: TviewPlan) {
|
|
387
|
+
return reads.map(r => renderRow(r, layout))
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export function toFasta({ reads }: TviewPlan, rows: string[]) {
|
|
391
|
+
return reads.map((r, i) => `>${r.name}\n${rows[i]}\n`).join('')
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export function renderTviewMsa(plan: TviewPlan) {
|
|
395
|
+
return toFasta(plan, renderRows(plan))
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* What each row is, keyed by its name: the sample it came from and, where there
|
|
400
|
+
* is a subject array, the copies it carries there and their length in bp.
|
|
401
|
+
*/
|
|
402
|
+
export function planRowData({ reads, subject }: TviewPlan): RowDataInput {
|
|
403
|
+
return Object.fromEntries(
|
|
404
|
+
reads.map(({ name, sample }) => {
|
|
405
|
+
const copies = subject?.copiesByName.get(name)
|
|
406
|
+
const alleleBp = subject?.lengthByName.get(name)
|
|
407
|
+
return [
|
|
408
|
+
name,
|
|
409
|
+
{
|
|
410
|
+
...(sample === undefined ? {} : { sample }),
|
|
411
|
+
...(copies === undefined ? {} : { copies }),
|
|
412
|
+
...(alleleBp === undefined ? {} : { alleleBp }),
|
|
413
|
+
},
|
|
414
|
+
]
|
|
415
|
+
}),
|
|
416
|
+
)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** the value most of `values` share, the larger of two equally common */
|
|
420
|
+
function commonest(values: number[]) {
|
|
421
|
+
const counts = new Map<number, number>()
|
|
422
|
+
for (const value of values) {
|
|
423
|
+
counts.set(value, (counts.get(value) ?? 0) + 1)
|
|
424
|
+
}
|
|
425
|
+
return [...counts].sort((a, b) => b[1] - a[1] || b[0] - a[0])[0]![0]
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// A bracket is kept in the session and drawn in one strip, and a noisy window
|
|
429
|
+
// has an insertion site every few bases, so only the sites carrying the most
|
|
430
|
+
// inserted sequence are marked.
|
|
431
|
+
const MAX_INSERTION_BRACKETS = 100
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* A bracket over the runs of columns that are not one reference base: the
|
|
435
|
+
* insertion sites, and each array block. Columns are the file's, 1-based.
|
|
436
|
+
*/
|
|
437
|
+
export function planHighlights({
|
|
438
|
+
reads,
|
|
439
|
+
layout,
|
|
440
|
+
region,
|
|
441
|
+
referenceName,
|
|
442
|
+
}: TviewPlan): Highlight[] {
|
|
443
|
+
const { start, offsets, insWidths, arrays } = layout
|
|
444
|
+
const n = (x: number) => x.toLocaleString('en-US')
|
|
445
|
+
const column = (pos: number) => offsets[pos - start]! + 1
|
|
446
|
+
const lengthsAt = new Map<number, number[]>()
|
|
447
|
+
for (const read of reads) {
|
|
448
|
+
for (const [pos, ins] of read.insertions) {
|
|
449
|
+
const lengths = lengthsAt.get(pos) ?? []
|
|
450
|
+
lengths.push(ins.replaceAll(SPANNED_GAP, '').length)
|
|
451
|
+
lengthsAt.set(pos, lengths)
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const insertedBases = (pos: number) =>
|
|
455
|
+
(lengthsAt.get(pos) ?? []).reduce((a, b) => a + b, 0)
|
|
456
|
+
const insertions = [...insWidths]
|
|
457
|
+
.filter(([, width]) => width > 0)
|
|
458
|
+
.sort((a, b) => insertedBases(b[0]) - insertedBases(a[0]) || a[0] - b[0])
|
|
459
|
+
.slice(0, MAX_INSERTION_BRACKETS)
|
|
460
|
+
.map(([pos, width]) => ({
|
|
461
|
+
start: column(pos),
|
|
462
|
+
end: column(pos) + width - 1,
|
|
463
|
+
label: `+${commonest(lengthsAt.get(pos) ?? [width])}bp after ${region.refName}:${n(pos)}`,
|
|
464
|
+
}))
|
|
465
|
+
const blocks = arrays.map(array => {
|
|
466
|
+
const copies = referenceName
|
|
467
|
+
? array.copiesByName.get(referenceName)
|
|
468
|
+
: undefined
|
|
469
|
+
return {
|
|
470
|
+
start: column(array.start),
|
|
471
|
+
end: column(array.start) + array.width - 1,
|
|
472
|
+
label:
|
|
473
|
+
copies === undefined
|
|
474
|
+
? `(${array.unit})n`
|
|
475
|
+
: `(${array.unit})x${copies} in the reference`,
|
|
476
|
+
}
|
|
477
|
+
})
|
|
478
|
+
return [...insertions, ...blocks]
|
|
479
|
+
.sort((a, b) => a.start - b.start)
|
|
480
|
+
.map((h): Highlight => ({ ...h, mark: 'bracket' }))
|
|
415
481
|
}
|
|
416
482
|
|
|
417
483
|
export function buildTviewMsa(args: PlanArgs) {
|
|
418
484
|
const plan = planTviewMsa(args)
|
|
419
485
|
return {
|
|
420
486
|
msa: renderTviewMsa(plan),
|
|
421
|
-
|
|
422
|
-
|
|
487
|
+
rowData: planRowData(plan),
|
|
488
|
+
highlights: planHighlights(plan),
|
|
489
|
+
insertionWidths: [...plan.layout.insWidths].sort((a, b) => a[0] - b[0]),
|
|
423
490
|
region: plan.region,
|
|
424
491
|
arrays: plan.arrays,
|
|
425
492
|
subject: plan.subject,
|
|
@@ -1,20 +1,49 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
2
|
|
|
3
|
+
import { ErrorMessage, LoadingEllipses } from '@jbrowse/core/ui'
|
|
4
|
+
import { Button, Typography } from '@mui/material'
|
|
3
5
|
import { observer } from 'mobx-react'
|
|
4
6
|
import { MSAView } from 'react-msaview'
|
|
5
7
|
|
|
6
8
|
import type { JBrowsePluginTViewModel } from '../model'
|
|
7
9
|
import type { MsaViewModel } from 'react-msaview'
|
|
8
10
|
|
|
11
|
+
const padded = { padding: 20 }
|
|
12
|
+
|
|
9
13
|
const TViewPanel = observer(function TViewPanel2({
|
|
10
14
|
model,
|
|
11
15
|
}: {
|
|
12
16
|
model: JBrowsePluginTViewModel
|
|
13
17
|
}) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
const state = model.viewState
|
|
19
|
+
return state.kind === 'alignment' ? (
|
|
20
|
+
// react-msaview types its model prop with type: 'MsaView' baked in, but a
|
|
21
|
+
// JBrowse view model's `type` must equal its registered view type name.
|
|
22
|
+
// That literal is the only difference, and the MSA renderer never reads it.
|
|
23
|
+
<MSAView model={model as unknown as MsaViewModel} />
|
|
24
|
+
) : (
|
|
25
|
+
<div style={padded}>
|
|
26
|
+
{state.kind === 'loading' ? (
|
|
27
|
+
<LoadingEllipses variant="h6" message={state.message} />
|
|
28
|
+
) : state.kind === 'notice' ? (
|
|
29
|
+
<Typography>{state.message}</Typography>
|
|
30
|
+
) : (
|
|
31
|
+
<>
|
|
32
|
+
<ErrorMessage error={state.error} />
|
|
33
|
+
{state.retry ? (
|
|
34
|
+
<Button
|
|
35
|
+
variant="contained"
|
|
36
|
+
onClick={() => {
|
|
37
|
+
model.retryLoad()
|
|
38
|
+
}}
|
|
39
|
+
>
|
|
40
|
+
Retry
|
|
41
|
+
</Button>
|
|
42
|
+
) : null}
|
|
43
|
+
</>
|
|
44
|
+
)}
|
|
45
|
+
</div>
|
|
46
|
+
)
|
|
18
47
|
})
|
|
19
48
|
|
|
20
49
|
export default TViewPanel
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface HoverPosition {
|
|
2
|
+
/** 1-based */
|
|
3
|
+
coord: number
|
|
4
|
+
refName: string
|
|
5
|
+
/** the genome view the cursor is in, where the host says */
|
|
6
|
+
viewId?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** the base a genome view's cursor is over, from `session.hovered` */
|
|
10
|
+
export function hoverPosition(hovered: unknown): HoverPosition | undefined {
|
|
11
|
+
const hover = !!hovered && typeof hovered === 'object' ? hovered : undefined
|
|
12
|
+
const pos =
|
|
13
|
+
hover && 'hoverPosition' in hover ? hover.hoverPosition : undefined
|
|
14
|
+
const viewId =
|
|
15
|
+
hover && 'viewId' in hover && typeof hover.viewId === 'string'
|
|
16
|
+
? hover.viewId
|
|
17
|
+
: undefined
|
|
18
|
+
return !!pos &&
|
|
19
|
+
typeof pos === 'object' &&
|
|
20
|
+
'coord' in pos &&
|
|
21
|
+
typeof pos.coord === 'number' &&
|
|
22
|
+
'refName' in pos &&
|
|
23
|
+
typeof pos.refName === 'string'
|
|
24
|
+
? { coord: pos.coord, refName: pos.refName, viewId }
|
|
25
|
+
: undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** how many entries of an ascending index are below `value` */
|
|
29
|
+
export function lowerBound(index: ArrayLike<number>, value: number) {
|
|
30
|
+
let lo = 0
|
|
31
|
+
let hi = index.length
|
|
32
|
+
while (lo < hi) {
|
|
33
|
+
const mid = (lo + hi) >>> 1
|
|
34
|
+
if (index[mid]! < value) {
|
|
35
|
+
lo = mid + 1
|
|
36
|
+
} else {
|
|
37
|
+
hi = mid
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return lo
|
|
41
|
+
}
|