jbrowse-plugin-tview 2.0.6 → 2.1.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 +157 -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 +93 -9
- package/dist/LaunchTView/align.js +290 -15
- package/dist/LaunchTView/align.js.map +1 -1
- package/dist/LaunchTView/alleles.d.ts +57 -0
- package/dist/LaunchTView/alleles.js +94 -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/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 +190 -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 +47 -31
- 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 +22 -22
- 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 +5 -2
- package/src/LaunchTView/TviewGetPlanRpc/TviewGetPlan.ts +264 -0
- package/src/LaunchTView/TviewGetPlanRpc/index.ts +14 -0
- package/src/LaunchTView/align.ts +343 -17
- package/src/LaunchTView/alleles.ts +132 -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/repeats.ts +218 -0
- package/src/LaunchTView/sampleTree.ts +73 -0
- package/src/LaunchTView/tview.ts +259 -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,145 @@
|
|
|
1
|
+
import { newickSafe } from './sampleTree'
|
|
2
|
+
|
|
3
|
+
/** the subset of an @jbrowse/core Feature that the layout reads */
|
|
4
|
+
export interface AlignmentFeature {
|
|
5
|
+
get: (key: string) => any
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Only '-' and '.' are treated as gaps by react-msaview (anything else counts
|
|
10
|
+
* as a base in the conservation track and column stats), so the two of them
|
|
11
|
+
* carry the whole vocabulary for "no base here". They are split by meaning:
|
|
12
|
+
*
|
|
13
|
+
* - SPANNED_GAP: the read covers this reference position but aligns no base to
|
|
14
|
+
* it, i.e. a deletion, or padding in an insertion column
|
|
15
|
+
* - ABSENT: the read is not here at all, i.e. outside its bounds, or skipping
|
|
16
|
+
* over the position through an N (an intron, not a deleted allele)
|
|
17
|
+
*/
|
|
18
|
+
export const SPANNED_GAP = '-'
|
|
19
|
+
export const ABSENT = '.'
|
|
20
|
+
|
|
21
|
+
export interface ReadLayout {
|
|
22
|
+
/**
|
|
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.
|
|
25
|
+
*/
|
|
26
|
+
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
|
+
*/
|
|
40
|
+
sample?: string
|
|
41
|
+
start: number
|
|
42
|
+
/** one char per reference position consumed: the aligned base, or a gap */
|
|
43
|
+
refChars: string
|
|
44
|
+
/** reference position -> bases inserted immediately before that position */
|
|
45
|
+
insertions: Map<number, string>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const cigarRegex = /([MIDNSHPX=])/
|
|
49
|
+
|
|
50
|
+
export const byStart = (a: ReadLayout, b: ReadLayout) => a.start - b.start
|
|
51
|
+
|
|
52
|
+
export function parseCigar(cigar: string) {
|
|
53
|
+
return cigar.split(cigarRegex).slice(0, -1)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function parseRead(
|
|
57
|
+
feature: AlignmentFeature,
|
|
58
|
+
name: string,
|
|
59
|
+
sample?: string,
|
|
60
|
+
): ReadLayout {
|
|
61
|
+
const seq = feature.get('seq') as string
|
|
62
|
+
const ops = parseCigar(feature.get('CIGAR') as string)
|
|
63
|
+
const start = feature.get('start')
|
|
64
|
+
const insertions = new Map<number, string>()
|
|
65
|
+
let refChars = ''
|
|
66
|
+
let seqOffset = 0
|
|
67
|
+
|
|
68
|
+
for (let i = 0; i < ops.length; i += 2) {
|
|
69
|
+
const len = +ops[i]!
|
|
70
|
+
const op = ops[i + 1]!
|
|
71
|
+
if (op === 'S') {
|
|
72
|
+
seqOffset += len
|
|
73
|
+
} else if (op === 'I') {
|
|
74
|
+
// consecutive I ops (e.g. 3M2I3I3M) land on the same reference position
|
|
75
|
+
const pos = start + refChars.length
|
|
76
|
+
const prev = insertions.get(pos) ?? ''
|
|
77
|
+
insertions.set(pos, prev + seq.slice(seqOffset, seqOffset + len))
|
|
78
|
+
seqOffset += len
|
|
79
|
+
} else if (op === 'D') {
|
|
80
|
+
refChars += SPANNED_GAP.repeat(len)
|
|
81
|
+
} else if (op === 'N') {
|
|
82
|
+
refChars += ABSENT.repeat(len)
|
|
83
|
+
} else if (op === 'M' || op === 'X' || op === '=') {
|
|
84
|
+
refChars += seq.slice(seqOffset, seqOffset + len)
|
|
85
|
+
seqOffset += len
|
|
86
|
+
}
|
|
87
|
+
// H and P consume neither the read sequence nor the reference
|
|
88
|
+
}
|
|
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
|
+
return {
|
|
92
|
+
name,
|
|
93
|
+
sample: sample === undefined ? undefined : newickSafe(sample),
|
|
94
|
+
start,
|
|
95
|
+
refChars,
|
|
96
|
+
insertions,
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Reads are fetched by overlap, so they carry insertions on either side of the
|
|
102
|
+
* region. Those get no columns and are never rendered, so dropping them keeps
|
|
103
|
+
* the pairwise alignment below off the wasted sites entirely.
|
|
104
|
+
*/
|
|
105
|
+
export function clipInsertions(
|
|
106
|
+
reads: ReadLayout[],
|
|
107
|
+
start: number,
|
|
108
|
+
end: number,
|
|
109
|
+
) {
|
|
110
|
+
return reads.map(read => ({
|
|
111
|
+
...read,
|
|
112
|
+
insertions: new Map(
|
|
113
|
+
[...read.insertions].filter(([pos]) => pos >= start && pos < end),
|
|
114
|
+
),
|
|
115
|
+
}))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function mateSuffix(flags: number | undefined) {
|
|
119
|
+
return flags === undefined || !(flags & 1) ? '' : flags & 64 ? '/1' : '/2'
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
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.
|
|
130
|
+
*/
|
|
131
|
+
export function getReadNames(
|
|
132
|
+
features: AlignmentFeature[],
|
|
133
|
+
sampleOf?: (index: number) => string | undefined,
|
|
134
|
+
) {
|
|
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}`
|
|
144
|
+
})
|
|
145
|
+
}
|
|
@@ -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
|
+
}
|