Where does Pyrococcus abyssi, our tiny deep-sea organism, begin copying its DNA? The researchers who studied this question reported eight different 13-nucleotide DNA patterns in Figure 2A of their paper. They looked at a wider stretch of the chromosome that included the origin of replication and the DNA around it. Those patterns give us something specific to look for.
In Part 2, we saved a 799-nucleotide region around the reported origin as a FASTA file. In Part 3, we used count_kmer() to look for exact matches to the eight patterns in the stored sequence. We found five of the eight. The other three had zero exact matches in the strand we searched. Could any of them be on the other strand of that same region?
Our FASTA stores the sequence of one strand of the region. Both count_kmer() and find_most_frequent_kmers() work with the sequence we give them. We passed in the stored strand, so that’s the strand they could search. DNA base pairing gives us a way to work out the sequence of the other strand from the one already in the file. How DNA letters pair
This matters in real searches too. A DNA fragment can match either strand of a sequence in a database, so tools such as NCBI’s nucleotide BLAST check both. We can do the same for our 799-nucleotide region. We’ll add a reverse_complement() function to work out the other strand, then use the same exact counter on both strands. That will tell us which of the three patterns we can recover there and which we need to look for elsewhere. NCBI’s nucleotide BLAST guide
In This Part
We’ll start with a small drawing of DNA and work out the other strand by hand. That will show us two things Python needs to know: which letters pair and which end to read from. Then we’ll write a function to apply the same rules to the full 799-nucleotide sequence. We’ll use the exact counter from Part 3 to compare the two strands of that region.
Where we are now
Follow the progress animation from the NCBI chromosome record to the Part 3 result. DoriC tells us that the region runs from 122,701 through 123,499 on that chromosome. Counting both ends gives us the 799-nucleotide region we saved. The final step shows the Part 3 result. Five of the eight patterns had an exact match on the stored strand.
Part 1. Read the Other Strand
The drawing below shows two strands of the same piece of DNA. The top strand reads ATGC. We chose those four bases so we can see all four kinds in one example.
Look at the first two pairs. A (adenine) pairs with T (thymine). In the first pair, A is on top. In the next pair, T is on top. The rule works either way. Now look at the last two pairs. G (guanine) pairs with C (cytosine). If we know one base, this rule tells us the base across from it.
Let’s apply the rule to ATGC. Put T below A, A below T, C below G, and G below C. The lower row reads TACG from left to right. That’s the complement.
The arrows show which way we read each strand. The ends are called 5′ (five prime) and 3′ (three prime), named after two numbered carbon atoms in the DNA sugar. When a cell builds a new strand, it can add a base only at the 3′ end. The new strand grows from 5′ to 3′. We also write DNA sequences in that order. How DNA is built
In the drawing, the top strand starts at 5′ on the left. The lower strand starts at 5′ on the right. If we copy the lower row straight from left to right, we get TACG in 3′ to 5′ order. To write it in the usual 5′ to 3′ order, start at the G on the right and read G, C, A, T. That gives us GCAT. We call it the reverse complement because we paired the letters and reversed the order of the paired row.
Now watch those two steps in the animation. First, pair each base in ATGC to fill in the lower row. Then read that row from its 5′ end on the right. Compare the lower row, TACG, with the sequence the animation writes out, GCAT.
This is why our FASTA only needs to store one strand. Saving one strand takes half as many DNA letters as saving both. Our file gives us 799 nucleotides from one strand of the region, and Python can work out the corresponding 799 nucleotides on the other strand. We already have what we need to search both.
What can we find on the second strand?
Let’s try a small version of the search we’ll run on the 799-nucleotide region. Start with ATGC, the sequence on the top strand in our diagram. Pair its bases to get TACG on the lower row, then read that row from right to left to get GCAT. Now we have the two sequences of the same piece of DNA, each written from 5′ to 3′.
Now search both sequences for the same pattern, GCA. We picked it because we can already see it at the start of GCAT, the sequence we just worked out. Let’s first search ATGC by itself, the way our Part 3 program searched the strand in the FASTA. Then we’ll repeat the same search on GCAT and compare the results.
A match means the three consecutive bases are G, C and A, in that order. The brackets mark the two places where a three-base pattern fits. The first check starts at base 1. The second starts at base 2.
On the first strand, we check ATG and TGC and get zero matches. On the other strand, we check GCA and CAT and get one match, at the first base. Starting at base 3 would leave only two bases, so neither sequence has a third place to check. The same region contains the match, but we would miss it if we searched only the stored strand.
Python can check both strands the way we just did. We write each sequence from its own 5′ end, with its first letter on the left. Then the counter from Part 3 can check both strings from left to right. We only have one search to write and test. When we use it on the 799-letter sample, we’ll see whether the second search finds any of the patterns we missed.
For a closer look at why the arrows point in opposite directions, watch Khan Academy’s short video on the two DNA strands.
How does a cell know which letter to put back?
This same rule also helps cells look after their DNA. When one strand is damaged, a cell can use the letter on the other strand to tell which letter to put back.
For example, a chemical change can turn C into U, a base called uracil. A protein called uracil-DNA glycosylase recognizes and removes the U. After other repair proteins prepare the gap, DNA polymerase uses the intact strand as a guide. If there’s a G across from the gap, the polymerase adds a C. DNA ligase then seals the join. How cells repair DNA
UV light can make two neighboring DNA bases stick together. A mistake during copying can leave two letters that don’t pair properly. Cells have different repair systems for those problems. Repair helps keep changes from building up in our DNA. Some changes that escape repair can contribute to cancer if they affect how a cell grows or divides. DNA changes and cancer
Before we continue, we recommend watching this short TED-Ed animation by Monica Menesini. A little after the first minute, watch how DNA polymerase matches each letter with its partner. You’ll also see why different kinds of damage need different kinds of repair.
In the repair example, DNA polymerase used the G on the intact strand as a guide to put a C back on the damaged strand. Tomas Lindahl’s experiments helped explain how cells make that repair. A cell uses the pairing rule to repair a letter. We can use it in Python to work out the letters on a whole strand. How scientists discovered DNA repair
Part 2. Add the Function and Check Four Letters
We just used two facts about DNA to solve a problem by hand. We knew which letters pair, and we knew which end to read from. Now we can teach Python those same rules. A small dictionary called complements tells Python what goes across from each letter. Reading from the end puts the letters in the order our counter needs. Once our function can do this for four letters, we can try it on hundreds. We started with a question about real DNA. Now we have a rule we can write down, test and use again.
We built a reverse complement in DNA Toolkit Part 2. Here we’ll write it from scratch so Ori Finder works on its own and we can see where the function fits into our two-strand search.
Reopen the ori-finder folder from Part 3 in VS Code. Open algorithms.py and add the new function below the two counters. It takes one DNA string and gives us another string that the counters already know how to read.
def reverse_complement(sequence):
# 1. Use the same letter case as our pairing table.
sequence = sequence.upper()
complements = {"A": "T", "T": "A", "C": "G", "G": "C"}
# 2. Walk from the end and save each letter's partner.
opposite = []
for base in reversed(sequence):
if base not in complements:
raise ValueError("Use only A, C, G and T for this DNA sequence.")
opposite.append(complements[base])
# 3. Join the letters into one string.
return "".join(opposite)- Write down the complementary letters.
upper()letsatgcuse the same table asATGC. The dictionary holds one answer for each DNA letter. For example,complements["C"]gives us G, the letter that pairs with C. - Start at the right end. In the picture we paired the letters first and then read from the right. In Python we can get the same result by visiting the original letters from right to left and looking up each partner as we go.
reversed()visitsATGCin the order C, G, T, A. The first partner we save is G, then C, A and T.append()adds each partner to the end of our list. If a letter has no entry in our four-letter table, the function stops and tells us which letters it accepts. - Give back the new string.
join()puts the saved letters together. The empty string in"".join(opposite)means there’s nothing between them. We getGCAT, ready for the counting function.
Watch the function run
We’ll pass in ATGCCA, six practice letters we chose so we can follow every step. The two Cs next to each other let us see what Python does when it reads the same letter twice in a row.
- Start at the right. Look at the top row. We pass
ATGCCAto the function, andreversed(sequence)starts with the last A. - Look up a letter and save its complement. A pairs with T, so the first letter we save is T. The next letter to the left is C, so we add G. The C after that gives us another G. Keep watching the two rows. Python reads one letter on the top row and adds one letter to the end of the list below. After six checks, the list holds T, G, G, C, A and T.
- Join the letters and return them. Watch the bottom row. Python joins the six saved letters into one string,
TGGCAT, and returns it. Now we have two strings the same counter can read from left to right.
Let’s check our four letters before we change how the experiment searches. In ori_finder.py, add reverse_complement to the existing import from algorithms at the top:
from algorithms import count_kmer, find_most_frequent_kmers, reverse_complementLeave the Part 3 experiment in place and add this quick check at the bottom of the file.
# Check the same four letters we worked out by hand.
example = "ATGC"
print("Original sequence:", example)
print("Reverse complement:", reverse_complement(example))Save both files and run uv run ori_finder.py in the project terminal. The Part 3 results still print first. Look at the last two lines:
Original sequence: ATGC
Reverse complement: GCATPython gave us GCAT, the same answer we got by following the lower arrow. Now we can use that same function on the real sample.
Part 3. Give the Eight Patterns a Second Search
Let’s go back to the three patterns we couldn’t find on the stored strand. One of them, GTTTCCACTGGAA, is a 13-nucleotide sequence from Figure 2A of the paper. Part 3 found no exact match on the stored strand of our 799-nucleotide region. We’ll keep the same region, all eight patterns, and the same counter. This time we’ll also search the opposite strand, so we can see exactly what changes.
The DNA we already have is in sample_dna. If you want to see where it comes from, open helpers.py and look for return header[1:], dna. The first value gives us the sample name. The second gives us the stored sequence of the 799-nucleotide region. That is what we pass to reverse_complement(). Once we have both strings, we’ll count each pattern in each string and print the counts side by side.
We’ll keep the earlier frequency check on the FASTA strand. That check answers its own question about repeated 13-letter patterns. The new table will tell us which of the eight patterns from the paper appear on either strand.
Keep working in the ori_finder.py file from Part 3. We’ll keep the FASTA reader, the frequency check, and the eight patterns already in the file. The quick check has done its job, so remove those four temporary lines at the bottom. Then add the line that works out opposite_dna and update the loop that prints the counts. The complete file below is there to check your edits. The eight patterns are the same mini-ORBs from Part 3. ORB stands for origin recognition box, a DNA pattern associated with a protein that recognizes DNA near the origin.
# 1. Bring in the two counting functions we just checked.
from algorithms import count_kmer, find_most_frequent_kmers, reverse_complement
# Bring in the FASTA reader we wrote in Part 2.
from helpers import read_fasta
# 2. Open the 799-letter sample we saved in Part 2.
sample_path = "samples/NC_000868.1_known_ori_799.fasta"
sample_name, sample_dna = read_fasta(sample_path)
opposite_dna = reverse_complement(sample_dna)
print("Sample:", sample_name)
print("Letters in the sample:", len(sample_dna))
# 3. Count every 13-letter subsequence in the sample.
pattern_length = 13
patterns, highest = find_most_frequent_kmers(sample_dna, pattern_length)
print("Patterns tied for the highest count:", len(patterns))
print("Highest count:", highest)
# 4. Check the eight different mini-ORB patterns from Figure 2A.
published_patterns = [
"ATTTCCACTGGAG",
"ATTTCCAGTGGAA",
"GTTTCCACTGGAA",
"ATTTCCACAGGAA",
"ATTTCCGATGGAA",
"ATTTCCACTGGAA",
"GGTTCCAGTGGAA",
"AGTTCCACTGGAA",
]
print()
print(f"{'Pattern':<15}{'FASTA strand':<15}Opposite strand")
for pattern in published_patterns:
fasta_count = count_kmer(sample_dna, pattern)
opposite_count = count_kmer(opposite_dna, pattern)
print(f"{pattern:<15}{fasta_count:<15}{opposite_count}")- Keep the import from the quick check. We already added
reverse_complementat the top. Leave it there withread_fasta()and both counters from Part 3. - Add one line after
read_fasta().sample_dnais what we read from the file. We pass it toreverse_complement()and save the answer asopposite_dna. Each letter has one partner, so both strings have 799 letters. We’ll need both strings for the search. - Keep a result we can recognize from Part 3.
find_most_frequent_kmers()still looks for repeats in the original sequence. It gives us the patterns with the highest count and the count itself. This check still answers the Part 3 question about repeats on the FASTA strand. Only the search for the eight published patterns uses both strands today. - Replace only the final loop. Keep the eight patterns just as they are. Add the table headings and print two counts per pattern. FASTA strand counts matches in the sequence we loaded. Opposite strand counts matches in the sequence we worked out. In both calls,
patternis the same. The emptyprint()gives the table a little space, and:<15leaves room for each heading and lines up the rows. Those spaces only affect what we see in the terminal.
Save and run the experiment:
uv run ori_finder.pySample: P_abyssi_GE5_NC_000868.1_known_ori_region_799bp
Letters in the sample: 799
Patterns tied for the highest count: 787
Highest count: 1
Pattern FASTA strand Opposite strand
ATTTCCACTGGAG 0 0
ATTTCCAGTGGAA 1 1
GTTTCCACTGGAA 0 1
ATTTCCACAGGAA 1 0
ATTTCCGATGGAA 1 0
ATTTCCACTGGAA 1 0
GGTTCCAGTGGAA 0 1
AGTTCCACTGGAA 1 0Find GTTTCCACTGGAA in the table. FASTA strand shows no exact matches, just as in Part 3. Opposite strand shows one exact match in the same 799-nucleotide region. We see the same result for GGTTCCAGTGGAA. Adding the second strand recovered two of the three patterns the original search missed.
Where we are after Part 4
Compare this progress animation with the one at the start. We still open the same 799-nucleotide sample. Python works out the opposite strand, and the exact counter searches the two sequences from that same region. Searching the opposite strand finds two more of the eight patterns reported in the study.
Conclusion
What did we find when we searched both strands?
We opened the same FASTA file and searched for the same eight patterns from the paper. This time, we found seven of them. Five were on the strand in the file, and two more appeared only on the other strand. One of the seven, ATTTCCAGTGGAA, appeared once on each strand. That’s why the table has eight matches across seven different patterns.
Why did the researchers look for these patterns in the first place? They studied where a protein called Cdc6/Orc1 attaches to DNA in Pyrococcus abyssi. That protein helps start DNA copying, and the paper shows groups of short patterns near the start site. Our search tells us which of the reported DNA patterns we can find in this region. The researchers’ lab experiments tell us about the protein’s behavior. The researchers’ paper
Why is one pattern still missing?
Take ATTTCCACTGGAG. There’s no exact match on either strand of our 799-nucleotide region. Our wider check in Part 3 found this pattern elsewhere on the chromosome. The paper also reports another group of patterns outside the origin. Once Ori Finder can show where each match starts, we can compare the matches near the origin with those farther away.
How does this help us build Ori Finder?
Look what we did with the file we already had. It stores the sequence of one strand of the 799-nucleotide region, so we save half the DNA characters we’d need to store both strands. The pairing rules let us work out the other strand when we need it. We used the same search code on both and found two patterns we’d missed before.
We’re already building a bioinformatics pipeline. Our FASTA reader gives us the DNA sequence. Our new reverse complement function gives us the other strand. Then our exact counter can tell us how many times a pattern appears in either sequence. We also have the frequency finder from Part 3, which can tell us which short patterns appear most often. Each function does one small job. Together, they let us ask questions we couldn’t answer with any one of them alone.
Summary
We turned a rule from biology into a tool we can use again. Starting with one stored strand, we worked out the other strand and searched both with the code we already had. We found seven of the eight patterns the paper gave us. The same function can also help us see which patterns in a list are related.
What’s Next?
Can Python tell us which patterns in a list are reverse complements of each other? We can already work out the reverse complement of one pattern. Checking a whole list by hand would mean doing the same thing over and over.
In Part 5, we’ll use the same 799-nucleotide sample and the 13-nucleotide patterns our frequency finder counted in Part 3. We’ll write a small helper that takes one list. For each pattern, it works out the reverse complement and checks whether those letters are already on the list. When it finds a pair, it reports it once. Then we can ask a new question about the patterns from the paper. Where do they occur in the DNA, and are any of the matches close together?
Your Turn
Can you find one pair by hand? Try it with AACCGTT. We picked this seven-letter example so you can check it at a glance.
- Start with
AAC, the first three letters. - Write its reverse complement.
- Look for those three letters in
AACCGTT.
The answer is GTT, the last three letters. You’ve found a pair in one short sequence. In Part 5, we’ll teach Python to find pairs like this for us.
Until next time, rebelCoder, signing out.
Completed Part 4 project on GitHub.
Video version of this article:

Loading the conversation…