Skillversitas

Population Genetics

Population genetics in Biopython revolves around the Bio.PopGen module: reading allele-frequency data, computing diversity statistics (nucleotide diversity, Tajima's D), and testing for departures from neutrality. The inputs are usually aligned sequences from multiple individuals across populations.

Allele Frequency Data

from Bio.PopGen import AlleleFreqs

# build an allele-frequency table from aligned sequences
# (each sequence = one individual; populations labeled in ids)
from Bio import AlignIO

aln = AlignIO.read("pop_align.clustal", "clustal")
# individuals grouped by population prefix in their ids
print(len(aln), "sequences")
print([s.id[:20] for s in aln.sequences[:4]])

Diversity Statistics

from Bio.PopGen.Statistics import nucleotide_diversity, tajima_d

# per-population nucleotide diversity (pi) and Tajima's D
# (simplified illustration of the API surface)
print("use Bio.PopGen.Statistics for pi, thetaW, Tajima's D")

The module exposes functions for the standard estimators; you feed it aligned sequences (or site-frequency data) and it returns the statistic per population.

Fixation Index (Fst)

Fst measures genetic differentiation between populations - 0 means no differentiation, 1 means complete. With allele frequencies per population:

# conceptual: Fst = 1 - (Hs / Ht)
# Hs = average heterozygosity within populations
# Ht = heterozygosity of the total sample
# compute with Bio.PopGen allele-frequency objects, or from a table:
def heterozygosity(freqs):
    p = sum(freqs) / 2 if len(freqs) == 2 else freqs[0]
    return 2 * p * (1 - p)

# Hs across populations, then Ht on pooled frequencies, then Fst = 1 - Hs/Ht

Watterson's Theta

ThetaW estimates diversity from the count of segregating sites (S) normalized by a harmonic-factor constant - a common neutral expectation to compare against pi:

def theta_w(n_samples, n_segregating_sites):
    harmonic = sum(1.0 / i for i in range(1, n_samples))
    return n_segregating_sites / harmonic

print(theta_w(20, 45))  # example: 20 sequences, 45 segregating sites

Reading the Statistics

  • High pi relative to thetaW suggests balancing selection or recent population growth.
  • Very low diversity in one population vs others suggests a bottleneck.
  • Tajima's D negative = excess of low-frequency variants (expansion); positive = balancing selection.
  • Always report sample sizes; small n inflates variance of every estimator.

Summary

Bio.PopGen provides the population-genetics toolkit: build allele-frequency objects from aligned sequences, compute nucleotide diversity (pi), Watterson's theta, Tajima's D, and Fst between populations. Interpret the statistics together - their relative values, with sample sizes reported, tell the demographic and selective story.