react-msaview 6.0.0 → 6.1.1
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/bundle/index.js +98 -98
- package/bundle/index.js.map +4 -4
- package/dist/columnCounts.d.ts +7 -0
- package/dist/columnCounts.js +18 -0
- package/dist/columnCounts.js.map +1 -1
- package/dist/components/DragHandle.d.ts +25 -0
- package/dist/components/DragHandle.js +49 -0
- package/dist/components/DragHandle.js.map +1 -0
- package/dist/components/ResizeHandles.d.ts +4 -2
- package/dist/components/ResizeHandles.js +29 -32
- package/dist/components/ResizeHandles.js.map +1 -1
- package/dist/components/VerticalScrollbar.js +2 -2
- package/dist/components/VerticalScrollbar.js.map +1 -1
- package/dist/components/dialogs/InterProScanDialog.js +52 -129
- package/dist/components/dialogs/InterProScanDialog.js.map +1 -1
- package/dist/components/header/settingsMenuItems.js +16 -1
- package/dist/components/header/settingsMenuItems.js.map +1 -1
- package/dist/components/minimap/Minimap.js +32 -4
- package/dist/components/minimap/Minimap.js.map +1 -1
- package/dist/components/msa/MSACanvas.js +8 -2
- package/dist/components/msa/MSACanvas.js.map +1 -1
- package/dist/components/msa/MSACanvasBlock.js +12 -3
- package/dist/components/msa/MSACanvasBlock.js.map +1 -1
- package/dist/components/msa/msaRaster.d.ts +63 -0
- package/dist/components/msa/msaRaster.js +252 -0
- package/dist/components/msa/msaRaster.js.map +1 -0
- package/dist/components/msa/renderMSABlock.d.ts +2 -1
- package/dist/components/msa/renderMSABlock.js +13 -21
- package/dist/components/msa/renderMSABlock.js.map +1 -1
- package/dist/components/msa/tileColor.d.ts +6 -0
- package/dist/components/msa/tileColor.js +35 -0
- package/dist/components/msa/tileColor.js.map +1 -0
- package/dist/components/renderCtx.d.ts +1 -1
- package/dist/components/tracks/TrackBlocks.d.ts +14 -0
- package/dist/components/tracks/TrackBlocks.js +65 -0
- package/dist/components/tracks/TrackBlocks.js.map +1 -0
- package/dist/components/tracks/{renderTracksSvg.d.ts → drawTracks.d.ts} +22 -10
- package/dist/components/tracks/drawTracks.js +193 -0
- package/dist/components/tracks/drawTracks.js.map +1 -0
- package/dist/components/tree/TreeCanvas.js +8 -2
- package/dist/components/tree/TreeCanvas.js.map +1 -1
- package/dist/components/tree/TreeCanvasBlock.js +7 -4
- package/dist/components/tree/TreeCanvasBlock.js.map +1 -1
- package/dist/components/tree/renderTreeCanvas.js +27 -21
- package/dist/components/tree/renderTreeCanvas.js.map +1 -1
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +8 -0
- package/dist/constants.js.map +1 -1
- package/dist/model/DataModel.d.ts +1 -4
- package/dist/model/DataModel.js +8 -10
- package/dist/model/DataModel.js.map +1 -1
- package/dist/model/treeModel.d.ts +1 -1
- package/dist/model/treeModel.js +1 -1
- package/dist/model.d.ts +36 -7
- package/dist/model.js +113 -58
- package/dist/model.js.map +1 -1
- package/dist/neighborJoining.js +34 -18
- package/dist/neighborJoining.js.map +1 -1
- package/dist/renderTestEnv.d.ts +43 -0
- package/dist/renderTestEnv.js +88 -0
- package/dist/renderTestEnv.js.map +1 -0
- package/dist/renderToSvg.js +1 -1
- package/dist/renderToSvg.js.map +1 -1
- package/dist/sequenceLogo.d.ts +28 -0
- package/dist/sequenceLogo.js +47 -0
- package/dist/sequenceLogo.js.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -3
- package/src/columnCounts.test.ts +15 -0
- package/src/columnCounts.ts +19 -0
- package/src/components/DragHandle.tsx +81 -0
- package/src/components/ResizeHandles.tsx +46 -54
- package/src/components/VerticalScrollbar.tsx +3 -2
- package/src/components/dialogs/InterProScanDialog.tsx +54 -131
- package/src/components/header/settingsMenuItems.ts +19 -1
- package/src/components/minimap/Minimap.tsx +47 -4
- package/src/components/msa/MSACanvas.tsx +30 -10
- package/src/components/msa/MSACanvasBlock.tsx +13 -3
- package/src/components/msa/msaRaster.test.ts +145 -0
- package/src/components/msa/msaRaster.ts +350 -0
- package/src/components/msa/renderMSABlock.ts +15 -20
- package/src/components/msa/tileColor.ts +37 -0
- package/src/components/renderCtx.ts +2 -0
- package/src/components/tracks/TrackBlocks.tsx +111 -0
- package/src/components/tracks/drawTracks.ts +323 -0
- package/src/components/tree/TreeCanvas.tsx +16 -4
- package/src/components/tree/TreeCanvasBlock.tsx +18 -11
- package/src/components/tree/renderTreeCanvas.test.ts +55 -0
- package/src/components/tree/renderTreeCanvas.ts +28 -19
- package/src/constants.ts +10 -0
- package/src/impgRender.test.tsx +2 -57
- package/src/largeTree.test.ts +16 -2
- package/src/model/DataModel.ts +11 -11
- package/src/model/treeModel.ts +1 -1
- package/src/model.ts +128 -60
- package/src/neighborJoining.ts +36 -18
- package/src/renderDomainsSvg.test.tsx +2 -57
- package/src/renderMinimapSvg.test.tsx +2 -56
- package/src/renderSequenceLogoSvg.test.tsx +176 -0
- package/src/renderTestEnv.ts +96 -0
- package/src/renderToSvg.tsx +1 -1
- package/src/sequenceLogo.test.ts +88 -0
- package/src/sequenceLogo.ts +63 -0
- package/src/types.ts +7 -1
- package/src/version.ts +1 -1
- package/dist/components/ConservationTrack.d.ts +0 -8
- package/dist/components/ConservationTrack.js +0 -49
- package/dist/components/ConservationTrack.js.map +0 -1
- package/dist/components/ScrollbarThumb.d.ts +0 -14
- package/dist/components/ScrollbarThumb.js +0 -28
- package/dist/components/ScrollbarThumb.js.map +0 -1
- package/dist/components/TextTrack.d.ts +0 -8
- package/dist/components/TextTrack.js +0 -58
- package/dist/components/TextTrack.js.map +0 -1
- package/dist/components/tracks/renderTracksSvg.js +0 -126
- package/dist/components/tracks/renderTracksSvg.js.map +0 -1
- package/src/components/ConservationTrack.tsx +0 -99
- package/src/components/ScrollbarThumb.tsx +0 -48
- package/src/components/TextTrack.tsx +0 -103
- package/src/components/tracks/renderTracksSvg.ts +0 -247
package/src/neighborJoining.ts
CHANGED
|
@@ -31,25 +31,43 @@ const BLOSUM62_DATA = [
|
|
|
31
31
|
[-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4, 1],
|
|
32
32
|
]
|
|
33
33
|
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
const NUM_SYMBOLS = BLOSUM62_KEYS.length
|
|
35
|
+
const BLOSUM62 = Int8Array.from(BLOSUM62_DATA.flat())
|
|
36
|
+
|
|
37
|
+
// charCode -> row/column in BLOSUM62, -1 for anything the matrix does not name.
|
|
38
|
+
// A flat table rather than a Map of Maps keyed by an upper-cased character, for
|
|
39
|
+
// the reason columnCounts.ts gives: the distance matrix is O(rows^2 * columns)
|
|
40
|
+
// lookups, and on a 120x800 alignment the Map form spent nearly all of its time
|
|
41
|
+
// allocating one-character strings in toUpperCase: 2.6s to build a tree, versus
|
|
42
|
+
// 0.34s here for a byte-identical newick.
|
|
43
|
+
const symbolOfCode = new Int8Array(128).fill(-1)
|
|
44
|
+
for (let i = 0; i < NUM_SYMBOLS; i++) {
|
|
45
|
+
const key = BLOSUM62_KEYS[i]!
|
|
46
|
+
symbolOfCode[key.charCodeAt(0)] = i
|
|
47
|
+
symbolOfCode[key.toLowerCase().charCodeAt(0)] = i
|
|
41
48
|
}
|
|
42
49
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
50
|
+
// gap, or past the end of a row shorter than the alignment
|
|
51
|
+
const GAP = -2
|
|
46
52
|
|
|
47
53
|
// stockholm and a3m write gaps as '.', not just '-'. Scoring a '.' as a residue
|
|
48
54
|
// charges the pair the unknown-symbol penalty on every padded column, so two
|
|
49
55
|
// sequences that differ only in where an unrelated row forced padding come out
|
|
50
56
|
// as divergent.
|
|
51
|
-
function
|
|
52
|
-
|
|
57
|
+
function symbolAt(seq: string, i: number) {
|
|
58
|
+
if (i >= seq.length) {
|
|
59
|
+
return GAP
|
|
60
|
+
}
|
|
61
|
+
const code = seq.charCodeAt(i)
|
|
62
|
+
// bit trick: (code - 45) >>> 0 <= 1 checks for '-' (45) or '.' (46)
|
|
63
|
+
if ((code - 45) >>> 0 <= 1) {
|
|
64
|
+
return GAP
|
|
65
|
+
}
|
|
66
|
+
return code < 128 ? symbolOfCode[code]! : -1
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function blosumScore(a: number, b: number) {
|
|
70
|
+
return a < 0 || b < 0 ? -4 : BLOSUM62[a * NUM_SYMBOLS + b]!
|
|
53
71
|
}
|
|
54
72
|
|
|
55
73
|
function computePairwiseDistance(seq1: string, seq2: string) {
|
|
@@ -63,11 +81,11 @@ function computePairwiseDistance(seq1: string, seq2: string) {
|
|
|
63
81
|
let maxPossibleScore = 0
|
|
64
82
|
|
|
65
83
|
for (let i = 0; i < len; i++) {
|
|
66
|
-
const a = seq1
|
|
67
|
-
const b = seq2
|
|
84
|
+
const a = symbolAt(seq1, i)
|
|
85
|
+
const b = symbolAt(seq2, i)
|
|
68
86
|
|
|
69
|
-
const aGap =
|
|
70
|
-
const bGap =
|
|
87
|
+
const aGap = a === GAP
|
|
88
|
+
const bGap = b === GAP
|
|
71
89
|
if (aGap && bGap) {
|
|
72
90
|
continue
|
|
73
91
|
}
|
|
@@ -77,8 +95,8 @@ function computePairwiseDistance(seq1: string, seq2: string) {
|
|
|
77
95
|
continue
|
|
78
96
|
}
|
|
79
97
|
|
|
80
|
-
totalScore +=
|
|
81
|
-
maxPossibleScore += Math.max(
|
|
98
|
+
totalScore += blosumScore(a, b)
|
|
99
|
+
maxPossibleScore += Math.max(blosumScore(a, a), blosumScore(b, b))
|
|
82
100
|
}
|
|
83
101
|
|
|
84
102
|
if (compared === 0) {
|
|
@@ -9,67 +9,12 @@ import { enableStaticRendering } from 'mobx-react'
|
|
|
9
9
|
import { beforeAll, expect, test } from 'vitest'
|
|
10
10
|
|
|
11
11
|
import MSAModelF from './model.ts'
|
|
12
|
+
import { installRenderTestEnv } from './renderTestEnv.ts'
|
|
12
13
|
import { renderToSvg } from './renderToSvg.tsx'
|
|
13
14
|
|
|
14
|
-
class Mat {
|
|
15
|
-
constructor(
|
|
16
|
-
public a = 1,
|
|
17
|
-
public b = 0,
|
|
18
|
-
public c = 0,
|
|
19
|
-
public d = 1,
|
|
20
|
-
public e = 0,
|
|
21
|
-
public f = 0,
|
|
22
|
-
) {}
|
|
23
|
-
multiply(o: Mat) {
|
|
24
|
-
return new Mat(
|
|
25
|
-
this.a * o.a + this.c * o.b,
|
|
26
|
-
this.b * o.a + this.d * o.b,
|
|
27
|
-
this.a * o.c + this.c * o.d,
|
|
28
|
-
this.b * o.c + this.d * o.d,
|
|
29
|
-
this.a * o.e + this.c * o.f + this.e,
|
|
30
|
-
this.b * o.e + this.d * o.f + this.f,
|
|
31
|
-
)
|
|
32
|
-
}
|
|
33
|
-
translate(x: number, y = 0) {
|
|
34
|
-
return this.multiply(new Mat(1, 0, 0, 1, x, y))
|
|
35
|
-
}
|
|
36
|
-
scale(x: number, y = x) {
|
|
37
|
-
return this.multiply(new Mat(x, 0, 0, y, 0, 0))
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
class Pt {
|
|
41
|
-
constructor(
|
|
42
|
-
public x = 0,
|
|
43
|
-
public y = 0,
|
|
44
|
-
) {}
|
|
45
|
-
matrixTransform(m: Mat) {
|
|
46
|
-
return new Pt(
|
|
47
|
-
m.a * this.x + m.c * this.y + m.e,
|
|
48
|
-
m.b * this.x + m.d * this.y + m.f,
|
|
49
|
-
)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
15
|
beforeAll(() => {
|
|
54
16
|
enableStaticRendering(true)
|
|
55
|
-
|
|
56
|
-
g.DOMMatrix = Mat
|
|
57
|
-
g.DOMPoint = Pt
|
|
58
|
-
HTMLCanvasElement.prototype.getContext = function () {
|
|
59
|
-
let font = '10px sans-serif'
|
|
60
|
-
return {
|
|
61
|
-
get font() {
|
|
62
|
-
return font
|
|
63
|
-
},
|
|
64
|
-
set font(v: string) {
|
|
65
|
-
font = v
|
|
66
|
-
},
|
|
67
|
-
measureText(t: string) {
|
|
68
|
-
const size = Number.parseFloat(font) || 10
|
|
69
|
-
return { width: t.length * size * 0.6 } as TextMetrics
|
|
70
|
-
},
|
|
71
|
-
} as unknown as CanvasRenderingContext2D
|
|
72
|
-
} as unknown as typeof HTMLCanvasElement.prototype.getContext
|
|
17
|
+
installRenderTestEnv()
|
|
73
18
|
})
|
|
74
19
|
|
|
75
20
|
const msa = `>seq1
|
|
@@ -9,66 +9,12 @@ import { enableStaticRendering } from 'mobx-react'
|
|
|
9
9
|
import { beforeAll, expect, test } from 'vitest'
|
|
10
10
|
|
|
11
11
|
import MSAModelF from './model.ts'
|
|
12
|
+
import { installRenderTestEnv } from './renderTestEnv.ts'
|
|
12
13
|
import { renderToSvg } from './renderToSvg.tsx'
|
|
13
14
|
|
|
14
|
-
class Mat {
|
|
15
|
-
constructor(
|
|
16
|
-
public a = 1,
|
|
17
|
-
public b = 0,
|
|
18
|
-
public c = 0,
|
|
19
|
-
public d = 1,
|
|
20
|
-
public e = 0,
|
|
21
|
-
public f = 0,
|
|
22
|
-
) {}
|
|
23
|
-
multiply(o: Mat) {
|
|
24
|
-
return new Mat(
|
|
25
|
-
this.a * o.a + this.c * o.b,
|
|
26
|
-
this.b * o.a + this.d * o.b,
|
|
27
|
-
this.a * o.c + this.c * o.d,
|
|
28
|
-
this.b * o.c + this.d * o.d,
|
|
29
|
-
this.a * o.e + this.c * o.f + this.e,
|
|
30
|
-
this.b * o.e + this.d * o.f + this.f,
|
|
31
|
-
)
|
|
32
|
-
}
|
|
33
|
-
translate(x: number, y = 0) {
|
|
34
|
-
return this.multiply(new Mat(1, 0, 0, 1, x, y))
|
|
35
|
-
}
|
|
36
|
-
scale(x: number, y = x) {
|
|
37
|
-
return this.multiply(new Mat(x, 0, 0, y, 0, 0))
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
class Pt {
|
|
41
|
-
constructor(
|
|
42
|
-
public x = 0,
|
|
43
|
-
public y = 0,
|
|
44
|
-
) {}
|
|
45
|
-
matrixTransform(m: Mat) {
|
|
46
|
-
return new Pt(
|
|
47
|
-
m.a * this.x + m.c * this.y + m.e,
|
|
48
|
-
m.b * this.x + m.d * this.y + m.f,
|
|
49
|
-
)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
15
|
beforeAll(() => {
|
|
54
16
|
enableStaticRendering(true)
|
|
55
|
-
|
|
56
|
-
g.DOMMatrix = Mat
|
|
57
|
-
g.DOMPoint = Pt
|
|
58
|
-
HTMLCanvasElement.prototype.getContext = function () {
|
|
59
|
-
let font = '10px sans-serif'
|
|
60
|
-
return {
|
|
61
|
-
get font() {
|
|
62
|
-
return font
|
|
63
|
-
},
|
|
64
|
-
set font(v: string) {
|
|
65
|
-
font = v
|
|
66
|
-
},
|
|
67
|
-
measureText: (t: string) => ({
|
|
68
|
-
width: t.length * (Number.parseFloat(font) || 10) * 0.6,
|
|
69
|
-
}),
|
|
70
|
-
} as unknown as CanvasRenderingContext2D
|
|
71
|
-
} as unknown as typeof HTMLCanvasElement.prototype.getContext
|
|
17
|
+
installRenderTestEnv()
|
|
72
18
|
})
|
|
73
19
|
|
|
74
20
|
const width = 1000
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
//
|
|
3
|
+
// The logo track is the first track kind whose letters are drawn under a
|
|
4
|
+
// per-letter transform rather than at a fixed size, so it is also the first that
|
|
5
|
+
// can silently draw nothing if the export context loses that transform. These
|
|
6
|
+
// tests go through the real SVG export to pin the whole path: the track is off
|
|
7
|
+
// until asked for, the stack is ordered and scaled, and the glyphs land inside
|
|
8
|
+
// the track's own band.
|
|
9
|
+
import { createJBrowseTheme } from '@jbrowse/core/ui/theme'
|
|
10
|
+
import { enableStaticRendering } from 'mobx-react'
|
|
11
|
+
import { beforeAll, expect, test } from 'vitest'
|
|
12
|
+
|
|
13
|
+
import MSAModelF from './model.ts'
|
|
14
|
+
import { TEST_CHAR_WIDTH_RATIO, installRenderTestEnv } from './renderTestEnv.ts'
|
|
15
|
+
import { renderToSvg } from './renderToSvg.tsx'
|
|
16
|
+
|
|
17
|
+
beforeAll(() => {
|
|
18
|
+
enableStaticRendering(true)
|
|
19
|
+
installRenderTestEnv()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
// column 0 is fully conserved A, column 1 is a 3:1 split of C over G, and
|
|
23
|
+
// column 2 is an even four-way split carrying no information at all
|
|
24
|
+
const msa = `>s1\nACA\n>s2\nACC\n>s3\nACG\n>s4\nAGT\n`
|
|
25
|
+
const colWidth = 40
|
|
26
|
+
|
|
27
|
+
function makeModel() {
|
|
28
|
+
const model = MSAModelF().create({
|
|
29
|
+
type: 'MsaView',
|
|
30
|
+
msaFormat: 'fasta',
|
|
31
|
+
height: 400,
|
|
32
|
+
colWidth,
|
|
33
|
+
data: { msa },
|
|
34
|
+
})
|
|
35
|
+
model.setWidth(1000)
|
|
36
|
+
return model
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function exportSvg(model: ReturnType<typeof makeModel>) {
|
|
40
|
+
return renderToSvg(model, {
|
|
41
|
+
theme: createJBrowseTheme(),
|
|
42
|
+
exportType: 'entire',
|
|
43
|
+
includeTracks: true,
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The logo's glyphs are the only <text> drawn at the track's own font size, and
|
|
48
|
+
// the only ones carrying a non-unit vertical scale, which is what separates them
|
|
49
|
+
// from the alignment letters and tree labels in the same document.
|
|
50
|
+
function logoGlyphs(svg: string, fontSize: number) {
|
|
51
|
+
return [
|
|
52
|
+
...svg.matchAll(
|
|
53
|
+
/<text[^>]*font-size="([\d.]+)px"[^>]*transform="matrix\(([^)]*)\)"[^>]*>([A-Z])<\/text>/g,
|
|
54
|
+
),
|
|
55
|
+
]
|
|
56
|
+
.filter(m => Number(m[1]) === fontSize)
|
|
57
|
+
.map(m => {
|
|
58
|
+
const [sx, , , sy, tx, ty] = m[2]!.split(/[\s,]+/).map(Number)
|
|
59
|
+
return { letter: m[3]!, sx: sx!, sy: sy!, x: tx!, y: ty! }
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function glyphsOf(model: ReturnType<typeof makeModel>) {
|
|
64
|
+
return logoGlyphs(await exportSvg(model), model.sequenceLogoTrackHeight)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
test('the logo track is hidden until it is asked for', () => {
|
|
68
|
+
const model = makeModel()
|
|
69
|
+
expect(model.tracks.map(t => t.model.id)).toContain('sequence-logo')
|
|
70
|
+
expect(model.turnedOnTracks.map(t => t.model.id)).not.toContain(
|
|
71
|
+
'sequence-logo',
|
|
72
|
+
)
|
|
73
|
+
// and being default-off writes nothing into the shareable snapshot
|
|
74
|
+
expect(model.turnedOffTracks.size).toBe(0)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('toggling it on shows it, and off again hides it', () => {
|
|
78
|
+
const model = makeModel()
|
|
79
|
+
model.toggleTrack('sequence-logo')
|
|
80
|
+
expect(model.turnedOnTracks.map(t => t.model.id)).toContain('sequence-logo')
|
|
81
|
+
model.toggleTrack('sequence-logo')
|
|
82
|
+
expect(model.turnedOnTracks.map(t => t.model.id)).not.toContain(
|
|
83
|
+
'sequence-logo',
|
|
84
|
+
)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('an on-by-default track still toggles off and back on', () => {
|
|
88
|
+
const model = makeModel()
|
|
89
|
+
expect(model.turnedOnTracks.map(t => t.model.id)).toContain('conservation')
|
|
90
|
+
model.toggleTrack('conservation')
|
|
91
|
+
expect(model.turnedOnTracks.map(t => t.model.id)).not.toContain(
|
|
92
|
+
'conservation',
|
|
93
|
+
)
|
|
94
|
+
model.toggleTrack('conservation')
|
|
95
|
+
expect(model.turnedOnTracks.map(t => t.model.id)).toContain('conservation')
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('the conserved column draws one full-height letter', async () => {
|
|
99
|
+
const model = makeModel()
|
|
100
|
+
model.toggleTrack('sequence-logo')
|
|
101
|
+
const col0 = (await glyphsOf(model)).filter(g => g.x === 0)
|
|
102
|
+
|
|
103
|
+
expect(col0.map(g => g.letter)).toEqual(['A'])
|
|
104
|
+
// a fully conserved nucleotide column is 2 of 2 bits, so the glyph stretches
|
|
105
|
+
// to the whole track height over the cap height of the reference font
|
|
106
|
+
expect(col0[0]!.sy).toBeCloseTo(1 / 0.72, 5)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('a split column stacks the minor letter below the major one', async () => {
|
|
110
|
+
const model = makeModel()
|
|
111
|
+
model.toggleTrack('sequence-logo')
|
|
112
|
+
const col1 = (await glyphsOf(model)).filter(g => g.x === colWidth)
|
|
113
|
+
|
|
114
|
+
// ascending order means C (3 of 4) is drawn last, so it ends up on top
|
|
115
|
+
expect(col1.map(g => g.letter)).toEqual(['G', 'C'])
|
|
116
|
+
const [g, c] = col1
|
|
117
|
+
expect(c!.sy).toBeGreaterThan(g!.sy)
|
|
118
|
+
// and the taller letter sits higher up the track (smaller y is nearer the top)
|
|
119
|
+
expect(c!.y).toBeLessThan(g!.y)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('the uninformative column draws nothing', async () => {
|
|
123
|
+
const model = makeModel()
|
|
124
|
+
model.toggleTrack('sequence-logo')
|
|
125
|
+
const glyphs = await glyphsOf(model)
|
|
126
|
+
expect(glyphs.filter(g => g.x === colWidth * 2)).toEqual([])
|
|
127
|
+
// the other two columns did draw, so an empty column 2 is the logo's own
|
|
128
|
+
// verdict rather than the whole track missing
|
|
129
|
+
expect(glyphs).toHaveLength(3)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('every glyph stays inside the track band', async () => {
|
|
133
|
+
const model = makeModel()
|
|
134
|
+
model.toggleTrack('sequence-logo')
|
|
135
|
+
|
|
136
|
+
// the export stacks the tracks, so the logo's band is offset by whatever is
|
|
137
|
+
// drawn above it -- assert against that band rather than against 0..height,
|
|
138
|
+
// which would pass only while the logo happened to be the first track
|
|
139
|
+
const above = model.turnedOnTracks
|
|
140
|
+
.slice(
|
|
141
|
+
0,
|
|
142
|
+
model.turnedOnTracks.findIndex(t => t.model.id === 'sequence-logo'),
|
|
143
|
+
)
|
|
144
|
+
.reduce((a, t) => a + t.model.height, 0)
|
|
145
|
+
const bandTop = above
|
|
146
|
+
const bandBottom = above + model.sequenceLogoTrackHeight
|
|
147
|
+
|
|
148
|
+
const glyphs = await glyphsOf(model)
|
|
149
|
+
expect(glyphs.length).toBeGreaterThan(0)
|
|
150
|
+
for (const glyph of glyphs) {
|
|
151
|
+
// baselines run from the band's bottom edge upward, never past either end
|
|
152
|
+
expect(glyph.y).toBeGreaterThan(bandTop)
|
|
153
|
+
expect(glyph.y).toBeLessThanOrEqual(bandBottom)
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('letters are stretched to fill their column', async () => {
|
|
158
|
+
const model = makeModel()
|
|
159
|
+
model.toggleTrack('sequence-logo')
|
|
160
|
+
const glyphs = await glyphsOf(model)
|
|
161
|
+
|
|
162
|
+
const expected =
|
|
163
|
+
colWidth / (model.sequenceLogoTrackHeight * TEST_CHAR_WIDTH_RATIO)
|
|
164
|
+
for (const glyph of glyphs) {
|
|
165
|
+
expect(glyph.sx).toBeCloseTo(expected, 5)
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
test('the logo appears in the export only when the track is on', async () => {
|
|
170
|
+
const off = makeModel()
|
|
171
|
+
expect(await glyphsOf(off)).toEqual([])
|
|
172
|
+
|
|
173
|
+
const on = makeModel()
|
|
174
|
+
on.toggleTrack('sequence-logo')
|
|
175
|
+
expect((await glyphsOf(on)).length).toBeGreaterThan(0)
|
|
176
|
+
})
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* jsdom shims for the SVG-export tests. Test-only -- nothing in the viewer
|
|
3
|
+
* imports this.
|
|
4
|
+
*
|
|
5
|
+
* jsdom implements neither DOMMatrix nor a canvas 2D context, and
|
|
6
|
+
* `@jbrowse/svgcanvas` needs both: it tracks the current transform as a real
|
|
7
|
+
* matrix, and the renderers ask a throwaway canvas to measure text.
|
|
8
|
+
*
|
|
9
|
+
* The matrix stub has to accept the ARRAY constructor. svgcanvas normalizes
|
|
10
|
+
* every transform through `new DOMMatrix([a, b, c, d, e, f])`, so a stub that
|
|
11
|
+
* only takes six positional arguments silently binds the whole array to `a` and
|
|
12
|
+
* leaves the rest undefined -- every emitted transform then carries `NaN` in its
|
|
13
|
+
* x components, which no assertion on a drawn x-position can survive.
|
|
14
|
+
*/
|
|
15
|
+
export class TestDOMMatrix {
|
|
16
|
+
a: number
|
|
17
|
+
b: number
|
|
18
|
+
c: number
|
|
19
|
+
d: number
|
|
20
|
+
e: number
|
|
21
|
+
f: number
|
|
22
|
+
|
|
23
|
+
constructor(init?: number[]) {
|
|
24
|
+
const [a = 1, b = 0, c = 0, d = 1, e = 0, f = 0] = init ?? []
|
|
25
|
+
this.a = a
|
|
26
|
+
this.b = b
|
|
27
|
+
this.c = c
|
|
28
|
+
this.d = d
|
|
29
|
+
this.e = e
|
|
30
|
+
this.f = f
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
multiply(o: TestDOMMatrix) {
|
|
34
|
+
return new TestDOMMatrix([
|
|
35
|
+
this.a * o.a + this.c * o.b,
|
|
36
|
+
this.b * o.a + this.d * o.b,
|
|
37
|
+
this.a * o.c + this.c * o.d,
|
|
38
|
+
this.b * o.c + this.d * o.d,
|
|
39
|
+
this.a * o.e + this.c * o.f + this.e,
|
|
40
|
+
this.b * o.e + this.d * o.f + this.f,
|
|
41
|
+
])
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
translate(x: number, y = 0) {
|
|
45
|
+
return this.multiply(new TestDOMMatrix([1, 0, 0, 1, x, y]))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
scale(x: number, y = x) {
|
|
49
|
+
return this.multiply(new TestDOMMatrix([x, 0, 0, y, 0, 0]))
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class TestDOMPoint {
|
|
54
|
+
constructor(
|
|
55
|
+
public x = 0,
|
|
56
|
+
public y = 0,
|
|
57
|
+
) {}
|
|
58
|
+
|
|
59
|
+
matrixTransform(m: TestDOMMatrix) {
|
|
60
|
+
return new TestDOMPoint(
|
|
61
|
+
m.a * this.x + m.c * this.y + m.e,
|
|
62
|
+
m.b * this.x + m.d * this.y + m.f,
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Install the shims. Call from `beforeAll` in any test that exports SVG.
|
|
69
|
+
*
|
|
70
|
+
* `measureText` reports 0.6em per character, close enough to a real sans-serif
|
|
71
|
+
* average for layout assertions and, more usefully, exactly predictable: a test
|
|
72
|
+
* can compute the width a renderer will see rather than hardcoding a number
|
|
73
|
+
* measured from one machine's fonts.
|
|
74
|
+
*/
|
|
75
|
+
export function installRenderTestEnv() {
|
|
76
|
+
const g = globalThis as Record<string, unknown>
|
|
77
|
+
g.DOMMatrix = TestDOMMatrix
|
|
78
|
+
g.DOMPoint = TestDOMPoint
|
|
79
|
+
HTMLCanvasElement.prototype.getContext = function () {
|
|
80
|
+
let font = '10px sans-serif'
|
|
81
|
+
return {
|
|
82
|
+
get font() {
|
|
83
|
+
return font
|
|
84
|
+
},
|
|
85
|
+
set font(v: string) {
|
|
86
|
+
font = v
|
|
87
|
+
},
|
|
88
|
+
measureText: (t: string) => ({
|
|
89
|
+
width: t.length * (Number.parseFloat(font) || 10) * 0.6,
|
|
90
|
+
}),
|
|
91
|
+
} as unknown as CanvasRenderingContext2D
|
|
92
|
+
} as unknown as typeof HTMLCanvasElement.prototype.getContext
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** the per-character width factor `measureText` above reports */
|
|
96
|
+
export const TEST_CHAR_WIDTH_RATIO = 0.6
|
package/src/renderToSvg.tsx
CHANGED
|
@@ -7,7 +7,7 @@ import { when } from 'mobx'
|
|
|
7
7
|
import MinimapSVG from './components/minimap/MinimapSVG.tsx'
|
|
8
8
|
import { renderBoxFeatureCanvasBlock } from './components/msa/renderBoxFeatureCanvasBlock.ts'
|
|
9
9
|
import { renderMSABlock } from './components/msa/renderMSABlock.ts'
|
|
10
|
-
import { renderAllTracks } from './components/tracks/
|
|
10
|
+
import { renderAllTracks } from './components/tracks/drawTracks.ts'
|
|
11
11
|
import { renderTreeCanvas } from './components/tree/renderTreeCanvas.ts'
|
|
12
12
|
import { colorContrast } from './util.ts'
|
|
13
13
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { expect, test } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { columnCountsFromColumns } from './columnCounts.ts'
|
|
4
|
+
import { columnLogoStack, maxBitsFor } from './sequenceLogo.ts'
|
|
5
|
+
|
|
6
|
+
const dnaBits = maxBitsFor('dna')
|
|
7
|
+
const aminoBits = maxBitsFor('amino')
|
|
8
|
+
|
|
9
|
+
function stackOf(column: string, maxBits = dnaBits) {
|
|
10
|
+
return columnLogoStack(columnCountsFromColumns([column]), 0, maxBits)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
test('maxBits is log2 of the alphabet', () => {
|
|
14
|
+
expect(maxBitsFor('dna')).toBe(2)
|
|
15
|
+
expect(maxBitsFor('rna')).toBe(2)
|
|
16
|
+
expect(maxBitsFor('amino')).toBeCloseTo(4.3219, 4)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
test('a fully conserved column is one letter at the full height', () => {
|
|
20
|
+
const stack = stackOf('AAAA')
|
|
21
|
+
expect(stack).toHaveLength(1)
|
|
22
|
+
expect(stack[0]!.letter).toBe('A')
|
|
23
|
+
expect(stack[0]!.bits).toBeCloseTo(2, 10)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('an even four-way column carries no information', () => {
|
|
27
|
+
expect(stackOf('ACGT')).toEqual([])
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('a two-way split is half the height, shared evenly', () => {
|
|
31
|
+
const stack = stackOf('AACC')
|
|
32
|
+
expect(stack.map(s => s.letter)).toEqual(['A', 'C'])
|
|
33
|
+
// entropy 1 bit, so information content is 2 - 1 = 1, split evenly
|
|
34
|
+
expect(stack[0]!.bits).toBeCloseTo(0.5, 10)
|
|
35
|
+
expect(stack[1]!.bits).toBeCloseTo(0.5, 10)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('letters are ordered ascending so the tallest draws last, on top', () => {
|
|
39
|
+
const stack = stackOf('AAAC')
|
|
40
|
+
expect(stack.map(s => s.letter)).toEqual(['C', 'A'])
|
|
41
|
+
expect(stack[0]!.bits).toBeLessThan(stack[1]!.bits)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('ties break on letter so redraws are stable', () => {
|
|
45
|
+
expect(stackOf('GGCC').map(s => s.letter)).toEqual(['C', 'G'])
|
|
46
|
+
expect(stackOf('CCGG').map(s => s.letter)).toEqual(['C', 'G'])
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('gaps scale the stack down without skewing the frequencies', () => {
|
|
50
|
+
// 'A' is the only residue, so it takes the whole stack either way; the height
|
|
51
|
+
// is what the gaps change
|
|
52
|
+
const clean = stackOf('AAAA')
|
|
53
|
+
const gappy = stackOf('AA--')
|
|
54
|
+
expect(gappy[0]!.letter).toBe('A')
|
|
55
|
+
expect(gappy[0]!.bits).toBeCloseTo(clean[0]!.bits / 2, 10)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('gaps do not make a two-residue column look like a four-way one', () => {
|
|
59
|
+
// frequencies come from the two real residues, so this is a 1-bit choice
|
|
60
|
+
// scaled by the half-gap occupancy, not a 4-way split
|
|
61
|
+
const stack = stackOf('AC--')
|
|
62
|
+
expect(stack.map(s => s.letter)).toEqual(['A', 'C'])
|
|
63
|
+
expect(stack[0]!.bits + stack[1]!.bits).toBeCloseTo(0.5, 10)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('an all-gap column has no stack', () => {
|
|
67
|
+
expect(stackOf('----')).toEqual([])
|
|
68
|
+
expect(stackOf('....')).toEqual([])
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('the protein ceiling is higher than the nucleotide one', () => {
|
|
72
|
+
const protein = stackOf('WWWW', aminoBits)
|
|
73
|
+
expect(protein[0]!.bits).toBeCloseTo(aminoBits, 10)
|
|
74
|
+
expect(protein[0]!.bits).toBeGreaterThan(stackOf('AAAA')[0]!.bits)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('stack height never exceeds the ceiling', () => {
|
|
78
|
+
const columns = ['AAAA', 'AACG', 'A-C-', 'ACGT', 'AAAT']
|
|
79
|
+
for (const column of columns) {
|
|
80
|
+
const total = stackOf(column).reduce((a, b) => a + b.bits, 0)
|
|
81
|
+
expect(total).toBeLessThanOrEqual(dnaBits + 1e-9)
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('a column past the end of a ragged alignment is empty, not a crash', () => {
|
|
86
|
+
const counts = columnCountsFromColumns(['AC'])
|
|
87
|
+
expect(columnLogoStack(counts, 5, dnaBits)).toEqual([])
|
|
88
|
+
})
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { letterOfResidueSlot } from './columnCounts.ts'
|
|
2
|
+
|
|
3
|
+
import type { ColumnCounts } from './columnCounts.ts'
|
|
4
|
+
|
|
5
|
+
export interface LogoLetter {
|
|
6
|
+
letter: string
|
|
7
|
+
/** height of this letter's slice of the stack, in bits */
|
|
8
|
+
bits: number
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** the y-axis ceiling of a logo: the information content of a fully conserved
|
|
12
|
+
* column, log2 of the alphabet size */
|
|
13
|
+
export function maxBitsFor(sequenceType: 'dna' | 'rna' | 'amino') {
|
|
14
|
+
return Math.log2(sequenceType === 'amino' ? 20 : 4)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* One column of a sequence logo, tallest letter last so a caller can draw the
|
|
19
|
+
* stack bottom-up in array order.
|
|
20
|
+
*
|
|
21
|
+
* Heights follow Schneider & Stephens: the column's information content is
|
|
22
|
+
* `log2(alphabet) - H` bits, and each residue takes the share of that its own
|
|
23
|
+
* frequency earns. Frequencies come from the non-gap residues only, so a column
|
|
24
|
+
* of two residues and eight gaps is read as a two-way choice rather than a
|
|
25
|
+
* ten-way one -- otherwise every gappy column would look uninformative for the
|
|
26
|
+
* wrong reason.
|
|
27
|
+
*
|
|
28
|
+
* The whole stack is then scaled by the non-gap fraction, which plain WebLogo
|
|
29
|
+
* does not do. Here it earns its place: the track sits directly under an
|
|
30
|
+
* alignment where the gaps are visible, and a column that is 90% gap drawing a
|
|
31
|
+
* full-height stack off its one remaining pair of residues reads as a conserved
|
|
32
|
+
* site. Scaling makes stack height mean "how much this column tells you about
|
|
33
|
+
* the family", matching what the conservation track above it already plots.
|
|
34
|
+
*/
|
|
35
|
+
export function columnLogoStack(
|
|
36
|
+
colStats: ColumnCounts,
|
|
37
|
+
col: number,
|
|
38
|
+
maxBits: number,
|
|
39
|
+
): LogoLetter[] {
|
|
40
|
+
const total = colStats.total(col)
|
|
41
|
+
const nonGapTotal = total - colStats.gapCount(col)
|
|
42
|
+
if (nonGapTotal === 0) {
|
|
43
|
+
return []
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const informationContent =
|
|
47
|
+
Math.max(0, maxBits - colStats.entropy(col)) * (nonGapTotal / total)
|
|
48
|
+
if (informationContent === 0) {
|
|
49
|
+
return []
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const stack: LogoLetter[] = []
|
|
53
|
+
colStats.forEachResidue(col, (slot, count) => {
|
|
54
|
+
stack.push({
|
|
55
|
+
letter: letterOfResidueSlot(slot),
|
|
56
|
+
bits: (count / nonGapTotal) * informationContent,
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
// ascending, so drawing in order stacks the tallest letter on top. Ties break
|
|
60
|
+
// on letter to keep the stack order stable between redraws.
|
|
61
|
+
stack.sort((a, b) => a.bits - b.bits || a.letter.localeCompare(b.letter))
|
|
62
|
+
return stack
|
|
63
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -5,11 +5,17 @@ export interface Accession {
|
|
|
5
5
|
name: string
|
|
6
6
|
description: string
|
|
7
7
|
}
|
|
8
|
+
// which renderer draws a track's content. Every track kind draws into the same
|
|
9
|
+
// per-column coordinate space, so the kind picks the draw function rather than
|
|
10
|
+
// the geometry -- see drawTracks.ts, which dispatches on it.
|
|
11
|
+
export type TrackKind = 'text' | 'bar' | 'logo'
|
|
12
|
+
|
|
8
13
|
export interface BasicTrackModel {
|
|
9
14
|
id: string
|
|
10
15
|
name: string
|
|
11
16
|
associatedRowName?: string
|
|
12
17
|
height: number
|
|
18
|
+
kind: TrackKind
|
|
13
19
|
}
|
|
14
20
|
|
|
15
21
|
export interface TextTrackModel extends BasicTrackModel {
|
|
@@ -18,7 +24,7 @@ export interface TextTrackModel extends BasicTrackModel {
|
|
|
18
24
|
}
|
|
19
25
|
|
|
20
26
|
// a track that draws a per-column bar chart (conservation, property
|
|
21
|
-
// conservation)
|
|
27
|
+
// conservation)
|
|
22
28
|
export interface BarTrackModel extends BasicTrackModel {
|
|
23
29
|
barColor?: string
|
|
24
30
|
}
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '6.
|
|
1
|
+
export const version = '6.1.1'
|