前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Open Babel的安装与使用

Open Babel的安装与使用

作者头像
DechinPhy
发布2021-05-21 15:51:04
4.4K0
发布2021-05-21 15:51:04
举报
文章被收录于专栏:Dechin的专栏Dechin的专栏

技术背景

Open Babel是化学领域常用的一个文件格式转换工具,它可以支持xyz的坐标格式、SMILES表达式、InChI表达式和mol以及mol2等格式之间的互相转化。比如说,你只有一个甲烷的SMILES表达式C,那么你就可以使用Open Babel将其转化成一个mol2文件,这样就可以用vmd等工具进行分子的可视化(参考这篇博客)。

OBABEL的安装

OBABEL的安装方式有两种,一种是从官方地址获取最新版本的源码进行手动编译安装,另一种是基于Conda的自动化安装,但是后者的版本会比手动编译安装的版本更低一些。因此,如果需要获取最新的版本,只能采取手动编译安装的方案。

手动编译安装

首先访问官方的源码下载地址去下载一个适合本地硬件设备和软件版本的源码压缩包,下载到本地后可以创建一个目录用于解压和编译,具体的操作流程如下代码所示:

代码语言:javascript
复制
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是否安装成功:

代码语言:javascript
复制
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会自动帮我们解决一些系统环境的依赖,比较人性化一些,具体的安装指令如下:

代码语言:javascript
复制
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的虚拟环境:

代码语言:javascript
复制
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:

代码语言:javascript
复制
(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的文件,内容如下所示:

代码语言:javascript
复制
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表达式:

代码语言:javascript
复制
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://cloud.tencent.com/developer/article/1827479

作者ID:DechinPhy

更多原著文章请参考:https://www.cnblogs.com/dechinphy/

参考链接

  1. https://zhuanlan.zhihu.com/p/40577681
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-05-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 技术背景
  • OBABEL的安装
    • 手动编译安装
      • 非编译模式的安装
      • OBABEL的使用
      • 总结概要
      • 版权声明
      • 参考链接
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档