title: DNA Motif Enumeration with Try/Except and Loops - Python3
body: <p>I've been struggling with a particular bioinformatics problem for the last few days, and I was wondering if somebody could either find the fault in my logic or fault in my code (or both).
  The function is supposed to find all k-mers with a Hamming distance of at most d from all of the strings of DNA. 
  This is what I'm trying to do:</p>
  
  <ol>
  <li><p>Start with an iteration of all possible k-mers and compare them to each string </p></li>
  <li><p>This implies I need another loop that goes through the strings of DNA as well</p></li>
  <li><p>I make a while loop for <code>c+k &lt;= len(DNA[0])-1</code>. <code>c+k</code> is my window of size <code>k</code>, and I want to find at least one window in each string of DNA where my combo has a Hamming distance from that string equal to or less than an arbitrary <code>d</code>. If the Hamming distance meets the criterion, then the while loop breaks,     allowing the next string to be compared. If it doesn't, then the window is changed, and if <code>c+k==len(DNA[0])-1</code>, and the Hamming distance still doesn't meet the criterion, I create a name error <code>int(a)</code> and the exception kills the <code>inner_loop</code>.</p></li>
  </ol>
  
  <p>However, my function returns nothing except <code>set()</code>, which I don't understand.</p>
  
  <pre><code>import itertools
  def combination(k):
      bases=['A','T','G','C']
      combo=[''.join(p) for p in itertools.product(bases, repeat=k)]
      return combo
  
  def hammingDistance(Pattern, seq):
          if Pattern == seq:
                 return 0
          else:
                  dist=0
                  for i in range(len(seq)):
                          if Pattern[i] != seq[i]:
                                  dist += 1
          return dist
  
  def motif_enumeration(k, d, DNA):
      combos = combination(k)
      global pattern
      for combo in combos:
          try:
              inner_loop(k, d, DNA, combo)
          except:
              continue
  
      return set(pattern)
  
  def inner_loop(k, d, DNA, combo):
      global pattern
      for strings in DNA:
          inner_loop_two(k, d, DNA, combo, strings)
  
  
  def inner_loop_two(k, d, DNA, combo, strings):
      global pattern
      c=0
      while c+k &lt; len(DNA[0]):
          print(combo, strings[c:c+k], hammingDistance(combo, strings[c:c+k]))
          if d &gt;= hammingDistance(combo, strings[c:c+k]) and strings == DNA[len(DNA)-1]:
              #if we've reached the last string and the condition is met,
              #that means that the combo is suitable for each string of DNA
              pattern += [combo]
          elif d &gt;= hammingDistance(combo, strings[c:c+k]):
              #condition is met for one string, now move onto next
              break
          elif d &lt; hammingDistance(combo, strings[c:c+k]) and c+k == len(DNA[0])-1:
              #Name error causes this inner loop two to crash, thus causing the first inner loop
              #to pass
              int(a)
          elif d &lt; hammingDistance(combo, strings[c:c+k]):
              #change the window to see if the combo is valid later in the string
              c += 1
  
  
  pattern = []
  DNA=['ATTTGGC', 'TGCCTTA', 'CGGTATC', 'GAAAATT']
  print(motif_enumeration(3,1,DNA))
  print(pattern)
  </code></pre>
  
  <p>I thought that since my second inner loop crashed, this would cause my first inner loop to pass, and then another combo in <code>motif_enumeration</code> would be tested, but the first conditional in my <code>inner_loop_two</code> never prints anything. I noticed also that when the inner loop crashes and <code>motif_enumeration</code> continues, it continues for both the outer and the inner loop. Here's an example of what I mean...</p>
  
  <pre><code>AAA ATT 2
  AAA TTT 3
  AAA TTG 3
  AAA TGG 3
  AAT ATT 1
  AAT TGC 3
  AAT GCC 3
  AAT CCT 2
  AAT CTT 2
  AAG ATT 2
  AAG TTT 3
  AAG TTG 2
  AAG TGG 2
  AAC ATT 2
  AAC TTT 3
  AAC TTG 3
  AAC TGG 3
  ATA ATT 1 etc...
  </code></pre>
  
  <p>My expected output is <code>pattern=[ATA, ATT, GTT, TTT]</code></p>