0

I'm kind of lost I don't know how I can compare the max number of occurrences with the database. My program here can get the max number of it stored in a list called counts to get it I used a function that uses re stuff. So what I have is a list of the max number of STR's like counts = [4, 1, 5] but once I have this how will I compare it with the database I don't quite get for loops in python.

from sys import exit, argv
import csv
import re

def main():

    # exit file if missing com-line arguments
    if len(argv) != 3:
        print("Usage... [python file.py database.csv sequence.txt]")
        exit(1)

    #open the csv file that serves as the database then store them
    # within a list wherein each row is a list
    with open(argv[1], newline='') as database:
        # read the file then store them as a list
        read = csv.reader(database)
        # store the first line of the database in a list
        headers = next(read)
        # store the read database into a 2d list
        data = list(read)

    # remove name in the headers list
    del headers[0]

    # open the sequence file
    dna = open(argv[2], "r")

    # read the sequence file and store into sequence and strip the trailing new line
    sequence = dna.read().rstrip('\n')

    # list that will store the max counts of each header
    counts = []

    # create a function that counts the max number of consecutive occurrences
    # of the STR's in the sequence 
    for x in range(len(headers)):
        # traverse through the header list then call count_occurences function
        # to return the max number of occurrences of each STR then store it
        # into counts[] list
        counts.append(count_occurrences(sequence, headers[x]))

    print(counts)

# function that will find the max number of occurences by
# using the re library and regular expressions
def count_occurrences(sequence, STR):
    pattern = "(?=((" + re.escape(STR) + ")+))"
    matches = re.findall( pattern, sequence )
    return max( len( m[0] ) // len( STR ) for m in matches )


main()
0

I haven't used re, but you could loop thru your dict/list/tuple of sequences and have two counters (temp and tempMax). first locate the str and then check if it is repeated, if yes count that w the counter and after the whole seq is done(it does happen simultaneously), compare w tempmax, the longest str rep gets stored in your dict/tuple/list and this is the count you'd match to your given sequence.

for genestr in dictsequences:
    ls = len(genestr)
    tempMax = 0
    temp = 0
    for i in realdna:
        # after having counted a sequence it skips at the end of it to avoid counting again
        while temp > 0:
            temp -= 1
            continue

        # if the segment of dna corresponds to the key and there is a repetition of it we start counting
        if realdna[i: i + ls] == genestr:
            while realdna[i - ls: i] == realdna[i: i + ls]:
                temp += 1
                i += 1

            # it compares the value to the previous longest sequence and if it is longer it overrides it
            if temp > tempMax:
                tempMax = temp

    # store the longest sequences in the dictionary using the correspondent key
    dictsequences.genestr == tempMax
``

You must log in to answer this question.

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