Skip to main content

Shannon Entropy in Diagnostic Design

In information theory, Shannon entropy quantifies the amount of uncertainty or surprise in a set of possible outcomes. In bioinformatics, we use it to measure sequence variability at individual positions within a multiple sequence alignment (MSA). This page provides the mathematical formulation of Shannon entropy, a step-by-step calculation example, and explains how the pipeline translates entropy into a normalized physical conservation score (CC).

Mathematical Formulation

For a given position (column) ii in a multiple sequence alignment, the Shannon entropy HiH_i is calculated as: Hi=−∑b∈Apiblog⁡2(pib)H_i = -\sum_{b \in \mathcal{A}} p_{ib} \log_2(p_{ib}) Where:
  • A\mathcal{A} is the alphabet of possible states at that position. In our pipeline, we use a 5-letter alphabet: A={A,T,G,C,−}\mathcal{A} = \{A, T, G, C, -\} where $-$ represents an alignment gap.
  • pibp_{ib} is the observed frequency (proportion) of state bb in column ii, such that ∑b∈Apib=1.0\sum_{b \in \mathcal{A}} p_{ib} = 1.0.
  • By definition, if pib=0p_{ib} = 0, we define piblog⁡2(pib)=0p_{ib} \log_2(p_{ib}) = 0.

Entropy Bounds

  • Minimum Entropy (H=0H = 0): Occurs when a position is 100% conserved (only one base is present across all sequences). There is zero uncertainty or variation.
  • Maximum Entropy (H=log⁡2(5)≈2.322H = \log_2(5) \approx 2.322): Occurs when all 5 states in the alphabet are present in equal proportions (pib=0.2p_{ib} = 0.2 for each state). There is maximum uncertainty.

Normalised Conservation Score (CC)

To integrate entropy with other signals in the Bridge Builder (M07), we normalise the entropy value to a scale of [0,1][0, 1], where 1.01.0 represents maximum conservation and 0.00.0 represents maximum randomness. Ci=1−Hilog⁡2(5)≈1−Hi2.322C_i = 1 - \frac{H_i}{\log_2(5)} \approx 1 - \frac{H_i}{2.322}

Step-by-Step Calculation Example

Suppose we have an alignment of 10 viral sequences at a specific position ii, and we observe the following base counts:
  • Adenine (A): 8 sequences
  • Guanine (G): 1 sequence
  • Gap (-): 1 sequence
  • Thymine (T) / Cytosine (C): 0 sequences

Step 1: Calculate Proportions (pibp_{ib})

piA=810=0.8p_{i\text{A}} = \frac{8}{10} = 0.8 piG=110=0.1p_{i\text{G}} = \frac{1}{10} = 0.1 pi-=110=0.1p_{i\text{-}} = \frac{1}{10} = 0.1 piT=piC=0.0p_{i\text{T}} = p_{i\text{C}} = 0.0

Step 2: Compute plog⁡2(p)p \log_2(p) for each active state

  • For A: 0.8⋅log⁡2(0.8)≈0.8⋅(−0.3219)≈−0.25750.8 \cdot \log_2(0.8) \approx 0.8 \cdot (-0.3219) \approx -0.2575
  • For G: 0.1⋅log⁡2(0.1)≈0.1⋅(−3.3219)≈−0.33220.1 \cdot \log_2(0.1) \approx 0.1 \cdot (-3.3219) \approx -0.3322
  • For -: 0.1⋅log⁡2(0.1)≈0.1⋅(−3.3219)≈−0.33220.1 \cdot \log_2(0.1) \approx 0.1 \cdot (-3.3219) \approx -0.3322

Step 3: Sum the terms to find HiH_i

Hi=−(−0.2575−0.3322−0.3322)=0.9219 bitsH_i = -(-0.2575 - 0.3322 - 0.3322) = 0.9219 \text{ bits}

Step 4: Calculate Normalized Conservation (CiC_i)

Ci=1−0.92192.3219≈1−0.3970≈0.6030C_i = 1 - \frac{0.9219}{2.3219} \approx 1 - 0.3970 \approx 0.6030 At this position, the conservation score is 0.603. This is well below the pipeline’s strict target threshold (usually C≥0.85C \geq 0.85), meaning this position would be avoided when designing primer binding sites.

Integrating Gaps in the Alphabet

Many bioinformatics tools exclude gaps from entropy calculations or treat them as missing data. However, treating gaps as a 5th character state is critical for diagnostic design:
  • A high gap frequency at a position indicates that a subset of circulating strains contains a deletion relative to the reference.
  • Designing a primer over a deletion region will cause immediate hybridization failure in those strains.
  • By incorporating gaps into the alphabet, any deletion results in a significant increase in entropy, driving the conservation score CC down and disqualifying the region from primer design.

The Configured Thresholds, in Interpretable Terms

Two different scales are in play, and confusing them causes silent errors:
  • The conservation score C=1−H/log⁡2(5)C = 1 - H / \log_2(5) is normalised to [0,1][0, 1] and is what the tier grades and reports display.
  • The configured thresholds are compared against raw entropy in bits, not against CC.
Converted to the maximum minor-allele frequency a biallelic column can carry and still pass: !!! warning “Corrected: these thresholds must not be rescaled with the alphabet” An earlier revision multiplied all three by log⁡2(5)/log⁡2(4)=1.161\log_2(5)/\log_2(4) = 1.161 when the entropy alphabet was widened from 4 symbols to 5, giving 0.023 / 0.116 / 0.58. That was wrong and has been reverted. The normaliser log⁡2(5)\log_2(5) affects only the display score CC. The thresholds are compared against raw HH, and a column containing no gaps has identical raw entropy under both alphabets — the gap frequency is zero, so it contributes nothing to the sum. Multiplying the thresholds therefore rescaled nothing; it simply loosened every criterion by 16% (the exclusion threshold went from MAF 11.0% to 13.8%). Widening the alphabet was still correct — it is what makes a deletion raise entropy, as described above. Only the thresholds should have stayed put. A second annotation error was corrected at the same time: the exclusion threshold was documented as “0.50 ~ 30% MAF”. It is ~11%; 30% MAF corresponds to 0.88 bits.

Shannon Entropy vs. Minor Allele Frequency (MAF)

While Minor Allele Frequency (MAF) measures the frequency of the second-most common allele, Shannon entropy provides a more robust signal for multiple-base variation: In the pipeline:
  • Shannon Entropy is used as the continuous signal to calculate the base conservation score.
  • MAF is used as a discrete threshold (via global_params.iupac_threshold) to decide when to introduce degenerate IUPAC bases into the consensus sequence for PrimerLab.