src/unoisenim/nbc_algo

Naive Bayesian Classifier (NBC) for amplicon taxonomy.

Implements the RDP-style NBC algorithm using 8-mer word frequencies and bootstrap resampling to assign taxonomic ranks with confidence estimates. Both forward and reverse-complement strands are evaluated.

Reference: Wang Q, Garrity GM, Tiedje JM, Cole JR. Naive Bayesian Classifier for Rapid Assignment of rRNA Sequences into the New Bacterial Taxonomy. Appl Environ Microbiol. 2007; doi: 10.1128/AEM.00062-07

Types

NbcHit = object
  rankNames*: seq[string]    ## Taxonomy rank strings for the predicted path
  rankProbs*: seq[float]     ## Bootstrap confidence per rank (0.0–1.0)
  strand*: char              ## Matched strand: ``'+'`` or ``'-'``
Result of an NBC taxonomic classification.
NbcIndex = object

Taxonomy tree index built from reference sequences for NBC classification.

Internal tree nodes store per-rank k-mer word counts used during Bayesian scoring. Construct with buildNbcIndex.

Consts

NbcKmerSize = 8
Length of k-mer words used for NBC classification (8-mers = 65 536 possible words).

Procs

proc buildNbcIndex(seqs: seq[string]; taxStrings: seq[string]): NbcIndex {.
    ...raises: [], tags: [], forbids: [].}

Builds a taxonomy tree index from reference sequences and taxonomy strings.

Each sequence in seqs is paired with the corresponding entry in taxStrings. Unique 8-mers from each sequence are accumulated into the tree nodes along the taxonomy path, enabling Bayesian scoring during classification.

Parameters

  • seqs — reference DNA sequences
  • taxStrings — taxonomy strings matching each sequence (comma-delimited ranks)

Returns an NbcIndex ready for use with nbc.

proc nbc(query: string; idx: NbcIndex; bootIters: int = 100; minWords: int = 5): NbcHit {.
    ...raises: [], tags: [], forbids: [].}

Classifies query against the NBC index using bootstrap resampling.

Both the forward sequence and its reverse complement are classified; the strand that produces a longer (deeper) taxonomy path wins, with log-score as a tiebreaker.

Parameters

  • query — DNA query sequence
  • idx — prebuilt NBC index from buildNbcIndex
  • bootIters — number of bootstrap iterations for confidence estimation (default: 100)
  • minWords — minimum subsample size per bootstrap iteration (default: 5)

Returns an NbcHit with rank names, per-rank bootstrap confidences, and the matched strand ('+' or '-').

proc parseTaxRanks(tax: string): seq[string] {....raises: [], tags: [], forbids: [].}

Splits a comma-delimited taxonomy string into a list of rank strings.

Leading/trailing whitespace is stripped from each token; empty tokens are omitted. For example "d:Bacteria, p:Firmicutes" returns @["d:Bacteria", "p:Firmicutes"].

proc reverseComplement(s: string): string {....raises: [], tags: [], forbids: [].}

Returns the reverse complement of DNA/RNA sequence s.

Both upper- and lower-case bases are handled; non-ACGTU characters are passed through unchanged.