0

I'm trying to record the longest run of each STR I need and I'm close. Without the final "else" the code works fine with small dictionary and almost fine with long (because the count I useonce doesn't get back to zero if another run of the same STR starts again) But if I put the "else" to solve that problem the code says that the longest run is always 1. Why is the "else" messing everything up? here is my code:

with open (argv[2], 'r') as (txtfile):

dna = txtfile.read()

countAGATC = 0
countAATG = 0
countTATC = 0

maxAGATC = 0
maxAATG = 0
maxTATC = 0

for i in range(len(dna)):
    
    if dna[i:i+5] == 'AGATC':
            
    
        countAATG = 0
        countTATC = 0

        countAGATC += 1
        i = i+5
        
        if countAGATC > maxAGATC:
             maxAGATC = countAGATC
            
            
    elif dna[i:i+4] == 'AATG':
            
        
        countAGATC = 0
        countTATC = 0
        countAATG += 1
        i = i+4
        
        if countAATG > maxAATG:
            maxAATG = countAATG
            
            
    elif dna[i:i+4] == 'TATC':
            
        
        countAGATC = 0
        countAATG = 0
        countTATC += 1
        i = i+4
        
        if countTATC > maxTATC:
            maxTATC = countTATC
     
    else:
        countAGATC = 0
        countAATG = 0
        countTATC = 0

result without else:

small dictionary : 4,1,5 and correct answer is 4,1,5

large dictionary: 37,12,51 and correct answer is 37,10,48

result with else: (1,1,1) with both dictionaries

thanks for your time!

1 Answer 1

0

Your i = i+5 (or whatever) does nothing inside a python for loop. If you debug you will find that for advances i by 1 regardless. Since you are checking every i and you only need to count consecutive bases, try testing for a match and then test for a match at i + (length of the base you are seeking). In this example I am using j to loop through the thing being sought and i to loop through the sample:

for j in range(len(sought)):
    counter[j] = 1

 if sample[i:i+len(sought)] == sought:

    if sample[i:i+len(sought)] == sample[i+len(sought):i+len(sought)+len(sought)]:
       counter[j] += 1

    else:
       counter[j] = 1

    if counter[j] > high_count[j]:
         high_count[j] = counter[j]

Good luck

1
  • how can you debug in python? Sep 27, 2020 at 15:18

You must log in to answer this question.

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