技术背景

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/

参考链接

  1. https://zhuanlan.zhihu.com/p/40577681

Open Babel的安装与使用的更多相关文章

  1. 让nodeJS支持ES6的词法----babel的安装和使用

    要使用Babel, 我们需要nodeJS的环境和npm, 主要安装了nodeJS, npm就默认安装了 , 现在安装nodeJS很简单了, 直接下载安装就好了: 安装es-checker 在使用Bab ...

  2. 前端自动化之babel本地化安装

    npm添加package.json cd到项目根目录直接调用npm init 会创建package.json文件 本地安装bebel(并非全局安装,这种情况下cmd命令中babel命令不识别): 步骤 ...

  3. npm+webpack+babel+react安装

    npm+webpack+babel+react安装 1.首先要安装 Node.js, Node.js 自带了软件包管理器 npm 2.在项目文件目录下生成package.json # 进入项目目录$ ...

  4. babel的安装和使用方法

    要使用Babel, 我们需要nodeJS的环境和npm, 主要安装了nodeJS, npm就默认安装了 , 现在安装nodeJS很简单了, 直接下载安装就好了: 安装es-checker 在使用Bab ...

  5. babel环境安装与编译

    babel:将浏览器不支持的ES6语法转为javascript 查看node是否安装: npm -v node -v 实例演示:在桌面新建part5目录在cmd命令行中 cd desktop cd p ...

  6. Babel的安装和使用

    安装Node.JS 和 npm,如未安装可参照其他文章 1.创建一个package.json npm init (回车, 一直下一步即可) 安装 Babel npm install --save-de ...

  7. webpack4.x下babel的安装、配置及使用

    前言 目前,ES6(ES2015)这样的语法已经得到很大规模的应用,它具有更加简洁.功能更加强大的特点,实际项目中很可能会使用采用了ES6语法的模块,但浏览器对于ES6语法的支持并不完善.为了实现兼容 ...

  8. Babel 7 安装与配置

    Babel:帮我们把高级的语法(ES6)转为低级的语法 /*    Babel 7.x版本  安装如下 (cnpm i @babel/cli -D)                     //Bab ...

  9. es6安装babel包

    1.前面下载node.js及安装淘宝镜像可以查看我写的vue.js环境搭建 2.安装完node后,安装babel npm install -g babel-cli 3.检验babel是否安装成功: b ...

随机推荐

  1. Linux下Wordpress忘记密码后的解决方法

    进入Wordpress的数据库,找到wp_users表,使用MD5('你的密码')函数添加密码 示例: 修改admin的密码为123456 UPDATE wp_users SET user_pass= ...

  2. php7.3编译安装 支持微擎2.0

    再次整理   //一下配置在命令粘贴时注意句尾加 \ , 在 \ 后不能有空格,不然会自动执行,相当于回车./configure --prefix=/usr/local/php \ --with-co ...

  3. Java基础复习(三)

    1. &和&&的区别. &和&&都可以用作逻辑与的运算符,表示逻辑与(and),当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则 ...

  4. LVS-DR群集

    LVS-DR群集 目录 LVS-DR群集 一.LVS-DR的工作原理 1. LVS-DR数据包流向分析 2. IP包头及数据帧头信息的变化 3. DR模式的特点 4.LVS-DR中的ARP问题 (1) ...

  5. VUE集成keycloak和Layui集成keycloak

    一:KEYCLOAK配置部分: 1,下载keycloak,官网地址:https://www.keycloak.org/downloads.html.下载第一个就行 2,下载完毕之后,打开文件,访问 b ...

  6. 《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)

    1.简介 在自动化测试,有些我们的测试数据是放到excel文件中,尤其是在做数据驱动测试的时候,所以需要懂得如何操作获取excel内的内容.由于java不像python那样有直接操作Excle文件的类 ...

  7. 学习Spring5必知必会(1)~未使用spring前的麻烦

    一.未使用spring前的麻烦 开闭原则:扩展是开放的,但是对于修改是"封闭的". 1.代码耦合度比较高[不符合开闭原则]: public class EmployeeServic ...

  8. 3U VPX i7 刀片计算机

    产品概述 该产品是一款基于第三代Intel i7双核四线程的高性能3U VPX刀片式计算机.产品提供了多个高速PCIe总线接口,其中3个x4 PCIe 3.0接口,1个x4 PCIe 2.0接口.x4 ...

  9. Solution -「UNR #5」「UOJ #671」诡异操作

    \(\mathcal{Desciprtion}\)   Link.   给定序列 \(\{a_n\}\),支持 \(q\) 次操作: 给定 \(l,r,v\),\(\forall i\in[l,r], ...

  10. [LeetCode]13.罗马数字转整数(Java)

    原题地址: roman-to-integer 题目描述: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M ...