My program gives valid output with small databases, but with large I always get "No match". Do you have any idea what may be the cause ? Belowe is my code, It may seem a little messy so if you find it difficult to understand some of functions just ask and I'll try to explain it as best as I can.

import sys
import csv
import re
#check command line argument
if len(sys.argv)!=3:
   print ("Usage: python dna.py data.csv sequence.txt")

genomes_names = []
#open csv file with names and numbers of repetitions
with open(sys.argv[1], "r") as csvfile:
   dna_data = csv.reader(csvfile)
   personal_data = list(dna_data)
   #print(f"personal data {personal_data}")
   genomes_names = (personal_data[0][1:])
   #print(f"Genomes names {genomes_names}")

counter = 0
#open a file with sequences
file = open(sys.argv[2],"r")
sequence = file.read()

occurences = []
#read name of genomes and check the number of occurences
#function that counts repetitions I got from 

 for i in range(0,len(genomes_names)):
   start = 0
   substr_count = 0
   while True:
       string = sequence
       substring = genomes_names[i]
       #print(f"I check for: {genomes_names[i]}")
       loc = string.find(substring,start)
       if loc == -1:
       substr_count += 1
       start = loc + 1
       #print(f"Checking for {genomes_names[i]}")
    #print(f"For {substring} number of repetitions is: {substr_count}")
    substr_count = 0

 #print(f"Occurences of every pattern: {occurences}")

#how many rows are there
rows_number = len(personal_data)

repetitions = []

#print(f"occurences {occurences}")
match = 0
name = 0
no_match = False
 for i in range(1,len(personal_data)):
     for j in range(1,len(personal_data[i])):
     for k in range(len(repetitions)):
         if int(repetitions[k]) == int(occurences[k]):
             match +=1
             no_match = True
             match = match
             no_match = False
     repetitions = []
     if match == len(occurences) and no_match == True:
         no_match = False
         match = 0

 if no_match == False and name == 0:
     print("No match")

1 Answer 1


That code on StackOverflow is meant to calculate something else, I guess. We don't need overlap, in our case, we have to keep track of the longest contiguous repetition sequence. For that, we need separate variables for current and longest streak, and update the longest if the current surpasses it. Also, we have to check whether the current match happens to be exactly len(substring) characters after the last one, a longer distance would indicate a restart.

My version for the next start would add len(substring) instead of 1, so I can later check whether loc != start, and reset the current length in that case (before I increment the count and maybe update the max count).

  • Hi. Thank you for answer, but somehow I don't really get a grasp of what you said. I tried to write code as you said but apparently I misunderstood your advice. Could you explain it one more ?
    – Bijacz
    Mar 14, 2020 at 16:46
  • 1
    Which part is it? On finding the next loc (and checking it for -1), if loc != start, I reset the current streak to 0. Then I add 1, check whether longest streak needs an update (and perform it), and finally set start = loc + len(substring), which is the earliest I'd expect the next occurrence at.
    – Blauelf
    Mar 14, 2020 at 17:15
  • Thanks, now it works :)
    – Bijacz
    Mar 15, 2020 at 12:14

You must log in to answer this question.

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