<p>Ok I think I almost solve this problem so I still using RE </p>

<pre><code>import re
myprotein = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
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;=[^HRK][^P][^R])(?=[FLWY][^P])|(?&lt;=[^HRK][^P][FLWY])(?=.[^P])','\n', my_pro)

print (peptides)
</code></pre>

<p>and it produce to </p>

<pre><code>TTG
W
M
F
KG
LY
Q
L
KSDEARMHN
LQRW
YV
F
KVIE
F
W
GINRKDQHDSRY
WF
SH
F
CW
K
L
EHGMVPKC
FNE
L
LMPPMSTHRSMF
NA
Y
SMS
F
CVQVKE
WRGSISRMCDHHQQQCQ
Y
W
RL
MAGGT
LI
</code></pre>

<p>but the true answer should be like (checking from <a href="https://web.expasy.org/peptide_cutter/" rel="nofollow noreferrer">https://web.expasy.org/peptide_cutter/</a> )</p>

<pre><code>TTG
W
M
F
KG
LY
Q
L
KSDEARMHN
LQRW
YV
F
KVIE
F
W
GINRKDQHDSRY
WF
SH
F
CW
K
L
EHGMVPKC
FNE
L
LMPPMSTHRSMF
NA
Y
SMS
F
CVQVKE
WRGSISRMCDHHQQQCQ
Y
W
RL
MAGGT
L
I 
</code></pre>

<p>so how can I make this work?</p>

CC BY-SA 3.0
2018-03-19T05:49:23.370Z
In addition if I manually add some space after the sequence  it will get the right result.
Nadecho Holme Attaveesin
CC BY-SA 3.0
2018-03-19T08:59:00.843Z
You don't need the space, just add a `*` to the last `[^P]`: `[^P]*` so it matches "0 or 1 not P". But don't you need a P at position 1 or 2?
terdon
CC BY-SA 3.0
2018-03-19T14:29:52.650Z
Oh thanks! I found that it is not necessary for P at position 1 or 2 so it's okay for no  P at position 1 or 2 :)
Nadecho Holme Attaveesin
CC BY-SA 3.0
2018-03-19T14:49:31.523Z
Ah, sorry, make that a `?` for "0 or 1", instead of a `*` for "0 or more"
terdon
CC BY-SA 3.0
2018-03-19T15:09:08.037Z
thank you kindly oh I have a few more question. so if I want to make it to loop that cleavage more than one sequence till the last sequence and every sequence start with ">" above (fasta format) and I want result that separate each sequence how can I make it work.
Nadecho Holme Attaveesin
CC BY-SA 3.0
2018-03-19T15:10:34.530Z
fasta format example:
>sequence1 
ATGCGGAGCTTAGATTCTCGAGATCTCGATATCGCGCTTATAAAAGGCCCGGATTAGGGC
TAGCTAGATATCGCGATAGCTAGGGATATCGAGATGCGATACG
>sequence2 
GTACTCGATACGCTACGCGATATTGCGCGATACGCATAGCTAACGATCGACTAGTGATGC
ATAGAGCTAGATCAGCTACGATAGCATCGATCGACTACGATCAGCATCAC
Nadecho Holme Attaveesin
CC BY-SA 3.0
2018-03-19T03:08:22.373Z
2018-03-19T03:08:22.373Z
Nadecho Holme Attaveesin
<p>I am really not sure if I've understood your rules correctly, but I think you want this:</p>

<ul>
<li><p>If all of these are true:</p>

<ul>
<li>position 2: not R, K or H</li>
<li>position 3: not P</li>
<li>position 4: not R</li>
<li>position 7: not P</li>
</ul></li>
<li><p>And, if at least one of these is true:</p>

<ul>
<li>position 1: P</li>
<li>position 1: P</li>
<li>position 4: L or W or Y or F</li>
<li>position 5: L or W or Y or F</li>
</ul></li>
</ul>

<p>Then cleave.</p>

<p>If so, then you probably don't want to do it using regular expressions. First because it will be hard, and second because it will be absolutely awful to maintain if you need to come back to it. Instead, try something like this:</p>



<pre><code>#!/usr/bin/env python3
# X = input(); 
X = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
seq = list(X)
for i, res in enumerate(seq):
    if i &gt; len(seq) - 7:
        break
    if (
            seq[i+1] in ('R','K','H') or
            seq[i+2] == 'P' or
            seq[i+3] == 'R' or
            seq[i+6] == 'P'
    ):
        continue
    if res == 'P':
        start = i
    elif seq[i+1] == 'P':
        start = i+1
    else:
        continue
    if (
            seq[i+4] in ('L', 'W', 'Y', 'F') or
            seq[i+5] in ('L', 'W', 'Y', 'F')
    ):
        print("%s\n%s\n%s" %
            ("".join(seq[0:start]),
             "".join(seq[start:start+7]),
             "".join(seq[start+7:])
            )
        )
</code></pre>

<p>This will produce the following output when run:</p>

<pre><code>$ foo.py
TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMV
PKCFNEL
LMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI
</code></pre>

<hr>

<p>If you insist on using regular expressions, you could separate it into two expressions and use something like this:</p>

<pre><code>import re
X = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
re1='P[^RKH][^P][^R][LWYF].[^P]'
re2='P[^P][^R][LWYF].[^P]'
r=re.findall('(?:P[^RKH][^P][^R][LWYF].[^P])|(?:P[^P][^R][LWYF].[^P])',X) 
print(r)
</code></pre>

<p>Before you do, however, please consider the wise words of <a href="https://www.xkcd.com/1171/" rel="nofollow noreferrer">XKCD 208</a>:</p>

<p><a href="https://i.sstatic.net/lIrt1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/lIrt1.png" alt="XKCD 1171"></a></p>

CC BY-SA 3.0
2018-03-19T02:43:11.927Z
thank for your reply but Okay I confuse with the rule so it should be like in this follow 2 pattern 
pattern1
position 2: not R, K or H
position 3: not P
position 4: not R
position 5: F or L or W or Y
position 6: not P

pattern2
position 2: not R, K or H
position 3: not P
position 4: F or L or W or Y
position 6: not P
Nadecho Holme Attaveesin
CC BY-SA 3.0
2018-03-19T02:46:33.307Z
I just made the pattern1 but still no idea how to combine two of these pattern 
import re
myprotein = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
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'(?<=[^HRK][^P][^R])(?=[FLWY][^P])','\n', my_pro)
print (peptides)

It's must be great if you could help.
Nadecho Holme Attaveesin
CC BY-SA 3.0
2018-03-18T22:59:37.990Z
2018-03-18T23:22:13.213Z
2018-03-18T23:22:13.213Z
terdon
<p>Aim is to code the theoretical peptic cleavage of protein sequences in Python. The cleavage (cutting) rule for pepsin is: <code>1234^567</code>. This is a position, <code>^</code> stands for the cleavage point. </p>

<p>There are rules that it mustn't have at all and rules where it must have at least one. The rule for mustn't is in position 2, it mustn't be R, K or H. In position 3 and 7, there can't be a P and in position 4, there mustn't be an R. </p>

<p>If it follows "the must not" rules, then it will check "the must" rules so that if any of one them is met, it will cleave. So if there is a P in position 1 or 2, or if there's an L, W, Y or F in position 4 or 5 then, it will be cleaved (use newline).</p>

<p>This is the code I have so far, but it doesn't work yet:</p>

<pre><code>import re
print('INSERT YOUR PEPTIDE') 
X= input(); 
r=re.findall('(?&lt;=[P])(?&lt;=[P,^R,^K,^H])(?&lt;=([^P])(?&lt;=[L,W,F,Y,^R])(?&lt;=[L,W,Y,F])/D(?&lt;=[^P])',X) 
print(r)
</code></pre>

<p>My input is:</p>

<pre><code>TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI 
</code></pre>

<p>How can I make this work?</p>

CC BY-SA 3.0
2018-03-18T22:07:16.530Z
I tried to clarify your question, please check and make sure I didn't change anything (it was very hard to understand). It would be much easier for us to help you if you could give some examples of input matching different rules and the output you expect from each of them.
terdon
<python>
Pepsin digest (cleavage) does not work using RE?
# Pepsin digest (cleavage) does not work using RE?

(Asked by: username_0 on 2018-03-18)

Aim is to code the theoretical peptic cleavage of protein sequences in Python. The cleavage (cutting) rule for pepsin is: 1234^567. This is a position, ^ stands for the cleavage point. 
There are rules that it mustn't have at all and rules where it must have at least one. The rule for mustn't is in position 2, it mustn't be R, K or H. In position 3 and 7, there can't be a P and in position 4, there mustn't be an R. 
If it follows "the must not" rules, then it will check "the must" rules so that if any of one them is met, it will cleave. So if there is a P in position 1 or 2, or if there's an L, W, Y or F in position 4 or 5 then, it will be cleaved (use newline).
This is the code I have so far, but it doesn't work yet:
import re
print('INSERT YOUR PEPTIDE') 
X= input(); 
r=re.findall('(?<=[P])(?<=[P,^R,^K,^H])(?<=([^P])(?<=[L,W,F,Y,^R])(?<=[L,W,Y,F])/D(?<=[^P])',X) 
print(r)

My input is:
TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI 

How can I make this work?

**Tags:** python
> **Comments:**
> **username_1**: I tried to clarify your question, please check and make sure I didn't change anything (it was very hard to understand). It would be much easier for us to help you if you could give some examples of input matching different rules and the output you expect from each of them.  

---

## 2 answers

### Answer by username_0 on 2018-03-19. Score: 2

Ok I think I almost solve this problem so I still using RE 
import re
myprotein = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
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'(?<=[^HRK][^P][^R])(?=[FLWY][^P])|(?<=[^HRK][^P][FLWY])(?=.[^P])','\n', my_pro)

print (peptides)

and it produce to 
TTG
W
M
F
KG
LY
Q
L
KSDEARMHN
LQRW
YV
F
KVIE
F
W
GINRKDQHDSRY
WF
SH
F
CW
K
L
EHGMVPKC
FNE
L
LMPPMSTHRSMF
NA
Y
SMS
F
CVQVKE
WRGSISRMCDHHQQQCQ
Y
W
RL
MAGGT
LI

but the true answer should be like (checking from https://web.expasy.org/peptide_cutter/ )
TTG
W
M
F
KG
LY
Q
L
KSDEARMHN
LQRW
YV
F
KVIE
F
W
GINRKDQHDSRY
WF
SH
F
CW
K
L
EHGMVPKC
FNE
L
LMPPMSTHRSMF
NA
Y
SMS
F
CVQVKE
WRGSISRMCDHHQQQCQ
Y
W
RL
MAGGT
L
I 

so how can I make this work?

> **Comments:**
> **username_0**: In addition if I manually add some space after the sequence  it will get the right result.  
> **username_1**: You don't need the space, just add a `*` to the last `[^P]`: `[^P]*` so it matches "0 or 1 not P". But don't you need a P at position 1 or 2?  
> **username_0**: Oh thanks! I found that it is not necessary for P at position 1 or 2 so it's okay for no  P at position 1 or 2 :)  
> **username_1**: Ah, sorry, make that a `?` for "0 or 1", instead of a `*` for "0 or more"  
> **username_0**: thank you kindly oh I have a few more question. so if I want to make it to loop that cleavage more than one sequence till the last sequence and every sequence start with ">" above (fasta format) and I want result that separate each sequence how can I make it work.  
> **username_0**: fasta format example: >sequence1  ATGCGGAGCTTAGATTCTCGAGATCTCGATATCGCGCTTATAAAAGGCCCGGATTAGGGC TAGCTAGATATCGCGATAGCTAGGGATATCGAGATGCGATACG >sequence2  GTACTCGATACGCTACGCGATATTGCGCGATACGCATAGCTAACGATCGACTAGTGATGC ATAGAGCTAGATCAGCTACGATAGCATCGATCGACTACGATCAGCATCAC  

---

### Answer by username_1 on 2018-03-18. Score: 2

I am really not sure if I've understood your rules correctly, but I think you want this:

If all of these are true:

position 2: not R, K or H
position 3: not P
position 4: not R
position 7: not P

And, if at least one of these is true:

position 1: P
position 1: P
position 4: L or W or Y or F
position 5: L or W or Y or F


Then cleave.
If so, then you probably don't want to do it using regular expressions. First because it will be hard, and second because it will be absolutely awful to maintain if you need to come back to it. Instead, try something like this:
#!/usr/bin/env python3
# X = input(); 
X = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
seq = list(X)
for i, res in enumerate(seq):
    if i > len(seq) - 7:
        break
    if (
            seq[i+1] in ('R','K','H') or
            seq[i+2] == 'P' or
            seq[i+3] == 'R' or
            seq[i+6] == 'P'
    ):
        continue
    if res == 'P':
        start = i
    elif seq[i+1] == 'P':
        start = i+1
    else:
        continue
    if (
            seq[i+4] in ('L', 'W', 'Y', 'F') or
            seq[i+5] in ('L', 'W', 'Y', 'F')
    ):
        print("%s\n%s\n%s" %
            ("".join(seq[0:start]),
             "".join(seq[start:start+7]),
             "".join(seq[start+7:])
            )
        )

This will produce the following output when run:
$ foo.py
TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMV
PKCFNEL
LMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI


If you insist on using regular expressions, you could separate it into two expressions and use something like this:
import re
X = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
re1='P[^RKH][^P][^R][LWYF].[^P]'
re2='P[^P][^R][LWYF].[^P]'
r=re.findall('(?:P[^RKH][^P][^R][LWYF].[^P])|(?:P[^P][^R][LWYF].[^P])',X) 
print(r)

Before you do, however, please consider the wise words of XKCD 208:

> **Comments:**
> **username_0**: thank for your reply but Okay I confuse with the rule so it should be like in this follow 2 pattern  pattern1 position 2: not R, K or H position 3: not P position 4: not R position 5: F or L or W or Y position 6: not P  pattern2 position 2: not R, K or H position 3: not P position 4: F or L or W or Y position 6: not P  
> **username_0**: I just made the pattern1 but still no idea how to combine two of these pattern  import re myprotein = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI" 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'(?<=[^HRK][^P][^R])(?=[FLWY][^P])','\n', my_pro) print (peptides)  It's must be great if you could help.  

---

