Why Proteins are just High-Dimensional Vectors (And how to query them in SQL)

The pg_bio Series This post is part of a 4-part series on building a bioinformatics engine natively in PostgreSQL.


If you are a software engineer, you already know the frustration of inheriting a massive legacy codebase where 30% of the functions are named utils.do_stuff() and contain absolutely zero documentation.

In biology, we call this the Dark Proteome.

Right now, there are hundreds of millions of proteins in global databases labeled simply as “Uncharacterized Protein”. We know their exact sequence of amino acids (the source code), but we have absolutely no idea what they do, what they bind to, or what biological processes they execute.

For decades, determining the function of a single protein required years of painstaking wet-lab experiments. Today, thanks to AI and PostgreSQL, we can de-orphanize the Dark Proteome in milliseconds using standard SQL. Here is how we do it, and how you can too.


The Paradigm Shift: From Strings to Floats

Historically, bioinformatics treated proteins as strings of text (e.g., "MVLSPADKTNVK..."). To find a match, scientists used algorithms like Smith-Waterman to calculate the “Levenshtein distance” (the edit distance) between two strings.

But evolution is tricky. Two proteins can have completely different source code (sequences) but compile into the exact same 3D structure (shape) to perform the same function. String matching misses these hidden connections entirely.

Enter Protein Language Models (like ESM). Just as ChatGPT translates English sentences into high-dimensional vectors (embeddings) to capture semantic meaning, we can use AI to translate protein sequences into 1024-dimensional vectors to capture structural meaning.

Suddenly, finding a protein’s function isn’t a string-matching problem anymore. It’s a Nearest Neighbor Search.


The Power of SQL: Combining AI with Classical Algorithms

To search this massive vector space, we built an extension called pg_bio that sits on top of PostgreSQL and pgvector. But we didn’t just stop at AI. AI can hallucinate, and pure vector distance sometimes groups proteins that look similar but act differently.

We needed a way to combine the raw blazing speed of AI Structural Search with the rigorous accuracy of Classical Sequence Alignment (Smith-Waterman), mixed with standard relational metadata.

Here is the exact SQL we use to mine the Dark Proteome. Look at how beautifully PostgreSQL allows us to combine these entirely different paradigms into a single query:

WITH closest_structures AS (
    -- STEP 1: AI Structural Search (Sub-millisecond)
    -- We use pgvector's HNSW index to instantly traverse a graph 
    -- of 2.3 million proteins and find the 50 closest 3D shapes.
    SELECT 
        p.uniprot_id, 
        p.name, 
        p.embedding, 
        p.sequence, 
        (p.embedding <=> v_bait_embedding) as structural_distance
    FROM proteins p
    ORDER BY p.embedding <=> v_bait_embedding ASC
    LIMIT 50
)
-- STEP 2: Relational Filtering & Classical Re-Ranking
SELECT 
    c.uniprot_id, 
    c.name,
    c.structural_distance,
    -- STEP 3: The Hybrid Operator (<~>)
    -- We wrote a custom Rust operator that fires up a dynamic programming
    -- matrix (Smith-Waterman) to calculate the exact sequence drift between
    -- the AI-suggested match and our original bait.
    (ROW(c.embedding, c.sequence)::bio_feature <~> ROW(v_bait_embedding, v_bait_seq)::bio_feature) as hybrid_score
FROM closest_structures c
WHERE c.name ILIKE '%uncharacterized%'     -- Only look at undocumented proteins
  AND c.structural_distance <= 0.35        -- Ensure high structural confidence
ORDER BY hybrid_score ASC;                 -- Bubble the absolute best matches to the top

This is the Oversample-and-Refine pattern. By isolating the vector search <=> in a CTE, Postgres uses a lightning-fast graph index. Then, it passes those 50 candidates to the outer query where we apply standard ILIKE text filters and our computationally heavy <~> Rust operator.


The Data Correlation: Discovering a New Argonaute

To prove this works, we unleashed this exact query on the Argonaute protein family. Argonautes are the engines behind RNA interference and gene silencing (think CRISPR’s cousins).

We used a known Argonaute (A0A0Q2M2Z1) as our bait, and within milliseconds, the query returned this:

Known Target (Bait) Orphan Discovery Vector Distance Hybrid Score
A0A0Q2M2Z1 (Argonaute family) A0A2Z2MSX3 (Uncharacterized) 0.0000 0.0128

Analyzing the Discovery

The Vector Distance of 0.0000 is breathtaking. The AI embedding model is telling us that despite billions of years of evolutionary drift, the 3D topology of this uncharacterized protein is mathematically identical to our known Argonaute.

The Hybrid Score of 0.0128 confirms it. Our custom operator calculated the exact sequence homology and factored it against the structural match. A score this close to zero means we haven’t just found a protein that looks similar—we’ve found a homologous sibling.

We just “wrote the documentation” for A0A2Z2MSX3. It is an Argonaute protein.


Expanding the Search: Mining the Helicase Family

To prove this architecture scales across the Tree of Life, we pointed our query at a completely different target: A0A1Y3GFM2. This is a known Lhr-like Helicase found in Methanonatronarchaeum thermophilum—a fascinating extremophile organism that thrives in boiling, highly alkaline, hyper-saline lakes. Helicases are the biological “motors” that physically unzip DNA strands so they can be copied or repaired, making them massive targets for biotechnology.

By querying our local Swiss-Prot database, Postgres instantly returned a cluster of highly confident, completely uncharacterized structural matches from the Dark Proteome:

Known Target (Bait) Orphan Discovery Vector Distance Hybrid Score Organism
A0A1Y3GFM2 (Helicase) A0A0W1R6X7 (Uncharacterized) 0.0895 0.3046 Haloprofundus marisrubri
A0A1Y3GFM2 (Helicase) L9VAL7 (Uncharacterized) 0.0841 0.3068 Halalkalicoccus jeotgali

The Biology Behind the SQL

Look at the organisms in the results! Our discovery, A0A0W1R6X7, was found in Haloprofundus marisrubri—another extremophile microbe discovered in the deep, hypersaline anoxic basins of the Red Sea.

Because both the Bait organism and the Discovery organism live in extreme salt environments, it makes perfect evolutionary sense that they share homologous DNA repair motors. What used to take months of wet-lab assay testing and genome mapping was just accomplished in a single SQL query. The vector distances (under 0.1) strongly suggest these proteins fold into identical helicase motor structures. We just found novel molecular motors hiding in the deep-sea wilderness!


See it to Believe it (Interactive 3D)

Don’t just trust the math—trust your eyes. Using 3Dmol.js, we can pull the predicted atomic structures directly from the AlphaFold database and render them right here in the browser.

Interact with the visualizer below. Spin them, zoom in, and look at the structural domains.

Target Bait: A0A0Q2M2Z1 (Cyan) vs Orphan Discovery: A0A2Z2MSX3 (Magenta)

Known Argonaute (A0A0Q2M2Z1)

Newly Discovered (A0A2Z2MSX3)

(If you look closely, you will notice that both proteins possess the distinct, bi-lobed "PAC" and "PIWI" domains characteristic of the Argonaute machinery!)


Visualizing the Helicase Motors

We can apply the exact same visualization technique to our new Helicase discovery. Below is the known Lhr-like Helicase (Bait) compared to our newly discovered, uncharacterized protein from the Red Sea:

Known Helicase (A0A1Y3GFM2)

Novel Motor (A0A0W1R6X7)

(Notice the large central "hole" in the structure—this is the physical channel where the DNA strand is threaded and unzipped by the motor!)


Welcome to Bioinformatics

Biology is no longer just for people in white coats holding pipettes. It is a data engineering problem, a vector mathematics problem, and a database indexing problem.

With tools like pg_bio and PostgreSQL, software engineers have the power to discover novel enzymes, accelerate drug discovery, and de-orphanize the dark proteome—all using the SQL skills you use every day.

Want to dive deeper? Check out our next post on how we used Z-Order curves to index 3D space and predict off-target drug interactions!


Next up in the series: Part 2: Writing a Hybrid Database Operator for Biology (<~>)

Jônatas Davi Paganini

Jônatas Davi Paganini

Senior developer and technical consultant with 20+ years of experience specializing in PostgreSQL, TimescaleDB, and distributed systems. Expert in database optimization, microservices architecture, and team enablement. Passionate about sharing knowledge through writing, speaking, and mentoring.

1/1