Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | 2x 2x 2x 2x 2x 2x 2x 32x 32x 32x 32x 32x 32x 2x 504x 504x 504x 504x 504x 504x 504x 2x 2x 2x 107x 107x 107x 107x 107x 107x 107x 79x 79x 20x 15x 1x 1x 2x 1020x 1020x 1020x 1020x 1020x 1020x 1020x 1020x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 2x 2x 2x 2x 2x 12x 12x 12x 2x 2x 2x 2x 2x 2x 2x 4x 4x 2x 2x 2x 2x 3x 3x 3x 3x 3x 12x 12x 18x 18x 12x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 6x 6x 1x 1x 10x 10x 10x 10x 5x 5x 5x 4x 1x 5x 5x 5x 10x 10x 10x 10x 10x 10x 30x 30x 30x 30x 30x 5x 5x 5x 10x 10x 10x 2x 4x 4x 4x 4x 4x 4x 4x 2x 3x 3x 3x 11x 11x 11x 11x 11x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 108x 108x 108x 1008x 1008x 174x 174x 174x 1008x 1008x 1008x 1008x 108x 108x 2x 268x 268x 268x 233x 233x 1x 1x 1x 1x 1x 1x 233x 232x 232x 232x 232x 232x 232x 233x 35x 92x 32x 32x 3x 92x 2x 2x 1x 1x 228x 228x 228x 20x 20x 20x 20x 20x 20x 20x 20x 20x 12x 12x 11x 1x 20x 20x 18x 18x 18x 18x 18x 14x 18x 18x 18x 10x 10x 14x 14x 2x 18x 18x 18x 18x 18x 18x 18x 18x 54x 54x 54x 54x 54x 2x 1x 1x 1x | /**
* What colour each bead is drawn in, and the key that says so (§25).
*
* One resolver, for the reason the map's colormap and the lanes' `featureInk`
* are one function each: the three.js adapter wants an RGB triple per bead in
* a `Float32Array` for its instance buffer, the vector painter wants a CSS
* string per bead, and the key in the corner wants the levels or the ramp — and
* three answers computed three ways would be three chances for the picture to
* disagree with its own legend. Everything here resolves once into
* {@link BeadColours}, which carries all three views of the same decision.
*
* ## Fitted to the file, never to the window
*
* `render/featureInk.ts`'s rule, kept: a ramp over the score column runs over
* the whole file's range, a category palette is assigned over the whole file's
* levels, and neither changes when the camera moves or the map zooms. A
* colouring that meant something different after a rotation would be a legend
* that lies.
*
* ## What is resolved here and what is not
*
* The built-ins — sequence, haplotype, position, score — and any field of the
* file are resolved from the row alone. A colouring by *another* row (a bigWig,
* a feature set) or by the pin (distance, contacts) needs things this module
* does not hold, so it is handed a {@link ColourSource} that answers a value
* per bead and stays a pure function of what it was given.
*/
import { featureColouring, type FeatureKey } from '../render/featureInk'
import { categoryColour } from '../render/meta'
import { colormapCss } from '../render/colormaps'
import type { StructureStyle } from '../annotations/types'
import {
STRUCTURE_COLOUR_CONTACTS,
STRUCTURE_COLOUR_DISPLACEMENT,
STRUCTURE_COLOUR_DISTANCE,
STRUCTURE_COLOUR_HAPLOTYPE,
STRUCTURE_COLOUR_POSITION,
STRUCTURE_COLOUR_SEQUENCE,
STRUCTURE_COLOUR_TRACK,
} from '../annotations/types'
import type { Structure } from './scene'
/** The ramp a per-bead number is drawn on, where nothing else is said. */
export const STRUCTURE_RAMP = 'viridis'
/**
* The colour scale that runs from white to **the track's own colour** (§25.5),
* as `StructureStyle.colormap` names it: a model coloured by H3K27ac in the
* orange its lane is drawn in. Not a colormap of `render/colormaps.ts`, since
* its top end is the row's and not the table's; a colouring with no colour of
* its own (`ColourSource.tint`) draws it on `STRUCTURE_RAMP`.
*/
export const TRACK_TINT_RAMP = 'track'
/**
* A ramp's colour at `t` in `0..1`: a colormap's, or white to `tint` under
* {@link TRACK_TINT_RAMP}. One function for the beads and the key, so the two
* cannot be drawn on two ramps.
*/
export function rampCss(colormap: string, t: number, tint?: string): string {
if (colormap !== TRACK_TINT_RAMP) return colormapCss(colormap, t)
const [r, g, b] = parseColour(tint ?? '#000000')
const u = Math.max(0, Math.min(1, t))
const mix = (end: number): number => Math.round(255 + (end * 255 - 255) * u)
return `rgb(${mix(r)}, ${mix(g)}, ${mix(b)})`
}
/**
* A value per bead from somewhere outside the row: a track sampled over each
* bead's interval, a feature set's colour where a bead overlaps one, the
* distance to the pinned bead. `NaN` is *no value* and draws as the ghost
* colour.
*/
export interface ColourSource {
/** What the key calls it. */
label: string
/** Either a number per bead (drawn on a ramp) or a colour per bead. */
values?: Float32Array
colours?: Array<string | null>
/** The ramp's extent, where `values` are given; fitted to the values otherwise. */
range?: { min: number; max: number }
/** The ramp, where `values` are given. */
colormap?: string
/** The row's own colour, the top of {@link TRACK_TINT_RAMP} — a track's lane colour. */
tint?: string
/**
* The categories `colours` are drawn from, where they are categories — a
* feature set's names (§25.5). Without them a colour per bead has no key.
*/
levels?: StructureLevel[]
}
/**
* One row of a levels key. `name` is what a swatch picked on it is written
* under, where the row can be recoloured — a feature set's name, or `''` for
* its `other` bucket.
*/
export interface StructureLevel {
label: string
colour: string
name?: string
}
/**
* Whether a colouring is drawn on a **ramp**, and so has a scale to change.
*
* Asked of the colouring rather than of a resolved {@link BeadColours}, because
* the settings card is painted before the colours are: a control that waited
* for them would never appear at all. Kept beside {@link resolveColours} so the
* two cannot drift about which colourings are ramps.
*
* A colouring by a *field* of the file is left out deliberately. It goes
* through §8.16's machinery, which decides levels or ramp from the column's own
* values, and the answer is the lane's to give rather than this module's.
*/
export function usesRamp(colourBy: string): boolean {
return (
colourBy === STRUCTURE_COLOUR_POSITION ||
colourBy === STRUCTURE_COLOUR_DISTANCE ||
colourBy === STRUCTURE_COLOUR_CONTACTS ||
colourBy === STRUCTURE_COLOUR_DISPLACEMENT ||
colourBy.startsWith(STRUCTURE_COLOUR_TRACK)
)
}
/** One resolved colouring: three views of the same decision. */
export interface BeadColours {
/** `r, g, b` per bead in `0..1`, for an instance buffer. */
rgb: Float32Array
/** The same as CSS strings, for a 2D context. */
css: string[]
/** What the corner key draws. */
key: StructureKey
}
/** The legend for a colouring, in the shapes the corner key can draw. */
export type StructureKey =
| { kind: 'levels'; title: string; levels: StructureLevel[] }
| { kind: 'ramp'; title: string; colormap: string; min: number; max: number; tint?: string }
| { kind: 'none'; title: string }
/** What a bead with nothing to say is drawn in — the grey a `not set` feature takes. */
export const UNSET_COLOUR = '#9a9a9a'
/**
* What a telomere bead is drawn in (§25.5): the palette's own yellow.
*
* `CATEGORY_PALETTE`'s yellow rather than a fresh one, because it is the yellow
* everything else in this application means when it means yellow, and because
* Okabe–Ito is the one palette here chosen for being legible to a colour-blind
* reader. It can collide with a sequence the palette also gave yellow to; a
* telomere bead is drawn at `MARKED_BEAD` times the radius and sits at the end
* of a fibre, which is enough to tell the two apart.
*/
export const TELOMERE_COLOUR = '#f0e442'
/**
* Resolve the colouring a style names, over a structure.
*
* `source` is consulted only for a `colourBy` this module cannot resolve on
* its own, and a style naming one with no source handed over falls back to
* colouring by sequence — a stored session reopened without the track it was
* coloured by is a model still worth looking at.
*/
export function resolveColours(
structure: Structure,
style: StructureStyle,
source: ColourSource | null = null,
): BeadColours {
const by = style.colourBy
// The reader's ramp where they chose one; each colouring's own otherwise.
const ramp = style.colormap === '' ? null : style.colormap
if (by === STRUCTURE_COLOUR_SEQUENCE || by === '') return bySequence(structure, style.sequenceColours)
if (by === STRUCTURE_COLOUR_HAPLOTYPE) return byHaplotype(structure)
if (by === STRUCTURE_COLOUR_POSITION) return byPosition(structure, ramp)
if (source !== null) return bySource(structure, source, ramp)
if (by.includes(':')) return bySequence(structure, style.sequenceColours)
return byField(structure, by)
}
/**
* One hue per contig, from the categorical palette, in contig order.
*
* Takes the structure so that a caller cannot pass a contig index of some
* other set, and so the day one palette per genome is wanted (a `.metacool`'s
* MAGs) the signature already has what it needs.
*/
export function sequenceColour(
structure: Structure,
contig: number,
/** The reader's own colours by sequence name (§25.5), over the palette's. */
chosen: Readonly<Record<string, string>> = {},
): string {
const own = chosen[structure.contigs[contig] ?? '']
if (own !== undefined) return own
return categoryColour(contig % Math.max(1, structure.contigs.length))
}
function bySequence(structure: Structure, chosen: Readonly<Record<string, string>> = {}): BeadColours {
const set = structure.set
const css = new Array<string>(set.length)
for (let i = 0; i < set.length; i++) css[i] = sequenceColour(structure, set.contig[i], chosen)
return {
rgb: rgbOf(css),
css,
key: {
kind: 'levels',
title: 'sequence',
levels: structure.contigs.map((name, at) => ({ label: name, colour: sequenceColour(structure, at, chosen) })),
},
}
}
/**
* The sequence's hue at one lightness per homologue, so a homologue reads as
* the *same* chromosome and the pair reads as a pair.
*
* Two lightnesses rather than two hues: a second hue per chromosome is
* thirty-two colours on a sixteen-chromosome genome, which no legend can carry.
*/
function byHaplotype(structure: Structure): BeadColours {
const set = structure.set
const haps = structure.haplotypes.length
const css = new Array<string>(set.length)
for (let i = 0; i < set.length; i++) {
const base = sequenceColour(structure, set.contig[i])
css[i] = haps > 1 ? lightness(base, structure.model.hap[i] / Math.max(1, haps - 1)) : base
}
return {
rgb: rgbOf(css),
css,
key: {
kind: 'levels',
title: 'haplotype',
levels: structure.haplotypes.map((name, at) => ({
label: name,
colour: haps > 1 ? lightness('#666666', at / Math.max(1, haps - 1)) : '#666666',
})),
},
}
}
/** A ramp from each chain's first bead to its last. */
function byPosition(structure: Structure, ramp: string | null = null): BeadColours {
const set = structure.set
const colormap = ramp === null || ramp === TRACK_TINT_RAMP ? STRUCTURE_RAMP : ramp
const css = new Array<string>(set.length).fill(UNSET_COLOUR)
for (const chain of structure.chains) {
const n = chain.beads.length
for (let k = 0; k < n; k++) {
css[chain.beads[k]] = colormapCss(colormap, n > 1 ? k / (n - 1) : 0)
}
}
return {
rgb: rgbOf(css),
css,
key: { kind: 'ramp', title: 'linear position along the sequence', colormap, min: 0, max: 1 },
}
}
/**
* A field of the file — the score column, a haplotype, anything the g3d's
* detail carries — through the §8.16 machinery the lanes already use, so the
* model and the lane of the same row cannot disagree about a colour.
*/
function byField(structure: Structure, key: string): BeadColours {
const set = structure.set
const colouring = featureColouring(set, 'structure', key, UNSET_COLOUR)
const css = new Array<string>(set.length)
for (let i = 0; i < set.length; i++) {
css[i] = colouring.colourOf === undefined ? UNSET_COLOUR : colouring.colourOf(i)
}
return { rgb: rgbOf(css), css, key: keyOf(colouring.key) }
}
/** A value or a colour per bead from outside the row. */
function bySource(structure: Structure, source: ColourSource, ramp: string | null = null): BeadColours {
const n = structure.set.length
const css = new Array<string>(n).fill(UNSET_COLOUR)
if (source.colours !== undefined) {
for (let i = 0; i < n; i++) css[i] = source.colours[i] ?? UNSET_COLOUR
const key: StructureKey =
source.levels === undefined
? { kind: 'none', title: source.label }
: { kind: 'levels', title: source.label, levels: source.levels }
return { rgb: rgbOf(css), css, key }
}
const values = source.values ?? new Float32Array(n).fill(Number.NaN)
// The reader's choice first, then the source's own — a track's lane scale,
// the map's colormap for contacts — and `STRUCTURE_RAMP` for the rest.
// White to the row's own colour asks for one; a colouring with none (a
// distance, the contacts) stays on its own ramp rather than on black.
const asked = ramp === TRACK_TINT_RAMP && source.tint === undefined ? null : ramp
const colormap = asked ?? source.colormap ?? STRUCTURE_RAMP
const tint = colormap === TRACK_TINT_RAMP ? source.tint : undefined
const range = source.range ?? robustRangeOf(values)
const span = range.max - range.min
for (let i = 0; i < n; i++) {
const value = values[i]
if (!Number.isFinite(value)) continue
const t = span > 0 ? (value - range.min) / span : 0.5
css[i] = rampCss(colormap, Math.max(0, Math.min(1, t)), tint)
}
return {
rgb: rgbOf(css),
css,
key: { kind: 'ramp', title: source.label, colormap, min: range.min, max: range.max, ...(tint === undefined ? {} : { tint }) },
}
}
/**
* The 2nd to the 98th percentile of the finite values, or `[0, 1]` for none.
*
* What a colouring with no limits of its own is fitted to: a track sampled
* over a whole genome has a spike somewhere — the rDNA, a collapsed repeat —
* and a ramp stretched to it paints every other bead the same dark, which is
* a picture of one outlier. The lane's own limits, where the reader set them,
* still win (`ColourSource.range`).
*/
export function robustRangeOf(values: Float32Array): { min: number; max: number } {
const finite = Array.from(values).filter((v) => Number.isFinite(v)).sort((a, b) => a - b)
if (finite.length === 0) return { min: 0, max: 1 }
const at = (q: number): number => finite[Math.min(finite.length - 1, Math.max(0, Math.round(q * (finite.length - 1))))]
const min = at(0.02)
const max = at(0.98)
return max > min ? { min, max } : rangeOf(values)
}
/** The finite extent of a value array, or `[0, 1]` for one with none. */
export function rangeOf(values: Float32Array): { min: number; max: number } {
let min = Infinity
let max = -Infinity
for (let i = 0; i < values.length; i++) {
const v = values[i]
if (!Number.isFinite(v)) continue
if (v < min) min = v
if (v > max) max = v
}
return min <= max ? { min, max } : { min: 0, max: 1 }
}
/** The lanes' key, in the corner's vocabulary. */
function keyOf(key: FeatureKey): StructureKey {
if (key.ramp !== null) {
// The lanes draw a numeric field on `viridis` and say so nowhere but in
// their own code; the key here names the same ramp so the two agree.
return {
kind: 'ramp',
title: key.field.label,
colormap: STRUCTURE_RAMP,
min: key.ramp.min,
max: key.ramp.max,
}
}
if (key.levels.length > 0) {
return {
kind: 'levels',
title: key.field.label,
levels: key.levels.map((level) => ({ label: level.label, colour: level.colour })),
}
}
return { kind: 'none', title: key.field.label }
}
// ---------------------------------------------------------------------------
// Colour arithmetic
// ---------------------------------------------------------------------------
/** CSS strings to an `r, g, b` triple per bead, in `0..1`. */
export function rgbOf(css: readonly string[]): Float32Array {
const out = new Float32Array(css.length * 3)
const cache = new Map<string, [number, number, number]>()
for (let i = 0; i < css.length; i++) {
let rgb = cache.get(css[i])
if (rgb === undefined) {
rgb = parseColour(css[i])
cache.set(css[i], rgb)
}
out[i * 3] = rgb[0]
out[i * 3 + 1] = rgb[1]
out[i * 3 + 2] = rgb[2]
}
return out
}
/**
* A CSS colour as `r, g, b` in `0..1`.
*
* Hex in its three lengths, `rgb()` and `hsl()` with either separator — which
* is every colour this app writes: the palettes are hex, the pickers write hex,
* `render/hue.ts` writes `hsl(h s% l%)` and the colormaps write `rgb(r, g, b)`.
* Anything else is the unset grey rather than black, so a colour this parser
* has not been taught is visible as *unknown* rather than as ink.
*/
export function parseColour(text: string): [number, number, number] {
const value = text.trim().toLowerCase()
const hex = /^#([0-9a-f]{3,8})$/.exec(value)
if (hex !== null) {
const digits = hex[1]
if (digits.length === 3 || digits.length === 4) {
return [
parseInt(digits[0] + digits[0], 16) / 255,
parseInt(digits[1] + digits[1], 16) / 255,
parseInt(digits[2] + digits[2], 16) / 255,
]
}
if (digits.length === 6 || digits.length === 8) {
return [
parseInt(digits.slice(0, 2), 16) / 255,
parseInt(digits.slice(2, 4), 16) / 255,
parseInt(digits.slice(4, 6), 16) / 255,
]
}
}
const rgb = /^rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)/.exec(value)
if (rgb !== null) {
return [Number(rgb[1]) / 255, Number(rgb[2]) / 255, Number(rgb[3]) / 255].map(clamp01) as [
number,
number,
number,
]
}
const hsl = /^hsla?\(\s*([\d.]+)(?:deg)?[\s,]+([\d.]+)%[\s,]+([\d.]+)%/.exec(value)
if (hsl !== null) {
return hslToRgb(Number(hsl[1]), Number(hsl[2]) / 100, Number(hsl[3]) / 100)
}
return parseColour(UNSET_COLOUR)
}
function clamp01(value: number): number {
return Math.max(0, Math.min(1, value))
}
function hslToRgb(h: number, s: number, l: number): [number, number, number] {
const hue = ((h % 360) + 360) % 360
const c = (1 - Math.abs(2 * l - 1)) * s
const x = c * (1 - Math.abs(((hue / 60) % 2) - 1))
const m = l - c / 2
let r = 0
let g = 0
let b = 0
if (hue < 60) [r, g, b] = [c, x, 0]
else if (hue < 120) [r, g, b] = [x, c, 0]
else if (hue < 180) [r, g, b] = [0, c, x]
else if (hue < 240) [r, g, b] = [0, x, c]
else if (hue < 300) [r, g, b] = [x, 0, c]
else [r, g, b] = [c, 0, x]
return [clamp01(r + m), clamp01(g + m), clamp01(b + m)]
}
function rgbToHsl(r: number, g: number, b: number): [number, number, number] {
const max = Math.max(r, g, b)
const min = Math.min(r, g, b)
const l = (max + min) / 2
if (max === min) return [0, 0, l]
const d = max - min
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
let h: number
if (max === r) h = ((g - b) / d + (g < b ? 6 : 0)) * 60
else if (max === g) h = ((b - r) / d + 2) * 60
else h = ((r - g) / d + 4) * 60
return [h, s, l]
}
/**
* A colour at another lightness: `t = 0` is the colour a little darker, `t = 1`
* a good deal lighter, so two homologues of one chromosome sit either side of
* the sequence's own colour.
*/
export function lightness(colour: string, t: number): string {
const [r, g, b] = parseColour(colour)
const [h, s, l] = rgbToHsl(r, g, b)
const dark = Math.max(0.15, l - 0.12)
const light = Math.min(0.85, l + 0.28)
const at = dark + (light - dark) * clamp01(t)
const [rr, gg, bb] = hslToRgb(h, s, at)
return `#${hex(rr)}${hex(gg)}${hex(bb)}`
}
function hex(value: number): string {
return Math.round(clamp01(value) * 255)
.toString(16)
.padStart(2, '0')
}
/** A colour string as a `0xrrggbb` number, for three.js's `Color.setHex`. */
export function hexOf(colour: string): number {
const [r, g, b] = parseColour(colour)
return (Math.round(r * 255) << 16) | (Math.round(g * 255) << 8) | Math.round(b * 255)
}
|