Genomic Similarity Search via Normalized Relative Compression
about
This project extends the finite-context modeling work from an earlier course project into a practical genomic similarity search engine, applying Algorithmic Information Theory to real bioinformatics data (viral and bacterial genomes, including Human Coronavirus NL63). Given a reference genome, the system trains a finite-context model on it and uses the resulting compression model to rank sequences in a database by how well the model compresses them, directly measuring information-theoretic similarity via Normalized Relative Compression (NRC). A lower NRC means a sequence is better predicted by the reference model and therefore more similar to it.
At the core is MetaClass, a shared compression engine reused across five command-line tools. Unlike the hash-map-based context representation used previously, MetaClass stores context counts in a flat integer array indexed as context_index * 4 + symbol_index, where the context index is a base-4 encoding of the k preceding DNA bases (A, C, G, T mapped to 0-3). This gives exact O(1) lookups with no hash collisions and far better cache locality, at the cost of memory that scales as 4^k (about 16MB for k=11, which is manageable for DNA). The sliding context window is updated incrementally with a single modulo and multiply per step rather than being recomputed from scratch.
The models_generator tool trains a reference model from a meta sequence and serializes it to a compact binary format, separating training from inference so the same model can be reused across many database queries without retraining — important for the hyperparameter search, which would otherwise need to retrain for every (k, alpha) combination. The main ranking tool loads a trained model, parses a FASTA-like sequence database, computes the NRC of every sequence against the model, and returns the top-k most similar matches.
Two similarity baselines complement the NRC ranking. similarities_levenshtein computes a space-optimized (O(min(m,n))) Levenshtein edit distance between two named sequences as a ground-truth comparison; for full-length genomes (30,000+ base pairs), this O(n^2) approach is computationally infeasible at database scale, which motivates the asymptotically superior O(n) NRC approach. similarities_models computes a bidirectional NRC similarity: it trains a model on each of two sequences, evaluates each model on the other sequence, averages the two NRC values (since NRC is not symmetric), and maps the result to a (0,1] similarity score via exponential decay.
A complexity_profile tool generates per-position information-content profiles, revealing which regions of a target sequence are conserved (low bits-per-symbol, similar to the reference) versus divergent (high bits-per-symbol). A Python/Jupyter analysis layer visualizes these complexity curves, renders pairwise similarity matrices across the sequence database, and automates a (k, alpha) hyperparameter grid search with heatmap visualization across five independent reference/database pairs spanning different viral families and genome sizes.
highlights
- ·Designed a flat-array (4^k x 4) compression engine for O(1) collision-free context lookups, replacing hash maps for better cache locality
- ·Built a genomic database ranking tool using Normalized Relative Compression (NRC) to identify sequences most similar to a reference genome in O(n)
- ·Implemented bidirectional NRC similarity with exponential-decay mapping, validated against a space-optimized Levenshtein baseline
- ·Automated (k, alpha) hyperparameter optimization with heatmap visualization across five reference/database genome pairs
- ·Achieved a top grade of 19/20
stack
Core
Analysis
Theory