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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 211x 211x 2x 4x 4x 2x 2x 2x 145x 145x 145x 7x 145x 448x 448x 448x 448x 448x 7x 7x 7x 7x 2x 267x 267x 1068x 1335x 1335x 1335x 1068x 267x 267x 267x 267x 267x 2x 2x 237x 237x 237x 237x 237x 237x 2x 218x 218x 2x 108x 108x 2x 2x 20x 20x 2x 2x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 98x 98x 98x 98x 14x 2x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 3x 6x 6x 2x 2x 2x | /**
* The genetic codes, and the six frames a sequence is read in (§8.8).
*
* Every table here is NCBI's, written the way NCBI writes it (`gc.prt`): two
* strings of 64 characters over the codons in `TCAG` order — the first base
* slowest, the third fastest, so `TTT` is 0, `TTC` 1 and `GGG` 63. `aas` is the
* amino acid each codon reads as, `*` for a stop; `starts` marks with `M` the
* codons a ribosome can begin at, and with `*` the stops. Copied as strings
* rather than rebuilt as a table of codons because the string is what every
* other tool prints and what a reader checking one would compare against: one
* typo in a dictionary of 64 entries is invisible, one in a string set beside
* NCBI's is a character out of a column.
*
* Pure, and with no idea of a canvas, so the lane, its readout and the tests all
* read a codon the same way.
*/
import type { BaseRegion } from './types'
/** One of NCBI's translation tables. */
export interface GeneticCode {
/** NCBI's number, the one `transl_table=` in a GenBank file names. */
id: number
name: string
/** The amino acid of each codon in `TCAG` order, `*` for a stop. */
aas: string
/** `M` where the codon can start a protein, `*` where it can stop one. */
starts: string
}
/**
* Every table NCBI publishes, by number.
*
* 7, 8, 17 – 20 are not missing by accident: NCBI retired them (7 and 8 were
* merged into 4 and 1), and a number that names nothing would be a choice that
* draws the standard code under another name. 27, 28 and 31 are the codes whose
* stops are *contextual* — `TGA` in a karyorelict is a tryptophan inside a gene
* and a stop at its end — and NCBI's `aas` gives the sense reading, which is
* what is drawn; the `*` in `starts` keeps the other one on record.
*/
export const GENETIC_CODES: readonly GeneticCode[] = [
{
id: 1,
name: 'Standard',
aas: 'FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '---M------**--*----M---------------M----------------------------',
},
{
id: 2,
name: 'Vertebrate Mitochondrial',
aas: 'FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNKKSS**VVVVAAAADDEEGGGG',
starts: '----------**--------------------MMMM----------**---M------------',
},
{
id: 3,
name: 'Yeast Mitochondrial',
aas: 'FFLLSSSSYY**CCWWTTTTPPPPHHQQRRRRIIMMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '----------**----------------------MM---------------M------------',
},
{
id: 4,
name: 'Mold, Protozoan, Coelenterate Mitochondrial; Mycoplasma, Spiroplasma',
aas: 'FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '--MM------**-------M------------MMMM---------------M------------',
},
{
id: 5,
name: 'Invertebrate Mitochondrial',
aas: 'FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNKKSSSSVVVVAAAADDEEGGGG',
starts: '---M------**--------------------MMMM---------------M------------',
},
{
id: 6,
name: 'Ciliate, Dasycladacean and Hexamita Nuclear',
aas: 'FFLLSSSSYYQQCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '--------------*--------------------M----------------------------',
},
{
id: 9,
name: 'Echinoderm and Flatworm Mitochondrial',
aas: 'FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIIMTTTTNNNKSSSSVVVVAAAADDEEGGGG',
starts: '----------**-----------------------M---------------M------------',
},
{
id: 10,
name: 'Euplotid Nuclear',
aas: 'FFLLSSSSYY**CCCWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '----------**-----------------------M----------------------------',
},
{
id: 11,
name: 'Bacterial, Archaeal and Plant Plastid',
aas: 'FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '---M------**--*----M------------MMMM---------------M------------',
},
{
id: 12,
name: 'Alternative Yeast Nuclear',
aas: 'FFLLSSSSYY**CC*WLLLSPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '----------**--*----M---------------M----------------------------',
},
{
id: 13,
name: 'Ascidian Mitochondrial',
aas: 'FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNKKSSGGVVVVAAAADDEEGGGG',
starts: '---M------**----------------------MM---------------M------------',
},
{
id: 14,
name: 'Alternative Flatworm Mitochondrial',
aas: 'FFLLSSSSYYY*CCWWLLLLPPPPHHQQRRRRIIIMTTTTNNNKSSSSVVVVAAAADDEEGGGG',
starts: '-----------*-----------------------M----------------------------',
},
{
id: 15,
name: 'Blepharisma Macronuclear',
aas: 'FFLLSSSSYY*QCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '----------*---*--------------------M----------------------------',
},
{
id: 16,
name: 'Chlorophycean Mitochondrial',
aas: 'FFLLSSSSYY*LCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '----------*---*--------------------M----------------------------',
},
{
id: 21,
name: 'Trematode Mitochondrial',
aas: 'FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNNKSSSSVVVVAAAADDEEGGGG',
starts: '----------**-----------------------M---------------M------------',
},
{
id: 22,
name: 'Scenedesmus obliquus Mitochondrial',
aas: 'FFLLSS*SYY*LCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '------*---*---*--------------------M----------------------------',
},
{
id: 23,
name: 'Thraustochytrium Mitochondrial',
aas: 'FF*LSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '--*-------**--*-----------------M--M---------------M------------',
},
{
id: 24,
name: 'Rhabdopleuridae Mitochondrial',
aas: 'FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSSKVVVVAAAADDEEGGGG',
starts: '---M------**-------M---------------M---------------M------------',
},
{
id: 25,
name: 'Candidate Division SR1 and Gracilibacteria',
aas: 'FFLLSSSSYY**CCGWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '---M------**-----------------------M---------------M------------',
},
{
id: 26,
name: 'Pachysolen tannophilus Nuclear',
aas: 'FFLLSSSSYY**CC*WLLLAPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '----------**--*----M---------------M----------------------------',
},
{
id: 27,
name: 'Karyorelict Nuclear',
aas: 'FFLLSSSSYYQQCCWWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '--------------*--------------------M----------------------------',
},
{
id: 28,
name: 'Condylostoma Nuclear',
aas: 'FFLLSSSSYYQQCCWWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '----------**--*--------------------M----------------------------',
},
{
id: 29,
name: 'Mesodinium Nuclear',
aas: 'FFLLSSSSYYYYCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '--------------*--------------------M----------------------------',
},
{
id: 30,
name: 'Peritrich Nuclear',
aas: 'FFLLSSSSYYEECC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '--------------*--------------------M----------------------------',
},
{
id: 31,
name: 'Blastocrithidia Nuclear',
aas: 'FFLLSSSSYYEECCWWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '----------**-----------------------M----------------------------',
},
{
id: 32,
name: 'Balanophoraceae Plastid',
aas: 'FFLLSSSSYY*WCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG',
starts: '---M------*---*----M------------MMMM---------------M------------',
},
{
id: 33,
name: 'Cephalodiscidae Mitochondrial',
aas: 'FFLLSSSSYYY*CCWWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSSKVVVVAAAADDEEGGGG',
starts: '---M-------*-------M---------------M---------------M------------',
},
]
/** The code a new lane reads with: NCBI's table 1. */
export const DEFAULT_GENETIC_CODE = 1
/** A table by number, or the standard one for a number NCBI does not use. */
export function geneticCode(id: number): GeneticCode {
return GENETIC_CODES.find((code) => code.id === id) ?? GENETIC_CODES[0]
}
/** `true` when NCBI has a table of this number. */
export function isGeneticCode(id: unknown): id is number {
return typeof id === 'number' && GENETIC_CODES.some((code) => code.id === id)
}
/**
* What a codon is, for the picture: the four things a frame is drawn as.
*
* `start` is a methionine the table lets a protein begin at — `ATG`, and `ATA`
* where a mitochondrion reads it as M. `alternative` is a start that reads as
* something else inside a gene (`TTG` as L, `GTG` as V): table 11 has six of
* them and table 4 seven, and drawing all of them green would turn every frame
* of a Mycoplasma genome into a start every few codons. They are marked, not
* filled.
*/
export const CODON = { sense: 0, start: 1, alternative: 2, stop: 3, unknown: 4 } as const
export type CodonKind = (typeof CODON)[keyof typeof CODON]
/** One table, unpacked for the paint loop: 64 kinds and 64 letters. */
export interface CodonTable {
code: GeneticCode
kinds: Uint8Array
}
const tables = new Map<number, CodonTable>()
/** The table for a code, built once. */
export function codonTable(id: number): CodonTable {
const code = geneticCode(id)
const held = tables.get(code.id)
if (held !== undefined) return held
const kinds = new Uint8Array(64)
for (let i = 0; i < 64; i++) {
const aa = code.aas[i]
const start = code.starts[i] === 'M'
kinds[i] =
aa === '*' ? CODON.stop : start && aa === 'M' ? CODON.start : start ? CODON.alternative : CODON.sense
}
const table = { code, kinds }
tables.set(code.id, table)
return table
}
/**
* A base's place in `TCAG` order, `-1` for anything else.
*
* Upper and lower case alike — soft-masking says a stretch is a repeat, not
* that it codes for something else — and `U` as `T`, so an RNA reads.
*/
const NUCLEOTIDE = ((): Int8Array => {
const table = new Int8Array(256).fill(-1)
const set = (letters: string, index: number): void => {
for (const letter of letters) {
table[letter.charCodeAt(0)] = index
table[letter.toLowerCase().charCodeAt(0)] = index
}
}
set('TU', 0)
set('C', 1)
set('A', 2)
set('G', 3)
return table
})()
/**
* A codon's index in `TCAG` order, or `-1` where any of its bases is not one.
*
* `reverse` reads the three bytes as the other strand does: the complement of
* the third, then the second, then the first. In `TCAG` order the complement of
* index `i` is `i ^ 2` (T↔A, C↔G), so that is arithmetic and not a second table.
*/
export function codonIndex(b0: number, b1: number, b2: number, reverse = false): number {
const n0 = NUCLEOTIDE[b0]
const n1 = NUCLEOTIDE[b1]
const n2 = NUCLEOTIDE[b2]
if (n0 < 0 || n1 < 0 || n2 < 0) return -1
return reverse ? ((n2 ^ 2) << 4) | ((n1 ^ 2) << 2) | (n0 ^ 2) : (n0 << 4) | (n1 << 2) | n2
}
/** The amino acid at an index, `X` for a codon with a base that is not one. */
export function aminoAcidAt(table: CodonTable, index: number): string {
return index < 0 ? 'X' : table.code.aas[index]
}
/** What the codon at an index is drawn as. */
export function codonKindAt(table: CodonTable, index: number): CodonKind {
return index < 0 ? CODON.unknown : (table.kinds[index] as CodonKind)
}
/**
* The six frames, top to bottom as the lane draws them.
*
* `+1` reads the codons that begin at a multiple of three, counted from the
* sequence's first base (0-based); `+2` and `+3` one and two bases on. `−1`,
* `−2` and `−3` are the reverse complement read in the **same boxes** as `+1`,
* `+2` and `+3`. Both are anchored to the start of the sequence rather than to
* the window or to its far end, so a frame is a property of the genome and does
* not move when the view does — and a codon in `+2` sits exactly above its
* complement in `−2`.
*/
export const READING_FRAMES = [1, 2, 3, -1, -2, -3] as const
export type ReadingFrame = (typeof READING_FRAMES)[number]
/** The first base of the frame's codons, as a residue mod 3. */
export function phaseOf(frame: ReadingFrame): number {
return Math.abs(frame) - 1
}
/** `+1`, `−2` — the frame as a label, with a real minus sign. */
export function frameName(frame: ReadingFrame): string {
return frame > 0 ? `+${frame}` : `−${-frame}`
}
/**
* Visit every whole codon of one frame inside `[from, to)` of one region.
*
* A callback rather than an array because the lane asks for it on every frame
* of a drag, and at the coarsest zoom it draws that is fifty thousand codons a
* row: a visitor allocates nothing. A codon that runs off either end of the
* region is not visited — its third base has not arrived, and guessing one would
* be translating a codon that is not there.
*/
export function forEachCodon(
region: BaseRegion,
frame: ReadingFrame,
table: CodonTable,
from: number,
to: number,
visit: (start: number, index: number, kind: CodonKind) => void,
): void {
const low = Math.max(region.start, Math.floor(from))
const high = Math.min(region.end, Math.ceil(to))
const phase = phaseOf(frame)
// The first codon start at or after `low` that is in this frame.
let start = low + ((((phase - low) % 3) + 3) % 3)
const reverse = frame < 0
const bases = region.bases
for (; start + 3 <= high; start += 3) {
const at = start - region.start
const index = codonIndex(bases[at], bases[at + 1], bases[at + 2], reverse)
visit(start, index, codonKindAt(table, index))
}
}
/**
* A stretch translated in one frame, as a string of one-letter amino acids.
*
* The lane draws through {@link forEachCodon}; this is the same reading as a
* word, for the readout and for a test to hold against a known protein.
*/
export function translateFrame(
region: BaseRegion,
frame: ReadingFrame,
code: number,
from = region.start,
to = region.end,
): string {
const table = codonTable(code)
const out: string[] = []
forEachCodon(region, frame, table, from, to, (_start, index) => out.push(aminoAcidAt(table, index)))
// The reverse frames are read right to left, so their protein is too.
return frame < 0 ? out.reverse().join('') : out.join('')
}
/**
* The codon of one frame that a position is inside, for the readout.
*
* `null` outside what has arrived. The codon is written as the strand reads it,
* so a reverse frame's `CAT` box answers `ATG → M`.
*/
export function codonAt(
regions: readonly BaseRegion[],
contig: string,
bp: number,
frame: ReadingFrame,
code: number,
): { start: number; codon: string; aa: string } | null {
const at = Math.floor(bp)
const phase = phaseOf(frame)
const start = at - ((((at - phase) % 3) + 3) % 3)
for (const region of regions) {
if (region.contig !== contig || start < region.start || start + 3 > region.end) continue
const i = start - region.start
const bytes = [region.bases[i], region.bases[i + 1], region.bases[i + 2]]
const index = codonIndex(bytes[0], bytes[1], bytes[2], frame < 0)
const letters = bytes.map((byte) => String.fromCharCode(byte).toUpperCase())
const codon =
frame < 0
? letters
.reverse()
.map((letter) => COMPLEMENT[letter] ?? letter)
.join('')
: letters.join('')
return { start, codon, aa: aminoAcidAt(codonTable(code), index) }
}
return null
}
const COMPLEMENT: Record<string, string> = { A: 'T', T: 'A', U: 'A', C: 'G', G: 'C' }
|