Lesson 10 of 16 · 10 min
Sequence composition: GC content
In earlier lessons you saw that a piece of DNA can be written as a string of letters. Each letter stands for one building block called a base (also called a nucleotide), and DNA uses just four of them: A, C, G, and T.
What GC content means
GC content is simply the share of the bases in a sequence that are either G or C, written as a percentage. A sequence where half of the bases are G or C has a GC content of 50 percent.
GC% = (G + C) / (A + C + G + T) * 100
(A, C, G, and T stand for how many times each base appears.)GC content matters in real work: different species and genomes have their own typical levels, so it acts like a rough fingerprint. It also guides primer design, where a primer is a short piece of DNA used to kick off copying a region. Because G and C bind with three hydrogen bonds while A and T use only two, GC-rich DNA holds together more tightly and needs a higher melting temperature to separate the strands, which matters when tuning PCR, the lab reaction that copies DNA.
Counting bases in Python
Python makes counting letters easy. The .count() method tallies how many times a letter appears, and len() gives the total length of the sequence.
seq = "ATGCGGCATTAGCCGTTAAGC"
g = seq.count("G")
c = seq.count("C")
total = len(seq)
gc_percent = (g + c) / total * 100
print(g, c, total)
print(round(gc_percent, 2))6 5 21
52.38
You can do the same at the shell, assuming a file seq.fasta holds that sequence. This pipeline strips out any FASTA header line (grep -v '^>' drops lines beginning with >), joins the sequence onto one line (tr -d '\n' deletes newline characters), keeps only G and C letters (tr -cd 'GCgc' deletes every character that is not G, C, g, or c), and counts what remains (wc -c). That gives the G plus C count, which is 11 here; divide it by the total length to get the percentage, exactly like the formula.
grep -v '^>' seq.fasta | tr -d '\n' | tr -cd 'GCgc' | wc -cTry it yourself: Take the short sequence GGGCACGT. Count the G bases and the C bases by hand, add them together, divide by the total length (8 bases), and multiply by 100 to get the GC percentage. Then check yourself by setting seq = "GGGCACGT" in the Python code above; you should land on a clean, round number.