2

我正在尝试编写一个 python 函数,该函数将两个列表作为输入:一个包含一些分子 SMILES 代码,另一个包含分子名称。

然后它计算所有分子对之间的 TANIMOTO 系数(我已经有一个函数),并分别返回两个新列表,其中包含 Tanimoto 与任何其他分子不高于某个阈值的所有分子的 SMILES 和名称。

这是我到目前为止所做的,但它给出了错误的结果(我得到的大多数分子几乎相同......):

def TanimotoFilter(molist,namelist,threshold):
    # molist is the smiles list
    # namelist is the name list (SURPRISE!) is this some global variable name?
    # threshold is the tanimoto threshold (SURPRISE AGAIN!)
    smilesout=[]
    names=[]
    tans=[]
    exclude=[]
    for i in range(1,len(molist)):
        if i not in exclude:
            smilesout.append(molist[i])
            names.append(namelist[i])
            for j in range(i,len(molist)):
                if i==j:
                   tans.append('SAME')
                else:
                   tanimoto=tanimoto_calc(molist[i],molist[j])
                   if tanimoto>threshold:
                      exclude.append(j)
                      #print 'breaking for '+str(i)+' '+str(j)
                      break
                   else:
                      tans.append(tanimoto)

    return smilesout, names, tans

如果您提出的修改尽可能基本,我将非常感谢,因为此代码适用于生活中几乎没有见过终端的人......它是否充满了使其变慢的循环并不重要.

谢谢你们!

4

1 回答 1

0

我对函数的逻辑进行了一些更改。如问题中所述,它返回两个带有 SMILES 和名称的列表。我不确定 tans 的用途,因为 tanimoto 值是针对元组而不是针对单个分子。无法在没有数据的情况下测试代码,让我知道这是否有效。

def TanimotoFilter(molist, namelist, threshold):
    # molist is the smiles list
    # namelist is the name list (SURPRISE!) is this some global variable name?
    # threshold is the tanimoto threshold (SURPRISE AGAIN!)
    smilesout=[]
    names=[]
    tans=[]
    exclude=[]

    for i in range(0, len(molist)):
        if i not in exclude:
            temp_exclude = []
            for j in range(i + 1, len(molist)):
                tanimoto = tanimoto_calc(molist[i], molist[j])
                if tanimoto > threshold:
                    temp_exclude.append(j)
            if temp_exclude:
                temp_exclude.append(i)
                exclude.extend(temp_exclude)
            else:
                smilesout.append(molist[i])
                names.append(namelist[i])

    return smilesout, names
于 2015-10-16T15:46:06.600 回答