Search code examples
python-2.7for-loopnestedbioinformaticsdna-sequence

Python: nested for loop - name not defined


first of all, I must point out that i'm not a programmer, so this is probably a stupid question, but i'd like to understand what's going on here.

The program should go through a string (genome), and slide a window of arbitrary length ('l' in this case). It searches for repeating sequences of characters of a given length (k) and notes the number of occurrences of a sequence. I did manage to find repeated sequences in the whole string, but the said window is troubling me. I tried using a nested loop:

for i in range(len(genome) - k + 1):
    for c in range(len(genome))[c:c+l]:
        kmer = genome[i:i+k]
        if kmer in d:
            d[kmer] += 1
        else:
            d[kmer] = 1

I get an error: "NameError: name 'c' is not defined" What is the cause of this problem, and is there an easy to comprehend way of fixing it? Efficiency is not really important, so i'd like to keep a similar structure (i found a lot of topics describing ways to avoid using nested for loop, but i find it quite confusing presently).

Thank you in advance.


Solution

  • You are defining c in the second for loop, and trying to use it in the same statement. Thus, c is not defined until you begin the for loop, so is not defined.

    Edit

    Judging by your comments, I believe what you are trying to do is slide a window of length l along a genome. Then you want to find the window that is enriched for some k-mer(s). To do that, I would modify your second loop to look at the next l locations from the current window start:

    for c in range(i, i+l):