Locating Restriction Sites

The Billion-Year War

The war between viruses and bacteria has been waged for over a billion years. Viruses called bacteriophages (or simply phages) require a bacterial host to propagate, and so they must somehow infiltrate the bacterium; such deception can only be achieved if the phage understands the genetic framework underlying the bacterium’s cellular functions. The phage’s goal is to insert DNA that will be replicated within the bacterium and lead to the reproduction of as many copies of the phage as possible, which sometimes also involves the bacterium’s demise.
To defend itself, the bacterium must either obfuscate its cellular functions so that the phage cannot infiltrate it, or better yet, go on the counterattack by calling in the air force. Specifically, the bacterium employs aerial scouts called restriction enzymes, which operate by cutting through viral DNA to cripple the phage. But what kind of DNA are restriction enzymes looking for?
The restriction enzyme is a homodimer, which means that it is composed of two identical substructures. Each of these structures separates from the restriction enzyme in order to bind to and cut one strand of the phage DNA molecule; both substructures are pre-programmed with the same target string containing 4 to 12 nucleotides to search for within the phage DNA. The chance that both strands of phage DNA will be cut (thus crippling the phage) is greater if the target is located on both strands of phage DNA, as close to each other as possible. By extension, the best chance of disarming the phage occurs when the two target copies appear directly across from each other along the phage DNA, a phenomenon that occurs precisely when the target is equal to its own reverse complement. Eons of evolution have made sure that most restriction enzyme targets now have this form.


A DNA string is a reverse palindrome if it is equal to its reverse complement. For instance, GCATGC is a reverse palindrome because its reverse complement is GCATGC.


A DNA string of length at most 1 kbp in FASTA format.


The position and length of every reverse palindrome in the string having length between 4 and 12. You may return these pairs in any order.


First, let’s read the FASTA file. Remember that first line includes a name, therefore, we skip the first line:

with open("rosalind_revp.txt") as f:
    for line in f:
        if line.startswith(">"):
            dna_str = str()
            dna_str += (line.strip("\n"))

To find its complementary sequence, we use the same function that we covered in “Complementing a String of DNA

def complementary(dna_sequence):
    replace_bases = {"A":"T","T":"A","G":"C","C":"G"}
    return ''.join([replace_bases[base] for base in reversed(dna_sequence)])

Then, we create another function to find out the location and the length of palindromes:

def LocatingRestrictionSites(dna):
    position_length = []
    for i in range(4, 13):
        for j in range(0, len(dna) - i + 1):
            if complementary(dna[j:j+i]) == dna[j:j+i]:
                position_length.append(str(j+1) + ' ' + str(i))
    return position_length

Finally, we print the potision and length of the palindromes:

for pos_len in LocatingRestrictionSites(dna_str):
Important note:

This problem is taken from rosalind.info. Please visit ROSALIND to find out more about Bioinformatics problems. You may also clink onto links for the definitions of each terminology.