Pepsin digest (cleavage) does not work using RE?

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
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?

• 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 Mar 18 '18 at 22:07

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:

• 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 Mar 19 '18 at 2:43
• 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 Mar 19 '18 at 2:46

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?

• In addition if I manually add some space after the sequence it will get the right result. – Nadecho Holme Attaveesin Mar 19 '18 at 5:49
• 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 Mar 19 '18 at 8:59
• 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 Mar 19 '18 at 14:29
• Ah, sorry, make that a ? for "0 or 1", instead of a * for "0 or more" – terdon Mar 19 '18 at 14:49
• 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 Mar 19 '18 at 15:09