jbrowse-plugin-tview 2.0.6 → 2.2.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 +222 -4
- package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.d.ts +132 -0
- package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js +152 -0
- package/dist/LaunchTView/TviewGetPlanRpc/TviewGetPlan.js.map +1 -0
- package/dist/LaunchTView/TviewGetPlanRpc/index.d.ts +3 -0
- package/dist/LaunchTView/TviewGetPlanRpc/index.js +5 -0
- package/dist/LaunchTView/TviewGetPlanRpc/index.js.map +1 -0
- package/dist/LaunchTView/align.d.ts +106 -9
- package/dist/LaunchTView/align.js +349 -15
- package/dist/LaunchTView/align.js.map +1 -1
- package/dist/LaunchTView/alleles.d.ts +83 -0
- package/dist/LaunchTView/alleles.js +180 -0
- package/dist/LaunchTView/alleles.js.map +1 -0
- package/dist/LaunchTView/components/ArrayReport.d.ts +10 -0
- package/dist/LaunchTView/components/ArrayReport.js +59 -0
- package/dist/LaunchTView/components/ArrayReport.js.map +1 -0
- package/dist/LaunchTView/components/LaunchTViewDialog.js +74 -37
- package/dist/LaunchTView/components/LaunchTViewDialog.js.map +1 -1
- package/dist/LaunchTView/fetchTviewPlan.d.ts +20 -13
- package/dist/LaunchTView/fetchTviewPlan.js +27 -27
- package/dist/LaunchTView/fetchTviewPlan.js.map +1 -1
- package/dist/LaunchTView/launchView.d.ts +25 -0
- package/dist/LaunchTView/launchView.js +20 -0
- package/dist/LaunchTView/launchView.js.map +1 -0
- package/dist/LaunchTView/readLayout.d.ts +85 -0
- package/dist/LaunchTView/readLayout.js +96 -0
- package/dist/LaunchTView/readLayout.js.map +1 -0
- package/dist/LaunchTView/repeatStats.d.ts +56 -0
- package/dist/LaunchTView/repeatStats.js +109 -0
- package/dist/LaunchTView/repeatStats.js.map +1 -0
- package/dist/LaunchTView/repeats.d.ts +68 -0
- package/dist/LaunchTView/repeats.js +200 -0
- package/dist/LaunchTView/repeats.js.map +1 -0
- package/dist/LaunchTView/sampleTree.d.ts +37 -0
- package/dist/LaunchTView/sampleTree.js +65 -0
- package/dist/LaunchTView/sampleTree.js.map +1 -0
- package/dist/LaunchTView/tview.d.ts +67 -44
- package/dist/LaunchTView/tview.js +193 -121
- package/dist/LaunchTView/tview.js.map +1 -1
- package/dist/LaunchTView/useTviewMsa.d.ts +13 -9
- package/dist/LaunchTView/useTviewMsa.js +15 -11
- package/dist/LaunchTView/useTviewMsa.js.map +1 -1
- package/dist/TViewPanel/coords.d.ts +9 -1
- package/dist/TViewPanel/coords.js +22 -2
- package/dist/TViewPanel/coords.js.map +1 -1
- package/dist/TViewPanel/init.d.ts +56 -0
- package/dist/TViewPanel/init.js +59 -0
- package/dist/TViewPanel/init.js.map +1 -0
- package/dist/TViewPanel/model.d.ts +109 -93
- package/dist/TViewPanel/model.js +73 -51
- package/dist/TViewPanel/model.js.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/jbrowse-plugin-tview.umd.production.min.js +25 -25
- 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 +9 -3
- package/src/LaunchTView/TviewGetPlanRpc/TviewGetPlan.ts +264 -0
- package/src/LaunchTView/TviewGetPlanRpc/index.ts +14 -0
- package/src/LaunchTView/align.ts +406 -17
- package/src/LaunchTView/alleles.ts +229 -0
- package/src/LaunchTView/components/ArrayReport.tsx +64 -0
- package/src/LaunchTView/components/LaunchTViewDialog.tsx +122 -39
- package/src/LaunchTView/fetchTviewPlan.ts +42 -42
- package/src/LaunchTView/launchView.ts +50 -0
- package/src/LaunchTView/readLayout.ts +145 -0
- package/src/LaunchTView/repeatStats.ts +170 -0
- package/src/LaunchTView/repeats.ts +218 -0
- package/src/LaunchTView/sampleTree.ts +73 -0
- package/src/LaunchTView/tview.ts +265 -157
- package/src/LaunchTView/useTviewMsa.ts +20 -17
- package/src/TViewPanel/coords.ts +23 -1
- package/src/TViewPanel/init.ts +127 -0
- package/src/TViewPanel/model.ts +82 -61
- package/src/index.ts +5 -1
- package/src/version.ts +1 -1
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a built alignment says about the arrays in it, as numbers rather than as
|
|
3
|
+
* a picture.
|
|
4
|
+
*
|
|
5
|
+
* Everything a report or a README claims about a locus is derived here, from
|
|
6
|
+
* the same plan the view renders, so a number in prose is a number something
|
|
7
|
+
* computed. Nothing in this file fetches or prints — it takes a plan and gives
|
|
8
|
+
* back counts, which is what makes it testable without the network.
|
|
9
|
+
*/
|
|
10
|
+
import { ABSENT, SPANNED_GAP } from './readLayout'
|
|
11
|
+
import { renderRow } from './tview'
|
|
12
|
+
|
|
13
|
+
import type { ArrayBlock } from './alleles'
|
|
14
|
+
import type { TviewPlan } from './tview'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Least support an allele needs before it is reported as one of a sample's,
|
|
18
|
+
* as a fraction of that sample's spanning reads.
|
|
19
|
+
*
|
|
20
|
+
* Reads scatter around an allele — slippage in the read, slippage in the
|
|
21
|
+
* aligner — so the counts either side of a real allele are populated too, and
|
|
22
|
+
* a rule that took the top two would report the shoulder of one allele as the
|
|
23
|
+
* other. A fraction of coverage rather than a distance in copies is what lets
|
|
24
|
+
* two alleles a single copy apart both be reported, which at FMR1 is the
|
|
25
|
+
* mother's genotype and at ATXN3 the father's.
|
|
26
|
+
*/
|
|
27
|
+
const MIN_ALLELE_FRACTION = 0.15
|
|
28
|
+
|
|
29
|
+
/** ...and never on one read, at any coverage. */
|
|
30
|
+
const MIN_ALLELE_READS = 2
|
|
31
|
+
|
|
32
|
+
export interface Tally {
|
|
33
|
+
value: number
|
|
34
|
+
reads: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Each distinct value with how many rows carried it, smallest value first. */
|
|
38
|
+
export function tally(values: number[]): Tally[] {
|
|
39
|
+
const counts = new Map<number, number>()
|
|
40
|
+
for (const v of values) {
|
|
41
|
+
counts.set(v, (counts.get(v) ?? 0) + 1)
|
|
42
|
+
}
|
|
43
|
+
return [...counts]
|
|
44
|
+
.map(([value, reads]) => ({ value, reads }))
|
|
45
|
+
.sort((a, b) => a.value - b.value)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The copy counts a sample carries, as the counts enough of its reads agree on.
|
|
50
|
+
*
|
|
51
|
+
* This is a report of what the reads say, not a genotype call: it does not know
|
|
52
|
+
* a ploidy, so a homozygote and a haploid locus both come back as one number,
|
|
53
|
+
* and a sample whose reads scatter can come back with none.
|
|
54
|
+
*/
|
|
55
|
+
export function alleleModes(copies: number[]): Tally[] {
|
|
56
|
+
const floor = Math.max(MIN_ALLELE_READS, MIN_ALLELE_FRACTION * copies.length)
|
|
57
|
+
return tally(copies).filter(t => t.reads >= floor)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface SampleArrayStats {
|
|
61
|
+
sample: string
|
|
62
|
+
/** rows that reached both ends of the interval, i.e. that were measured */
|
|
63
|
+
spanning: number
|
|
64
|
+
/** copy counts enough of them agreed on */
|
|
65
|
+
modes: Tally[]
|
|
66
|
+
/** every copy count seen, with its support */
|
|
67
|
+
copies: Tally[]
|
|
68
|
+
/** rows whose copy count is not one of the modes */
|
|
69
|
+
offMode: number
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ArrayStats {
|
|
73
|
+
start: number
|
|
74
|
+
end: number
|
|
75
|
+
period: number
|
|
76
|
+
unit: string
|
|
77
|
+
/** reference positions the interval covers */
|
|
78
|
+
span: number
|
|
79
|
+
/** columns the block occupies */
|
|
80
|
+
width: number
|
|
81
|
+
/** the reference's own copy count, when the reference was a row */
|
|
82
|
+
referenceCopies?: number
|
|
83
|
+
/** whether the rows were ordered and labelled by this array */
|
|
84
|
+
subject: boolean
|
|
85
|
+
samples: SampleArrayStats[]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function sampleOfRow(name: string) {
|
|
89
|
+
const i = name.indexOf('|')
|
|
90
|
+
return i < 0 ? undefined : name.slice(0, i)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function arrayStats(
|
|
94
|
+
array: ArrayBlock,
|
|
95
|
+
plan: TviewPlan,
|
|
96
|
+
samples: string[],
|
|
97
|
+
): ArrayStats {
|
|
98
|
+
return {
|
|
99
|
+
start: array.start,
|
|
100
|
+
end: array.end,
|
|
101
|
+
period: array.period,
|
|
102
|
+
unit: array.unit,
|
|
103
|
+
span: array.end - array.start,
|
|
104
|
+
width: array.width,
|
|
105
|
+
referenceCopies: plan.referenceName
|
|
106
|
+
? array.copiesByName.get(plan.referenceName)
|
|
107
|
+
: undefined,
|
|
108
|
+
subject: array === plan.subject,
|
|
109
|
+
samples: samples.map(sample => {
|
|
110
|
+
const copies = [...array.copiesByName]
|
|
111
|
+
.filter(([name]) => sampleOfRow(name) === sample)
|
|
112
|
+
.map(([, n]) => n)
|
|
113
|
+
const modes = alleleModes(copies)
|
|
114
|
+
const called = new Set(modes.map(m => m.value))
|
|
115
|
+
return {
|
|
116
|
+
sample,
|
|
117
|
+
spanning: copies.length,
|
|
118
|
+
modes,
|
|
119
|
+
copies: tally(copies),
|
|
120
|
+
offMode: copies.filter(c => !called.has(c)).length,
|
|
121
|
+
}
|
|
122
|
+
}),
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface PlanStats {
|
|
127
|
+
rows: number
|
|
128
|
+
columns: number
|
|
129
|
+
/**
|
|
130
|
+
* columns exactly one row has a base in. One read's miscalled indel costs
|
|
131
|
+
* every row a column, so this is the size of what "hide columns w/ N% gaps"
|
|
132
|
+
* takes off a figure — read error, measured rather than estimated.
|
|
133
|
+
*/
|
|
134
|
+
singleRowColumns: number
|
|
135
|
+
/** columns no row has a base in, which no setting is needed to justify */
|
|
136
|
+
emptyColumns: number
|
|
137
|
+
arrays: ArrayStats[]
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Rows, columns, and every array, from a plan the view could equally render. */
|
|
141
|
+
export function summarizePlan(plan: TviewPlan, samples?: string[]): PlanStats {
|
|
142
|
+
const rows = plan.reads.map(read => renderRow(read, plan.layout))
|
|
143
|
+
const perColumn = new Int32Array(plan.layout.totalColumns)
|
|
144
|
+
for (const row of rows) {
|
|
145
|
+
for (let col = 0; col < row.length; col++) {
|
|
146
|
+
const char = row[col]!
|
|
147
|
+
if (char !== ABSENT && char !== SPANNED_GAP) {
|
|
148
|
+
perColumn[col] = perColumn[col]! + 1
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
let singleRowColumns = 0
|
|
153
|
+
let emptyColumns = 0
|
|
154
|
+
for (const count of perColumn) {
|
|
155
|
+
if (count === 0) {
|
|
156
|
+
emptyColumns++
|
|
157
|
+
} else if (count === 1) {
|
|
158
|
+
singleRowColumns++
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
rows: plan.reads.length,
|
|
163
|
+
columns: plan.layout.totalColumns,
|
|
164
|
+
singleRowColumns,
|
|
165
|
+
emptyColumns,
|
|
166
|
+
arrays: plan.arrays.map(array =>
|
|
167
|
+
arrayStats(array, plan, samples ?? plan.samples),
|
|
168
|
+
),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finding tandem arrays in the *reference*, rather than in what the reads
|
|
3
|
+
* inserted.
|
|
4
|
+
*
|
|
5
|
+
* The insertion-anchored reading — "an array is the sequence some read inserted
|
|
6
|
+
* at one reference position" — is what the aligner happens to produce, not what
|
|
7
|
+
* is there. Measured against the GIAB trio at HTT, ATXN3 and TCF4 it fails in
|
|
8
|
+
* three ways at once:
|
|
9
|
+
*
|
|
10
|
+
* - **The array is already in the reference.** A read's insertion is only its
|
|
11
|
+
* allele's *excess* over the reference copy count, so `n=5` at HTT means five
|
|
12
|
+
* copies more than hg19's, not a five-copy allele. A contracted allele inserts
|
|
13
|
+
* nothing at all and vanishes from the site.
|
|
14
|
+
* - **One array reports as several sites.** An indel inside an array has no
|
|
15
|
+
* unique placement, so the aligner anchors different reads at different
|
|
16
|
+
* positions: ATXN3 in HG002 came back as four separate arrays at 92537353,
|
|
17
|
+
* ...354, ...358 and ...372, splitting one locus's alleles across four counts.
|
|
18
|
+
* - **A read that spans the array without an insertion is invisible**, so the
|
|
19
|
+
* reference-length allele — usually the commonest one — is never counted.
|
|
20
|
+
*
|
|
21
|
+
* Anchoring on a reference interval fixes all three: the interval is the same
|
|
22
|
+
* for every read, so an allele is "what this read has between here and here",
|
|
23
|
+
* however the aligner chose to write it, and the reference is just another
|
|
24
|
+
* allele.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
// scoring for the lag-p self-comparison below. A match fraction of 0.75 scores
|
|
28
|
+
// zero, so a real array (~85% identity between copies, plus read error on the
|
|
29
|
+
// reference's own history) accumulates and random sequence (25%) does not.
|
|
30
|
+
const LAG_MATCH = 1
|
|
31
|
+
const LAG_MISMATCH = -3
|
|
32
|
+
|
|
33
|
+
// Shortest unit worth reporting. A homopolymer is periodic at every lag and its
|
|
34
|
+
// unit is an arbitrary slice of one run, so period 1 is not a unit.
|
|
35
|
+
export const MIN_PERIOD = 2
|
|
36
|
+
|
|
37
|
+
// Longest unit scanned. Covers STRs and the common VNTR range. The scan is
|
|
38
|
+
// O(regionLength * MAX_PERIOD), which is what bounds this rather than biology.
|
|
39
|
+
export const MAX_PERIOD = 300
|
|
40
|
+
|
|
41
|
+
// Copies needed before a run counts as an array. At two the period cannot be
|
|
42
|
+
// told from the sequence alone, and two copies of anything is a duplication.
|
|
43
|
+
export const MIN_COPIES = 3
|
|
44
|
+
|
|
45
|
+
// Shortest array worth reporting, regardless of copies. Three copies of a
|
|
46
|
+
// dinucleotide is six bases, which occurs by chance every few hundred bases —
|
|
47
|
+
// a TP53 exon scanned as a control turned up two — and firing the whole
|
|
48
|
+
// unit-per-block layout on one would relabel an ordinary pileup.
|
|
49
|
+
const MIN_SPAN = 12
|
|
50
|
+
|
|
51
|
+
export interface ReferenceArray {
|
|
52
|
+
/** reference position the array starts at, inclusive */
|
|
53
|
+
start: number
|
|
54
|
+
/** reference position the array ends at, exclusive */
|
|
55
|
+
end: number
|
|
56
|
+
/** repeat unit length in bp */
|
|
57
|
+
period: number
|
|
58
|
+
/** the reference's own first copy, used as the alignment consensus */
|
|
59
|
+
unit: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Runs where `scores` stays positive on balance, each cut back to where it
|
|
64
|
+
* scored best.
|
|
65
|
+
*
|
|
66
|
+
* A run is opened at the first positive score, extended while the running total
|
|
67
|
+
* stays above zero, and closed at its peak — so the mismatching tail that ended
|
|
68
|
+
* it is not part of the array, and neither are the short mismatch stretches
|
|
69
|
+
* inside it that the total absorbed. Restarting after each close is what lets
|
|
70
|
+
* one pass find every array of a given period, which a region routinely carries
|
|
71
|
+
* several of.
|
|
72
|
+
*/
|
|
73
|
+
function positiveRuns(scores: Int32Array) {
|
|
74
|
+
const ret: { start: number; end: number }[] = []
|
|
75
|
+
let start = -1
|
|
76
|
+
let total = 0
|
|
77
|
+
let best = 0
|
|
78
|
+
let bestEnd = -1
|
|
79
|
+
const close = () => {
|
|
80
|
+
if (bestEnd > start) {
|
|
81
|
+
ret.push({ start, end: bestEnd })
|
|
82
|
+
}
|
|
83
|
+
start = -1
|
|
84
|
+
total = 0
|
|
85
|
+
best = 0
|
|
86
|
+
bestEnd = -1
|
|
87
|
+
}
|
|
88
|
+
for (let i = 0; i < scores.length; i++) {
|
|
89
|
+
if (start < 0) {
|
|
90
|
+
if (scores[i]! <= 0) {
|
|
91
|
+
continue
|
|
92
|
+
}
|
|
93
|
+
start = i
|
|
94
|
+
}
|
|
95
|
+
total += scores[i]!
|
|
96
|
+
if (total > best) {
|
|
97
|
+
best = total
|
|
98
|
+
bestEnd = i + 1
|
|
99
|
+
} else if (total <= 0) {
|
|
100
|
+
close()
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
close()
|
|
104
|
+
return ret
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Arrays of exactly `period`, as intervals into `seq`. */
|
|
108
|
+
function arraysOfPeriod(seq: string, period: number) {
|
|
109
|
+
const n = seq.length - period
|
|
110
|
+
if (n <= 0) {
|
|
111
|
+
return []
|
|
112
|
+
}
|
|
113
|
+
const scores = new Int32Array(n)
|
|
114
|
+
for (let i = 0; i < n; i++) {
|
|
115
|
+
scores[i] = seq[i] === seq[i + period] ? LAG_MATCH : LAG_MISMATCH
|
|
116
|
+
}
|
|
117
|
+
const minSpan = Math.max(MIN_SPAN, MIN_COPIES * period)
|
|
118
|
+
return positiveRuns(scores)
|
|
119
|
+
.map(s => ({
|
|
120
|
+
// a lag-p match at the last scored position still asserts p more bases
|
|
121
|
+
start: s.start,
|
|
122
|
+
end: s.end + period,
|
|
123
|
+
period,
|
|
124
|
+
}))
|
|
125
|
+
.filter(s => s.end - s.start >= minSpan)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Tandem arrays in `seq`, which is the reference over the viewed region
|
|
130
|
+
* starting at `offset`.
|
|
131
|
+
*
|
|
132
|
+
* Scanning periods upward and refusing to start an array inside one already
|
|
133
|
+
* found is what reports a 3bp array as 3bp rather than as 6bp or 9bp: every
|
|
134
|
+
* multiple of a real period scores just as well, and the smallest one is the
|
|
135
|
+
* unit. It also means a long array with a short unit hides a spurious long-unit
|
|
136
|
+
* one inside it, which is the failure mode of scanning periods independently.
|
|
137
|
+
*/
|
|
138
|
+
export function findReferenceArrays(seq: string, offset = 0) {
|
|
139
|
+
const upper = seq.toUpperCase()
|
|
140
|
+
const claimed = new Uint8Array(upper.length)
|
|
141
|
+
const ret: ReferenceArray[] = []
|
|
142
|
+
const maxPeriod = Math.min(MAX_PERIOD, Math.floor(upper.length / MIN_COPIES))
|
|
143
|
+
for (let period = MIN_PERIOD; period <= maxPeriod; period++) {
|
|
144
|
+
for (const span of arraysOfPeriod(upper, period)) {
|
|
145
|
+
let free = 0
|
|
146
|
+
for (let i = span.start; i < span.end; i++) {
|
|
147
|
+
free += claimed[i] ? 0 : 1
|
|
148
|
+
}
|
|
149
|
+
// mostly inside an array already reported at a shorter period, so it is
|
|
150
|
+
// that array's higher harmonic rather than an array of its own
|
|
151
|
+
if (free < 0.5 * (span.end - span.start)) {
|
|
152
|
+
continue
|
|
153
|
+
}
|
|
154
|
+
const unit = upper.slice(span.start, span.start + period)
|
|
155
|
+
// a homopolymer is periodic at every lag, so it turns up here as an
|
|
156
|
+
// array of `AA` or `AAA` whose unit boundary is an arbitrary cut through
|
|
157
|
+
// one run of A. Counting its copies is counting its length in twos.
|
|
158
|
+
if (new Set(unit).size < 2) {
|
|
159
|
+
continue
|
|
160
|
+
}
|
|
161
|
+
for (let i = span.start; i < span.end; i++) {
|
|
162
|
+
claimed[i] = 1
|
|
163
|
+
}
|
|
164
|
+
ret.push({
|
|
165
|
+
start: offset + span.start,
|
|
166
|
+
end: offset + span.end,
|
|
167
|
+
period,
|
|
168
|
+
unit,
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return ret.sort((a, b) => a.start - b.start || a.period - b.period)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* How much of the merged interval an array has to cover before its period is a
|
|
177
|
+
* candidate to name the whole thing. A dinucleotide abutting a VNTR should not
|
|
178
|
+
* rename it; two descriptions of the same locus should compete.
|
|
179
|
+
*/
|
|
180
|
+
const RIVAL_SPAN_FRACTION = 0.5
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Merge arrays that touch or overlap into one interval, named by the shortest
|
|
184
|
+
* period that describes most of it.
|
|
185
|
+
*
|
|
186
|
+
* An allele is measured over an interval, so two intervals sharing reference
|
|
187
|
+
* positions would measure the same read bases twice. Overlaps are the norm
|
|
188
|
+
* rather than an edge case, because one locus is genuinely periodic at more
|
|
189
|
+
* than one lag: the ABCA7 VNTR's copies are 25 and 26 bases long, so it scans
|
|
190
|
+
* as a 25mer array, a 26mer array and — over the stretch where the two
|
|
191
|
+
* alternate — a 77mer one, all describing the same 466 bases. Taking the
|
|
192
|
+
* shortest period among the rivals reports it as the 25bp unit it is known by,
|
|
193
|
+
* rather than as a 77bp unit that is three copies wearing one name.
|
|
194
|
+
*/
|
|
195
|
+
export function mergeArrays(arrays: ReferenceArray[], gap = 0) {
|
|
196
|
+
const groups: ReferenceArray[][] = []
|
|
197
|
+
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)
|
|
202
|
+
} else {
|
|
203
|
+
groups.push([array])
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return groups.map(group => {
|
|
207
|
+
const span = (a: ReferenceArray) => a.end - a.start
|
|
208
|
+
const widest = Math.max(...group.map(span))
|
|
209
|
+
const rivals = group.filter(a => span(a) >= RIVAL_SPAN_FRACTION * widest)
|
|
210
|
+
const named = rivals.reduce((a, b) => (b.period < a.period ? b : a))
|
|
211
|
+
return {
|
|
212
|
+
start: Math.min(...group.map(a => a.start)),
|
|
213
|
+
end: Math.max(...group.map(a => a.end)),
|
|
214
|
+
period: named.period,
|
|
215
|
+
unit: named.unit,
|
|
216
|
+
}
|
|
217
|
+
})
|
|
218
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Newick tree whose only job is to group rows.
|
|
3
|
+
*
|
|
4
|
+
* With one file the rows are a pileup and their order is the whole structure.
|
|
5
|
+
* With several, the rows of two samples interleave into a list nothing separates
|
|
6
|
+
* — and react-msaview already draws a tree, collapses its clades and can show
|
|
7
|
+
* one branch alone, all of it keyed off a Newick string it is currently never
|
|
8
|
+
* given. Handing it one grouped by sample turns those controls on for free: a
|
|
9
|
+
* clade per sample, collapsible, and the allele ladder inside each.
|
|
10
|
+
*
|
|
11
|
+
* The tree is a grouping, not a phylogeny. Branch lengths are all 1 and say
|
|
12
|
+
* nothing; the topology is the sample the row came from, and the leaf order
|
|
13
|
+
* inside a clade is the copy-number order the rows were already in.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Newick reserves these, and a name is not quoted when it goes in.
|
|
18
|
+
*
|
|
19
|
+
* **Applied where the row name is built, not here.** react-msaview joins a tree
|
|
20
|
+
* leaf to an alignment row by name, so sanitizing on the way into the Newick
|
|
21
|
+
* and leaving the FASTA defline alone matches nothing: every row whose name
|
|
22
|
+
* needed fixing renders blank, with its label still in the tree, which looks
|
|
23
|
+
* like a rendering bug rather than a naming one. A track named `HG004 (mother)`
|
|
24
|
+
* is enough to trigger it.
|
|
25
|
+
*/
|
|
26
|
+
export function newickSafe(name: string) {
|
|
27
|
+
return name.replaceAll(/[(),:;[\]'\s]+/g, '_').replace(/_+$/, '')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface TreeRow {
|
|
31
|
+
name: string
|
|
32
|
+
/** what the defline says, when that is not just the name */
|
|
33
|
+
label?: string
|
|
34
|
+
sample?: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Groups rows by sample, keeping the order they arrive in both for the clades
|
|
39
|
+
* and inside them. Returns undefined when there is nothing to group — one
|
|
40
|
+
* sample, or none — since a tree of one clade is a line drawn beside a list.
|
|
41
|
+
*/
|
|
42
|
+
export function buildSampleTree(rows: TreeRow[]) {
|
|
43
|
+
const bySample = new Map<string, string[]>()
|
|
44
|
+
const ungrouped: string[] = []
|
|
45
|
+
for (const row of rows) {
|
|
46
|
+
// the leaf is the row's *defline*, already Newick-safe from getReadNames.
|
|
47
|
+
// The tree must not rewrite it, and must not use the identity where the
|
|
48
|
+
// defline carries a label — react-msaview joins the two by string, and a
|
|
49
|
+
// leaf naming no row leaves that row blank with its label still drawn
|
|
50
|
+
const leaf = row.label ?? row.name
|
|
51
|
+
if (row.sample === undefined) {
|
|
52
|
+
ungrouped.push(leaf)
|
|
53
|
+
} else {
|
|
54
|
+
const group = bySample.get(row.sample)
|
|
55
|
+
if (group) {
|
|
56
|
+
group.push(leaf)
|
|
57
|
+
} else {
|
|
58
|
+
bySample.set(row.sample, [leaf])
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (bySample.size < 2) {
|
|
63
|
+
return undefined
|
|
64
|
+
}
|
|
65
|
+
const clades = [
|
|
66
|
+
// the reference has no sample and belongs above all of them
|
|
67
|
+
...ungrouped.map(name => `${name}:1`),
|
|
68
|
+
...[...bySample].map(
|
|
69
|
+
([sample, names]) => `(${names.map(n => `${n}:1`).join(',')})${sample}:1`,
|
|
70
|
+
),
|
|
71
|
+
]
|
|
72
|
+
return `(${clades.join(',')});`
|
|
73
|
+
}
|