numrs2 0.3.0

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
Documentation
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Bioinformatics module for NumRS2.
//!
//! Wraps and extends the bioinformatics capabilities from `scirs2-core` 0.3.0,
//! providing ergonomic Rust interfaces for:
//!
//! - **Sequence alignment**: Needleman-Wunsch (global), Smith-Waterman (local),
//!   Levenshtein edit distance.
//! - **Phylogenetic distances**: Hamming distance, Jukes-Cantor evolutionary
//!   distance, pairwise distance matrix.
//! - **Sequence statistics**: nucleotide frequencies, GC content, codon usage.
//!
//! All functions accept `&str` or `&[&str]` rather than raw byte slices,
//! converting internally so callers work with familiar Rust string types.
//!
//! # Quick Start
//!
//! ```rust
//! use numrs2::new_modules::bioinformatics::{
//!     needleman_wunsch, smith_waterman, edit_distance,
//!     hamming_distance, jukes_cantor_distance, distance_matrix,
//!     nucleotide_frequencies, gc_content, codon_usage,
//! };
//!
//! // Global alignment
//! let result = needleman_wunsch("AGCT", "AGT", 1, -1, -2).expect("alignment failed");
//! assert_eq!(result.aligned_seq1.len(), result.aligned_seq2.len());
//!
//! // GC content
//! let gc = gc_content("AATGCG");
//! assert!((gc - 0.5).abs() < 1e-10);
//! ```
//!
//! # References
//!
//! - Needleman & Wunsch (1970), *J. Mol. Biol.* 48: 443-453.
//! - Smith & Waterman (1981), *J. Mol. Biol.* 147: 195-197.
//! - Jukes & Cantor (1969), *Mammalian Protein Metabolism* 21-132.
//! - Levenshtein (1966), *Soviet Physics Doklady* 10: 707-710.

use std::collections::HashMap;

use scirs2_core::bioinformatics::alignment::{
    edit_distance as core_edit_distance, needleman_wunsch as core_nw, smith_waterman as core_sw,
};
use scirs2_core::bioinformatics::phylo::{
    distance_matrix as core_distance_matrix, hamming_distance as core_hamming,
    jukes_cantor_distance as core_jc,
};
use scirs2_core::bioinformatics::sequence::gc_content as core_gc_content;
use scirs2_core::bioinformatics::stats::{
    codon_usage_table as core_codon_usage, nucleotide_frequencies as core_nuc_freq,
};

use crate::error::{NumRs2Error, Result};

// ─── Result and helper types ──────────────────────────────────────────────────

/// Converts a `scirs2_core` error into a `NumRs2Error`.
fn map_core_err(e: scirs2_core::error::CoreError) -> NumRs2Error {
    NumRs2Error::ComputationError(format!("scirs2-core bioinformatics error: {e}"))
}

// ─── AlignmentResult ─────────────────────────────────────────────────────────

/// Result of a pairwise sequence alignment.
///
/// Both `aligned_seq1` and `aligned_seq2` have equal length; gap characters
/// are represented by `'-'`.
#[derive(Debug, Clone, PartialEq)]
pub struct AlignmentResult {
    /// Alignment score (higher is better for global; 0 is the minimum for
    /// local alignments).
    pub score: f64,
    /// First aligned sequence, possibly padded with `'-'` characters.
    pub aligned_seq1: String,
    /// Second aligned sequence, possibly padded with `'-'` characters.
    pub aligned_seq2: String,
}

// ─── Sequence alignment ───────────────────────────────────────────────────────

/// Performs global pairwise sequence alignment using the Needleman-Wunsch
/// dynamic-programming algorithm.
///
/// # Parameters
///
/// - `seq1`, `seq2` — sequences to align (ASCII, case-sensitive).
/// - `match_score`  — score for matching characters.
/// - `mismatch`     — score for mismatching characters (typically negative).
/// - `gap`          — linear gap penalty (typically negative).
///
/// # Errors
///
/// Returns `NumRs2Error::ComputationError` on allocation failure (extremely
/// large sequences).
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::needleman_wunsch;
///
/// let result = needleman_wunsch("AGCT", "AGT", 1, -1, -2).expect("alignment failed");
/// assert_eq!(result.aligned_seq1.len(), result.aligned_seq2.len());
/// assert!(result.score > 0.0);
/// ```
pub fn needleman_wunsch(
    seq1: &str,
    seq2: &str,
    match_score: i32,
    mismatch: i32,
    gap: i32,
) -> Result<AlignmentResult> {
    let (score, a1, a2) = core_nw(seq1.as_bytes(), seq2.as_bytes(), match_score, mismatch, gap)
        .map_err(map_core_err)?;
    Ok(AlignmentResult {
        score: score as f64,
        aligned_seq1: a1,
        aligned_seq2: a2,
    })
}

/// Performs local pairwise sequence alignment using the Smith-Waterman
/// dynamic-programming algorithm.
///
/// Local alignment finds the highest-scoring subsequence pair; gap characters
/// are `'-'`.
///
/// # Parameters
///
/// - `seq1`, `seq2` — sequences to align (ASCII, case-sensitive).
/// - `match_score`  — score for matching characters.
/// - `mismatch`     — score for mismatching characters (typically negative).
/// - `gap`          — linear gap penalty (typically negative).
///
/// # Errors
///
/// Returns `NumRs2Error::ComputationError` on internal error.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::smith_waterman;
///
/// let result = smith_waterman("TGTTACGG", "GGTTGACTA", 3, -3, -2)
///     .expect("alignment failed");
/// assert!(result.score >= 0.0);
/// assert_eq!(result.aligned_seq1.len(), result.aligned_seq2.len());
/// ```
pub fn smith_waterman(
    seq1: &str,
    seq2: &str,
    match_score: i32,
    mismatch: i32,
    gap: i32,
) -> Result<AlignmentResult> {
    let (score, a1, a2) = core_sw(seq1.as_bytes(), seq2.as_bytes(), match_score, mismatch, gap)
        .map_err(map_core_err)?;
    Ok(AlignmentResult {
        score: score as f64,
        aligned_seq1: a1,
        aligned_seq2: a2,
    })
}

/// Computes the Levenshtein edit distance between two sequences.
///
/// The edit distance is the minimum number of single-character insertions,
/// deletions, or substitutions needed to transform `seq1` into `seq2`.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::edit_distance;
///
/// assert_eq!(edit_distance("kitten", "sitting"), 3);
/// assert_eq!(edit_distance("ATGC", "ATCC"), 1);
/// assert_eq!(edit_distance("", ""), 0);
/// ```
pub fn edit_distance(seq1: &str, seq2: &str) -> usize {
    core_edit_distance(seq1.as_bytes(), seq2.as_bytes())
}

// ─── Phylogenetic distances ───────────────────────────────────────────────────

/// Returns the Hamming distance between two sequences of equal length.
///
/// The Hamming distance counts positions at which the two sequences differ
/// (comparison is ASCII case-insensitive).
///
/// # Errors
///
/// Returns `NumRs2Error::DimensionMismatch` when the sequences have different
/// lengths.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::hamming_distance;
///
/// assert_eq!(hamming_distance("ATGC", "ATGC").expect("equal lengths"), 0);
/// assert_eq!(hamming_distance("ATGC", "TTGT").expect("equal lengths"), 2);
/// ```
pub fn hamming_distance(seq1: &str, seq2: &str) -> Result<usize> {
    core_hamming(seq1.as_bytes(), seq2.as_bytes()).map_err(map_core_err)
}

/// Converts a p-distance (fraction of differing sites) to a Jukes-Cantor
/// evolutionary distance.
///
/// ## Formula
///
/// ```text
/// d_JC = -3/4 × ln(1 − 4p/3)
/// ```
///
/// # Parameters
///
/// - `p` — observed p-distance in `[0.0, 0.75)`.
///
/// # Errors
///
/// Returns `NumRs2Error::ValueError` when `p` is outside `[0, 0.75)` or is
/// NaN.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::jukes_cantor_distance;
///
/// let d = jukes_cantor_distance(0.0).expect("valid p");
/// assert!((d - 0.0).abs() < 1e-10);
///
/// let d2 = jukes_cantor_distance(0.1).expect("valid p");
/// assert!((d2 - 0.10753).abs() < 1e-5);
/// ```
pub fn jukes_cantor_distance(p: f64) -> Result<f64> {
    core_jc(p).map_err(map_core_err)
}

/// Computes the pairwise Jukes-Cantor distance matrix for a slice of
/// sequences.
///
/// All sequences must have the same length.  The returned `Vec<Vec<f64>>` is
/// an `n × n` symmetric matrix with zeros on the diagonal.  Pairs whose
/// p-distance is ≥ 0.75 (where the JC formula is undefined) get
/// `f64::INFINITY`.
///
/// # Errors
///
/// Returns `NumRs2Error::DimensionMismatch` when the input is empty or
/// sequences have different lengths.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::distance_matrix;
///
/// let seqs = &["ATGC", "ATGC", "TTGT"];
/// let mat = distance_matrix(seqs).expect("valid sequences");
/// assert_eq!(mat.len(), 3);
/// assert!((mat[0][0] - 0.0).abs() < 1e-10);
/// ```
pub fn distance_matrix(sequences: &[&str]) -> Result<Vec<Vec<f64>>> {
    // Convert &[&str] → Vec<&[u8]>
    let byte_seqs: Vec<&[u8]> = sequences.iter().map(|s| s.as_bytes()).collect();
    let arr = core_distance_matrix(&byte_seqs).map_err(map_core_err)?;

    let n = sequences.len();
    let mut mat = vec![vec![0.0f64; n]; n];
    for i in 0..n {
        for j in 0..n {
            mat[i][j] = arr[[i, j]];
        }
    }
    Ok(mat)
}

// ─── Sequence statistics ──────────────────────────────────────────────────────

/// Computes the relative frequencies of A, C, G, T in `seq`.
///
/// The returned `HashMap` maps each nucleotide character (`'A'`, `'C'`, `'G'`,
/// `'T'`) to its relative frequency.  Ambiguous or non-ACGT characters are
/// ignored in both the counts and the denominator.
///
/// Returns an empty map if `seq` contains no ACGT bases.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::nucleotide_frequencies;
///
/// let freqs = nucleotide_frequencies("AACGT");
/// assert!((freqs[&'A'] - 0.4).abs() < 1e-10);
/// assert!((freqs[&'C'] - 0.2).abs() < 1e-10);
/// ```
pub fn nucleotide_frequencies(seq: &str) -> HashMap<char, f64> {
    let raw = core_nuc_freq(seq.as_bytes()); // [A, C, G, T]
    let mut map = HashMap::with_capacity(4);
    map.insert('A', raw[0]);
    map.insert('C', raw[1]);
    map.insert('G', raw[2]);
    map.insert('T', raw[3]);
    map
}

/// Returns the GC content of a nucleotide sequence.
///
/// GC content is the fraction of G and C bases among all ACGT bases.
/// Non-ACGT characters are ignored.  Returns `0.0` for an empty or
/// all-non-ACGT sequence.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::gc_content;
///
/// assert!((gc_content("AATGCG") - 0.5).abs() < 1e-10);
/// assert!((gc_content("AAAA") - 0.0).abs() < 1e-10);
/// assert!((gc_content("GCGC") - 1.0).abs() < 1e-10);
/// ```
pub fn gc_content(seq: &str) -> f64 {
    core_gc_content(seq.as_bytes())
}

/// Returns a codon usage table for a DNA sequence.
///
/// Each codon (3-character substring) that contains only ACGT characters is
/// counted.  The returned map uses the uppercase codon string (e.g. `"ATG"`)
/// as the key and the raw count as the value.  Codons containing ambiguous
/// bases are skipped.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::codon_usage;
///
/// let usage = codon_usage("ATGATGATG");
/// assert_eq!(*usage.get("ATG").unwrap_or(&0), 3);
/// ```
pub fn codon_usage(seq: &str) -> HashMap<String, usize> {
    // core returns HashMap<[u8;3], f64> (relative frequency); we convert to
    // counts by multiplying by the number of codons and rounding.
    let bytes = seq.as_bytes();
    let num_codons = bytes.len() / 3;

    // Build raw count map directly for accuracy (avoid float rounding).
    let mut counts: HashMap<String, usize> = HashMap::new();
    for i in (0..bytes.len().saturating_sub(2)).step_by(3) {
        let codon = &bytes[i..i + 3];
        // Validate: all three characters must be ACGT.
        let valid = codon
            .iter()
            .all(|&b| matches!(b.to_ascii_uppercase(), b'A' | b'C' | b'G' | b'T'));
        if valid {
            let key = String::from_utf8_lossy(codon).to_ascii_uppercase();
            *counts.entry(key).or_insert(0) += 1;
        }
    }

    // Suppress unused variable warning from num_codons (used implicitly above).
    let _ = num_codons;

    counts
}

// ─── Additional helpers ───────────────────────────────────────────────────────

/// Returns the complement of a DNA sequence (A↔T, C↔G).
///
/// Non-ACGT characters are passed through unchanged.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::complement;
///
/// assert_eq!(complement("ATGC"), "TACG");
/// ```
pub fn complement(seq: &str) -> String {
    let rc = scirs2_core::bioinformatics::sequence::complement(seq.as_bytes());
    String::from_utf8_lossy(&rc).into_owned()
}

/// Returns the reverse complement of a DNA sequence.
///
/// # Examples
///
/// ```rust
/// use numrs2::new_modules::bioinformatics::reverse_complement;
///
/// assert_eq!(reverse_complement("ATGCTT"), "AAGCAT");
/// ```
pub fn reverse_complement(seq: &str) -> String {
    let rc = scirs2_core::bioinformatics::sequence::reverse_complement(seq.as_bytes());
    String::from_utf8_lossy(&rc).into_owned()
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── edit_distance ─────────────────────────────────────────────────────────

    #[test]
    fn test_edit_distance_identical() {
        assert_eq!(edit_distance("ATGC", "ATGC"), 0);
    }

    #[test]
    fn test_edit_distance_single_substitution() {
        assert_eq!(edit_distance("ATGC", "ATCC"), 1);
    }

    #[test]
    fn test_edit_distance_insertion_deletion() {
        // "kitten" → "sitting" requires 3 edits
        assert_eq!(edit_distance("kitten", "sitting"), 3);
    }

    #[test]
    fn test_edit_distance_empty_sequences() {
        assert_eq!(edit_distance("", ""), 0);
        assert_eq!(edit_distance("ABC", ""), 3);
        assert_eq!(edit_distance("", "XYZ"), 3);
    }

    #[test]
    fn test_edit_distance_biological_sequences() {
        // One deletion: AGCT → AGT
        assert_eq!(edit_distance("AGCT", "AGT"), 1);
        // Two substitutions
        assert_eq!(edit_distance("AAAAAA", "AAGGAA"), 2);
    }

    // ── needleman_wunsch ──────────────────────────────────────────────────────

    #[test]
    fn test_needleman_wunsch_identical() {
        let result = needleman_wunsch("AGCT", "AGCT", 1, -1, -2).expect("alignment should succeed");
        // Identical sequences: no gaps, score = length × match_score
        assert_eq!(result.score, 4.0);
        assert_eq!(result.aligned_seq1, "AGCT");
        assert_eq!(result.aligned_seq2, "AGCT");
    }

    #[test]
    fn test_needleman_wunsch_with_gap() {
        let result = needleman_wunsch("AGCT", "AGT", 1, -1, -2).expect("alignment should succeed");
        // Lengths must be equal after alignment
        assert_eq!(result.aligned_seq1.len(), result.aligned_seq2.len());
        // Score should be positive (3 matches, 1 gap)
        assert!(result.score > 0.0);
        // One gap character somewhere
        let gaps1 = result.aligned_seq1.chars().filter(|&c| c == '-').count();
        let gaps2 = result.aligned_seq2.chars().filter(|&c| c == '-').count();
        assert_eq!(gaps1 + gaps2, 1, "expected exactly one gap total");
    }

    #[test]
    fn test_needleman_wunsch_empty_sequences() {
        let result = needleman_wunsch("", "", 1, -1, -2).expect("empty alignment should succeed");
        assert_eq!(result.score, 0.0);
        assert_eq!(result.aligned_seq1, "");
        assert_eq!(result.aligned_seq2, "");
    }

    #[test]
    fn test_needleman_wunsch_one_empty() {
        let result =
            needleman_wunsch("ATGC", "", 1, -1, -2).expect("one-empty alignment should succeed");
        // All gaps on the second aligned sequence
        assert_eq!(result.aligned_seq1.len(), result.aligned_seq2.len());
        assert!(result.aligned_seq2.chars().all(|c| c == '-'));
    }

    // ── smith_waterman ────────────────────────────────────────────────────────

    #[test]
    fn test_smith_waterman_local_alignment() {
        // Classic SW example: best local alignment should yield score > 0
        let result =
            smith_waterman("TGTTACGG", "GGTTGACTA", 3, -3, -2).expect("alignment should succeed");
        assert!(result.score > 0.0);
        assert_eq!(result.aligned_seq1.len(), result.aligned_seq2.len());
    }

    #[test]
    fn test_smith_waterman_no_match() {
        // Sequences with no similar region (all mismatches, high penalty)
        let result = smith_waterman("AAAA", "TTTT", 1, -5, -5).expect("alignment should succeed");
        // Score may be 0 when no local alignment is profitable
        assert!(result.score >= 0.0);
    }

    // ── hamming_distance ──────────────────────────────────────────────────────

    #[test]
    fn test_hamming_distance_identical() {
        assert_eq!(hamming_distance("ATGC", "ATGC").expect("equal lengths"), 0);
    }

    #[test]
    fn test_hamming_distance_two_differences() {
        assert_eq!(hamming_distance("ATGC", "TTGT").expect("equal lengths"), 2);
    }

    #[test]
    fn test_hamming_distance_length_mismatch() {
        assert!(
            hamming_distance("ATGC", "ATG").is_err(),
            "unequal lengths must return an error"
        );
    }

    #[test]
    fn test_hamming_distance_empty() {
        assert_eq!(hamming_distance("", "").expect("empty sequences"), 0);
    }

    // ── jukes_cantor_distance ─────────────────────────────────────────────────

    #[test]
    fn test_jukes_cantor_zero_p() {
        let d = jukes_cantor_distance(0.0).expect("p=0 is valid");
        assert!((d - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_jukes_cantor_known_value() {
        // p = 0.1 → d = -0.75 * ln(1 - 4/3 * 0.1) ≈ 0.10733
        let d = jukes_cantor_distance(0.1).expect("p=0.1 is valid");
        let expected = -0.75 * (1.0 - (4.0 / 3.0) * 0.1_f64).ln();
        assert!(
            (d - expected).abs() < 1e-10,
            "expected d ≈ {expected}, got {d}"
        );
    }

    #[test]
    fn test_jukes_cantor_invalid_p() {
        assert!(jukes_cantor_distance(0.75).is_err(), "p=0.75 is invalid");
        assert!(jukes_cantor_distance(1.0).is_err(), "p=1.0 is invalid");
        assert!(jukes_cantor_distance(-0.1).is_err(), "p<0 is invalid");
    }

    // ── distance_matrix ───────────────────────────────────────────────────────

    #[test]
    fn test_distance_matrix_basic() {
        let seqs = &["ATGC", "ATGC", "TTGT"];
        let mat = distance_matrix(seqs).expect("valid sequences");
        assert_eq!(mat.len(), 3);
        assert_eq!(mat[0].len(), 3);
        // Diagonal must be zero
        assert!((mat[0][0] - 0.0).abs() < 1e-10);
        assert!((mat[1][1] - 0.0).abs() < 1e-10);
        assert!((mat[2][2] - 0.0).abs() < 1e-10);
        // Symmetry
        assert!((mat[0][1] - mat[1][0]).abs() < 1e-10);
        assert!((mat[0][2] - mat[2][0]).abs() < 1e-10);
        // Identical sequences → distance 0
        assert!((mat[0][1] - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_distance_matrix_empty_error() {
        let seqs: &[&str] = &[];
        assert!(distance_matrix(seqs).is_err());
    }

    #[test]
    fn test_distance_matrix_different_lengths_error() {
        let seqs = &["ATGC", "ATG"];
        assert!(distance_matrix(seqs).is_err());
    }

    // ── nucleotide_frequencies ────────────────────────────────────────────────

    #[test]
    fn test_nucleotide_frequencies_basic() {
        let freqs = nucleotide_frequencies("AACGT");
        // A=2, C=1, G=1, T=1 out of 5
        assert!(
            (freqs[&'A'] - 0.4).abs() < 1e-10,
            "A frequency wrong: {}",
            freqs[&'A']
        );
        assert!(
            (freqs[&'C'] - 0.2).abs() < 1e-10,
            "C frequency wrong: {}",
            freqs[&'C']
        );
        assert!(
            (freqs[&'G'] - 0.2).abs() < 1e-10,
            "G frequency wrong: {}",
            freqs[&'G']
        );
        assert!(
            (freqs[&'T'] - 0.2).abs() < 1e-10,
            "T frequency wrong: {}",
            freqs[&'T']
        );
    }

    #[test]
    fn test_nucleotide_frequencies_sum_to_one() {
        let freqs = nucleotide_frequencies("ATGCATGCATGC");
        let sum: f64 = freqs.values().sum();
        assert!((sum - 1.0).abs() < 1e-10, "frequencies must sum to 1.0");
    }

    #[test]
    fn test_nucleotide_frequencies_empty() {
        let freqs = nucleotide_frequencies("");
        assert!((freqs[&'A'] - 0.0).abs() < 1e-10);
        assert!((freqs[&'C'] - 0.0).abs() < 1e-10);
        assert!((freqs[&'G'] - 0.0).abs() < 1e-10);
        assert!((freqs[&'T'] - 0.0).abs() < 1e-10);
    }

    // ── gc_content ────────────────────────────────────────────────────────────

    #[test]
    fn test_gc_content_fifty_percent() {
        assert!((gc_content("AATGCG") - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_gc_content_zero() {
        assert!((gc_content("AAAA") - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_gc_content_one_hundred_percent() {
        assert!((gc_content("GCGC") - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_gc_content_empty() {
        assert!((gc_content("") - 0.0).abs() < 1e-10);
    }

    // ── codon_usage ───────────────────────────────────────────────────────────

    #[test]
    fn test_codon_usage_single_codon_repeated() {
        let usage = codon_usage("ATGATGATG");
        assert_eq!(*usage.get("ATG").unwrap_or(&0), 3);
    }

    #[test]
    fn test_codon_usage_multiple_codons() {
        // "ATGCTT" → ATG, CTT
        let usage = codon_usage("ATGCTT");
        assert_eq!(*usage.get("ATG").unwrap_or(&0), 1);
        assert_eq!(*usage.get("CTT").unwrap_or(&0), 1);
    }

    #[test]
    fn test_codon_usage_skips_invalid_codons() {
        // "ATGNNN" → ATG is valid, NNN contains ambiguous bases
        let usage = codon_usage("ATGNNN");
        assert_eq!(*usage.get("ATG").unwrap_or(&0), 1);
        assert!(!usage.contains_key("NNN"));
    }

    #[test]
    fn test_codon_usage_empty() {
        let usage = codon_usage("");
        assert!(usage.is_empty());
    }

    // ── complement / reverse_complement ───────────────────────────────────────

    #[test]
    fn test_complement_basic() {
        assert_eq!(complement("ATGC"), "TACG");
    }

    #[test]
    fn test_reverse_complement_basic() {
        assert_eq!(reverse_complement("ATGCTT"), "AAGCAT");
    }
}