Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

DNA sequences as strings in Python

After this lesson: I can count the bases in a DNA sequence and work out its GC content and reverse complement.

55 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

A DNA sequence has four letters

A DNA sequence is a long string made of four letters: A, C, G and T. Each letter names one base, a building block of DNA.

Bioinformatics uses code to count, compare and search these strings, because a computer reads millions of letters in seconds. Every sequence in this module is invented, so you can practise freely.

By the end of this lesson

Note

I can count the bases in a DNA sequence and work out its GC content and reverse complement.

Key words

WordWhat it meansExample
Baseone letter of a DNA sequenceA
Sequencethe bases in order, written as a string
GC contentthe percentage of bases that are G or C
Complementeach base swapped for its partner
Reverse complementthe complement, read backwards

The cells students fill in are left blank in this preview.

A sequence is a string you can count

A DNA sequence is written as a string such as ATGGCGCATTAGCC. Each letter is a base.

Because it is a string, your string tools work on it. len gives the length and a for loop visits each base. count says how many times one base appears.

Worked example: the bases of one short sequence

BaseTimes in ATGGCGCATTAGCCShare of 14
A321.4 percent
C428.6 percent
G428.6 percent
T321.4 percent
Total14100 percent

The cells students fill in are left blank in this preview.

Count each base and the GC content

Code: python
# An invented sequence of 14 bases.
seq = "ATGGCGCATTAGCC"
for base in "ACGT":
    print(base, seq.count(base))
gc = seq.count("G") + seq.count("C")
print("GC content:", round(100 * gc / len(seq), 1), "percent")
# Change seq to "GGCCGGCC" and run again. Work out the answer first.

GC content summarises a sequence in one number

GC content is the number of G and C bases divided by the length, times 100. For our sequence it is 8 out of 14, which is 57.1 percent.

Different stretches of DNA have different GC content, so this single number is a quick way to compare them. Always divide by the full length, never by the number of A and T.

Count the G and C bases with Python

Command
docker exec jupyter python3 -c "s = 'ATGGCGCATTAGCC'; print(s.count('G') + s.count('C'))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The command prints the G and C count

The command counts the G bases, counts the C bases and adds them. It prints 8, which is the top of the fraction 8 out of 14.

The docker exec jupyter part runs Python inside the lab, where the terminal has no Python of its own.

The reverse complement reads the opposite strand

DNA has two strands. Each base pairs with a partner: A with T, and C with G. The complement swaps every base for its partner.

The other strand runs the opposite way, so you also read it backwards. The result is the reverse complement. For ATGC, the complement is TACG and the reverse complement is GCAT.

Build the reverse complement with a loop

Code: python
# The reverse complement: swap each base, then read backwards.
pairs = {"A": "T", "T": "A", "C": "G", "G": "C"}
seq = "ATGC"
complement = ""
for base in seq:
    complement = complement + pairs[base]
reverse = ""
for base in complement:
    reverse = base + reverse
print(complement)
print(reverse)
# Change seq to "GATTACA". Predict both lines before you run.

A common mistake

Warning

Many people think the complement and the reverse complement are the same. The complement keeps the order and only swaps the bases. The opposite strand is read the other way, so you must reverse as well. Do both steps, in either order.

Find the reverse complement of GATTACA

Command
docker exec jupyter python3 -c "print(''.join({'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}[b] for b in 'GATTACA'[::-1]))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The command reads backwards and swaps each base

The slice [::-1] reads GATTACA backwards. The dictionary swaps each base for its partner. The command prints TGTAATC.

This is the same job as the loop above, written on one line.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.