title: Finding longest perfect match between two strings
body: <p>I am trying to write a script that will perform two functions, when provided with two strings:</p>
  
  <p><strong>1</strong>.  Find the longest sequence of characters starting from <code>pos[0]</code> that is the same in both strings</p>
  
  <pre><code>Seq1 = 'ATCCTTAGC'
  Seq2 = 'ATCCAGCAATTC'
          ^^^^ Match from pos[0] to pos[3]
  Pos: 0:3
  Length: 4
  Seq: ATCC
  </code></pre>
  
  <p><strong>2</strong>.  Find the longest run of characters that exists in both strings</p>
  
  <pre><code>Seq1 = 'TAGCTCCTTAGC' # Contains 'TCCTT'
  Seq2 = 'GCAGCCATCCTTA' # Contains 'TCCTT'
          ^ No match at pos[0]
  Pos1: 4:8
  Pos2  7:11
  Length: 5
  Seq: TCCTT
  </code></pre>
  
  <p>To accomplish problem 1, I have the following:</p>
  
  <pre><code>#!/usr/bin/python
  
  upstream_seq = 'ATACATTGGCCTTGGCTTAGACTTAGATCTAGACCTGAAAATAACCTGCCGAAAAGACCCGCCCGACTGTTAATACTTTACGCGAGGCTCACCTTTTTGTTGTGCTCCC'
  downstream_seq = 'ATACACGAAAAGCGTTCTTTTTTTGCCACTTTTTTTTTATGTTTCAAAACGGAAAATGTCGCCGTCGTCGGGAGAGTGCCTCCTCTTAGTTTATCAAATAAAGCTTTCG'
  
  print("Upstream:   %s\nDownstream: %s\n") % (upstream_seq, downstream_seq)
  
  mh = 0
  pos_count = 0
  seq = ""
  position =""
  longest_hom=""
  for i in range(len(upstream_seq)):
      pos_count += 1
      if upstream_seq[i] == downstream_seq[i]:
          mh += 1
          seq += upstream_seq[i]
          position = pos_count
          longest_hom = mh
  
      else:
          mh = 0
          break
  
  print("Pos: 0:%s\nLength: %s\nSeq: %s\n") % (position , longest_hom, seq)
  </code></pre>
  
  <hr>
  
  <pre><code>Upstream:   ATACATTGGCCTTGGCTTAGACTTAGATCTAGACCTGAAAATAACCTGCCGAAAAGACCCGCCCGACTGTTAATACTTTACGCGAGGCTCACCTTTTTGTTGTGCTCCC
  Downstream: ATACACGAAAAGCGTTCTTTTTTTGCCACTTTTTTTTTATGTTTCAAAACGGAAAATGTCGCCGTCGTCGGGAGAGTGCCTCCTCTTAGTTTATCAAATAAAGCTTTCG
  
  Pos: 0:5
  Length: 5
  Seq: ATACA
  </code></pre>
  
  <hr>
  
  <p>I'm having trouble with Problem <strong>2</strong>. So far, I've considered an alignment between the two sequences using <a href="http://biopython.org/DIST/docs/api/Bio.pairwise2-module.html" rel="nofollow noreferrer">BioPython's pairwise2</a>. However, in this case, I only want perfect matches (no gaps, no extensions), and I only want to see the longest sequence, not a consensus which is what I appear to get:</p>
  
  <pre><code>from Bio import pairwise2 as pw2
  
  global_align = pw2.align.globalms(upstream_seq, downstream_seq, 3, -1, -.5, -.5)
  
  print(global_align[0])
  </code></pre>
  
  <hr>
  
  <pre><code>('ATACATT-G----GCC-TTGGCTTA-----G--ACTTAGATCTAG-----ACCTGAA----AATAACCTGCCGAAAA-GACC-CGCCCGACTGTTAATACTT-TACGCG-AG-GCT-CAC-C-T-TT--TTGT-TG----T---GCTCC--C-', 'ATACA--CGAAAAG-CGTT--CTT-TTTTTGCCACTT---T-T--TTTTTA--TG--TTTCAA-AA-C-G--GAAAATG---TCG--C--C-G----T-C--GT-CG-GGAGAG-TGC-CTCCTCTTAGTT-TAT-CAAATAAAGCT--TTCG', 151.0, 0, 153)
  </code></pre>
  
  <hr>
  
  <p><strong>Question</strong>: How can I find the longest run of characters that exists in both strings? </p>