Which part of the problem are you working on?

I am at a very early stage of the problem, trying to scan a specific sequence for STRs, counting how many times they show up and storing this count as a variable.

What problem are you having? Describe the problem in detail. If appropriate, include any error messages, screenshots, or small snippets of code to describe the issue. (Keeping in mind the course's policy on academic honesty, do not share code that may contain spoilers or solutions)

I want to store the number the count of STRs in a dictionary that I have defined initially as follows:

repeat_count = {
    'AGATC' : 0,
    'AATG' : 0,
    'TATC' : 0

To do so, I want to use Regular Expressions. In particular, I'm going to use .FindAll() and get the length of the string list as count.

repeat_count['AGATC'] = len(re.findall('AGATC', sequence.read()))
repeat_count['AATG'] = len(re.findall('AATG', sequence.read()))
repeat_count['TATC'] = len(re.findall('TATC', sequence.read()))

When printing the repeat_count dictionary before an after, I get:

enter image description here

A quick manual search of Sequence 1 identifies 4 instances of AGATC (correct), 1 of AATG and 5 of TATC.

My programme has found the 4 instances of AGATC in the sequence, but nothing for AATG or TATC, despite the lines of code being identical for each STR.

What have you already tried so far to resolve the problem?

Interestingly, if I remove Line 1 from the code snippet above, the one regarding AGATC, the result changes:

enter image description here

Now it's identified the right number of AATG (which is now first line). If once more I get rid of the line regarding AATG, I get:

enter image description here

What I conclude from this testing is that once the method .read() has been called once for the sequence, somehow successive callings of the method are not reading the sequence correctly, as if the file has been exhausted after one read.

Why? And how do I fix it?

P.S. I know that I'm still not defining my RegEx right, as this counts how many times an STR is found throughout a sequence whereas what I am really interested in is the maximum amount of times an STR repeats next to each other throughout the sequence. But I'm currently stuck in a more basic, fundamental part.

Thanks in advance!


I think that your logic is correct but instead of hardcoding the DNA sequences, have you tried not hardcoding them and instead, let the program count the maximum count of the appearance of every sequence?

I was making the same mistake while solving this problem and it worked for me.

Please upvote if this answer helps you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .