技術背景
Open Babel是化學領域常用的一個文件格式轉換工具,它可以支持xyz的坐標格式、SMILES表達式、InChI表達式和mol以及mol2等格式之間的互相轉化。比如說,你只有一個甲烷的SMILES表達式C
,那么你就可以使用Open Babel將其轉化成一個mol2文件,這樣就可以用vmd等工具進行分子的可視化(參考這篇博客)。
OBABEL的安裝
OBABEL的安裝方式有兩種,一種是從官方地址獲取最新版本的源碼進行手動編譯安裝,另一種是基於Conda的自動化安裝,但是后者的版本會比手動編譯安裝的版本更低一些。因此,如果需要獲取最新的版本,只能采取手動編譯安裝的方案。
手動編譯安裝
首先訪問官方的源碼下載地址去下載一個適合本地硬件設備和軟件版本的源碼壓縮包,下載到本地后可以創建一個目錄用於解壓和編譯,具體的操作流程如下代碼所示:
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/$ tar zxf openbabel-3.1.1.tar.bz2
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/$ cd openbabel-3.1.1
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1$ mkdir build
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1$ cd build/
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1/build$ cmake ..
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1/build$ make
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1/build$ sudo make install
其中注意的是在build
目錄下執行的是cmake ..
,這一點跟官方所提供的指令安裝方案略有區別,但是在Ubuntu 20.04系統中這個安裝策略才是正確的。經過sudo make install
之后我們就可以在全局使用obabel指令,比如可以用如下指令檢驗Open Babel是否安裝成功:
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1/build$ obabel --help
Open Babel converts chemical structures from one file format to another
Usage:
obabel[-i<input-type>] <infilename> [-o<output-type>] -O<outfilename> [Options]
The extension of a file decides the format, unless it is overridden
by -i or -o options, e.g. -icml, or -o smi
See below for available format-types, which are the same as the
file extensions and are case independent.
If no input or output file is given stdin or stdout are used instead.
More than one input file can be specified and their names can contain
wildcard chars (* and ?). The format of each file can be different unless
the -i option has been used, when they are all the same.
By default, the molecules are aggregated in the output file,
but see -m option, Splitting, below.
Options, other than -i -o -O -m, must come after the input files.
Conversion options
-f <#> Start import at molecule # specified
-l <#> End import at molecule # specified
-e Continue with next object after error, if possible
-k Attempt to translate keywords
-H Outputs this help text
-Hxxx (xxx is file format ID e.g. -Hcml) gives format info
-Hall Outputs details of all formats
-V Outputs version number
-L <category> Lists plugin classes of this category, e.g. <formats>
Use just -L for a list of plugin categories.
Use -L <ID> e.g. -L sdf for details of a format or other plugin.
-m Produces multiple output files, to allow:
Splitting: e.g. obabel infile.mol -O new.smi -m
puts each molecule into new1.smi new2.smi etc
Batch conversion: e.g. obabel *.mol -osmi -m
converts each input file to a .smi file
Interface to OBAPI internals
API options, e.g. ---errorlevel 2
errorlevel # min warning level displayed
To see a list of recognized file formats use
babel -L formats [read] [write]
To see details and specific options for a particular format, e.g CML, use
babel -L cml
如果顯示結果如上,則表示安裝成功。
非編譯模式的安裝
用conda來安裝,conda會自動幫我們解決一些系統環境的依賴,比較人性化一些,具體的安裝指令如下:
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1/build$ conda install -c openbabel openbabel
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: \
Found conflicts! Looking for incompatible packages.
This can take several minutes. Press CTRL-C to abort.
failed
UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:
Specifications:
- openbabel -> python[version='2.7.*|3.4.*|3.5.*|3.6.*|>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.4,<3.5.0a0']
Your python: python=3.8
If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.
這里第一次執行安裝指令時遇到了一些問題,我們從這個錯誤日志中可以看到,是因為本地我們的python版本是3.8,而Open Babel依賴於更低一些級別的python環境。這時候我們可以用conda來統一的管理這些python環境,比如創建一個新的python3.7.5的虛擬環境:
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1/build$ conda create -n py37 python=3.7.5
(base) dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1/build$ conda activate py37
(py37) dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1/build$ python3
Python 3.7.5 (default, Oct 25 2019, 15:51:11)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
創建成功后,我們在這個py37
的虛擬環境下再嘗試一下安裝Open Babel:
(py37) dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd/openbabel/openbabel-3.1.1/build$ conda install -c openbabel openbabel
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.9.2
latest version: 4.10.1
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: /home/dechin/anaconda3/envs/py37
added / updated specs:
- openbabel
The following packages will be downloaded:
package | build
---------------------------|-----------------
fontconfig-2.13.1 | h6c09931_0 250 KB
glib-2.63.1 | h5a9c865_0 2.9 MB
openbabel-2.4.1 | py37_6 5.1 MB openbabel
------------------------------------------------------------
Total: 8.2 MB
The following NEW packages will be INSTALLED:
bzip2 pkgs/main/linux-64::bzip2-1.0.8-h7b6447c_0
cairo pkgs/main/linux-64::cairo-1.14.12-h8948797_3
fontconfig pkgs/main/linux-64::fontconfig-2.13.1-h6c09931_0
freetype pkgs/main/linux-64::freetype-2.10.4-h5ab3b9f_0
glib pkgs/main/linux-64::glib-2.63.1-h5a9c865_0
icu pkgs/main/linux-64::icu-58.2-he6710b0_3
libpng pkgs/main/linux-64::libpng-1.6.37-hbc83047_0
libuuid pkgs/main/linux-64::libuuid-1.0.3-h1bed415_2
libxcb pkgs/main/linux-64::libxcb-1.14-h7b6447c_0
libxml2 pkgs/main/linux-64::libxml2-2.9.10-hb55368b_3
openbabel openbabel/linux-64::openbabel-2.4.1-py37_6
pcre pkgs/main/linux-64::pcre-8.44-he6710b0_0
pixman pkgs/main/linux-64::pixman-0.40.0-h7b6447c_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
glib-2.63.1 | 2.9 MB | ################################################################################## | 100%
openbabel-2.4.1 | 5.1 MB | ################################################################################## | 100%
fontconfig-2.13.1 | 250 KB | ################################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(py37) dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd$ obabel -V
Open Babel 2.4.1 -- Sep 2 2019 -- 21:57:37
可以看到,我們成功的安裝了Open Babel 2.4.1的版本,雖然比前面手動編譯安裝的3.1.1版本更低一些,但是也都具備基本的功能。這里補充一個conda的虛擬環境的使用方法,如果我們想要關閉一個虛擬環境,只需要執行deactivate env_name
即可。如果是需要激活一個虛擬環境,則需要運行conda activate env_name
的指令。
OBABEL的使用
不論是手動編譯安裝還是conda直接安裝的方案,安裝成功后都具備基本的功能,比如如下所展示的基本示例。首先我們創建一個名為file.xyz
的文件,內容如下所示:
24
mol
C -1.615 -0.739 -3.043
C -0.076 -0.706 -3.045
H -1.963 -1.227 -3.929
H -1.959 -1.275 -2.183
C 0.469 -2.145 -3.087
H 0.268 -0.169 -3.905
H 0.272 -0.218 -2.159
H 1.539 -2.122 -3.089
H 0.126 -2.682 -2.227
H 0.122 -2.633 -3.974
C -2.160 0.701 -3.001
C -3.700 0.667 -2.999
H -1.817 1.237 -3.861
H -1.812 1.188 -2.114
C -4.245 2.107 -2.957
H -4.047 0.179 -3.885
H -4.043 0.131 -2.139
H -3.897 2.594 -2.070
H -3.901 2.643 -3.817
C -5.784 2.073 -2.955
O -6.394 1.131 -3.525
N -6.541 3.141 -2.286
H -7.407 2.773 -1.946
H -6.007 3.500 -1.521
這是一個擁有24個分子的體系,在前面這篇博客中用展示這個分子模型的3D圖像。通過Open Babel,我們可以將這個坐標文件導出為一個smi文件,也就是SMILES表達式:
dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd$ obabel -ixyz file.xyz -osmi -O file.smi
1 molecule converted
(py37) dechin@ubuntu2004:~/projects/gitlab/dechin/src/vmd$ cat file.smi
C(CC)CCCC(=O)N mol
關於更多的使用方法和示例,這里不做過多的展開,參考鏈接1中的內容非常的全面,感興趣的童鞋可以參考之。
總結概要
本文主要介紹了在化學領域中常用的文件格式轉化工具Open Babel的兩種安裝方法,與基本的使用案例。其中如果選擇手動編譯安裝可以使用最新的release版本,如果使用conda就只能使用老舊的穩定版本,但是可以很大程度上簡化安裝的步驟。在基本的案例中我們演示了使用obabel來將一個xyz坐標格式的文件轉化成一個SMILES表達式。
版權聲明
本文首發鏈接為:https://www.cnblogs.com/dechinphy/p/obabel.html
作者ID:DechinPhy
更多原著文章請參考:https://www.cnblogs.com/dechinphy/