我试图在Python中对蛋白质序列的理论性胰蛋白酶切割进行编码。胰蛋白酶的切割规则是:在R或K之后,但不在P之前(即胰蛋白酶在每个K或R之后切割(切割)蛋白质序列,除非(K或R)之后是P)。
示例:序列MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
的切割(切割)应该导致这4个序列(肽):
MVPPPPSR
GGAAKPGQLGR
SLGPLLLLLRPEEPEDGDR
EICSESK
注意第二个肽中K后没有切割(因为P来自K后),第三个肽中的R后没有切割(因为P来自R后)。
我用Python编写了这段代码,但效果不好。有没有办法更有意义地实现这个正则表达式?
# 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('(?<=[RK])(?=[^P])','', protein)
for peptide in protein:
outfile.write(peptide)
print 'results written to:\n', os.getcwd() +'\ trypsin_digest.txt'
这就是我让它为我工作的方式
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'(?<=[RK])(?=[^P])','\n', my_pro)
print peptides
蛋白质序列:
MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
输出(胰蛋白酶切割位点)或肽
MVPPPPSR
GGAAKPGQLGR
SLGPLLLLLRPEEPEDGDR
EICSESK
MVPPPPSR
GGAAKPGQLGR
SLGPLLLLLRPEEPEDGDR
EICSESK
MVPPPPSR
GGAAKPGQLGR
SLGPLLLLLRPEEPEDGDR
EICSESK
答案 0 :(得分:5)
正则表达式很好,但这是一个使用常规python的解决方案。既然你是 寻找基地的后续序列,将其构建为发电机是有意义的, 产生碎片。
example = 'MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK'
def trypsin(bases):
sub = ''
while bases:
k, r = bases.find('K'), bases.find('R')
cut = min(k, r)+1 if k > 0 and r > 0 else max(k, r)+1
sub += bases[:cut]
bases = bases[cut:]
if not bases or bases[0] != 'P':
yield sub
sub = ''
print list(trypsin(example))
答案 1 :(得分:3)
编辑稍作修改,您的正则表达式就可以正常运行:
在您的评论中,您提到文件中有多个序列(我们称之为 sequences.dat ):
$ cat sequences.dat
MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
>>> with open('sequences.dat') as f:
s = f.read()
>>> print(s)
MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
MVPPPPSRGGAAKPGQLGRSLGPLLLLLRPEEPEDGDREICSESK
>>> protein = re.sub(r'(?<=[RK])(?=[^P])','\n', s, re.DOTALL)
>>> protein.split()
['MVPPPPSR', 'GGAAKPGQLGR', 'SLGPLLLLLRPEEPEDGDR', 'EICSESK', 'MVPPPPSR', 'GGAAKPGQLGR', 'SLGPLLLLLRPEEPEDGDR', 'EICSESK', 'MVPPPPSR', 'GGAAKPGQLGR', 'SLGPLLLLLRPEEPEDGDR', 'EICSESK']
>>> print protein
MVPPPPSR
GGAAKPGQLGR
SLGPLLLLLRPEEPEDGDR
EICSESK
MVPPPPSR
GGAAKPGQLGR
SLGPLLLLLRPEEPEDGDR
EICSESK
MVPPPPSR
GGAAKPGQLGR
SLGPLLLLLRPEEPEDGDR
EICSESK
答案 2 :(得分:2)
我相信以下正则表达式将如您所述:
([KR]?[^P].*?[KR](?!P))
的结果
>>> regex = re.compile("([KR]?[^P].*?[KR](?!P))")
>>> r = regex.search(string)
>>> r
<_sre.SRE_Match object at 0xb1a9f49eb4111980>
>>> regex.match(string)
<_sre.SRE_Match object at 0xb1a9f49eb4102980>
# List the groups found
>>> r.groups()
(u'MVPPPPSR',)
# List the named dictionary objects found
>>> r.groupdict()
{}
# Run findall
>>> regex.findall(string)
[u'MVPPPPSR', u'GGAAKPGQLGR', u'SLGPLLLLLRPEEPEDGDR', u'EICSESK']