0

My code works fine for the small csv file; however, when I get to the big csv file, my counts for the number of consecutive STR are less than the actual value by about 1 or 2.

Here is my code:

import csv
from sys import argv, exit


def main():
    argcheck()
    header = read_header()
    sample = read_sequence()
    repeat = comp_repeats(header, sample)
    check(header, repeat)

def argcheck():
    if len(argv) != 3:
        print("Usage: python program csvfile txtfile")
        exit(1)
    return


def read_header():
    header = list()
    with open(argv[1], "r") as file:
        preview = csv.reader(file)
        header = next(preview)
    return header


def read_sequence():
    sample = ""
    with open(argv[2], "r") as file:
        sample = file.read()
    return sample


def comp_repeats(header, sample):
    repeat = dict()
    for STR in header[1:]:
        length = len(STR)
        index = 0
        final = 0
        if STR not in sample:
            repeat[STR] = final
        else:
            for nucleotide in sample:
                counter = 0
                if nucleotide != STR[0]:
                    index += 1
                    continue
                else:
                    while sample[index:index + length] == STR:
                        counter += 1
                        index += length
                if counter > final:
                    final = counter
                index += 1
        repeat[STR] = final
    return repeat


def check(header, repeat):
    database = dict()
    with open(argv[1], "r") as file:
        database = csv.DictReader(file)
        for row in database:
            for STR in header[1:]:
                if repeat[STR] == int(row[STR]):
                    pass
                else:
                    break
                if STR == header[-1]:
                    print(row["name"])
                    exit(1)
    print("No match")


main()

I tried using debug50 but the amount of time it takes to iterate over the entire string of the longer sequences for every STR is far too great to get to the exact moment/moments to see where my code is going wrong. I'm hoping that there's something blatantly wrong with my code that I'm just missing somehow. I would greatly appreciate any help or feedback on my code!

1 Answer 1

1

Your problem is that the nucleotide loop steps through each letter of the sample, but when you find a match your index variable falls out of sync with the letter you are checking.

I generally prefer not to just write code as an answer, but have a look at this variation of your comp_repeats function to see the difference:

def comp_repeats(header, sample):
    repeat = dict()
    for STR in header[1:]:
        length = len(STR)
        index = 0
        final = 0
        if STR not in sample:
            repeat[STR] = final
        else:
            while index < len(sample):
                if sample[index] == STR[0]:
                    counter = 0
                    while sample[index:index + length] == STR:
                        counter += 1
                        index += length
                    if counter > final:
                        final = counter
                index += 1
        repeat[STR] = final
    return repeat

As an aside, I would say that this is a somewhat traditional way of solving the problem, i.e. a very procedural approach. If you exploit Python's features (writing more Pythonic code) you could solve this more succinctly. Just something to explore if you're interested in Python.

2
  • Wow! This was exactly the problem! Thank you so much! Your side note makes sense to me, as I solved this pset the same way that I would have solved it with C, except it was a lot easier because the language is more high-level I guess. By writing more "Pythonic" code, would that require me to have knowledge of other modules and their functions? Or are there functions in-built into Python that could be exploited for this PSET? Again, thank you so much for your help! Commented Nov 16, 2020 at 4:03
  • It's less about having a large repertoire of Python knowledge (although knowing more never hurts!), but rather learning to approach problems differently. Stack Exchange is a bit clumsy for this, but if you're interested just email me at s*****[email protected] (fill in my username) and I'm more than happy to point you towards some resources.
    – Sentox
    Commented Nov 16, 2020 at 8:32

You must log in to answer this question.

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