title: find and remove common substring with python
body: <p>So I have a two primer sequences:</p>
  
  <pre>
  fiveprime = "GATTCGAAGTCCACTATC"
  threeprime = "TGAGTAGGACGGCACTATC"
  </pre>
  
  <p>What I need to do is when I have another sequence I need to check if it contains a part 
  of one these primer sequences and if it does I need to remove the matching part leaving the non-matching part for further analysis.</p>
  
  <p>For example my sequence: <strong>CACTATC</strong>AAAAAAA
  has a part of the fiveprimer in it. I need to locate this common substring and then remove it leaving only the <code>AAAAA</code>. </p>
  
  <p>The problem I run in to is when you have a sequence like this <strong>CACTATC</strong>GAAG where GAAG is also in the primer, but not part of the primer sequence it's still removed. I tried to fix this by making sure that the found structure is at the left side of the primer and with the threeprimer on the right side so example:</p>
  
  <p>With the <strong>CACTATC</strong>GAAG we have 2 common structures <strong>CACTATC</strong> and <strong>GAAG</strong>
  now I can compare <strong>CACTATC</strong> with the end of the fiveprime GATTCGAAGTC<strong>CACTATC</strong> and tell it's part of the primer when it's a match and then remove it. So when we compare <strong>GAAG</strong> with the length of the last end of the fiveprimer it will give us this GATTCGAAGTCCAC<strong>TATC</strong> which is not a match so <strong>GAAG</strong> can go on for further analysis. </p>
  
  <p>For some reason my script bugs or doesn't work properly. Are there any other solutions to this problem or suggestions?</p>
  
  <pre><code>def longestSubstringFinder(string1, string2):
  answer = ""
  len1, len2 = len(string1), len(string2)
  for i in range(len1):
      match = ""
      for j in range(len2):
          if i + j &lt; len1 and string1[i + j] == string2[j]:
              match += string2[j]
          else:
              if len(match) &gt; len(answer): answer = match
              match = ""
  return answer
  
  def get_sequence(test, fiveprime, threeprime):
      if test == fiveprime:
          pass
      elif test == threeprime:
          pass
      elif test in fiveprime:
          pass
      elif test in threeprime:
          pass
  
          # find out if there is a matching part between the primers and the found
          # single stranded region, then calculates what part that is, and removes it
          # from the single stranded region
      else:
          overlap = longestSubstringFinder(fiveprime, test)
          l = len(overlap)
  
          if fiveprime[-l:] == test[:l]:
              check = test[l:]
          else:
              check = test
  
          overlap2 = longestSubstringFinder(check, threeprime)
          l = len(overlap2)
  
          if threeprime[:l] == check[-l:]:
              check2 = check[:-l]
              structure.append(check2)
          else:
              structure.append(check)
  
  return structure
  </code></pre>