我正在制作肽单同位素计算器,但有一些无法解决的问题。
import re
aminoacid = {
"I": "C6H13NO2",
"L": "C6H13NO2",
"K": "C6H14N2O2",
"M": "C5H11NO2S",
"F": "C9H11NO2",
"T": "C4H9NO3",
"W": "C11H12N2O2",
"V": "C5H11NO2",
"R": "C6H14N4O2",
"H": "C6H9N3O2",
"A": "C3H7NO2",
"N": "C4H8N2O3",
"D": "C4H7NO4",
"C": "C3H7NO2S",
"E": "C5H9NO4",
"Q": "C5H10N2O3",
"G": "C2H5NO2",
"P": "C5H9NO2",
"S": "C3H7NO3",
"Y": "C9H11NO3"
}
acid = input('Sequence: ')
s = re.findall('([A-Z][a-z]?)([0-9]*)', formula)
monoisotopic = {'S': 31.972, 'C': 12.0000, 'H': 1.0078, 'O': 15.9949, 'N': 14.0031}
weight = 0
for elem, count in s:
#For singular elements
if count=='':
count = 1
weight = weight + int(count) * monoisotopic[elem]
print('[M + H]+ : ', weight-(len(acid)-1)*18.0105+1.0078)
print('# of amino acids : ', len(acid))
如果我输入单个氨基酸,如“A”,效果很好。但是当我输入“AILWNG”之类的序列时,程序无法识别它。我怎样才能让这个程序识别两个或多个氨基酸?
如果我们将程序分成单独的函数,可能会有所帮助。首先,我们定义氨基酸和(相关)原子的原子量:
aminoacid = {
'I': 'C6H13NO2',
'L': 'C6H13NO2',
'K': 'C6H14N2O2',
'M': 'C5H11NO2S',
'F': 'C9H11NO2',
'T': 'C4H9NO3',
'W': 'C11H12N2O2',
'V': 'C5H11NO2',
'R': 'C6H14N4O2',
'H': 'C6H9N3O2',
'A': 'C3H7NO2',
'N': 'C4H8N2O3',
'D': 'C4H7NO4',
'C': 'C3H7NO2S',
'E': 'C5H9NO4',
'Q': 'C5H10N2O3',
'G': 'C2H5NO2',
'P': 'C5H9NO2',
'S': 'C3H7NO3',
'Y': 'C9H11NO3'
}
monoisotopic = {
'S': 31.972,
'C': 12.0000,
'H': 1.0078,
'O': 15.9949,
'N': 14.0031
}
然后可以定义一个函数来计算氨基酸(或任何其他原子)的重量:
from re import findall as refindall
def molecular_weight(molecule):
return sum(
monoisotopic[atom] * int(num or '1')
for atom, num in refindall(r'([A-Z][a-z]*)(\d*)', molecule)
)
然后我们可以总结氨基酸的分子量,例如:
def proteine_mass(proteine):
return sum(molecular_weight(aminoacid[char]) for char in proteine)
例如:
>>> proteine_mass('WATER')
733.3594
>>> proteine_mass('AILWNG')
762.4109000000001
(当然这不是H2O,可能上面的蛋白质不存在)
我是一名优秀的程序员,十分优秀!