Genome Assembly

Assignment Overview

When we sequence a genome, we don’t get one long readout — we get millions of short reads (50-300 bp), each a random fragment of the genome. Assembly is the process of piecing these reads back together.

One major strategy, used by assemblers like SPAdes and ABySS, is the De Bruijn graph approach:

  1. Break every read into overlapping substrings of length k, called k-mers.
  2. Build a graph where each node is a (k-1)-mer, and each edge represents a k-mer connecting its prefix to its suffix.
  3. Reconstructing the genome becomes a problem of finding a walk through this graph that uses the right edges — ideally a single path that visits every edge once (an Eulerian path).

Example: the k-mer ATG (k=3) becomes an edge from node AT → node TG.

The key advantage over earlier “overlap graph” approaches is that De Bruijn graphs scale well to millions of reads. But they introduce their own headaches, which you’ll discover in Part 4.

Data

The data for this assignment consist of two small text files containing overlapping reads from a simulated genome. You can copy them below and paste them into text files that you will use for the exercises.

The following reads should be pasted into a file named clean_reads.txt:

ATGGCGT
GGCGTGC
CGTGCAC
TGCACGT
GCACGTA
CACGTAT
ACGTATG

The following reads should be pasted into a file named repeat_reads.txt:

CAATTCG
AATTCGG
TTCGGAT
TCGGATC
CGGATCT
GGATCTT
ATTCGGA
TCGGATC
GGATCTT
ATCTTTC
CTTTCGG
TTTCGGA
TCGGAAT
GGAATTG
GAATTGG



Exercises

There are four parts to this assignment:

  1. Loading in reads and converting the to an arbitrary length set of k-mers
  2. Constructing a De Bruijn graph from the set of k-mers
  3. Reconstructing the original genome from the De Bruijn graph
  4. Exploring how repeats affect the graph and genome reconstruction

Submission

Before you begin, create a week11 folder in your QBB2026-answers repo. You will be expected to turn in 4 files for this assignment.

  1. A python script assembled across parts 1-3
  2. A README with written answers to questions 1c, 2c, 3b (the reconstructed sequence), 4b, and 4c.
  3. Two De Bruijn graph visualizations, one from 2a and one from 4a.

Part 1: From Reads to K-mers

The file clean_reads.txt contains simulated error-free reads from an unknown circular bacterial plasmid fragment, one per line.

1a. In your script, write a function get_kmers(read, k) that returns a list of all overlapping k-mers of length k from a single read.

1b. Using get_kmers, load the reads from clean_reads.txt and generate all k-mers (k=5) from the full set of reads. Store them in one combined list called all_kmers.

1c. (Written, 2-3 sentences) Why do we break reads into k-mers instead of just trying to find overlaps between the full-length reads directly? Hint: think about computational cost with millions of reads.

Part 2: Building the De Bruijn Graph (30 min)

A De Bruijn graph can be represented in Python as a dictionary, where each key is a (k-1)-mer node, and its value is a dictionary of output (k-1)-mer nodes and associated weights (i.e. the number of times that edge appears in the reads).

2a. Write a function build_debruijn_graph(kmers) that takes a list of k-mers and returns a dictionary representing the De Bruijn graph. For each k-mer, the prefix (first k-1 characters) should point to the suffix (last k-1 characters).

Hint: Don’t forget to add a node for each suffix (k-1)-mer, even though it may have no outgoing edges.

In order to visualize your de Bruijn graph, you will us a Python package called graphviz. In order to do this, you can include the following function in your code, assuming that the graph input is a dictionary of dictionaries as described above.

import graphviz

def save_graph(graph, fname):
    dot = graphviz.Digraph(format='png')
    for node in graph.keys():
        dot.node(node)
    for node, edges in graph.items():
        for edge, label in edges.items():
            dot.edge(node, edge, label=f"{label}")
    dot.render(fname, view=True)

2b. Run your function on all_kmers from Part 1b and print the resulting graph.

2c. (Written) Look at your printed graph. How many nodes have exactly one outgoing edge and one incoming edge? What does that tell you about the structure of this particular dataset (i.e., is there a unique, unambiguous path through it)? Why do the edges not all have the same weight?

Part 3: Reconstructing the Genome

Because this example graph is simple (no branching), you can reconstruct the genome by starting at the node with no incoming edges (or any node, if circular) and walking the graph, adding one new character at a time.

3a. In your code, write a function to find the start_node of your graph. It should be a node that has no incoming edges.

3b. Write a function walk_graph(graph, start_node) that walks the graph starting at start_node, following edges until it reaches a node with no outgoing edges (or has visited all edges). It should return the assembled sequence as a string. Record this sequence.

Hint: Since each node is a (k-1)-mer, and you’re moving to a node that overlaps the current one by k-2 characters, you only need to append the final character of each new node you visit.

Run your function and print the reconstructed sequence. Does it match a sequence you could have guessed by eye from the original reads?

Part 4: When Assembly Gets Hard

Now consider th set of reads in repeat_reads.txt, generated from a genome containing a repeated sequence.

4a. Run your code on this set of reads. When you visualize the De Bruijn graph for these reads (k=5), you should find that two nodes has two outgoing edges instead of one — this is the signature of a repeat in the genome that is longer than k-1.

4b. (Written) Find a valid sequence that could be represented by this graph. Is it the only one? Why or why not?

4c. (Written, 2-3 sentences) If you were an assembler developer, name one way you could reduce this ambiguity (e.g., something about read length, k-mer size, or paired-end information — one sentence of informed speculation is fine).

Submission

  1. Python script that generates and saves a De Bruijn graph based on user input (5.5 points total)
    • Part 1: Find all k-mers (1 point)
    • Part 2: Build De Bruijn graph (2 points)
    • Part 3a: Find start_node (1 point)
    • Part 3b: Walk graph (1.5 points)
  2. De Bruijn plot from part 2a (1 point)
  3. De Bruijn plot from part 4a (1 point)
  4. Question from part 1c (0.5 points)
  5. Question from part 2c (0.5 points)
  6. Question from part 3b (0.5 points)
  7. Question from part 4b (0.5 points)
  8. Question from part 4c (0.5 points)

Total Points: 10