def gc_content(dna):
    """Calculate the %GC content of a nucleotide sequence."""
    seqlength = len(dna)

    # Count A and T nucleotides, including the W ambiguity base representing
    # either A or T
    atcount = dna.count('A') + dna.count('a') + \
        dna.count('T') + dna.count('t') + \
        dna.count('W') + dna.count('w')

    # Count C and G nucleotides, including the S ambiguity base representing
    # either C or G
    gccount = dna.count('C') + dna.count('c') + \
        dna.count('G') + dna.count('g') + \
        dna.count('S') + dna.count('s')

    # Count all other ambiguous nucleotides; most will be Ns, but occasionally
    # there will be other IUPAC ambiguity symbols
    ncount = seqlength - atcount - gccount

    if atcount + gccount == 0:
        assert ncount == seqlength
        gccontent = 0.0
    else:
        gccontent = float(gccount) / float(gccount + atcount)
    return gccontent