Guided project
Module 4 · Comparing sequences · Self-checked
A 60-residue protein fragment arrives without a label. Search a reference set, check the scores against chance, align, build a tree and describe the one difference from its closest match.
Research question
A colleague sends you a 60-residue protein fragment with no label. What protein is it, which species could it come from, and how does it differ from its closest known relative?
How to approach this
This is the everyday job that the lessons of this module prepare you for. In real work you would paste the fragment into web BLAST (Searching databases fast: similarity search and E-values). Here you search a small reference set yourself, so that every step is visible and everyone gets the same numbers. The set holds eight real proteins from UniProt: haemoglobin beta from five species (human, mouse, cow, horse and chicken), human haemoglobin alpha, human myoglobin, and human lysozyme. Some are close relatives of one another, some distant, and one is unrelated to the rest. Part of the task is telling which is which.
Keep one limit in mind from the start. A search can only return what is in the set you search. The best hit tells you which reference your unknown is closest to, not what the unknown is: if its true source is missing from the set, the top hit is merely its nearest available relative. Real databases are vast, so this matters less on web BLAST, but it never disappears. The same caution applies to every result in the project, and task 6 asks you to say how far the evidence goes.
The tasks follow the order an experienced analyst would use. First find candidates and rank them; then check that the best scores are better than chance; then look at many relatives together to see which positions are fixed; then place the unknown in a tree; and only then describe the difference in detail. Each step guards against a mistake the one before could make: a high score that is really chance, or a close match that is really a paralogue.
- Work in one Colab notebook, top to bottom. Start with
!pip install biopython, then paste the data block below into the next cell. Every later cell uses the names it creates:unknownandrefs. - Use the same settings throughout. One aligner, set up once in task 1, is reused in every task: local mode, BLOSUM62, gap opening −12 and gap extension −1, the BLAST defaults used in Scoring matrices: PAM and BLOSUM. If you change a setting, every score changes, and the checkpoints will no longer match.
- Fix the random seed in task 2 with
random.seed(1), so your shuffled sequences match. - Write numbers down as you go, in a text cell under each result. Task 6 asks for them.
The figure shows how the six tasks connect. Each one narrows the answer or checks the one before.
Go deeper: what a z-score from shuffles can and cannot say
The z-score in task 2 treats the shuffled scores as if they followed the familiar bell-shaped curve. Scores of best local alignments do not. Their spread has a long tail towards high scores, an extreme-value distribution, which is also the basis of BLAST's E-values (Karlin and Altschul, 1990). In a long tail, high chance scores are commoner than the bell curve predicts, so a modest z-score is weaker evidence than it looks. A z of 61, as for human beta globin, is beyond doubt either way; a z of about 2, as for myoglobin, is not convincing.
Shuffling has a second limit. It keeps the amino acid content but destroys local patterns, such as stretches rich in one residue, so real unrelated proteins can score a little higher than shuffles do.
The data
Paste this block into your notebook and run it. The unknown is a real protein fragment; its identity is given only in the model answer at the end. The reference sequences are copied from UniProt, with each accession after the name.
The names are UniProt entry names, which join a short code for the protein to a short code for the species: HBB_HUMAN is haemoglobin beta from human, HBB_BOVIN from cow, HBB_CHICK from chicken, and LYSC_HUMAN is human lysozyme C. The accession after the space, such as P68871, is the stable identifier you met in Identifiers and cross-references: following one gene across databases. SeqIO keeps only the text before the first space as the record's id, so the names become the keys of refs.
# The unknown and the reference set (all real UniProt sequences)
from io import StringIO
from Bio import SeqIO
unknown = "LAHLDNLKGTFATLSELHCDKLHVDPENFKLLGNVLVCVLAHHFGKEFTPPVQAAYQKVV"
reference_fasta = """>HBB_HUMAN P68871
MVHLTPEEKSAVTALWGKVNVDEVGGEALGRLLVVYPWTQRFFESFGDLSTPDAVMGNPK
VKAHGKKVLGAFSDGLAHLDNLKGTFATLSELHCDKLHVDPENFRLLGNVLVCVLAHHFG
KEFTPPVQAAYQKVVAGVANALAHKYH
>HBB1_MOUSE P02088
MVHLTDAEKAAVSCLWGKVNSDEVGGEALGRLLVVYPWTQRYFDSFGDLSSASAIMGNAK
VKAHGKKVITAFNDGLNHLDSLKGTFASLSELHCDKLHVDPENFRLLGNMIVIVLGHHLG
KDFTPAAQAAFQKVVAGVATALAHKYH
>HBB_BOVIN P02070
MLTAEEKAAVTAFWGKVKVDEVGGEALGRLLVVYPWTQRFFESFGDLSTADAVMNNPKVK
AHGKKVLDSFSNGMKHLDDLKGTFAALSELHCDKLHVDPENFKLLGNVLVVVLARNFGKE
FTPVLQADFQKVVAGVANALAHRYH
>HBB_HORSE P02062
VQLSGEEKAAVLALWDKVNEEEVGGEALGRLLVVYPWTQRFFDSFGDLSNPGAVMGNPKV
KAHGKKVLHSFGEGVHHLDNLKGTFAALSELHCDKLHVDPENFRLLGNVLVVVLARHFGK
DFTPELQASYQKVVAGVANALAHKYH
>HBB_CHICK P02112
MVHWTAEEKQLITGLWGKVNVAECGAEALARLLIVYPWTQRFFASFGNLSSPTAILGNPM
VRAHGKKVLTSFGDAVKNLDNIKNTFSQLSELHCDKLHVDPENFRLLGDILIIVLAAHFS
KDFTPECQAAWQKLVRVVAHALARKYH
>HBA_HUMAN P69905
MVLSPADKTNVKAAWGKVGAHAGEYGAEALERMFLSFPTTKTYFPHFDLSHGSAQVKGHG
KKVADALTNAVAHVDDMPNALSALSDLHAHKLRVDPVNFKLLSHCLLVTLAAHLPAEFTP
AVHASLDKFLASVSTVLTSKYR
>MYG_HUMAN P02144
MGLSDGEWQLVLNVWGKVEADIPGHGQEVLIRLFKGHPETLEKFDKFKHLKSEDEMKASE
DLKKHGATVLTALGGILKKKGHHEAEIKPLAQSHATKHKIPVKYLEFISECIIQVLQSKH
PGDFGADAQGAMNKALELFRKDMASNYKELGFQG
>LYSC_HUMAN P61626
MKALIVLGLVLLSVTVQGKVFERCELARTLKRLGMDGYRGISLANWMCLAKWESGYNTRA
TNYNAGDRSTDYGIFQINSRYWCNDGKTPGAVNACHLSCSALLQDNIADAVACAKRVVRD
PQGIRAWVAWRNRCQNRDVRQYVQGCGV
"""
refs = {r.id: str(r.seq) for r in SeqIO.parse(StringIO(reference_fasta), "fasta")}
print("Unknown:", len(unknown), "residues")
for name, seq in refs.items():
print(f"{name:11}", len(seq))The unknown has 60 residues. The references are full-length proteins of 142 to 154 residues, so a local alignment (Global and local alignment, without heavy maths) is the right choice: you expect the fragment to match part of a protein, not all of it.
Align and rank
Global and local alignment, without heavy maths and Searching databases fast: similarity search and E-values
- Set up a local
PairwiseAlignerwith BLOSUM62, gap opening −12 and extension −1. - Align the unknown to each of the eight references. For each, record the score, the percent identity, the query cover and the number of gaps, and sort the hits by score.
Hint. Global and local alignment, without heavy maths set up the aligner and Scoring matrices: PAM and BLOSUM loaded BLOSUM62. Take the best alignment with aligner.align(unknown, seq)[0]. Its .counts() gives identities, mismatches and gaps; percent identity is identities divided by the total of all three, as on a BLAST page (Searching databases fast: similarity search and E-values). For query cover, aln.coordinates[0] holds the positions in the unknown where the alignment starts and ends; divide that span by 60. Collect one tuple per hit in a list, and sort(reverse=True) puts the highest score first.
Why. Ranking by score first, and only then looking at identity and cover, follows the habit from Searching databases fast: similarity search and E-values. A high identity over a short stretch can rank below a lower identity over the whole fragment, and the score weighs both.
Checkpoint: HBB_HUMAN is the top hit, score 318.0, 98.3% identity over 100% of the unknown, no gaps. Then HBB_HORSE 270.0, HBB1_MOUSE 259.0, HBB_BOVIN 257.0, HBB_CHICK 229.0, HBA_HUMAN 143.0 (48.3%), MYG_HUMAN 34.0 and LYSC_HUMAN 21.0. Lysozyme shows 44.4% identity, but over only 30% of the unknown and with 2 gaps.
Check the scores against chance
Scoring matrices: PAM and BLOSUM and Searching databases fast: similarity search and E-values
- Make 200 shuffled copies of the unknown, with
random.seed(1). - For HBB_HUMAN, HBA_HUMAN, MYG_HUMAN and LYSC_HUMAN, compare the real score with the scores of the shuffled copies, and compute a z-score.
Hint. A shuffled sequence has exactly the same amino acids as the unknown, in a random order. It cannot be related to anything, so its scores show what chance alone achieves, the same idea that lies behind the E-value in Searching databases fast: similarity search and E-values. random.shuffle reorders a list in place, so turn the string into a list first and join it back afterwards. aligner.score(a, b) returns the score without building the alignment, which is much faster. The z-score is (real score − mean of shuffled scores) ÷ standard deviation of shuffled scores: how many spreads above chance the real score sits. The statistics module has mean and stdev.
Why. An E-value from this search would mean little, because it depends on the size of the database, and eight proteins is a tiny one. Shuffling gives a chance baseline that suits the search you actually ran. It is slower than a formula, which is why BLAST uses the statistics instead.
Checkpoint: HBB_HUMAN: real 318.0, shuffled mean 25.5, shuffled maximum 48.0, z = 60.9. HBA_HUMAN: z = 21.6. MYG_HUMAN: z = 1.9. LYSC_HUMAN: real 21.0, shuffled mean 22.0, z = −0.3, no better than chance.
Multiple alignment and conservation
Multiple alignment and conserved regions
- For each of the five beta globins, cut out the 60-residue region that matches the unknown, and stack the six sequences into a
MultipleSeqAlignment. - Mark the fully conserved columns and count them.
Hint. Task 1 showed that none of these five alignments has gaps, so the matching regions can be stacked directly, as in Multiple alignment and conserved regions. A local alignment may leave off a mismatched letter at either end, so do not use its end points as they are. Instead, take the start position in the reference (aln.coordinates[1][0]), subtract the start position in the unknown (aln.coordinates[0][0]), and cut 60 residues from there. Then reuse the column loop from Multiple alignment and conserved regions, with Counter and * marks.
Checkpoint: the regions are residues 76–135 of human, mouse and chicken, 74–133 of cow and 75–134 of horse. 35 of the 60 columns are fully conserved.
Build a tree
Trees from sequences
- Compute identity distances from the alignment of task 3 and build a neighbour-joining tree.
- Root it with chicken as the outgroup, draw it, and say where the unknown sits.
Hint. Trees from sequences has every line you need: DistanceCalculator("identity"), DistanceTreeConstructor().nj, rooting with an outgroup and sharing the root branch, then Phylo.draw_ascii. Print the distance table as well as the tree: the distance between the unknown and its nearest neighbour, multiplied by 60, gives the number of differences. Remember the warning in Trees from sequences about how little a short alignment can resolve.
Why. Identity in task 1 compares the unknown with each reference separately. A tree places it among all of them at once, so you can see whether it falls inside the beta globins as a group and which branch it joins, rather than relying on a single top score.
Checkpoint: the unknown pairs with HBB_HUMAN, at a distance of 0.016667, which is 1 difference in 60. Every other distance from the unknown is 0.166667 or more. Chicken sits alone on one side of the root.
Describe the difference
Scoring matrices: PAM and BLOSUM, Multiple alignment and conserved regions and Variants: what a change in sequence does, and how it is annotated
- Find the one position where the unknown differs from HBB_HUMAN, and give it in human numbering, counting the first methionine as 1.
- Write the change in HGVS protein style, say what type of change it is, and look at the same column in the other species.
Hint. Walk along the unknown and the matching human region together with zip and print where they differ. Position 1 of the unknown lines up with residue 76 of the human protein, so add 75 to turn a position in the unknown into a human position. Use three-letter codes as in Variants: what a change in sequence does, and how it is annotated. Then read the same column of the task 3 alignment, and look up the pair in BLOSUM62 (Scoring matrices: PAM and BLOSUM): blosum62["R", "K"].
Checkpoint: position 30 of the unknown, which is position 105 of human HBB: Arg in human, Lys in the unknown. A missense change, Arg105Lys. Cow also has Lys there; mouse, horse and chicken have Arg. BLOSUM62 scores the swap +2.
Check your answer
Arginine and lysine are both long, positively charged amino acids, so this is a conservative change: BLOSUM62 scores it +2, meaning such swaps are common between related proteins. The column holds only these two residues across five species, and cow has lysine too, so a lysine here is clearly tolerated. If this change were found in a person, it would be written p.(Arg105Lys) against human haemoglobin beta. Here it is a difference between species, not a variant in a patient.
Write a short report
All of this module
- In a text cell, answer the research question in two or three sentences, then give the evidence from tasks 1–5 in one sentence each.
- State the limits of what this small search can show.
Hint. Separate what the data show from what you infer. Similarity is measured; homology is inferred (Why compare sequences: homology, similarity and identity). Which hits are clearly homologous, which are borderline, and which are unrelated? Think about what one difference in 60 does and does not tell you about the species: the reference set holds only one primate. Myoglobin is a known, distant relative of the globins; ask why it scored barely above chance here.
Checkpoint: your report names the protein family and the closest reference, quotes identity, z-scores, conservation and the tree with your numbers, and says that the species cannot be pinned down from this reference set alone.
Model answer
The unknown is a fragment of haemoglobin subunit beta. It matches human HBB with 98.3% identity over its full length (59 of 60 residues), far ahead of the other beta globins (70.7–86.2%), and its score is 61 standard deviations above the scores of shuffled sequences. Human haemoglobin alpha is a clear but more distant homologue (48.3%, z = 21.6). Myoglobin, though a real relative, is not detected over this short stretch (z = 1.9), and lysozyme is unrelated (z = −0.3): its 44.4% identity covers only 18 alignment columns, gaps included. In the tree the unknown joins human, differing at one position, 105, where it has lysine instead of arginine, a conservative change.
The fragment is in fact residues 76–135 of haemoglobin beta from the western lowland gorilla (Gorilla gorilla gorilla, UniProt P02024), whose beta chain differs from the human one only at this position. From this reference set alone you could only say "human or a close relative": with one primate in the set and one difference in 60 residues, the species cannot be identified. A full BLAST search against a large database would settle it.
Take it further
Paste the unknown into web blastp against swissprot and compare the top hits with your ranking. Then run the five beta globins and the two human proteins, alpha globin and myoglobin, through Clustal Omega at EMBL-EBI, and see where it puts the gaps that your gap-free alignment avoided.
Share the notebook with a short note at the top saying what it does. Before you share, restart it and run every cell from the top, so every number in your report comes from the code as it stands.
This project worked with single sequences, a few hundred letters at a time. Module 5, Sequencing data, moves from these to the millions of short reads that a sequencer produces in one run. Each read has to be placed on a reference genome, and that placing uses the same ideas you have practised here: seeds to find candidate positions quickly, alignment with scores and gaps to fit each read, and care in judging which matches are real.
Further reading · Module 4Biopython tutorial: pairwise alignments. · BLAST help. · UniProt help. · All reading for this course
Up next
Lesson 1
From sample to reads