我正在尝试写一个小蟒蛇脚本来计算感兴趣的分子和分子数据库之间的Tanimoto相似指数。我用的是比伯。
数据库以.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操作应用于字符串格式,但我真的不知道如何克服这个问题,因为正如您所猜测的那样,我对编程非常陌生。
任何关于如何纠正这段代码的提示都将不胜感激。谢谢您抽时间见我。
发布于 2022-07-27 10:02:27
您只需将其更改为:tan[0] >= 0.8:
逗号, (tan = (fp | targetfp, mol.title)中的逗号)是元组的语法,元组基本上是一个不可变的数组,因此要访问元素,您需要通过索引来访问元素,比如列表。
https://stackoverflow.com/questions/73135873
复制相似问题