分光光度描述符的 Python 实现
项目描述
分光镜
spectrophores该模块包含从分子计算的 python 代码。它正在使用RDKit和numba工具包。
该技术及其应用已在Journal of Cheminformatics (2018) 10 , 9中进行了描述。该论文也包含在此发行版中。
该spectrophore代码可以通过两种方式使用:
- 作为一个独立程序,将 sd 文件中的分子转换为相应的
spectrophores; - 作为在您自己的代码
python中导入的模块。python
在以下部分中,将记录这两种用法。
安装
一、RDKit和Numba的安装
我们建议同时安装RDKit和Numba使用Anaconda. 如果conda您的系统上尚不可用,则应首先按照Anaconda 网站上的说明进行安装。Numba是一个开源 JIT 编译器,可将 Python 和 NumPy 代码的子集转换为快速机器代码。RDKit是开源化学信息学软件,提供与分子一起工作的代码。
安装所有东西的最简单方法是使用conda. 首先创建一个合适的环境来安装该spectrophore技术:
> conda create --name spectrophore python=3
这将安装一个安装了condaPython 3.9 的新环境。现在激活这个环境:
> conda activate spectrophore
Numba现在rdkit可以按如下方式安装(确保您首先激活了spectrophore环境):
> conda install numba
> conda install cudatoolkit
> conda install -c conda-forge rdkit
您可以通过从命令行rdkit打开会话(假设您仍在激活的环境中)并键入以下内容来测试安装:pythonspectrophore
>>> from rdkit import Chem
>>> mol = Chem.MolFromSmiles("C1CCCC1")
>>> print(mol.GetNumAtoms())
5
同样,您可以使用这个小片段测试numba安装:python
>>> import numba
>>> numba.__version__
'0.51.2'
使用numba -s命令,您还可以检查您是否安装了 CUDA 设备(检查部分__CUDA Information__)。
2.安装分光镜代码
在spectrophore环境仍处于活动状态的情况下,您现在可以spectrophore使用以下命令轻松安装模块:
> pip install uamc-spectrophore
通过打开python会话并输入以下内容来检查安装:
>>> from spectrophore import spectrophore
>>> spectrophore.__version__
'1.2.0'
1.作为独立程序使用
安装后,应该可以将spectrophore.py代码作为一个独立的程序spectrophores从带有分子的 sd 文件中计算出来。spectrophore.py您可以通过启动pythonshell 并键入以下内容来找出它的位置:
>>> from spectrophore import spectrophore
>>> spectrophore.__file__
'/Users/hans/anaconda3/envs/spectrophore/lib/python3.8/site-packages/spectrophore/spectrophore.py'
您可以使用此完整路径来调用spectrophore.py代码,也可以将其添加到 $PATH 环境变量中。
要使用spectrophore.py,请在命令行中键入以下内容:
> spectrophore.py -h
这将为您提供有关如何spectrophores从 sd 文件进行计算的所有详细信息:
usage: spectrophore.py [-h] [-n {none,mean,all,std}] [-s {none,unique,mirror,all}] [-a {1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180}]
[-r RESOLUTION] [-p MAX_WORKERS] -i INFILE -o OUTFILE
Calculate spectrophores
optional arguments:
-h, --help show this help message and exit
-n {none,mean,all,std}, --norm {none,mean,all,std}
normalization setting (default: all)
-s {none,unique,mirror,all}, --stereo {none,unique,mirror,all}
stereo setting (default: none)
-a {1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180}, --accuracy {1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180}
accuracy setting (default: 20)
-r RESOLUTION, --resolution RESOLUTION
resolution setting (>0) (default: 3)
-p MAX_WORKERS, --np MAX_WORKERS
number of processors to use; -1 is all processors (default: -1)
required arguments:
-i INFILE, --in INFILE
input sdf file (default: None)
-o OUTFILE, --out OUTFILE
output spectrophore file (default: None)
2.作为python模块使用
2.1。介绍
一旦您安装了所有必需的工具和uamc-spectrophore软件包,您就可以使用该工具了。以最简单的形式,spectrophores可以计算如下:
>>> from spectrophore import spectrophore
>>> from rdkit import Chem
>>> from rdkit.Chem import AllChem
>>> mol = Chem.MolFromSmiles("c1ncncc1")
>>> mol = Chem.AddHs(mol)
>>> AllChem.EmbedMolecule(mol, randomSeed=1)
0
>>> calculator = spectrophore.SpectrophoreCalculator(normalization='none')
Probes initialised: 48 number of probes in total
12 probes are used due to the imposed stereo flag
>>> calculator.calculate(mol)
array([ 1.409246 , 2.021652 , 1.6011626 , 3.034698 ,
2.4150815 , 5.0872273 , 2.285813 , 1.7250485 ,
3.436644 , 4.0012817 , 5.092206 , 2.9844987 ,
0.6417792 , 0.8024898 , 4.8707156 , 4.870761 ,
2.8789856 , 4.104702 , 1.9413302 , 3.5960448 ,
4.9019723 , 4.151822 , 4.5394773 , 5.766127 ,
44.79124 , 71.551796 , 106.82244 , 106.82059 ,
49.73703 , 61.662792 , 23.50798 , 81.88448 ,
77.47026 , 67.52185 , 57.44229 , 112.96884 ,
0.6794604 , 1.1607243 , 2.470075 , 2.470103 ,
1.0203087 , 1.1483352 , 0.51142335, 1.7433033 ,
1.8094715 , 1.3015395 , 1.2431506 , 2.5163455 ],
dtype=float32)
在所示示例中,前三行导入了所需的模块:spectrophore用于计算分光光团的模块Chem,用于从微笑字符串生成 RDKit 分子的模块,以及AllChem用于从分子生成 3D 构象的模块。接下来,从微笑字符串(第 4 行)创建一个分子,然后在第 5 行添加氢原子后在第 6 行生成构象。最后,在第 7 行和第 8 行, SpectrophoreCalculator生成了一个对象,然后使用该对象计算一个spectrophore描述符(第 8 行),其默认形式为 4 * 12 个数字。
注意:关于 a 形状的几句话
spectrophore:每个
spectrophore都由一组浮点数组成,并且该组始终是 4 的倍数。实际的数字计数取决于在计算分光团时如何处理立体化学;这由以下stereo()方法控制:
stereo("none"):a中的数字总数spectrophore为48(4 * 12;默认),stereo("unique"):a中的数字总数spectrophore为72(4 * 18),stereo("mirror"):a中的数字总数spectrophore为72(4 * 18),stereo("all"): a 中的数字总数spectrophore为 144 (4 * 36)。
无论实际点数是多少
spectrophore,这些总是根据相同的原子属性计算。例如,考虑一个spectrophore4 n个点,那么这些点代表以下内容:
- 点 1 到n :表示原子部分电荷与n 个盒子中的每一个之间的相互作用能;
- 点n +1 到 2 n :表示原子亲油性与n 个盒子中的每一个之间的相互作用能;
- 点 2 n +1 到 3 n :表示原子形状偏差与n 个盒子中的每一个之间的相互作用能;
- 点 3 n +1 到 4 n :表示原子亲电性和n 个盒子中的每一个之间的相互作用能。
请查看原始出版物表格,了解有关计算这些相互作用能的方式的更多信息,以及该
stereo()方法的实际含义。
如果一个分子包含一个以上的 3D 构象,那么可以指定应该使用哪种构象来计算spectrophores. 例如,考虑以下代码:
>>> calculator = spectrophore.SpectrophoreCalculator(normalization='none')
Probes initialised: 48 number of probes in total
12 probes are used due to the imposed stereo flag
>>> aspirin = Chem.MolFromSmiles("CC(Oc1ccccc1C(O)=O)=O")
>>> mol = Chem.AddHs(mol)
>>> cids = AllChem.EmbedMultipleConfs(aspirin, numConfs=3, randomSeed=1)
>>> print(len(cids))
3
>>> for i in range(len(cids)): calculator.calculate(aspirin, i)
...
array([ 2.964628 , 3.1078947, 2.927014 , 4.7348037, 7.507005 ,
6.7752705, 4.694607 , 4.9843326, 6.566493 , 8.246073 ,
10.165346 , 6.63523 , 4.858508 , 8.002102 , 6.8100824,
8.816333 , 15.715073 , 19.571812 , 10.928973 , 14.395827 ,
17.003227 , 18.447824 , 25.714355 , 15.146796 , 72.9549 ,
98.34449 , 169.34996 , 182.39804 , 131.36954 , 131.15866 ,
65.37012 , 130.0362 , 162.26236 , 149.89626 , 179.36638 ,
198.5693 , 2.2463505, 3.1564593, 5.1663566, 5.612588 ,
4.058919 , 4.409714 , 2.2037854, 4.4034805, 4.9583206,
5.239315 , 5.461795 , 6.264689 ], dtype=float32)
array([ 2.863708 , 3.1190798, 2.9663007, 4.770968 , 7.393107 ,
7.3158054, 4.9012723, 5.10262 , 6.548969 , 8.572092 ,
10.425214 , 6.4823613, 4.787042 , 8.08808 , 6.631177 ,
8.741646 , 16.067795 , 19.49238 , 10.819519 , 14.260894 ,
16.789541 , 18.33067 , 25.610632 , 14.279321 , 69.21315 ,
96.67396 , 170.67822 , 184.54782 , 119.22876 , 135.03757 ,
59.888947 , 119.49558 , 173.35124 , 145.16624 , 180.47777 ,
187.60854 , 2.1962543, 3.108443 , 5.2100787, 5.6747303,
3.8506951, 4.435027 , 2.1061015, 4.0988173, 5.171605 ,
5.103154 , 5.384299 , 5.955127 ], dtype=float32)
array([ 3.0309825, 3.435472 , 2.8768196, 4.706544 , 7.5557814,
6.4479575, 4.55689 , 4.953575 , 6.4871607, 8.706506 ,
8.518427 , 6.3662963, 4.9284625, 9.355401 , 6.6179686,
8.523829 , 15.459739 , 19.284777 , 10.792515 , 13.991817 ,
16.795666 , 18.597605 , 24.084375 , 13.117221 , 77.639145 ,
120.10927 , 169.49625 , 166.18648 , 131.14139 , 144.46242 ,
72.4695 , 149.30933 , 140.35475 , 155.59204 , 130.84991 ,
174.86932 , 2.4413445, 3.8801153, 5.1489463, 4.834638 ,
4.0795846, 4.013626 , 2.4914305, 4.5840054, 4.270138 ,
5.335861 , 4.6315002, 5.6371183], dtype=float32)
spectrophores通过绘制实际值可以轻松可视化。例如,考虑以下代码段:
>>> import matplotlib.pyplot as plt
>>> mol = Chem.MolFromSmiles("CC(CCC1=CC=CC=C1Cl)N1CCOCC1")
>>> mol = Chem.AddHs(mol)
>>> cids = AllChem.EmbedMultipleConfs(mol, numConfs = 10, randomSeed = 1)
>>> spectrophores = []
>>> for cid in cids: spectrophores.append(calculator.calculate(mol, cid))
...
>>> for i in range(len(spectrophores)): plt.plot(range(1,49), spectrophores[i], label='Conf %d' % (i+1))
...
[<matplotlib.lines.Line2D object at 0x7f989dc3f4f0>]
[<matplotlib.lines.Line2D object at 0x7f989dc3f850>]
[<matplotlib.lines.Line2D object at 0x7f989dc3fbb0>]
[<matplotlib.lines.Line2D object at 0x7f989dc3ff10>]
[<matplotlib.lines.Line2D object at 0x7f989dc512b0>]
[<matplotlib.lines.Line2D object at 0x7f989dc51610>]
[<matplotlib.lines.Line2D object at 0x7f989dc51970>]
[<matplotlib.lines.Line2D object at 0x7f989dc51cd0>]
[<matplotlib.lines.Line2D object at 0x7f989dc5e070>]
[<matplotlib.lines.Line2D object at 0x7f989dc5e3d0>]
>>> plt.legend(loc='upper left')
<matplotlib.legend.Legend object at 0x7f9899470e80>
>>> plt.grid()
>>> plt.savefig("spectrophore/images/exampleplot1.png")
生成以下图:
同样,人们可以很容易地比较spectrophores来自两个不同分子的分子,并量化差异:
>>> plt.close()
>>> spectrophores = []
>>> mols = [Chem.MolFromSmiles("ClC(Br)(I)F"), Chem.MolFromSmiles("CC(CCC1=CC=CC=C1Cl)N1CCOCC1")]
>>> for i in range(2):
... mols[i] = Chem.AddHs(mols[i])
... AllChem.EmbedMolecule(mols[i], randomSeed=1)
... spectrophores.append(calculator.calculate(mols[i]))
...
0
0
>>> for i in range(2): plt.plot(range(1,49), spectrophores[i], label='Molecule %d' % (i+1))
...
[<matplotlib.lines.Line2D object at 0x7faf420df520>]
[<matplotlib.lines.Line2D object at 0x7faf420df4c0>]
>>> plt.grid()
>>> plt.savefig("spectrophore/images/exampleplot2.png")
>>> from scipy.spatial import distance
>>> distance.euclidean(spectrophores[0],spectrophores[1])
2060.65478515625
从上一个示例中可以清楚地看出,实际spectrophore值可能会因分子类型而有很大差异。此外,绝对值取决于属性类型,一些属性会导致较大的值(例如形状偏差),而另一些则非常小。出于这个原因,提供了许多标准化方法,如下所示。
2.2. 模块方法
resolution()
该resolution()方法控制分子与周围盒子之间的最小距离。默认情况下,此值设置为 3.0 A。resolution()可以在创建类时指定,或者稍后使用以下resolution()方法指定:
>>> mol = Chem.MolFromSmiles("ClC(Br)(I)F")
>>> AllChem.EmbedMolecule(mol, randomSeed=1)
0
>>> calculator = spectrophore.SpectrophoreCalculator(normalization='none') # Default of 3.0
Probes initialised: 48 number of probes in total
12 probes are used due to the imposed stereo flag
>>> print(calculator.calculate(mol)[0])
2.9869986
>>> calculator = spectrophore.SpectrophoreCalculator(normalization='none', resolution = 3.0)
Probes initialised: 48 number of probes in total
12 probes are used due to the imposed stereo flag
>>> print(calculator.calculate(mol)[0])
2.9869986
>>> calculator = spectrophore.SpectrophoreCalculator(normalization='none', resolution = 5.0)
Probes initialised: 48 number of probes in total
12 probes are used due to the imposed stereo flag
>>> print(calculator.calculate(mol)[0])
1.341883
>>> calculator.resolution(10.0)
>>> print(calculator.calculate(mol)[0])
0.3347178
分辨率值越大(例如 10.0对3.0 A),相互作用能和相应spectrophore值就越小。
调用resolution()不带参数的方法会返回当前分辨率值:
>>> calculator.resolution()
10.0
accuracy()
该accuracy()方法控制分子在笼内旋转的角度步长。默认情况下,此值设置为 20°。此参数可以在创建类时修改,也可以accuracy()稍后使用该方法进行修改。精度应该是 180 的整数分数,因此 180 模精度应该等于 0。精度值越小(意味着角度步长越小),计算时间越长:
>>> calculator = spectrophore.SpectrophoreCalculator(accuracy = 20.0, normalization = 'none') # Default
Probes initialised: 48 number of probes in total
12 probes are used due to the imposed stereo flag
>>> print(calculator.calculate(mol)[0])
2.9869986
>>> calculator = spectrophore.SpectrophoreCalculator(normalization = 'none')
Probes initialised: 48 number of probes in total
12 probes are used due to the imposed stereo flag
>>> print(calculator.calculate(mol)[0])
2.9869986
>>> calculator = spectrophore.SpectrophoreCalculator(accuracy = 2.0, normalization = 'none')
Probes initialised: 48 number of probes in total
Only using 12 probes
>>> print(calculator.calculate(mol)[0]) # Takes some time
3.0315504
>>> 100.0 * (3.0315504 - 2.9869986) / 3.0315504
1.469604463775361
调用accuracy()不带参数的方法会返回当前精度值:
>>> calculator.accuracy()
2
normalization()
使用该normalization()方法,可以指定spectrophore归一化的类型。有四种可能:
normalization("none"):未应用归一化,spectrophore值是原始计算的相互作用能(乘以 -100),normalization("mean"):对于每个属性,计算平均值,每个单独的spectrophore属性值减去这些平均值。这将计算值集中在 0 附近,normalization("std"):对于每个属性,计算标准偏差,每个单独的spectrophore属性值除以这些标准偏差,normalization("all"):每个分光光度值都通过平均值和标准偏差进行归一化。这是故障选项。
默认值为“全部”。
>>> 计算器。准确度( 20 )
>>> 计算器。标准化(“无” )
>>> 规范 = 计算器。计算(摩尔)
>>> 打印(规格[:12 ])
[ 2.9869986 2.7023215 1.8029709 4.468909 7.3755445 7.2522745 4.1123347
4.0559936 5.9084597 8.5649605 9.328 ] 2 >>> 6 6规格 229.328 ] 2
[: 12 ])
64.78871297836304
>>> 计算器。归一化(“平均值” )
>>> 规范 = 计算器。calculate ( mol )
>>> print ( spec [: 12 ])
[ - 2.4120607 - 2.6967378 - 3.5960884 - 0.9301505 1.9764853 1.8532152
- 1.2867246 - 1.3430657 0.50940037 3.1659012 3.9291964 0.8306308 ]
>>> 总和(规格[:12 ])
1.430511474609375e-06
>>> 计算器。标准化(“std” )
>>> 规范 = 计算器。