@aleph-ai/tinyaleph 1.2.0 → 1.3.0

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 (35) hide show
  1. package/README.md +187 -2
  2. package/backends/bioinformatics/binding.js +503 -0
  3. package/backends/bioinformatics/dna-computing.js +664 -0
  4. package/backends/bioinformatics/encoding.js +339 -0
  5. package/backends/bioinformatics/folding.js +454 -0
  6. package/backends/bioinformatics/genetic-code.js +269 -0
  7. package/backends/bioinformatics/index.js +522 -0
  8. package/backends/bioinformatics/transcription.js +221 -0
  9. package/backends/bioinformatics/translation.js +264 -0
  10. package/backends/index.js +25 -1
  11. package/core/compound.js +532 -0
  12. package/core/hilbert.js +454 -1
  13. package/core/index.js +106 -12
  14. package/core/inference.js +605 -0
  15. package/core/resonance.js +245 -616
  16. package/core/symbols/archetypes.js +478 -0
  17. package/core/symbols/base.js +302 -0
  18. package/core/symbols/elements.js +487 -0
  19. package/core/symbols/hieroglyphs.js +303 -0
  20. package/core/symbols/iching.js +471 -0
  21. package/core/symbols/index.js +77 -0
  22. package/core/symbols/tarot.js +211 -0
  23. package/core/symbols.js +22 -0
  24. package/docs/design/BIOINFORMATICS_BACKEND_DESIGN.md +493 -0
  25. package/docs/guide/06-symbolic-ai.md +370 -0
  26. package/docs/guide/README.md +2 -1
  27. package/docs/reference/05-symbolic-ai.md +570 -0
  28. package/docs/reference/06-bioinformatics.md +546 -0
  29. package/docs/reference/README.md +32 -2
  30. package/docs/theory/11-prgraph-memory.md +559 -0
  31. package/docs/theory/12-resonant-attention.md +661 -0
  32. package/modular.js +33 -1
  33. package/package.json +1 -1
  34. package/physics/index.js +16 -0
  35. package/physics/kuramoto-coupled-ladder.js +603 -0
@@ -0,0 +1,522 @@
1
+ /**
2
+ * BioinformaticsBackend - Prime-based biological computation
3
+ *
4
+ * A complete backend for bioinformatics within the tinyaleph framework.
5
+ * Treats biological systems as prime-resonant information processors.
6
+ *
7
+ * Features:
8
+ * - DNA/RNA/Protein sequence encoding to primes
9
+ * - Central Dogma transforms (transcription, translation)
10
+ * - Protein folding via Kuramoto oscillator dynamics
11
+ * - Molecular binding via prime resonance
12
+ * - DNA computing (logic gates, strand displacement)
13
+ */
14
+
15
+ const { Backend } = require('../interface');
16
+ const { Hypercomplex } = require('../../core/hypercomplex');
17
+ const { primeToFrequency } = require('../../core/prime');
18
+
19
+ // Import all submodules
20
+ const encoding = require('./encoding');
21
+ const geneticCode = require('./genetic-code');
22
+ const { TranscriptionOperator } = require('./transcription');
23
+ const { TranslationOperator } = require('./translation');
24
+ const { FoldingTransform } = require('./folding');
25
+ const dnaComputing = require('./dna-computing');
26
+ const binding = require('./binding');
27
+
28
+ /**
29
+ * BioinformaticsBackend
30
+ *
31
+ * Implements the Backend interface for biological computation.
32
+ */
33
+ class BioinformaticsBackend extends Backend {
34
+ constructor(config = {}) {
35
+ super(config);
36
+
37
+ this.dimension = config.dimension || 32;
38
+
39
+ // Initialize encodings
40
+ this.nucleotidePrimes = config.nucleotidePrimes || encoding.NUCLEOTIDE_PRIMES;
41
+ this.aminoAcidPrimes = config.aminoAcidPrimes || encoding.AMINO_ACID_PRIMES;
42
+ this.geneticCode = config.geneticCode || geneticCode.STANDARD_GENETIC_CODE;
43
+
44
+ // Initialize operators
45
+ this.transcription = new TranscriptionOperator(config.transcriptionOptions || {});
46
+ this.translation = new TranslationOperator(this.geneticCode);
47
+ this.folding = new FoldingTransform(config.foldingOptions || {});
48
+ this.affinityCalculator = new binding.BindingAffinityCalculator(config.bindingOptions || {});
49
+ this.docker = new binding.MolecularDocker(config.dockingOptions || {});
50
+
51
+ // Build prime list (all biological primes)
52
+ this.config.primes = this.buildPrimeList();
53
+
54
+ // Build transform registry
55
+ this.transforms = this.buildTransforms();
56
+ }
57
+
58
+ // ===========================================================================
59
+ // Backend Interface Methods
60
+ // ===========================================================================
61
+
62
+ /**
63
+ * Encode biological input to prime array
64
+ * Accepts: DNA, RNA, Protein sequences, FASTA format
65
+ */
66
+ encode(input) {
67
+ if (typeof input !== 'string') {
68
+ throw new Error('Input must be a string sequence');
69
+ }
70
+
71
+ const inputType = encoding.detectSequenceType(input);
72
+
73
+ switch (inputType) {
74
+ case 'DNA':
75
+ return encoding.encodeDNA(input);
76
+ case 'RNA':
77
+ return encoding.encodeRNA(input);
78
+ case 'PROTEIN':
79
+ return encoding.encodeProtein(input);
80
+ case 'FASTA':
81
+ return this.encodeFASTA(input);
82
+ default:
83
+ // Try each encoding and return first success
84
+ const dna = encoding.encodeDNA(input);
85
+ if (dna.length > 0) return dna;
86
+ const protein = encoding.encodeProtein(input);
87
+ if (protein.length > 0) return protein;
88
+ throw new Error(`Cannot encode input: ${input.slice(0, 50)}...`);
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Decode prime array back to biological sequence
94
+ */
95
+ decode(primes) {
96
+ if (!primes || primes.length === 0) {
97
+ return '';
98
+ }
99
+
100
+ // Detect sequence type from prime range
101
+ const maxPrime = Math.max(...primes);
102
+ const minPrime = Math.min(...primes);
103
+
104
+ // Nucleotide range: 2, 3, 5, 7, 11
105
+ if (maxPrime <= 11) {
106
+ // Check if RNA (has U=5) or DNA
107
+ if (primes.includes(5)) {
108
+ return encoding.decodeRNA(primes);
109
+ }
110
+ return encoding.decodeDNA(primes);
111
+ }
112
+
113
+ // Amino acid range: 23-113
114
+ if (minPrime >= 23) {
115
+ return encoding.decodeProtein(primes);
116
+ }
117
+
118
+ // Mixed - try protein first
119
+ return encoding.decodeProtein(primes);
120
+ }
121
+
122
+ /**
123
+ * Convert primes to hypercomplex state vector
124
+ */
125
+ primesToState(primes) {
126
+ const state = Hypercomplex.zero(this.dimension);
127
+
128
+ if (!primes || primes.length === 0) {
129
+ return state;
130
+ }
131
+
132
+ const n = primes.length;
133
+
134
+ for (let i = 0; i < n; i++) {
135
+ const p = primes[i];
136
+ const idx = this.primeToIndex(p);
137
+
138
+ // Phase encodes position in sequence
139
+ const phase = (2 * Math.PI * i) / n;
140
+
141
+ // Amplitude from prime value (normalized)
142
+ const amplitude = 1 / Math.sqrt(p);
143
+
144
+ // Distribute across components using phase encoding
145
+ state.c[idx % this.dimension] += amplitude * Math.cos(phase);
146
+ state.c[(idx + 1) % this.dimension] += amplitude * Math.sin(phase);
147
+ }
148
+
149
+ return state.normalize();
150
+ }
151
+
152
+ /**
153
+ * Convert primes to oscillator frequencies
154
+ */
155
+ primesToFrequencies(primes) {
156
+ return primes.map(p => primeToFrequency(p, 1, 10));
157
+ }
158
+
159
+ /**
160
+ * Apply a biological transform
161
+ */
162
+ applyTransform(inputPrimes, transform) {
163
+ switch (transform.type) {
164
+ case 'transcription':
165
+ return this.transcription.apply(inputPrimes);
166
+
167
+ case 'translation':
168
+ const translationResult = this.translation.apply(inputPrimes);
169
+ return translationResult.success ? translationResult.protein : inputPrimes;
170
+
171
+ case 'complement':
172
+ return this.transcription.complement(inputPrimes);
173
+
174
+ case 'reverse_complement':
175
+ return this.transcription.reverseComplement(inputPrimes);
176
+
177
+ case 'fold':
178
+ const foldResult = this.folding.fold(inputPrimes);
179
+ return foldResult.success ? foldResult.phases.map(p => Math.floor(p * 100)) : inputPrimes;
180
+
181
+ case 'mutation':
182
+ return this.applyMutation(inputPrimes, transform);
183
+
184
+ default:
185
+ return inputPrimes;
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Get available transforms
191
+ */
192
+ getTransforms() {
193
+ return this.transforms;
194
+ }
195
+
196
+ /**
197
+ * Get prime list used by this backend
198
+ */
199
+ getPrimes() {
200
+ return this.config.primes;
201
+ }
202
+
203
+ /**
204
+ * Get backend name
205
+ */
206
+ getName() {
207
+ return 'BioinformaticsBackend';
208
+ }
209
+
210
+ // ===========================================================================
211
+ // Bioinformatics-Specific Methods
212
+ // ===========================================================================
213
+
214
+ /**
215
+ * Full transcription: DNA → RNA
216
+ */
217
+ transcribe(dnaPrimes, options = {}) {
218
+ return this.transcription.transcribe(dnaPrimes, options);
219
+ }
220
+
221
+ /**
222
+ * Full translation: RNA → Protein
223
+ */
224
+ translate(rnaPrimes, options = {}) {
225
+ return this.translation.apply(rnaPrimes, options);
226
+ }
227
+
228
+ /**
229
+ * Express gene: DNA → RNA → Protein (full Central Dogma)
230
+ */
231
+ express(dnaPrimes, options = {}) {
232
+ // Transcribe
233
+ const rnaResult = this.transcription.transcribe(dnaPrimes, { force: true, ...options });
234
+ const rnaPrimes = rnaResult.success ? rnaResult.rna : this.transcription.apply(dnaPrimes);
235
+
236
+ // Translate
237
+ const proteinResult = this.translation.apply(rnaPrimes, options);
238
+
239
+ return {
240
+ success: proteinResult.success,
241
+ dna: dnaPrimes,
242
+ rna: rnaPrimes,
243
+ protein: proteinResult.protein,
244
+ length: proteinResult.length,
245
+ sequence: this.decode(proteinResult.protein)
246
+ };
247
+ }
248
+
249
+ /**
250
+ * Fold protein: Protein primes → 3D structure
251
+ */
252
+ foldProtein(proteinPrimes, options = {}) {
253
+ return this.folding.fold(proteinPrimes);
254
+ }
255
+
256
+ /**
257
+ * Compute binding affinity between two molecules
258
+ */
259
+ bindingAffinity(mol1Primes, mol2Primes) {
260
+ return this.affinityCalculator.computeAffinity(mol1Primes, mol2Primes);
261
+ }
262
+
263
+ /**
264
+ * Dock two molecules
265
+ */
266
+ dock(receptorPrimes, ligandPrimes) {
267
+ return this.docker.dock(receptorPrimes, ligandPrimes);
268
+ }
269
+
270
+ /**
271
+ * Screen ligand library
272
+ */
273
+ screenLigands(targetPrimes, ligandLibrary) {
274
+ return this.affinityCalculator.screenLibrary(targetPrimes, ligandLibrary);
275
+ }
276
+
277
+ /**
278
+ * Calculate sequence similarity using prime resonance
279
+ */
280
+ similarity(primes1, primes2) {
281
+ const state1 = this.primesToState(primes1);
282
+ const state2 = this.primesToState(primes2);
283
+
284
+ // Coherence as similarity measure
285
+ const n1 = state1.norm();
286
+ const n2 = state2.norm();
287
+
288
+ if (n1 < 1e-10 || n2 < 1e-10) return 0;
289
+
290
+ return Math.abs(state1.dot(state2)) / (n1 * n2);
291
+ }
292
+
293
+ /**
294
+ * Align two sequences using oscillator synchronization
295
+ */
296
+ align(primes1, primes2) {
297
+ const freq1 = this.primesToFrequencies(primes1);
298
+ const freq2 = this.primesToFrequencies(primes2);
299
+
300
+ // Simple alignment score based on frequency overlap
301
+ let alignmentScore = 0;
302
+ const minLen = Math.min(freq1.length, freq2.length);
303
+
304
+ for (let i = 0; i < minLen; i++) {
305
+ const diff = Math.abs(freq1[i] - freq2[i]);
306
+ alignmentScore += 1 / (1 + diff);
307
+ }
308
+
309
+ return {
310
+ score: alignmentScore / minLen,
311
+ length: minLen,
312
+ similarity: this.similarity(primes1, primes2)
313
+ };
314
+ }
315
+
316
+ // ===========================================================================
317
+ // DNA Computing Methods
318
+ // ===========================================================================
319
+
320
+ /**
321
+ * Create DNA strand
322
+ */
323
+ createDNAStrand(sequence, options = {}) {
324
+ return new dnaComputing.DNAStrand(sequence, options);
325
+ }
326
+
327
+ /**
328
+ * Create AND gate
329
+ */
330
+ createANDGate(options = {}) {
331
+ return new dnaComputing.ANDGate(options);
332
+ }
333
+
334
+ /**
335
+ * Create OR gate
336
+ */
337
+ createORGate(options = {}) {
338
+ return new dnaComputing.ORGate(options);
339
+ }
340
+
341
+ /**
342
+ * Create NOT gate
343
+ */
344
+ createNOTGate(options = {}) {
345
+ return new dnaComputing.NOTGate(options);
346
+ }
347
+
348
+ /**
349
+ * Create DNA circuit
350
+ */
351
+ createCircuit(name = 'circuit') {
352
+ return new dnaComputing.DNACircuit(name);
353
+ }
354
+
355
+ /**
356
+ * Simulate strand displacement
357
+ */
358
+ simulateStrandDisplacement(options) {
359
+ const reaction = new dnaComputing.StrandDisplacementReaction(options);
360
+ return reaction.simulate(options.time || 1.0, options.dt || 0.001);
361
+ }
362
+
363
+ // ===========================================================================
364
+ // Helper Methods
365
+ // ===========================================================================
366
+
367
+ /**
368
+ * Map prime to dimension index
369
+ */
370
+ primeToIndex(prime) {
371
+ const primeList = this.config.primes;
372
+ const idx = primeList.indexOf(prime);
373
+ return idx >= 0 ? idx : prime % this.dimension;
374
+ }
375
+
376
+ /**
377
+ * Build combined prime list
378
+ */
379
+ buildPrimeList() {
380
+ const primes = new Set();
381
+
382
+ // Add nucleotide primes
383
+ Object.values(encoding.NUCLEOTIDE_PRIMES).forEach(p => primes.add(p));
384
+
385
+ // Add amino acid primes
386
+ Object.values(encoding.AMINO_ACID_PRIMES).forEach(p => primes.add(p));
387
+
388
+ return [...primes].sort((a, b) => a - b);
389
+ }
390
+
391
+ /**
392
+ * Build transform registry
393
+ */
394
+ buildTransforms() {
395
+ return [
396
+ { type: 'transcription', name: 'DNA→RNA', n: 'transcription' },
397
+ { type: 'translation', name: 'RNA→Protein', n: 'translation' },
398
+ { type: 'complement', name: 'DNA complement', n: 'complement' },
399
+ { type: 'reverse_complement', name: 'Reverse complement', n: 'reverse_complement' },
400
+ { type: 'fold', name: 'Protein folding', n: 'fold' },
401
+ { type: 'mutation', mutationType: 'point', name: 'Point mutation', n: 'point_mutation' },
402
+ { type: 'mutation', mutationType: 'insertion', name: 'Insertion', n: 'insertion' },
403
+ { type: 'mutation', mutationType: 'deletion', name: 'Deletion', n: 'deletion' },
404
+ ];
405
+ }
406
+
407
+ /**
408
+ * Apply mutation transform
409
+ */
410
+ applyMutation(primes, transform) {
411
+ const pos = transform.position || 0;
412
+
413
+ switch (transform.mutationType) {
414
+ case 'point':
415
+ const newPrimes = [...primes];
416
+ if (pos < newPrimes.length) {
417
+ newPrimes[pos] = transform.value || this.nucleotidePrimes['A'];
418
+ }
419
+ return newPrimes;
420
+
421
+ case 'insertion':
422
+ const insertPrimes = transform.insertPrimes || [this.nucleotidePrimes['A']];
423
+ return [
424
+ ...primes.slice(0, pos),
425
+ ...insertPrimes,
426
+ ...primes.slice(pos)
427
+ ];
428
+
429
+ case 'deletion':
430
+ const deleteLength = transform.length || 1;
431
+ return [
432
+ ...primes.slice(0, pos),
433
+ ...primes.slice(pos + deleteLength)
434
+ ];
435
+
436
+ default:
437
+ return primes;
438
+ }
439
+ }
440
+
441
+ /**
442
+ * Encode FASTA format
443
+ */
444
+ encodeFASTA(input) {
445
+ const entries = encoding.parseFASTA(input);
446
+
447
+ if (entries.length === 0) {
448
+ throw new Error('Invalid FASTA format');
449
+ }
450
+
451
+ // Encode first entry
452
+ const entry = entries[0];
453
+ const type = encoding.detectSequenceType(entry.sequence);
454
+
455
+ switch (type) {
456
+ case 'DNA':
457
+ return encoding.encodeDNA(entry.sequence);
458
+ case 'RNA':
459
+ return encoding.encodeRNA(entry.sequence);
460
+ case 'PROTEIN':
461
+ return encoding.encodeProtein(entry.sequence);
462
+ default:
463
+ return encoding.encodeDNA(entry.sequence);
464
+ }
465
+ }
466
+
467
+ /**
468
+ * Get amino acid properties
469
+ */
470
+ getAminoAcidProperties(aa) {
471
+ return encoding.getAminoAcidProperties(aa);
472
+ }
473
+
474
+ /**
475
+ * Get codon usage statistics
476
+ */
477
+ calculateCAI(sequence) {
478
+ return geneticCode.calculateCAI(sequence);
479
+ }
480
+
481
+ /**
482
+ * Get GC content
483
+ */
484
+ calculateGCContent(sequence) {
485
+ return geneticCode.calculateGCContent(sequence);
486
+ }
487
+ }
488
+
489
+ // Export everything
490
+ module.exports = {
491
+ // Main backend
492
+ BioinformaticsBackend,
493
+
494
+ // Encoding
495
+ ...encoding,
496
+
497
+ // Genetic code
498
+ ...geneticCode,
499
+
500
+ // Operators
501
+ TranscriptionOperator,
502
+ TranslationOperator,
503
+ FoldingTransform,
504
+
505
+ // Binding
506
+ BindingAffinityCalculator: binding.BindingAffinityCalculator,
507
+ MolecularDocker: binding.MolecularDocker,
508
+ ProteinProteinDocker: binding.ProteinProteinDocker,
509
+
510
+ // DNA Computing
511
+ DNAStrand: dnaComputing.DNAStrand,
512
+ DNADuplex: dnaComputing.DNADuplex,
513
+ ANDGate: dnaComputing.ANDGate,
514
+ ORGate: dnaComputing.ORGate,
515
+ NOTGate: dnaComputing.NOTGate,
516
+ NANDGate: dnaComputing.NANDGate,
517
+ SeesawGate: dnaComputing.SeesawGate,
518
+ StrandDisplacementReaction: dnaComputing.StrandDisplacementReaction,
519
+ DNACircuit: dnaComputing.DNACircuit,
520
+ createHalfAdder: dnaComputing.createHalfAdder,
521
+ createFullAdder: dnaComputing.createFullAdder,
522
+ };