This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dnaviz.ts
441 lines (433 loc) · 12.4 KB
/
dnaviz.ts
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
// different methods of transforming a sequence of dna into a series of coordinates for 2D visualization
// squiggle
/**
* Generates squiggle 2D Visualization of DNA from any sequence
*
* @param sequence - sequence of DNA or RNA (lowercase and mixed cases are valid)
*
* @returns coordinates for 2D visualization of DNA based on the Squiggle algorithm
*
* @remarks Lee, B. D. (2018). Squiggle: a user-friendly two-dimensional DNA sequence visualization tool. *Bioinformatics*. doi:10.1093/bioinformatics/bty807.
*
* Example Usage
* ```ts
* squiggle('ATGC')
* // returns [[0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4], [0, 0.5, 0, -0.5, -1, -0.5, 0, -0.5, 0]]
* ```
*
*
* Squiggle’s DNA visualization method is based on the UCSC .2bit format and the Qi et. al Huffman coding method. In essence, a DNA sequence is first converted into binary using the 2bit encoding scheme that maps T to 00, C to 01, A to 10, and G to 11. The bits can be mapped as end to end vectors.
*
* For more information visit: https://squiggle.readthedocs.io/en/latest/methods.html#squiggle
*/
export function squiggle(sequence: string): number[][] {
sequence = sequence.toUpperCase();
const x: number[] = [0.0];
const y: number[] = [0.0];
let yCoord: number = 0.0;
for (const character of sequence) {
x.push(x[x.length - 1] + 0.5);
x.push(x[x.length - 1] + 0.5);
if (character === 'A') {
y.push(yCoord + 0.5);
y.push(yCoord);
} else if (character === 'C') {
y.push(yCoord - 0.5);
y.push(yCoord);
} else if (character === 'T' || character === 'U') {
y.push(yCoord - 0.5);
y.push(yCoord - 1);
yCoord -= 1.0;
} else if (character === 'G') {
y.push(yCoord + 0.5);
y.push(yCoord + 1);
yCoord += 1.0;
} else {
y.push(yCoord);
y.push(yCoord);
}
}
const result: number[][] = [];
result.push(x);
result.push(y);
return result;
}
// yau
/**
* Generates yau 2D Visualization of DNA from any sequence
*
* @param sequence - sequence of DNA or RNA (lowercase and mixed cases are valid)
*
* @returns coordinates for 2D visualization of DNA based on the Yau algorithm
*
* @remarks Yau, S. S., Wang, J., Niknejad, A., Lu, C., Jin, N., & Ho, Y. K. (2003). DNA sequence representation without degeneracy. *Nucleic acids research, 31(12)*, 3078-80.
*
* Example Usage
* ```ts
* yau('ATGC')
* // returns [[0, 0.5, 1, 1 + 3 ** 0.5 / 2, 1 + 2 * (3 ** 0.5 / 2)], [0, -(3 ** 0.5 / 2), 0, -0.5, 0]]
* ```
*
*
* Yau et. al’s method uses unit vectors with upward vectors indicating pyrimidine bases (C and T) and downward vectors indicating purine bases (A and G). Similar to Squiggle, this method has no degeneracy.
*
* The unit vectors are based on 30 and 60 degree values on the unit circle.
*
* Specifically:
*
* ```ts
* A = (0.5, -3**0.5/2) // ** represents exponents in js
* T = (0.5, 3**0.5/2)
* C = (3**0.5/2, 0.5)
* G = (3**0.5/2, -0.5)
* ```
*
* *Warning: The x-coordinate is not equivalent to the base position
*
* For more information visit: https://squiggle.readthedocs.io/en/latest/methods.html#yau
*/
export function yau(sequence: string): number[][] {
sequence = sequence.toUpperCase();
const x: number[] = [0.0];
const y: number[] = [0.0];
let xCoord: number = 0.0;
let yCoord: number = 0.0;
for (const character of sequence) {
if (character === 'A') {
xCoord = xCoord + 0.5;
yCoord = yCoord - 3 ** 0.5 / 2;
} else if (character === 'C') {
xCoord = xCoord + 3 ** 0.5 / 2;
yCoord = yCoord + 0.5;
} else if (character === 'T' || character === 'U') {
xCoord = xCoord + 0.5;
yCoord = yCoord + 3 ** 0.5 / 2;
} else if (character === 'G') {
xCoord = xCoord + 3 ** 0.5 / 2;
yCoord = yCoord - 0.5;
} else {
throw new Error('non-atgcu base');
}
x.push(xCoord);
y.push(yCoord);
}
const result: number[][] = [];
result.push(x);
result.push(y);
return result;
}
// yau-bp
/**
* Generates yau-bp 2D Visualization of DNA from any sequence
*
* @param sequence - sequence of DNA or RNA (lowercase and mixed cases are valid)
*
* @returns coordinates for 2D visualization of DNA based on the Yau-BP algorithm
*
* @remarks Lee, B. D. (2018). Squiggle: a user-friendly two-dimensional DNA sequence visualization tool. *Bioinformatics*. doi:10.1093/bioinformatics/bty807. Yau, S. S., Wang, J., Niknejad, A., Lu, C., Jin, N., & Ho, Y. K. (2003). DNA sequence representation without degeneracy. *Nucleic acids research*, 31(12), 3078-80.
*
* Example Usage
* ```ts
* yau_bp('ATGC')
* // returns [[0, 1, 2, 3, 4], [0, -1, 0, -0.5, 0]]
* ```
*
*
* Unique to DNAViz is the Yau-BP method, a slight modification of Yau’s method that ensures that the x axis is equivalent to the base position. It preserves that salient feature of the method, which is the purine/pyrimidine split.
*
* For more information visit: https://squiggle.readthedocs.io/en/latest/methods.html#yau-bp
*/
export function yau_bp(sequence: string): number[][] {
sequence = sequence.toUpperCase();
const x: number[] = [0.0];
const y: number[] = [0.0];
let xCoord: number = 0.0;
let yCoord: number = 0.0;
for (const character of sequence) {
xCoord++;
x.push(xCoord);
if (character === 'A') {
yCoord = yCoord - 1;
} else if (character === 'C') {
yCoord = yCoord + 0.5;
} else if (character === 'T' || character === 'U') {
yCoord = yCoord + 1;
} else if (character === 'G') {
yCoord = yCoord - 0.5;
} else {
throw new Error('non-atgcu base');
}
y.push(yCoord);
}
const result: number[][] = [];
result.push(x);
result.push(y);
return result;
}
/**
* Generates integer-valued 2D Visualization of DNA from any sequence
*
* @param sequence - DNA or RNA sequence (lowercase and mixed cases are valid)
*
* @returns coordinates for 2D visualization of DNA based on the Yau-Int algorithm
*
*
* Example Usage
* ```ts
* yau_int('ATGC')
* // returns [[0, 1, 2, 3, 4], [0, -2, 0, -1, 0]]
* ```
*
* This produces identically shaped visualizations to {@link yau} and {@link yau_bp} but with coordinates that can be represented as integers.
* Additionally, this method **is defined for ambiguous bases**.
*/
export function yau_int(sequence: string): number[][] {
sequence = sequence.toUpperCase();
const x: number[] = new Array(sequence.length + 1);
const y: number[] = new Array(sequence.length + 1);
x[0] = 0;
y[0] = 0;
let xCoord = 0;
let yCoord = 0;
for (let i = 0; i < sequence.length; i++) {
switch (sequence.charCodeAt(i)) {
case 0x41: // "A"
yCoord -= 2;
break;
case 0x43: // "C"
yCoord += 1;
break;
case 0x54:
case 0x55: // "T" && "U"
yCoord += 2;
break;
case 0x47: // "G"
yCoord -= 1;
break;
}
xCoord++;
x[i + 1] = xCoord;
y[i + 1] = yCoord;
}
return [x, y];
}
// randic
/**
* Generates randic 2D Visualization of DNA from any sequence
*
* @param sequence - sequence of DNA or RNA (lowercase and mixed cases are valid)
*
* @returns coordinates for 2D visualization of DNA based on the Randic algorithm
*
* @remarks Randić, M., Vračko, M., Lerš, N., & Plavšić, D. (2003). Novel 2-D graphical representation of DNA sequences and their numerical characterization. *Chemical Physics Letters, 368(1–2)*, 1–6. doi:10.1016/s0009-2614(02)01784-0.
*
* Example Usage
* ```ts
* randic('ATGC')
* // returns [[0, 1, 2, 3], [3, 2, 1, 0]]
* ```
*
*
* Similar to tabalture, the Randic method assigns each base a different y-value.
*
* Specifically:
*
* ```ts
* A = 3
* T = 2
* G = 1
* C = 0
* ```
*
* *This visualization method isn't well suited to long sequences
*
* For more information visit: https://squiggle.readthedocs.io/en/latest/methods.html#randic-and-qi
*/
export function randic(sequence: string): number[][] {
sequence = sequence.toUpperCase();
const x: number[] = [];
const y: number[] = [];
let xCoord = 0;
const key: any = {
A: 3,
T: 2,
U: 2,
G: 1,
C: 0,
};
for (const character of sequence) {
if (
character != 'A' &&
character != 'T' &&
character != 'U' &&
character != 'G' &&
character != 'C'
) {
throw new Error('non-atgcu base');
}
x.push(xCoord);
xCoord++;
y.push(key[character]);
}
const result: number[][] = [];
result.push(x);
result.push(y);
return result;
}
// qi
/**
* Generates qi 2D Visualization of DNA from any sequence
*
* @param sequence - sequence of DNA or RNA (lowercase and mixed cases are valid)
*
* @returns coordinates for 2D visualization of DNA based on the Qi algorithm
*
* @remarks Qi, Z., & Qi, X. (2007). Novel 2D graphical representation of DNA sequence based on dual nucleotides. *Chemical Physics Letters, 440(1–3)*, 139–144. doi:10.1016/j.cplett.2007.03.107.
*
* Example Usage
* ```ts
* qi('ATGC')
* // returns [[0, 1, 2], [8, 7, 11]]
* ```
*
*
* Similar to Randic, Qi maps each 2-mer in the sequence to a unique y-value. Because there are more 2-mer combinations than lone bases, Qi graphs look like randic graphs with a larger range of y-values.
*
* Qi Assignment Key:
*
* ```ts
* AA = 12
* AC = 4
* GT = 6
* AG = 0
* CC = 13
* CA = 5
* CG = 10
* TT = 15
* GG = 14
* GC = 11
* AT = 8
* GA = 1
* TG = 7
* TA = 9
* TC = 3
* CT = 2
* ```
*
* For more information visit: https://squiggle.readthedocs.io/en/latest/methods.html#randic-and-qi
*/
export function qi(sequence: string): number[][] {
sequence = sequence.toUpperCase();
const x: number[] = [];
const y: number[] = [];
const key: any = {
AA: 12,
AC: 4,
GT: 6,
AG: 0,
CC: 13,
CA: 5,
CG: 10,
TT: 15,
GG: 14,
GC: 11,
AT: 8,
GA: 1,
TG: 7,
TA: 9,
TC: 3,
CT: 2,
};
for (let i = 0; i < sequence.length - 1; i++) {
let S_1 = sequence[i];
let S_2 = sequence[i + 1];
if (S_1 != 'A' && S_1 != 'T' && S_1 != 'U' && S_1 != 'G' && S_1 != 'C') {
throw new Error('non-atgcu base');
} else if (
S_2 != 'A' &&
S_2 != 'T' &&
S_2 != 'U' &&
S_2 != 'G' &&
S_2 != 'C'
) {
throw new Error('non-atgcu base');
} else if (S_1 === 'U') {
S_1 = 'T';
} else if (S_2 === 'U') {
S_2 = 'T';
}
x.push(i);
y.push(key[S_1 + S_2]);
}
const result: number[][] = [];
result.push(x);
result.push(y);
return result;
}
// gates
/**
* Generates gates 2D Visualization of DNA from any sequence
*
* @param sequence - sequence of DNA or RNA
*
* @returns coordinates for 2D visualization of DNA based on the Gates algorithm
*
* @remarks Gates MA. A simple way to look at DNA. *J Theor Biol*. 1986;119(3):319-328. doi:10.1016/s0022-5193(86)80144-8
*
* Example Usage
* ```ts
* gates('ATGC')
* // returns [[0, 0, 0, 1, 0], [0, -1, 0, 0, 0]]
* ```
*
*
* In Gates’s method, DNA sequences are converted into 2D walks in which Ts, As, Cs, and Gs are up, down, left, and right, respectively. This gives each sequence a “shape.” However, there is degeneracy, meaning that a visualization is not necessarily unique. For example, TGAC is a square (up, right, down, and left), but so is GTCA (right, up, left, down).
*
* For more information visit: https://squiggle.readthedocs.io/en/latest/methods.html#gates
*/
export function gates(sequence: string): number[][] {
sequence = sequence.toUpperCase();
const x: number[] = [0.0];
const y: number[] = [0.0];
let xCoord: number = 0.0;
let yCoord: number = 0.0;
for (const character of sequence) {
if (character === 'A') {
yCoord--;
} else if (character === 'C') {
xCoord--;
} else if (character === 'T' || character === 'U') {
yCoord++;
} else if (character === 'G') {
xCoord++;
} else {
throw new Error('non-atgcu base');
}
x.push(xCoord);
y.push(yCoord);
}
const result: number[][] = [];
result.push(x);
result.push(y);
return result;
}
export function gc_content(sequence: string, gap: number): number[][] {
sequence = sequence.toUpperCase();
const x: number[] = [0.0];
const y: number[] = [0.0];
let gc_count = 0;
for (let i = 0; i < Math.trunc(sequence.length / gap); i++) {
x.push((i + 1) * gap);
for (let j = i * gap; j < (i + 1) * gap; j++) {
if (sequence.charAt(j) === 'G' || sequence.charAt(j) === 'C') {
gc_count++
}
}
y.push(gc_count / gap);
gc_count = 0;
}
const result: number[][] = [];
result.push(x);
result.push(y);
return result;
}