# How to input the opposite strand of DNA?

I am trying to write a program that will count the repeats of a given DNA strand:

I am using a Counter library, something like this code shown below

from collections import Counter

s="XXXXXAGCCTGCCAAGCAAACTTCACTGGAGXXXXXTGTGCGTAGCATGCTAGTAACTGCAXXXXXTCTGAATCTTTCAGCTGCTTGTXXXXXTGGGCCTCTCACAAGGCAGAGTGTCTTCATGGGACTTTGATATTTATTTTTGTACAACCTAAGAGGAACAAATCCTTTGACACTGACAAATTGGCTTCCATATTTTATACCTTAATCATCTCCATGTTGAATTCATTGATCAACAGTTTAAGAAAAAAAGATGTAAAAATGCTTTTAGAAAGAGAGGCAAAGTTATGCACAATAACTTCTCATGAAGTCACAGTTTGTTAAAAGTTGCCTTAGTTCACAATAAATAATTATGTATGCTCTATAATTTCAGTGA"

def chunk_string(s, n):
return [s[i:i+n] for i in range(len(s)-n+1)]

counter = Counter(chunk_string(s, 5))


Repeat supposed to be "declared" as an identical sequence on the same strand (like XXXXX here that occurs 4 times). However, I can also have this repeat (for ex. XXXXX in this particular test case) on the OPPOSITE strand.

How am I suppose to intake the opposite strand in case of using .fasta files? As I know for today - fasta is presented only in a single strand way. Or am I completely mixed the terms?

Yes, you're going to want to take the reverse complement of your string in order to analyze the other strand, that is the standard.

Also, it is worth pointing out that you may want to use a generator expression instead of a list expression for your chunk_string function:

def chunk_string(s, n):
return (s[i:i+n] for i in range(len(s)-n+1))


This will result in a generator that yields kmers as the Counter constructor iterates through them, which means that you don't need to first construct and store the entire list of kmers first and then pass to Counter but instead can feed them directly to it. Not essential for small strings, of course, but a good practice and useful for larger strings. See this SO post for a better explanation.

You can also use Biopython's reverse_complement function in order to easily obtain the reverse complement.

What's going to be simplest is to generate the reverse complement of your short search sequence, and search for that too.

• Hi @swbarnes2, this looks like the start of a good answer; any chance you could expand on this approach?
– gringer
Apr 29 at 9:05