python - 如何使用 mw 字典添加新字符串列表的分子量?

标签 python python-2.7 biopython

我正在尝试用甲酸消化这个字符串,但我正在尝试计算消化后得到的每个片段,我只想知道如何将字典的值添加到我的新列表集中. (任何建议将不胜感激)

import string

aa_seq = 'MLCPWNFLLKPRYRGKYEPGSSPAADLNNNEKGIGNEKSLVNGHIPNCETINPhSKSFP'

formic_acid = aa_seq.replace('A', 'A|').replace('N', 'N|').upper().split('|')

formate = list(formic_acid)

weights = {'A': 71.04, 'C': 103.01, 'D': 115.03, 'E': 129.04, 'F': 147.07,
           'G': 57.02, 'H': 137.06, 'I': 113.08, 'K': 128.09, 'L': 113.08,
           'M': 131.04, 'N': 114.04, 'P': 97.05, 'Q': 128.06, 'R': 156.10,
           'S': 87.03, 'T': 101.05, 'V': 99.07, 'W': 186.08, 'Y': 163.06 }
weight = []

for acid in formate:
    weight = weight + weights[acid]
print "The molecular weight of this protein is", weight

输出:

Traceback (most recent call last):
  File "r.py", line 15, in <module>
    weight = weight + weights[acid]
KeyError: 'MLCPWN'

最佳答案

如果你想要权重的总和,得到酸中每个字母的总和:

for acid in formate:
    weight = sum(weights[a] for a in acid)
print "The molecular weight of this protein is", weight

如果你想要列表中的重量和酸:

weight_list = []
for acid in formate:
    weight = sum(weights[a] for a in acid)
    weight_list.append((acid,weight))
    print "The molecular weight of this protein is", weight



aa_seq = 'MLCPWNFLLKPRYRGKYEPGSSPAADLNNNEKGIGNEKSLVNGHIPNCETINPhSKSFP'

formic_acid = aa_seq.replace('A', 'A|').replace('N', 'N|').upper().split('|')

weights = {'A': 71.04, 'C': 103.01, 'D': 115.03, 'E': 129.04, 'F': 147.07,
           'G': 57.02, 'H': 137.06, 'I': 113.08, 'K': 128.09, 'L': 113.08,
           'M': 131.04, 'N': 114.04, 'P': 97.05, 'Q': 128.06, 'R': 156.10,
           'S': 87.03, 'T': 101.05, 'V': 99.07, 'W': 186.08, 'Y': 163.06 }

weight_list = []
for acid in formic_acid:
    weight = sum(weights[a] for a in acid)
    weight_list.append((acid,weight))
    print "The molecular weight of the protein {} is {}".format(acid,weight)
print(weight_list)

The molecular weight of the protein MLCPWN is 744.3
The molecular weight of the protein FLLKPRYRGKYEPGSSPA is 2047.06
The molecular weight of the protein A is 71.04
The molecular weight of the protein DLN is 342.15
The molecular weight of the protein N is 114.04
The molecular weight of the protein N is 114.04
The molecular weight of the protein EKGIGN is 598.29
The molecular weight of the protein EKSLVN is 670.35
The molecular weight of the protein GHIPN is 518.25
The molecular weight of the protein CETIN is 560.22
The molecular weight of the protein PHSKSFP is 780.38
[('MLCPWN', 744.3), ('FLLKPRYRGKYEPGSSPA', 2047.0599999999995), ('A', 71.04), ('DLN', 342.15000000000003), ('N', 114.04), ('N', 114.04), ('EKGIGN', 598.29), ('EKSLVN', 670.3499999999999), ('GHIPN', 518.25), ('CETIN', 560.22), ('PHSKSFP', 780.3799999999999)]

要获得最小值和最大值,只需跟踪整个循环:

mx = None
mn = None
for acid in formic_acid:
    weight = sum(weights[a] for a in acid)
    if mx is None or weight > mx:
        mx = weight
    if mn is None or weight < mn:
        mn = weight
    weight_list.append((acid,weight))
    print "The molecular weight of the protein {} is {}".format(acid,weight)
print("The minimum and maximum weights are:  {}, {}".format(mn,mx))

关于python - 如何使用 mw 字典添加新字符串列表的分子量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27361877/

相关文章:

python - 我可以使用以并行方式读取文件的迭代器吗?

python - 为什么语法无效(<string>,第 24 行)?

python - 代码中的 AWS 授权 - {"message": "The security token included in the request is invalid." }

python - 使用 PIL 裁剪图像时如何设置坐标?

python - 为什么Python不调用模块中的函数?

Python Pandas "Error: Could not install packages due to an OSError: No such file or directory:"

string - python 修改内置字符串函数

python - 如何改进这个python矢量函数

python - 运行setup.py后我可以扔掉源代码吗?

python - "NotImplementedError: SeqRecord"在使用 SeqIO 解析的 fasta 文件上使用排序时