@datagrok/bio 1.3.1 → 1.4.2

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/detectors.js CHANGED
@@ -11,7 +11,7 @@
11
11
 
12
12
  class BioPackageDetectors extends DG.Package {
13
13
 
14
- static semType = 'MACROMOLECULE';
14
+ static mmSemType = 'Macromolecule';
15
15
 
16
16
  static Units = {
17
17
  FastaSeqPt: 'fasta:SEQ:PT', FastaSeqNt: 'fasta:SEQ:NT', FastaMsaPt: 'fasta:MSA:PT', FastaMsaNt: 'fasta:MSA:NT',
@@ -40,25 +40,33 @@ class BioPackageDetectors extends DG.Package {
40
40
  // TODO: Lazy calculations could be helpful for performance and convenient for expressing classification logic.
41
41
  const statsAsChars = BioPackageDetectors.getStats(col, 5, BioPackageDetectors.splitterAsChars);
42
42
  if (statsAsChars.sameLength) {
43
- const alphabet = BioPackageDetectors.detectAlphabet(statsAsChars.freq, alphabetCandidates, '-');
44
- const units = `fasta:SEQ.MSA:${alphabet}`;
45
- col.setTag(DG.TAGS.UNITS, units);
46
- return BioPackageDetectors.semType;
43
+ if (Object.keys(statsAsChars.freq).length > 0) { // require non empty alphabet
44
+ const alphabet = BioPackageDetectors.detectAlphabet(statsAsChars.freq, alphabetCandidates, '-');
45
+ const units = `fasta:SEQ.MSA:${alphabet}`;
46
+ col.setTag(DG.TAGS.UNITS, units);
47
+ return BioPackageDetectors.mmSemType;
48
+ }
47
49
  } else {
48
- const sep = BioPackageDetectors.detectSeparator(statsAsChars.freq);
49
- const gapSymbol = sep ? '' : '-';
50
- const splitter = sep ? BioPackageDetectors.getSplitterWithSeparator(sep) : BioPackageDetectors.splitterAsFasta;
50
+ const separator = BioPackageDetectors.detectSeparator(statsAsChars.freq);
51
+ const gapSymbol = separator ? '' : '-';
52
+ const splitter = separator ? BioPackageDetectors.getSplitterWithSeparator(separator) : BioPackageDetectors.splitterAsFasta;
53
+
51
54
  const stats = BioPackageDetectors.getStats(col, 5, splitter);
55
+ if (Object.keys(stats.freq).length === 0) return null;
52
56
 
53
- const format = sep ? 'separator' : 'fasta';
57
+ const format = separator ? 'separator' : 'fasta';
54
58
  const seqType = stats.sameLength ? 'SEQ.MSA' : 'SEQ';
55
59
 
56
60
  // TODO: If separator detected, then extra efforts to detect alphabet are allowed.
57
61
  const alphabet = BioPackageDetectors.detectAlphabet(stats.freq, alphabetCandidates, gapSymbol);
58
62
 
59
- const units = `${format}:${seqType}:${alphabet}`;
60
- col.setTag(DG.TAGS.UNITS, units);
61
- return BioPackageDetectors.semType;
63
+ const forbidden = BioPackageDetectors.checkForbiddenWoSeparator(stats.freq);
64
+ if (separator || !forbidden) {
65
+ const units = `${format}:${seqType}:${alphabet}`;
66
+ col.setTag(DG.TAGS.UNITS, units);
67
+ if (separator) col.setTag('separator', separator);
68
+ return BioPackageDetectors.mmSemType;
69
+ }
62
70
  }
63
71
  }
64
72
 
@@ -73,9 +81,18 @@ class BioPackageDetectors extends DG.Package {
73
81
 
74
82
  // !!! But there is a caveat because exceptionally frequent char can be a gap symbol in MSA.
75
83
  // !!! What is the difference between the gap symbol and separator symbol in stats terms?
76
-
77
- const maxFreq = Math.max(...Object.values(freq));
78
- const sep = Object.entries(freq).find((kv) => kv[1] == maxFreq)[0];
84
+ // const noSeparatorRe = /[a-z\d]+$/i;
85
+ const noSeparatorRe = /[HBCNOFPSKVYI]/i; // Mendeleev's periodic table single char elements
86
+ const cleanFreq = Object.assign({}, ...Object.entries(freq)
87
+ .filter(([m, f]) => !noSeparatorRe.test(m) &&
88
+ !BioPackageDetectors.AminoacidsFastaAlphabet.has(m) &&
89
+ !BioPackageDetectors.NucleotidesFastaAlphabet.has(m))
90
+ .map(([m, f]) => ({[m]: f})));
91
+ if (Object.keys(cleanFreq).length == 0) return null;
92
+
93
+ const maxFreq = Math.max(...Object.values(cleanFreq));
94
+
95
+ const sep = Object.entries(freq).find(([k, v]) => v === maxFreq)[0];
79
96
  const sepFreq = freq[sep];
80
97
  const otherSumFreq = Object.entries(freq).filter((kv) => kv[0] !== sep)
81
98
  .map((kv) => kv[1]).reduce((pSum, a) => pSum + a, 0);
@@ -83,6 +100,12 @@ class BioPackageDetectors extends DG.Package {
83
100
  return sepFreq / otherSumFreq > freqThreshold ? sep : null;
84
101
  }
85
102
 
103
+ /** Without a separator, special symbols or digits are not allowed as monomers. */
104
+ static checkForbiddenWoSeparator(freq) {
105
+ const forbiddenRe = /[\d!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/i;
106
+ return Object.keys(freq).filter((m) => forbiddenRe.test(m)).length > 0;
107
+ }
108
+
86
109
  /** Stats of sequences with specified splitter func, returns { freq, sameLength } */
87
110
  static getStats(seqCol, minLength, splitter) {
88
111
  const freq = {};