Trypsin digest (cleavage) does not work using regular expression
<p>I have trying to code the theoretical tryptic cleavage of protein sequences in Python. The cleavage rule for trypsin is: after R or K, but not before P. (i.e. the trypsin cleaves (cuts) the protein sequence after each K or R, unless (K or R) is followed by a P).</p>

<p>Example: Cleavage (cut) of the sequence <code>MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK</code> should  result in these 4 sequences (peptides):</p>

<pre><code>MVPPPPSR
GGAAKPGQLGR
SLGPLLLLLRPEEPEDGDR
EICSESK 
</code></pre>

<p>Note that there is no cleavage after K in the second peptide (because P comes after K) and there is no cleavage after R in the third peptide (because P comes after R).</p>

<p>I have written this code in Python, but it doesn't work well. Is there any way to implement this regular expression more meaningfully?</p>

<pre><code>    # Open the file and read it line by line.

    myprotein = open(raw_input('Enter input filename: '),'r')
    if  os.path.exists("trypsin_digest.txt"):
        os.remove("trypsin_digest.txt")
    outfile = open("trypsin_digest.txt",'w+')

    for line in myprotein:
        protein = line.rstrip()
        protein = re.sub('(?&lt;=[RK])(?=[^P])','', protein)

    for peptide in protein:
        outfile.write(peptide)
    print 'results written to:\n', os.getcwd() +'\ trypsin_digest.txt'
</code></pre>

<p>This is how I got  it to work for me</p>

<pre><code>   myprotein = open(raw_input('Enter input filename: '),'r')
   my_protein = []

   for protein in myprotein:
   myprotein = protein.rstrip('\n')
   my_protein.append(myprotein)
   my_pro = (''.join(my_protein))

   #cleaves sequence    
   peptides = re.sub(r'(?&lt;=[RK])(?=[^P])','\n', my_pro)
   print peptides
</code></pre>

<p>Protein Sequence:</p>

<p>MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK</p>

<p>Output(trypsin cleaved sites) or peptides</p>

<p>MVPPPPSR<br>
GGAAKPGQLGR<br>
SLGPLLLLLRPEEPEDGDR<br>
EICSESK<br>
MVPPPPSR<br>
GGAAKPGQLGR<br>
SLGPLLLLLRPEEPEDGDR<br>
EICSESK<br>
MVPPPPSR<br>
GGAAKPGQLGR<br>
SLGPLLLLLRPEEPEDGDR<br>
EICSESK    </p>
