python - 用字典实现算法

标签 python algorithm python-3.x

我必须实现以下算法:equation其中“i”包含 R、K、H 和 Nterm,“j”包含 D、E、C、Y 和返回净电荷的正 Cterm。另一种方法使用该方法来计算 pI,pI 基本上是给出最低电荷的 pH 值。

这是我目前所拥有的:

class ProteinParam :


    aa2mw = {
        'A': 89.093,  'G': 75.067,  'M': 149.211, 'S': 105.093, 'C': 121.158,
        'H': 155.155, 'N': 132.118, 'T': 119.119, 'D': 133.103, 'I': 131.173,
        'P': 115.131, 'V': 117.146, 'E': 147.129, 'K': 146.188, 'Q': 146.145,
        'W': 204.225,  'F': 165.189, 'L': 131.173, 'R': 174.201, 'Y': 181.189
        }
    mwH2O = 18.015
    aa2abs280= {'Y':1490, 'W': 5500, 'C': 125}
    aacomp = {}
    aa2chargePos = {'K': 10.5, 'R':12.4, 'H':6}
    aa2chargeNeg = {'D': 3.86, 'E': 4.25, 'C': 8.33, 'Y': 10}
    aaNterm = 9.69
    aaCterm = 2.34


    def __init__ (self, protein):
        l = ''.join(protein).split()
        l = ''.join(l).upper()
        clean_prot = ""
        for aa in l:
            if aa in ProteinParam.aa2mw:
                clean_prot += aa
            else:
                pass
        self.protString = clean_prot
        for i in ProteinParam.aa2mw:
            ProteinParam.aacomp[i] = 0
        for aa in self.protString:
            if aa in ProteinParam.aacomp:
                ProteinParam.aacomp[aa] += 1

    def aaCount (self):
        return(len(self.protString))

    def pI (self):
        best_charge = 100000000
        for test_pH in range (14000):
            test_pH += 1
            test_pH = (test_pH / 100)
            new_charge = self.charge(test_pH)
            if new_charge < best_charge:
                best_charge = new_charge
        return best_charge

    def aaComposition (self) :
        return ProteinParam.aacomp

    def charge (self, pH):
        self.pH = pH
        negative = 0
        positive = 0
        for pos in ['R', 'K', 'H']:
            positive += ((ProteinParam.aacomp[pos]) * ((10 ** ProteinParam.aa2chargePos[pos]) / (((10 ** ProteinParam.aa2chargePos[pos])) + (10 ** self.pH))))
        positive += ProteinParam.aaNterm
        for neg in ['D', 'E', 'C', 'Y']:
            negative += ((ProteinParam.aacomp[neg]) * ((10 ** self.pH) / (((10 ** ProteinParam.aa2chargeNeg[neg])) + (10 ** self.pH))))
        negative += ProteinParam.aaCterm
        total = positive - negative
        return total    

prot_in = input("Enter a protein: ")
prot_obj = ProteinParam(prot_in)
x = prot_obj.pI()
print(x)

问题是无论我输入什么,当我调用 pI() 时我总是回到 6.35,我无法弄清楚问题是什么。我怀疑它在 charge() 方法中,因为我不知道它在哪里并且没有错误并没有缩小它的范围。输入“VLSPADKTNVKAAW”的 pI 应为 9.88。有什么想法吗?

最佳答案

编程问题

如果您使用的是 Python2,请注意以下行:

test_pH = (test_pH / 100)

因为这会除以并向下舍入到最接近的整数。

如果你使用它可能会有所帮助:

test_pH = (test_pH / 100.)

逻辑问题

还有3个逻辑问题:

  1. 您需要返回找到的最佳 pH 值,而不是找到的最佳电荷
  2. 您需要搜索电荷最接近零的 pH 值,而不是它达到最负值的 pH 值
  3. 您需要使用公式来调整 terminii 的值,而不是简单地添加原始值

换句话说,而不是:

positive += ProteinParam.aaNterm

你需要

positive +=  (10.**ProteinParam.aaNterm / (10. ** ProteinParam.aaNterm + 10. ** self.pH))

关于python - 用字典实现算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29686484/

相关文章:

python numpy 按条件设置列表元素

python - App Engine 给出有关过期文件的错误

python - 为什么pd.DataFrame每个项目的类型都是float,但pd.DataFrame的dtype是object?

python - 质数生成器解释?

python - 如何使用 Python 记录当前行和堆栈信息?

algorithm - 使用最大流算法查找网络的边缘连通性

c++ - Quicksort 在对降序-升序数据进行排序时的奇怪行为

java - 基于时间的分组

python - 我是否必须卸载旧的 python 版本才能在 Windows 上更新到新版本?

python-3.x - 如何使用opencv通过xmax xmin ymax ymin编写矩形(边界框)