用字典实现算法

时间:2015-04-16 21:41:06

标签: python algorithm python-3.x

我必须实现以下算法:equation其中“i”包含R,K,H和Nterm,“j”包含D,E,C,Y和返回网络的Cterm正数收费。该方法被另一种方法用于计算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。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

编程问题

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

test_pH = (test_pH / 100)

因为这将分割并向下舍入到最接近的整数。

如果您使用以下内容可能会有所帮助:

test_pH = (test_pH / 100.)

逻辑问题

还有3个逻辑问题:

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

    positive += ProteinParam.aaNterm
    

    你需要

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