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

Free lesson

Pairwise sequence alignment and scoring

After this lesson: I can score an alignment of two sequences with a match, mismatch and gap score.

70 min lesson1 commandJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Pairwise alignment lines up two sequences

Pairwise alignment lines up two sequences so that matching letters sit in the same column. It is how we measure how alike two sequences are, even when some letters changed or went missing.

You will score an alignment by hand, then let a program find the best one.

By the end of this lesson

Note

I can score an alignment of two sequences with a match, mismatch and gap score.

Key words

WordWhat it meansExample
Alignmenttwo sequences written in columns
Matcha column where both letters are the same
Mismatcha column where the letters differ
Gapa dash standing for a missing letter-
Scorethe total of the column scores

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

An alignment puts two sequences in columns

Write one sequence above the other. Every column is one of three things. A match has the same letter in both rows. A mismatch has different letters. A gap is a dash, where one sequence has a letter and the other has none.

A gap stands for a letter that was added or lost. Gaps let sequences of different length line up.

Worked example: score one alignment column by column

ColumnTopBottomKindScore
1AAMatch+1
2-CGap-1
3GGMatch+1
4TTMatch+1
Total+2

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

The score adds up the columns

A scoring rule gives each kind of column a number. A common choice is +1 for a match, -1 for a mismatch and -1 for a gap. A different rule gives a different score for the same alignment.

The score of the whole alignment is the total of its columns. A higher score means a better alignment.

Score an alignment with a loop

Code: python
# Score an alignment column by column.
top = "A-GT"
bottom = "ACGT"
match = 1
mismatch = -1
gap = -1
score = 0
for i in range(len(top)):
    if top[i] == "-" or bottom[i] == "-":
        score = score + gap
    elif top[i] == bottom[i]:
        score = score + match
    else:
        score = score + mismatch
print("score:", score)
# Change the top row to "AAGT" and predict the score first.

Score two rows with one line of Python

Command
docker exec jupyter python3 -c "print(sum(1 if a == b else -1 for a, b in zip('AC-GT', 'A-AGT')))"

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

The command prints 1

The command walks down the five columns of AC-GT over A-AGT. The columns score +1, -1, -1, +1 and +1.

It prints 1, the total. Here a mismatch and a gap cost the same, so one test is enough.

The best alignment has the highest score

Many alignments are possible for two sequences, and trying every one is far too slow. Dynamic programming is the way out. It builds a table where each cell holds the best score for a pair of prefixes.

Each cell comes from one of three neighbours. A diagonal step is a match or mismatch. A step down or a step right is a gap. Take the highest.

Fill the table in code

Code: python
# Fill the table: the best score for each pair of prefixes.
a = "ACGT"
b = "AGT"
match = 1
mismatch = -1
gap = -1
table = []
for i in range(len(a) + 1):
    table.append([0] * (len(b) + 1))
for i in range(1, len(a) + 1):
    table[i][0] = i * gap
for j in range(1, len(b) + 1):
    table[0][j] = j * gap
for i in range(1, len(a) + 1):
    for j in range(1, len(b) + 1):
        if a[i - 1] == b[j - 1]:
            diagonal = table[i - 1][j - 1] + match
        else:
            diagonal = table[i - 1][j - 1] + mismatch
        up = table[i - 1][j] + gap
        left = table[i][j - 1] + gap
        table[i][j] = max(diagonal, up, left)
for row in table:
    print(row)
# The bottom right cell is the best score. Change b to "ACT".

Worked example: the table for ACGT and AGT

startAGT
start0-1-2-3
A-110-1
C-200-1
G-3-110
T-4-202

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

A common mistake

Warning

Many people think the best alignment is the one with the most matches. A match adds to the score, but each gap and mismatch takes some away. An alignment with many matches still scores badly if it needs a lot of gaps.

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.