2015-10-16 34 views
2

我想写一个需要两个列表作为输入的python函数:一个包含一些分子SMILES代码和另一个包含分子名称的代码。基于Tanimoto系数丢弃过于相似的分子的python函数?

然后它计算所有分子对之间的TANIMOTO系数(我已经有了这个功能),并分别返回两个新列表,其中所有分子的斯米尔和其他任何其他分子的名字都不高于a一定的阈值。

这是我迄今所做的,但它给错误的结果(最让我得到分子几乎是一样的...):

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 

我会,如果修改非常感谢你提出的建议是尽可能基本的,因为这些代码适用于那些在他们的生活中几乎看不到终端的人......无论它是否充满循环,都会让它变慢。

谢谢大家!

回答

0

我对函数的逻辑做了一些修改。正如问题中提到的那样,它返回两个带有SMILES和名字的列表。我不清楚tan的目的,因为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 
+0

嗨Vivek!谢谢你的想法。它完美的工作(我认为)。 tans list实际上是一个列表清单(我写的函数是错误的)。多亏了你的想法,我才能够获得所有这些分子对之间的所有Tanimoto系数列表。 – user3091644