类型错误:"元组"和"浮点数"实例之间不支持">="



我正在尝试编写一个小python脚本,用于计算感兴趣的分子和分子数据库之间的Tanimoto相似性指数。我在用pybel。

该数据库采用.smi格式,第一列有分子的化学信息,第二列有分子名称,看起来像这样:

C[C@]12CC[C@H](C1(C)C)CC2=O     (-)-CAMPHOR
CC1=CC[C@H](C(=C)C)C[C@@H]1O    (-)-CARVEOL
CC1=CC[C@H](CC1=O)C(=C)C        (-)-CARVONE
O=CC[C@@H](C)CCC=C(C)C  (-)-CITRONELLAL
OCC[C@@H](C)CCC=C(C)C   (-)-CITRONELLOL
C[C@@H]1CC[C@@H](C(=C)C)C[C@H]1O        (-)-DIHYDROCARVEOL
C[C@@]12CC[C@@H](C1)C(C2=O)(C)C (-)-Fenchone
C[C@@H]1CC[C@H]([C@@H](C1)O)C(C)C       (-)-MENTHOL
C[C@@H]1CC[C@H](C(=O)C1)C(C)C   (-)-MENTHONE
C[C@@H]1CCCCCCCCCCCCC(=O)C1     (-)-MUSCONE
CC(=C)[C@H]1CCC(=CC1)C=O        (-)-PERILLALDEHYDE
.
.
.

这个版本的脚本正如我所期望的那样工作:

from openbabel import pybel
targetmol = next(pybel.readfile("smi", "/path/to/sample.smi"))
targetfp = targetmol.calcfp()              <--- calculate fingerprints of the sample
for mol in pybel.readfile("smi", "/path/to/db.smi"):
fp = mol.calcfp()                      <--- calculate fingerprints of the db
tan = fp | targetfp                    <--- calculate the Tanimoto index via the "|" operator
if tan>=0.8:
print(tan)

输出:

1.0
1.0
0.9285714285714286
0.8571428571428571
1.0
1.0
0.9285714285714286
0.8571428571428571
.
.
.

显然,为了给我收到的数字赋予意义,我需要将分子名称添加到相应的Tanimoto索引中。我试过这个:

from openbabel import pybel
targetmol = next(pybel.readfile("smi", "/path/to/sample.smi"))
targetfp = targetmol.calcfp()              
for mol in pybel.readfile("smi", "/path/to/db.smi"):
fp = mol.calcfp()                      
tan = (fp | targetfp, mol.title)                   
if tan>=0.8:
print(tan, title)

从标题来看,我收到以下错误:

Traceback (most recent call last):
File "test3.py", line 15, in <module>
if tan>=0.8:
TypeError: '>=' not supported between instances of 'tuple' and 'float'

我的猜测是,python显然无法将if tan>=0.8操作应用于字符串格式,但我真的不知道如何克服这个问题,因为正如你所猜测的,我对编程非常陌生。

任何关于如何更正这段代码的提示都将不胜感激。谢谢你抽出时间。

您只需要将其更改为:tan[0] >= 0.8:

逗号,(tan = (fp | targetfp, mol.title)中的逗号(是元组的语法,元组基本上是一个不可变的数组,因此要访问元素,需要通过类似于列表的索引来访问。

最新更新