@datagrok-libraries/utils 0.0.7 → 0.0.11

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.
Files changed (43) hide show
  1. package/README.md +2 -2
  2. package/package.json +29 -29
  3. package/src/bit-array.d.ts +82 -0
  4. package/src/bit-array.d.ts.map +1 -0
  5. package/src/bit-array.js +666 -666
  6. package/src/operations.d.ts +66 -0
  7. package/src/operations.d.ts.map +1 -0
  8. package/src/operations.js +144 -144
  9. package/src/reduce-dimensionality.d.ts +38 -0
  10. package/src/reduce-dimensionality.d.ts.map +1 -0
  11. package/src/reduce-dimensionality.js +233 -233
  12. package/src/sequence-encoder.d.ts +59 -0
  13. package/src/sequence-encoder.d.ts.map +1 -0
  14. package/src/sequence-encoder.js +116 -117
  15. package/src/spe.d.ts +68 -0
  16. package/src/spe.d.ts.map +1 -0
  17. package/src/spe.js +151 -151
  18. package/src/string-measure.d.ts +33 -0
  19. package/src/string-measure.d.ts.map +1 -0
  20. package/src/string-measure.js +46 -46
  21. package/src/string-utils.d.ts +2 -0
  22. package/src/string-utils.d.ts.map +1 -0
  23. package/src/string-utils.js +9 -9
  24. package/src/table-validation.d.ts +2 -0
  25. package/src/table-validation.d.ts.map +1 -0
  26. package/src/table-validation.js +47 -47
  27. package/src/test-utils.d.ts +11 -0
  28. package/src/test-utils.d.ts.map +1 -0
  29. package/src/test-utils.js +81 -81
  30. package/src/{type_declarations.ts → type-declarations.d.ts} +56 -54
  31. package/src/type-declarations.d.ts.map +1 -0
  32. package/src/type-declarations.js +37 -0
  33. package/tsconfig.json +71 -71
  34. package/src/bit-array.ts +0 -767
  35. package/src/operations.ts +0 -160
  36. package/src/reduce-dimensionality.ts +0 -278
  37. package/src/sequence-encoder.ts +0 -132
  38. package/src/spe.ts +0 -172
  39. package/src/string-measure.ts +0 -54
  40. package/src/string-utils.ts +0 -8
  41. package/src/table-validation.ts +0 -67
  42. package/src/test-utils.ts +0 -97
  43. package/src/type_declarations.js +0 -37
package/src/operations.ts DELETED
@@ -1,160 +0,0 @@
1
- import {Matrix, Vector, Coordinates, Vectors, DistanceMetric} from './type_declarations';
2
-
3
- /**
4
- * Asserts a condition by throwing an Error.
5
- *
6
- * @export
7
- * @param {boolean} [condition=false] Condition to assert.
8
- * @param {string} [message='Assertion error.'] Message to output.
9
- * @throws {Error}
10
- */
11
- export function assert(condition: boolean = false, message: string = 'Assertion error.') {
12
- if (!condition) {
13
- throw new Error(message);
14
- }
15
- }
16
-
17
- /**
18
- * Generates single random integer from 0 to range.
19
- *
20
- * @export
21
- * @param {number} range Max generating value.
22
- * @return {number} A random integer generated.
23
- */
24
- export function randomInt(range: number): number {
25
- return Math.floor(Math.random() * range);
26
- }
27
-
28
- /**
29
- * Creates new two-dimensional array and fills it with the value given.
30
- *
31
- * @param {number} dimension1 The first dimension of the coordinates (number of rows).
32
- * @param {number} dimension2 The second dimension of the coordinates (number of columns).
33
- * @param {number} [fill=0] A value to fill the coordinates with.
34
- * @return {Coordinates} A two-dimensional filled with the value given.
35
- * @todo Might be slow since used Array.map. Probably needs performance revision.
36
- */
37
- function initCoordinates(dimension1: number, dimension2: number, fill: number = 0): Coordinates {
38
- return new Array(dimension1).fill(fill).map(() => (new Vector(dimension2).fill(fill)));
39
- }
40
-
41
- /**
42
- * Transpose matrix.
43
- *
44
- * @export
45
- * @param {Matrix} matrix The matrix to be transposed.
46
- * @return {Matrix} Transposed matrix.
47
- * @todo Might be slow since used Array.map. Probably needs performance revision.
48
- */
49
- export function transposeMatrix(matrix: Matrix): Matrix {
50
- return new Array(matrix[0].length).fill(0).map((_, i) => (new Vector(matrix.length).fill(0).map((_, j) => (matrix[j][i]))));
51
- }
52
-
53
- /**
54
- * Adds two vectors with the second one to be multiplied by the given ratio.
55
- *
56
- * @export
57
- * @param {Vector} p The first vector to add.
58
- * @param {Vector} q The second vector to add.
59
- * @param {number} [multiplier=1] A multiplier to be used before the second vector is added.
60
- * @return {Vector} New vector contained the result of operation p+multiplier*q.
61
- */
62
- export function vectorAdd(p: Vector, q: Vector, multiplier: number = 1): Vector {
63
- const nItems = p.length;
64
-
65
- assert(nItems == q.length, 'Vector lengths do not match.')
66
-
67
- let total = new Vector(nItems);
68
-
69
- for (let i = 0; i < p.length; ++i) {
70
- total[i] = p[i]+multiplier*q[i];
71
- }
72
- return total;
73
- }
74
-
75
- /**
76
- * Sums the vector's items.
77
- *
78
- * @param {Vector} v The vector to be summed.
79
- * @return {number} The vector's items sum.
80
- */
81
- function itemsSum(v: Vector): number {
82
- let total = 0;
83
-
84
- for (let i = 0; i < v.length; ++i) {
85
- total += v[i];
86
- }
87
- return total;
88
- }
89
-
90
- /**
91
- * Suqares the vector's items.
92
- *
93
- * @param {Vector} v The vector to square.
94
- * @return {Vector} A new vector containing the original's items squared.
95
- */
96
- function vectorSquare(v: Vector): Vector {
97
- const nItems = v.length;
98
- let total = new Vector(nItems);
99
-
100
- for (let i = 0; i < v.length; ++i) {
101
- total[i] = v[i]*v[i];
102
- }
103
- return total;
104
- }
105
-
106
- /**
107
- * Creates a matrix filled with random floating point values.
108
- *
109
- * @export
110
- * @param {number} dimension1 The first dimension of the matrix.
111
- * @param {number} dimension2 The second dimension of the matrix.
112
- * @param {number} [scale=1.] Max value given by random generator.
113
- * @return {Matrix} A new matrix filled with random floating point values.
114
- */
115
- export function fillRandomMatrix(dimension1: number, dimension2: number, scale: number = 1.): Matrix {
116
- let matrix = initCoordinates(dimension1, dimension2);
117
-
118
- for (let i = 0; i < dimension1; ++i) {
119
- for (let j = 0; j < dimension2; ++j) {
120
- matrix[i][j] = Math.random() * scale;
121
- }
122
- }
123
- return matrix;
124
- }
125
-
126
- /**
127
- * Calculates Euclidean distance between two vectors.
128
- *
129
- * @export
130
- * @param {Vector} p The first vector.
131
- * @param {Vector} q The second vector.
132
- * @return {number} Euclidean distance between the given vectors.
133
- */
134
- export function calculateEuclideanDistance(p: Vector, q: Vector): number {
135
- const diff = vectorAdd(p, q, -1);
136
- const sqdiff = vectorSquare(diff);
137
- const sqdiffSumm = itemsSum(sqdiff);
138
- return Math.sqrt(sqdiffSumm);
139
- }
140
-
141
- /**
142
- * Creates a distance matrix using a custom distance function.
143
- *
144
- * @export
145
- * @param {Vectors} data Input vectors to calculate distances.
146
- * @param {DistanceMetric} distance Custom distance function.
147
- * @return {Matrix} Calculated custom distance matrix.
148
- */
149
- export function calcDistanceMatrix(data: Vectors, distance: DistanceMetric): Matrix {
150
- const nItems = data.length;
151
- let matrix = initCoordinates(nItems, nItems, 0);
152
-
153
- for (let i = 0; i < nItems; ++i) {
154
- for (let j = i+1; j < nItems; ++j) {
155
- const d: number = (data[i] == null) || (data[j] == null) ? 0 : distance(data[i], data[j]);
156
- matrix[i][j] = matrix[j][i] = d;
157
- }
158
- }
159
- return matrix;
160
- }
@@ -1,278 +0,0 @@
1
- import * as umj from 'umap-js';
2
- import {TSNE} from '@keckelt/tsne';
3
-
4
- import {Options, DistanceMetric, Coordinates, Vector, Vectors, Matrix} from './type_declarations';
5
- import {calcDistanceMatrix, transposeMatrix} from './operations';
6
- import {SPEBase, PSPEBase} from './spe';
7
- import {Measurer} from './string-measure';
8
- import {AlignedSequenceEncoder} from './sequence-encoder';
9
- import {assert} from './operations';
10
-
11
- /**
12
- * Abstract dimensionality reducer.
13
- *
14
- * @abstract
15
- * @class Reducer
16
- */
17
- abstract class Reducer {
18
- protected data: Vectors;
19
-
20
- /**
21
- * Creates an instance of Reducer.
22
- * @param {Options} options Options to pass to the constructor.
23
- * @memberof Reducer
24
- */
25
- constructor(options: Options) {
26
- this.data = options.data;
27
- }
28
-
29
- /**
30
- * Is to embed the data given into the two-dimensional space.
31
- *
32
- * @abstract
33
- * @return {Coordinates} Cartesian coordinate of this embedding.
34
- * @memberof Reducer
35
- */
36
- abstract transform(): Coordinates;
37
- }
38
-
39
- /**
40
- * Implements t-SNE dimensionality reduction.
41
- *
42
- * @class TSNEReducer
43
- * @extends {Reducer}
44
- */
45
- class TSNEReducer extends Reducer {
46
- protected reducer: TSNE;
47
- protected iterations: number;
48
- protected distance: DistanceMetric;
49
-
50
- /**
51
- * Creates an instance of TSNEReducer.
52
- * @param {Options} options Options to pass to the constructor.
53
- * @memberof TSNEReducer
54
- */
55
- constructor(options: Options) {
56
- super(options);
57
- this.reducer = new TSNE(options);
58
- this.iterations = options?.iterations ?? 100;
59
- this.distance = options.distance;
60
- }
61
-
62
- /**
63
- * Embeds the data given into the two-dimensional space using t-SNE method.
64
- * @return {Coordinates} Cartesian coordinate of this embedding.
65
- */
66
- public transform(): Coordinates {
67
- this.reducer.initDataDist(calcDistanceMatrix(this.data, this.distance));
68
-
69
- for (let i = 0; i < this.iterations; ++i) {
70
- this.reducer.step(); // every time you call this, solution gets better
71
- }
72
- return this.reducer.getSolution();
73
- }
74
- }
75
-
76
- /**
77
- * Implements UMAP dimensionality reduction.
78
- *
79
- * @class UMAPReducer
80
- * @extends {Reducer}
81
- */
82
- class UMAPReducer extends Reducer {
83
- protected reducer: umj.UMAP;
84
- protected encoder: AlignedSequenceEncoder;
85
- protected distanceFn: Function;
86
- protected vectors: number[][];
87
-
88
- /**
89
- * Creates an instance of UMAPReducer.
90
- * @param {Options} options Options to pass to the constructor.
91
- * @memberof UMAPReducer
92
- */
93
- constructor(options: Options) {
94
- super(options);
95
-
96
- assert('distanceFn' in options);
97
-
98
- this.encoder = new AlignedSequenceEncoder();
99
- this.distanceFn = options.distanceFn;
100
- this.vectors = [];
101
- options.distanceFn = this._encodedDistance.bind(this);
102
- this.reducer = new umj.UMAP(options);
103
- }
104
-
105
- /**
106
- * Custom distance wrapper to have numeric inputs instead of string ones.
107
- *
108
- * @protected
109
- * @param {number[]} a The first item.
110
- * @param {number[]} b The first item.
111
- * @return {number} Distance metric.
112
- * @memberof UMAPReducer
113
- */
114
- protected _encodedDistance(a: number[], b: number[]): number {
115
- return this.distanceFn(this.data[a[0]], this.data[b[0]]);
116
- }
117
-
118
- /**
119
- * Encodes the input data as a vector of indices.
120
- *
121
- * @protected
122
- * @memberof UMAPReducer
123
- */
124
- protected _encode() {
125
- for (let i = 0; i < this.data.length; ++i) {
126
- this.vectors.push([i]);
127
- }
128
- }
129
-
130
- /**
131
- * Embeds the data given into the two-dimensional space using UMAP method.
132
- * @return {Coordinates} Cartesian coordinate of this embedding.
133
- */
134
- public transform(): Coordinates {
135
- this._encode();
136
-
137
- const embedding = this.reducer.fit(this.vectors);
138
-
139
- function arrayCast2Coordinates(data: number[][]): Coordinates {
140
- return new Array(data.length).fill(0).map((_, i) => (Vector.from(data[i])));
141
- }
142
-
143
- return arrayCast2Coordinates(embedding);
144
- }
145
- }
146
-
147
- /**
148
- * Implements original SPE dimensionality reduction.
149
- *
150
- * @class SPEReducer
151
- * @extends {Reducer}
152
- */
153
- class SPEReducer extends Reducer {
154
- protected reducer: SPEBase;
155
-
156
- /**
157
- * Creates an instance of SPEReducer.
158
- * @param {Options} options Options to pass to the constructor.
159
- * @memberof SPEReducer
160
- */
161
- constructor(options: Options) {
162
- super(options);
163
- this.reducer = new SPEBase(options);
164
- }
165
-
166
- /**
167
- * Embeds the data given into the two-dimensional space using the original SPE method.
168
- * @return {Coordinates} Cartesian coordinate of this embedding.
169
- */
170
- public transform(): Coordinates {
171
- return this.reducer.embed(this.data);
172
- }
173
- }
174
-
175
- /**
176
- * Implements modified SPE dimensionality reduction.
177
- *
178
- * @class PSPEReducer
179
- * @extends {Reducer}
180
- */
181
- class PSPEReducer extends Reducer {
182
- protected reducer: PSPEBase;
183
-
184
- /**
185
- * Creates an instance of PSPEReducer.
186
- * @param {Options} options Options to pass to the constructor.
187
- * @memberof PSPEReducer
188
- */
189
- constructor(options: Options) {
190
- super(options);
191
- this.reducer = new PSPEBase(options);
192
- }
193
-
194
- /**
195
- * Embeds the data given into the two-dimensional space using the modified SPE method.
196
- * @return {Coordinates} Cartesian coordinate of this embedding.
197
- */
198
- public transform(): Coordinates {
199
- return this.reducer.embed(this.data);
200
- }
201
- }
202
-
203
- /**
204
- * Unified class implementing different dimensionality reduction methods.
205
- *
206
- * @export
207
- * @class DimensionalityReducer
208
- */
209
- export class DimensionalityReducer {
210
- private reducer: Reducer | undefined;
211
- public static methods: string[] = ['UMAP', 't-SNE', 'SPE', 'pSPE'];
212
- private measurer: Measurer;
213
-
214
- /**
215
- * Creates an instance of DimensionalityReducer.
216
- * @param {any[]} data Vectors to embed.
217
- * @param {string} method Embedding method to be applied
218
- * @param {string} metric Distance metric to be computed between each of the vectors.
219
- * @param {Options} [options] Options to pass to the implementing embedders.
220
- * @memberof DimensionalityReducer
221
- */
222
- constructor(data: any[], method: string, metric: string, options?: Options) {
223
- assert(DimensionalityReducer.availableMethods.includes(method), `The method '${method}' is not supported`);
224
-
225
- this.measurer = new Measurer(metric);
226
- const measure = this.measurer.getMeasure()
227
-
228
- if (method == 'UMAP') {
229
- this.reducer = new UMAPReducer({
230
- ...{data: data},
231
- ...{distanceFn: measure},
232
- ...{nEpochs: options?.cycles},
233
- ...options
234
- });
235
- } else if (method == 't-SNE') {
236
- this.reducer = new TSNEReducer({
237
- ...{data: data},
238
- ...{distance: measure},
239
- ...{iterations: options?.cycles ?? undefined},
240
- ...options,
241
- });
242
- } else if (method == 'SPE') {
243
- this.reducer = new SPEReducer({...{data: data}, ...{distance: measure}, ...options});
244
- } else {
245
- this.reducer = new PSPEReducer({...{data: data}, ...{distance: measure}, ...options});
246
- }
247
- }
248
-
249
- /**
250
- * Embeds the data given into the two-dimensional space using the chosen method.
251
- *
252
- * @param {boolean} transpose Whether to transform coordinates to have columns-first orientation.
253
- * @throws {Error} If the embedding method was not found.
254
- * @return {Coordinates} Cartesian coordinate of this embedding.
255
- * @memberof DimensionalityReducer
256
- */
257
- public transform(transpose: boolean = false): Coordinates {
258
- if (this.reducer == undefined) {
259
- throw new Error('Reducer was not defined.');
260
- }
261
- let embedding = this.reducer.transform();
262
-
263
- if (transpose) {
264
- embedding = transposeMatrix(embedding);
265
- }
266
- return embedding;
267
- }
268
-
269
- /**
270
- * Returns dimensionality reduction methods available.
271
- *
272
- * @readonly
273
- * @memberof DimensionalityReducer
274
- */
275
- static get availableMethods() {
276
- return DimensionalityReducer.methods;
277
- }
278
- }
@@ -1,132 +0,0 @@
1
- import {assert} from './operations';
2
-
3
- /**
4
- * Class to categorial encode/decode aligned amino acid residues sequence.
5
- *
6
- * @export
7
- * @class AlignedSequenceEncoder
8
- */
9
- export class AlignedSequenceEncoder {
10
- protected aa2num: {[name: string]: number};
11
- protected num2aa: {[code: number]: string};
12
-
13
- constructor () {
14
- this.aa2num = {
15
- '-': 0,
16
- 'A': 1,
17
- 'C': 2,
18
- 'D': 3,
19
- 'E': 4,
20
- 'F': 5,
21
- 'G': 6,
22
- 'H': 7,
23
- 'I': 8,
24
- 'K': 9,
25
- 'L': 10,
26
- 'M': 11,
27
- 'N': 12,
28
- 'P': 13,
29
- 'Q': 14,
30
- 'R': 15,
31
- 'S': 16,
32
- 'T': 17,
33
- 'V': 18,
34
- 'W': 19,
35
- 'Y': 20,
36
- };
37
- this.num2aa = {};
38
- Object.entries(this.aa2num).forEach(([k, v]) => (this.num2aa[v] = k));
39
- }
40
-
41
- /**
42
- * Truncate NH2 and -COOH terminals of the given sequence.
43
- *
44
- * @static
45
- * @param {string} seq The sequence provided.
46
- * @return {string} Truncated sequence.
47
- * @memberof AlignedSequenceEncoder
48
- */
49
- static _truncateSequence(seq: string): string {
50
- let start = 0;
51
- let end = seq.length;
52
- const termina = ['NH2', 'COOH'];
53
-
54
- if (seq.startsWith(termina[0])) {
55
- const l = termina[0].length; // Cut only 'NH2' without following '-'.
56
- assert(seq[l] == '-', `Wrong sequence format: ${termina[0]} without following '-' in '${seq}'.`)
57
- start = l;
58
- }
59
- if (seq.endsWith(termina[1])) {
60
- const l = termina[1].length+1; // Cut both 'COOH' and precending '-'.
61
- assert(seq[end-l] == '-', `Wrong sequence format: ${termina[1]} without '-' precending in '${seq}'.`)
62
- end -= l;
63
- }
64
- return seq.substring(start, end);
65
- }
66
-
67
- /**
68
- * Cuts auxiliary defises before a residue.
69
- *
70
- * @static
71
- * @param {string} seq The sequence to process.
72
- * @return {string} Processed sequence.
73
- * @memberof AlignedSequenceEncoder
74
- */
75
- static _dropDefises(seq: string): string {
76
- return seq.replace(/(-)([^-]+)/g, '$2')
77
- }
78
-
79
- /**
80
- * Performs truncation and cutting auxiliary defises.
81
- *
82
- * @static
83
- * @param {string} sequence The sequence work under process.
84
- * @return {string} Result of cleaning.
85
- * @memberof AlignedSequenceEncoder
86
- */
87
- static clean(sequence: string): string {
88
- return AlignedSequenceEncoder._dropDefises(AlignedSequenceEncoder._truncateSequence(sequence));
89
- }
90
-
91
- /**
92
- * Categorial encode of the sequence provided.
93
- *
94
- * @param {string} sequence The sequence.
95
- * @return {number[]} Encoded vector.
96
- * @memberof AlignedSequenceEncoder
97
- */
98
- public encode(sequence: string): number[] {
99
- const seq = AlignedSequenceEncoder.clean(sequence);
100
- const nItems = seq.length;
101
- let values = new Array(nItems).fill(0);
102
-
103
- for (let i = 0; i < nItems; ++i) {
104
- let char = seq[i];
105
-
106
- assert(char in this.aa2num, `Unknown char '${char}' found in sequence '${seq}'`);
107
-
108
- values[i] = this.aa2num[char];
109
- }
110
- return values;
111
- }
112
-
113
- /**
114
- * Decode the encoded vector into the sequence back.
115
- *
116
- * @param {number[]} value The vector encoded.
117
- * @return {string} Decoded sequence.
118
- * @memberof AlignedSequenceEncoder
119
- */
120
- public decode(value: number[]): string {
121
- let s: string = '';
122
-
123
- for (let i = 0; i < value.length; ++i) {
124
- let code = value[i];
125
-
126
- assert(code in this.num2aa, `Unknown code '${code}' found in vector '${value}'`);
127
-
128
- s += this.num2aa[code];
129
- }
130
- return s;
131
- }
132
- }