-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdetectors.js
655 lines (562 loc) · 23.9 KB
/
detectors.js
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/* eslint-disable max-lines-per-function */
/* eslint-disable max-lines */
'use strict';
/**
* The class contains semantic type detectors.
* Detectors are functions tagged with `DG.FUNC_TYPES.SEM_TYPE_DETECTOR`.
* See also: https://datagrok.ai/help/develop/how-to/define-semantic-type-detectors
* The class name is comprised of <PackageName> and the `PackageDetectors` suffix.
* Follow this naming convention to ensure that your detectors are properly loaded.
*
* TODO: Use detectors from WebLogo pickUp.. methods
*/
// eslint-disable-next-line max-lines
const SEQ_SAMPLE_LIMIT = 100;
const SEQ_SAMPLE_LENGTH_LIMIT = 100;
/** enum type to simplify setting "user-friendly" notation if necessary */
const NOTATION = {
FASTA: 'fasta',
SEPARATOR: 'separator',
HELM: 'helm',
};
const ALPHABET = {
DNA: 'DNA',
RNA: 'RNA',
PT: 'PT',
UN: 'UN',
};
const ALIGNMENT = {
SEQ_MSA: 'SEQ.MSA',
SEQ: 'SEQ',
};
const SeqTemps = {
seqHandler: `seq-handler`,
notationProvider: `seq-handler.notation-provider`,
};
/** Class for handling notation units in Macromolecule columns */
const SeqHandler = {
TAGS: {
aligned: 'aligned',
alphabet: 'alphabet',
alphabetSize: '.alphabetSize',
alphabetIsMultichar: '.alphabetIsMultichar',
separator: 'separator',
},
};
const isUrlRe = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/i;
class LoggerWrapper {
constructor(_package, logger, componentName) {
this.package = _package;
this.logger = logger;
this.componentName = componentName;
this.debugEnabled = false;
}
debug(message, params) {
if (!this.debugEnabled) return;
this.logger.debug(message, params);
}
error(message, params, stackTrace) {
this.logger.error(message, params, stackTrace);
}
}
class BioPackageDetectors extends DG.Package {
static objCounter = -1;
objId = ++BioPackageDetectors.objCounter;
constructor() {
super();
this.forbiddenMulticharAll = ' .:';
this.forbiddenMulticharFirst = ']' + this.forbiddenMulticharAll;
this.forbiddenMulticharMiddle = '][' + this.forbiddenMulticharAll;
this.forbiddenMulticharLast = '[' + this.forbiddenMulticharAll;
// replace super._logger
this._logger = new LoggerWrapper(this, this.logger, 'detectors');
}
/** Parts of the column name required in the column's name under the detector. It must be in lowercase. */
likelyColNamePartList = ['seq', 'msa', 'dna', 'rna', 'fasta', 'helm', 'sense', 'protein'];
peptideFastaAlphabet = new Set([
'G', 'L', 'Y', 'S', 'E', 'Q', 'D', 'N', 'F', 'A',
'K', 'R', 'H', 'C', 'V', 'P', 'W', 'I', 'M', 'T',
'MeNle', 'MeA', 'MeG', 'MeF',
]);
dnaFastaAlphabet = new Set(['A', 'C', 'G', 'T']);
rnaFastaAlphabet = new Set(['A', 'C', 'G', 'U']);
numbersRawAlphabet = new Set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);
smilesRawAlphabet = new Set([
'C', 'F', 'H', 'N', 'O', 'P', 'S', 'B', /**/'A', 'E', 'I', 'K', 'L', 'M', 'R', 'Z',
'c', 'n', 'o', 's', /**/'a', 'e', 'g', 'i', 'l', 'r', 't', 'u',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'+', '-', '.', , '/', '\\', '@', '[', ']', '(', ')', '#', '%', '=']);
smartsRawAlphabet = new Set([
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'!', '#', '$', '&', '(', ')', '*', '+', ',', '-', '.', ':', ';', '=', '@', '~', '[', ']',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M',
'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm',
'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'y',
]);
/** @param s {String} - string to check
* @returns {boolean} */
isHelm(s) {
return s.startsWith('PEPTIDE1{') || s.startsWith('CHEM1{') || s.startsWith('BLOB1{') ||
s.startsWith('RNA1{') || s.startsWith('DNA1{');
}
//name: detectMacromoleculeEnableStore
//output: object result
detectMacromoleculeEnableStore() {
return window.$detectMacromoleculeStore = {last: null};
}
/** Returns last object (stores it if enabled earlier). */
detectMacromoleculeStoreLast() {
const last = {};
if (window.$detectMacromoleculeStore) window.$detectMacromoleculeStore.last = last;
return last;
}
/** Detector MUST NOT be async, causes error:
* Concurrent modification during iteration: Instance of 'JSArray<Column>'.
*/
//tags: semTypeDetector
//input: column col
//output: string semType
detectMacromolecule(col) {
const tableName = col.dataFrame ? col.dataFrame.name : null;
this.logger.debug(`Bio: detectMacromolecule( table: ${tableName}.${col.name} ), start`);
const t1 = Date.now();
try {
const last = this.detectMacromoleculeStoreLast();
const colName = col.name;
const colNameLikely = this.likelyColNamePartList.some(
(requiredColNamePart) => colName.toLowerCase().includes(requiredColNamePart));
const seqMinLength = colNameLikely ? 7 : 10;
const maxBadRatio = colNameLikely ? 0.05 : 0.005;
// Fail early
if (col.type !== DG.TYPE.STRING) {
last.rejectReason = `The column must be of type '${DG.TYPE.STRING}'.`;
return null;
}
const categoriesSample = [...new Set((col.length < SEQ_SAMPLE_LIMIT ?
wu.count(0).take(Math.min(SEQ_SAMPLE_LIMIT, col.length)).map((rowI) => col.get(rowI)) :
this.sample(col, SEQ_SAMPLE_LIMIT))
.map((seq) => !!seq ? seq.substring(0, SEQ_SAMPLE_LENGTH_LIMIT * 5) : '')
.filter((seq) => seq.length !== 0/* skip empty values for detector */),
)];
last.categoriesSample = categoriesSample;
// To collect alphabet freq three strategies can be used:
// as chars, as fasta (single or within square brackets), as with the separator.
if (
!(col.categories.length === 1 && !col.categories[0]) && // TODO: Remove with tests for single empty category
DG.Detector.sampleCategories(col, (s) => this.isHelm(s), 1, SEQ_SAMPLE_LIMIT)
) {
const statsAsHelm = this.getStats(categoriesSample, 2,
this.getSplitterAsHelm(SEQ_SAMPLE_LENGTH_LIMIT));
col.meta.units = NOTATION.HELM;
// alphabetSize calculated on (sub)sample of data is incorrect
// const alphabetSize = Object.keys(statsAsHelm.freq).length;
const alphabetIsMultichar = Object.keys(statsAsHelm.freq).some((m) => m.length > 1);
// col.setTag(SeqHandler.TAGS.alphabetSize, alphabetSize.toString());
col.setTag(SeqHandler.TAGS.alphabetIsMultichar, alphabetIsMultichar ? 'true' : 'false');
col.setTag(DG.TAGS.CELL_RENDERER, 'helm');
return DG.SEMTYPE.MACROMOLECULE;
}
const decoyAlphabets = [
['NUMBERS', this.numbersRawAlphabet, 0.25, undefined],
['SMILES', this.smilesRawAlphabet, 0.25, (seq) => seq.replaceAll()],
['SMARTS', this.smartsRawAlphabet, 0.45, undefined],
];
const candidateAlphabets = [
[ALPHABET.PT, this.peptideFastaAlphabet, 0.50],
[ALPHABET.DNA, this.dnaFastaAlphabet, 0.55],
[ALPHABET.RNA, this.rnaFastaAlphabet, 0.55],
];
// Check for url column, maybe it is too heavy check
const isUrlCheck = (s) => {
let res = true;
try {
const url = new URL(s);
res = true;
} catch {
res = false;
}
return res;
// return isUrlRe.test(s);
};
const isUrl = categoriesSample.every((v) => !v || isUrlCheck(v));
if (isUrl) {
last.rejectReason = 'URL detected.';
return null;
}
// TODO: Detect HELM sequence
// TODO: Lazy calculations could be helpful for performance and convenient for expressing classification logic.
const statsAsChars = this.getStats(categoriesSample, seqMinLength,
this.getSplitterAsChars(SEQ_SAMPLE_LENGTH_LIMIT));
// Empty statsAsShars.freq alphabet means no strings of enough length presented in the data
if (Object.keys(statsAsChars.freq).length === 0) {
last.rejectReason = 'Monomer set (alphabet) is empty.';
return null;
}
const decoy = this.detectAlphabet(statsAsChars.freq, decoyAlphabets, null, colNameLikely ? -0.05 : 0);
if (decoy !== ALPHABET.UN) {
last.rejectReason = `Decoy alphabet '${decoy}' detected.`;
return null;
}
const separator = this.detectSeparator(statsAsChars.freq, categoriesSample, seqMinLength);
const checkForbiddenSeparatorRes = this.checkForbiddenSeparator(separator);
if (checkForbiddenSeparatorRes) {
last.rejectReason = `Separator '${separator}' is forbidden.`;
return null;
}
const units = separator ? NOTATION.SEPARATOR : NOTATION.FASTA;
const gapSymbol = separator ? '' : '-';
const splitter = separator ? this.getSplitterWithSeparator(separator, SEQ_SAMPLE_LENGTH_LIMIT) :
this.getSplitterAsFasta(SEQ_SAMPLE_LENGTH_LIMIT);
if (statsAsChars.sameLength && !separator &&
!(['[', ']'].some((c) => c in statsAsChars.freq)) // not fasta ext notation
) { // MSA FASTA single character
const stats = this.getStats(categoriesSample, seqMinLength, splitter);
const alphabet = this.detectAlphabet(stats.freq, candidateAlphabets, '-', colNameLikely ? 0.20 : 0);
if (alphabet === ALPHABET.UN) {
last.rejectReason = `MSA FASTA single character alphabet is not allowed to be 'UN'.`;
return null;
}
col.meta.units = units;
if (separator) col.setTag(SeqHandler.TAGS.separator, separator);
col.setTag(SeqHandler.TAGS.aligned, ALIGNMENT.SEQ_MSA);
col.setTag(SeqHandler.TAGS.alphabet, alphabet);
if (alphabet === ALPHABET.UN) {
const alphabetIsMultichar = Object.keys(stats.freq).some((m) => m.length > 1);
col.setTag(SeqHandler.TAGS.alphabetIsMultichar, alphabetIsMultichar ? 'true' : 'false');
}
col.setTag(DG.TAGS.CELL_RENDERER, 'sequence');
return DG.SEMTYPE.MACROMOLECULE;
} else {
// for fasta, we need to include every sequence
const stats = this.getStats(categoriesSample, separator ? seqMinLength : 2, splitter);
const alphabetIsMultichar = Object.keys(stats.freq).some((m) => m.length > 1);
// Empty monomer alphabet is not allowed
if (Object.keys(stats.freq).length === 0) {
last.rejectReason = 'Monomer set (alphabet) is empty';
return null;
}
// Single- and multi-char monomer names for sequences with separators have constraints
if (units === NOTATION.SEPARATOR || (units === NOTATION.FASTA && alphabetIsMultichar)) {
const badSymbol /*: string | null*/ = this.checkBadMultichar(stats.freq);
if (badSymbol) {
last.rejectReason = `Forbidden multi-char monomer: '${badSymbol}'.`;
return null;
}
}
const aligned = stats.sameLength ? ALIGNMENT.SEQ_MSA : ALIGNMENT.SEQ;
// TODO: If separator detected, then extra efforts to detect alphabet are allowed.
const alphabet = this.detectAlphabet(stats.freq, candidateAlphabets, gapSymbol, colNameLikely ? 0.15 : 0);
if (units === NOTATION.FASTA && alphabet === ALPHABET.UN && !alphabetIsMultichar) {
last.rejectReason = `FASTA single character alphabet is not allowed to be 'UN'.`;
return null;
}
// const forbidden = this.checkForbiddenWoSeparator(stats.freq);
col.meta.units = units;
if (separator) col.setTag(SeqHandler.TAGS.separator, separator);
col.setTag(SeqHandler.TAGS.aligned, aligned);
col.setTag(SeqHandler.TAGS.alphabet, alphabet);
if (alphabet === ALPHABET.UN) {
// alphabetSize calculated on (sub)sample of data is incorrect
const alphabetIsMultichar = Object.keys(stats.freq).some((m) => m.length > 1);
col.setTag(SeqHandler.TAGS.alphabetIsMultichar, alphabetIsMultichar ? 'true' : 'false');
}
refineSeqSplitter(col, stats, separator).then(() => { });
col.setTag(DG.TAGS.CELL_RENDERER, 'sequence');
return DG.SEMTYPE.MACROMOLECULE;
}
} catch (err) {
const errMsg = err instanceof Error ? err.message : err.toString();
const errStack = err instanceof Error ? err.stack : undefined;
const colTops = wu.count(0).take(Math.max(col.length, 4)).map((rowI) => col.get(rowI))
.reduce((a, b) => a === undefined ? b : a + '\n' + b, undefined);
this.logger.error(`Bio: detectMacromolecule( table: ${tableName}.${col.name} ), error:\n${errMsg}` +
`${errStack ? '\n' + errStack : ''}` + `\n${colTops}`);
} finally {
// Prevent too much log spam
// const t2 = Date.now();
// this.logger.debug(`Bio: detectMacromolecule( table: ${tableName}.${col.name} ), ` + `ET = ${t2 - t1} ms.`);
}
}
/** Detects the most frequent char with a rate of at least 0.15 of others in sum.
* Does not use any splitting strategies, estimates just by single characters.
* @param freq Dictionary of characters freqs
* @param categoriesSample A string array of seqs sample
* @param seqMinLength A threshold on min seq length for contributing to stats
*/
detectSeparator(freq, categoriesSample, seqMinLength) {
// To detect a separator we analyze col's sequences character frequencies.
// If there is an exceptionally frequent symbol, then we will call it the separator.
// The most frequent symbol should occur with a rate of at least 0.15
// of all other symbols in sum to be called the separator.
// !!! But there is a caveat because exceptionally frequent char can be a gap symbol in MSA.
// !!! What is the difference between the gap symbol and separator symbol in stats terms?
// const noSeparatorRe = /[a-z\d]+$/i;
const noSeparatorChemRe = /[HBCNOFPSKVYI]/i; // Mendeleev's periodic table single char elements
const noSeparatorAlphaDigitRe = /[\dA-Z]/i;
const noSeparatorBracketsRe = /[\[\]()<>{}]/i;
const cleanFreq = Object.assign({}, ...Object.entries(freq)
.filter(([m, f]) =>
!noSeparatorChemRe.test(m) && !noSeparatorAlphaDigitRe.test(m) && !noSeparatorBracketsRe.test(m) &&
!this.peptideFastaAlphabet.has(m) &&
!this.dnaFastaAlphabet.has(m))
.map(([m, f]) => ({[m]: f})));
if (Object.keys(cleanFreq).length === 0) return null;
const maxFreq = Math.max(...Object.values(cleanFreq));
const sep = Object.entries(freq).find(([k, v]) => v === maxFreq)[0];
const sepFreq = freq[sep];
const otherSumFreq = Object.entries(freq).filter((kv) => kv[0] !== sep)
.map((kv) => kv[1]).reduce((pSum, a) => pSum + a, 0);
// Splitter with separator test application
const splitter = this.getSplitterWithSeparator(sep, SEQ_SAMPLE_LENGTH_LIMIT);
const stats = this.getStats(categoriesSample, seqMinLength, splitter);
const badSymbol = this.checkBadMultichar(stats.freq);
if (badSymbol) return null;
// TODO: Test for Gamma/Erlang distribution
const totalMonomerCount = wu(Object.values(stats.freq)).reduce((sum, a) => sum + a, 0);
const mLengthAvg = wu.entries(stats.freq)
.reduce((sum, [m, c]) => sum + m.length * c, 0) / totalMonomerCount;
const mLengthVarN = Math.sqrt(wu.entries(stats.freq)
.reduce((sum, [m, c]) => sum + Math.pow(m.length - mLengthAvg, 2) * c, 0) / (totalMonomerCount - 1),
) / mLengthAvg;
const sepRate = sepFreq / (sepFreq + otherSumFreq);
const expSepRate = 1 / Object.keys(freq).length; // expected
// const freqThreshold = (1 / (Math.log2(Object.keys(freq).length) + 2));
return (sepRate / expSepRate > 2.2 && mLengthVarN < 0.8) ||
(sepRate / expSepRate > 3.5) ? sep : null;
}
checkForbiddenSeparator(separator) {
// comma, ampersand, space, underscore, CRLF, CR, LF
// 2023-04-15: dot is allowed to allow Helm like separator in Helm MSA results (no Helm monomers contains dot)
const forbiddenSepRe = /,|&| |_|\r\n|\r|\n/i;
return forbiddenSepRe.test(separator);
}
/** Dots and colons are nor allowed in multichar monomer names (but space is allowed).
* The monomer name/label cannot contain digits only (but single digit is allowed).
*/
checkBadMultichar(freq) /* : string | null */ {
for (const symbol of Object.keys(freq)) {
if (symbol && !isNaN(symbol))
return symbol; // performance evaluated better with RegExp
const symbolLen = symbol.length;
if (this.forbiddenMulticharFirst.includes(symbol[0]))
return symbol;
if (this.forbiddenMulticharLast.includes(symbol[symbolLen - 1]))
return symbol;
for (let cI = 1; cI < symbolLen - 1; ++cI) {
const c = symbol[cI];
if (this.forbiddenMulticharMiddle.includes(c))
return symbol;
}
if (symbol.match(/^\d+\W+.*/))
// symbols like '2,...' are forbidden
// we require an alphabet character just after the leading digit(s)
return symbol;
}
return null;
}
calcBad(freq, forbiddenSet) {
let allCount = 0;
let forbiddenCount = 0;
for (const [m, count] of Object.entries(freq)) {
if (forbiddenSet.has(m)) forbiddenCount += freq[m];
allCount += freq[m];
}
return [forbiddenCount, allCount];
}
// /** Without a separator, special symbols or digits are not allowed as monomers. */
// checkForbiddenWoSeparator(freq) {
// const forbiddenRe = /[\d!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/i;
// return Object.keys(freq).filter((m) => forbiddenRe.test(m)).length > 0;
// }
/** Stats of sequences with specified splitter func, returns { freq, sameLength } */
getStats(values, minLength, splitter) {
const freq = {};
let sameLength = true;
let firstLength = null;
for (const seq of values) {
const mSeq = !!seq ? splitter(seq) : [];
if (firstLength === null)
firstLength = mSeq.length;
else if (mSeq.length !== firstLength)
sameLength = false;
if (mSeq.length >= minLength) {
for (const m of mSeq) {
if (!(m in freq)) freq[m] = 0;
freq[m] += 1;
}
}
}
return {freq: freq, sameLength: sameLength};
}
/** Detects alphabet for freq by freq similarity to alphabet monomer set.
* @param freq frequencies of monomers in sequence set
* @param candidates an array of pairs [name, monomer set]
* @param {boolean} colNameLikely The column name suggests the column is Macromolecule more likely
*/
detectAlphabet(freq, candidates, gapSymbol, simAdj = 0) {
const candidatesSims = candidates.map((c) => {
const sim = this.getAlphabetSimilarity(freq, c[1], gapSymbol) + simAdj;
return [c[0], c[1], c[2], freq, sim];
});
let alphabetName;
const maxSim = Math.max(...candidatesSims.map((cs) => cs[4] > cs[2] ? cs[4] : -1));
if (maxSim > 0) {
const sim = candidatesSims.find((cs) => cs[4] === maxSim);
alphabetName = sim[0];
} else
alphabetName = ALPHABET.UN;
return alphabetName;
}
getAlphabetSimilarity(freq, alphabet, gapSymbol) {
const keys = new Set([...new Set(Object.keys(freq)), ...alphabet]);
keys.delete(gapSymbol);
const freqSum = Object.values(freq).reduce((a, b) => a + b, 0);
const freqA = [];
const alphabetA = [];
for (const m of keys) {
freqA.push(m in freq ? freq[m] / freqSum : -0.001);
alphabetA.push(alphabet.has(m) ? 10 : -20 /* penalty for character outside alphabet set*/);
}
/* There were a few ideas: chi-squared, pearson correlation (variance?), scalar product */
const cos = this.vectorDotProduct(freqA, alphabetA) / (this.vectorLength(freqA) * this.vectorLength(alphabetA));
return cos;
}
vectorLength(v) {
let sqrSum = 0;
for (let i = 0; i < v.length; i++)
sqrSum += v[i] * v[i];
return Math.sqrt(sqrSum);
}
vectorDotProduct(v1, v2) {
if (v1.length !== v2.length)
throw Error('The dimensionality of the vectors must match');
let prod = 0;
for (let i = 0; i < v1.length; i++)
prod += v1[i] * v2[i];
return prod;
}
/** For trivial checks split by single chars*/
getSplitterAsChars(lengthLimit) {
const resFunc = function(seq) {
return seq.split('', lengthLimit);
};
resFunc.T = 'splitterAsChars';
return resFunc;
}
getSplitterWithSeparator(separator, limit) {
const resFunc = function(seq) {
return !seq ? [] : seq.replaceAll('\"-\"', '').replaceAll('\'-\'', '').split(separator, limit);
};
resFunc.T = 'splitterWithSeparator';
return resFunc;
}
// Multichar monomer names in square brackets, single char monomers or gap symbol
monomerRe = /\[([A-Za-z0-9_\-,()]+)\]|(.)/g;
/** Split sequence for single character monomers, square brackets multichar monomer names or gap symbol. */
getSplitterAsFasta(lengthLimit) {
const resFunc = function(seq) {
const res = wu(seq.toString().matchAll(this.monomerRe))
.take(lengthLimit)
.map((ma) => {
let mRes;
const m = ma[0];
if (m.length > 1)
mRes = ma[1];
else
mRes = m;
return mRes;
}).toArray();
return res;
}.bind(this);
resFunc.T = 'splitterAsFasta';
return resFunc;
}
/** Only some of the synonyms. These were obtained from the clustered oligopeptide dataset. */
aaSynonyms = {
'[MeNle]': 'L', // Nle - norleucine
'[MeA]': 'A', '[MeG]': 'G', '[MeF]': 'F',
};
helmRe = /(PEPTIDE1|DNA1|RNA1)\{([^}]+)}/g;
helmPp1Re = /\[([^\[\]]+)]/g;
/** Splits Helm string to monomers, but does not replace monomer names to other notation (e.g. for RNA). */
getSplitterAsHelm(lengthLimit) {
const resFunc = function(seq) {
this.helmRe.lastIndex = 0;
const ea = this.helmRe.exec(seq.toString());
const inSeq = ea ? ea[2] : null;
const mmPostProcess = (mm) => {
this.helmPp1Re.lastIndex = 0;
const pp1M = this.helmPp1Re.exec(mm);
if (pp1M && pp1M.length >= 2)
return pp1M[1];
else
return mm;
};
const mmList = inSeq ? inSeq.split('.') : [];
const mmListRes = mmList.map(mmPostProcess);
return mmListRes;
}.bind(this);
resFunc.T = 'splitterAsHelm';
return resFunc;
}
sample(col, n) {
if (col.length < n)
throw new Error('Sample source is less than n requested.');
const idxSet = new Set();
while (idxSet.size < n) {
const idx = Math.floor(Math.random() * col.length);
if (!idxSet.has(idx)) idxSet.add(idx);
}
return wu(idxSet).map((idx) => col.get(idx));
}
// -- autostart --
//name: autostart
//tags: autostart
//description: Bio bootstrap
autostart() {
this.logger.debug('Bio: detectors.js: autostart()');
this.autostartContextMenu();
}
autostartContextMenu() {
grok.events.onContextMenu.subscribe((event) => {
if (event.args.item && event.args.item instanceof DG.GridCell &&
event.args.item.tableColumn && event.args.item.tableColumn.semType === DG.SEMTYPE.MACROMOLECULE
) {
const contextMenu = event.args.menu;
const cell = event.args.item.cell; // DG.Cell
grok.functions.call('Bio:addCopyMenu', {cell: cell, menu: contextMenu})
.catch((err) => {
grok.shell.error(err.toString());
});
event.preventDefault();
return true;
}
});
}
}
async function refineSeqSplitter(col, stats, separator) {
let invalidateRequired = false;
const refinerList = [
{package: 'SequenceTranslator', name: 'refineNotationProviderForHarmonizedSequence'},
];
for (const refineFuncFind of refinerList) {
try {
const funcList = DG.Func.find(refineFuncFind);
if (funcList.length === 0) continue;
const funcFc = funcList[0].prepare({col: col, stats: stats, separator: separator});
const refineRes = (await funcFc.call()).getOutputParamValue();
invalidateRequired ||= refineRes;
} catch (err) {
console.error(err);
}
}
if (invalidateRequired) {
// Applying custom notation provider MUST invalidate SeqHandler
delete col.temp[SeqTemps.seqHandler];
for (const view of grok.shell.tableViews) {
if (view.dataFrame === col.dataFrame)
view.grid.invalidate();
}
}
}