技术背景

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. Redis实现延迟对列

    一.应用场景: 订单超过 30 分钟未支付,则自动取消. 外卖商家超时未接单,则自动取消. 医生抢单电话点诊,超过 30 分钟未打电话,则自动退款.等等场景都可以用定时任务去轮询实现,但是当数据量过大 ...

  2. IOS开发之----常用函数和常数--秀清

    介绍一下Objective-c常用的函数,常数变量 算术函数 [算术函数] 函数名 说明 int rand() 随机数生成.(例)srand(time(nil)); //随机数初期化int val = ...

  3. kali中安装arpspoof 报错

    情境今天在使用arpspoof这个命令的时候,提示没有命令找不到,此时就想着安装一下没想到,碰上kali源不支持的问题  解决所以,此时需要做的就是安装阿里云或者其他镜像  步骤1. vim /etc ...

  4. Linux运行级别及解释

    Linux中有七个运行级别,分别为0~7 0级:关机 1级:单用户(找回丢失密码) 2级:多用户无网络 3级:多用户有网络,最常用的级别 4级:保留,目前Linux暂无使用 5级:图像界面 6级:重启 ...

  5. 解决/WEB-INF目录下的jsp页面引入webRoot下的Js、css和图片的问题

    通常把jsp页面放在webRoot的/WEB-INF下可以防止访问者直接输入页面. 而webRoot的/WEB-INF下的页面是受保护的,用户无法通过形如http://localhost:8080/t ...

  6. requests库session保持持久会话

      requests中cookie的原理 http://blog.csdn.net/zhu_free/article/details/50563756   requests - cookies的实现例 ...

  7. uni-app、Vue3 + ucharts 图表 H5 无法渲染

    文章已收录到 github,欢迎 Watch 和 Star. 简介 从问题定位开始,到给框架(uni-app)提 issue.出解决方案(PR),再到最后的思考,详细记录了整个过程. 前序 当你在业务 ...

  8. Spring Boot AOP 扫盲,实现接口访问的统一日志记录

    AOP 是 Spring 体系中非常重要的两个概念之一(另外一个是 IoC),今天这篇文章就来带大家通过实战的方式,在编程猫 SpringBoot 项目中使用 AOP 技术为 controller 层 ...

  9. Spring cloud是什么? 核心总结

    Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而简化了开发者的代码量. S ...

  10. ios开发 将json格式数据上传服务器

    看了一些大小牛的资料其实就3步 1.使用post 请求 ,因为get是不能上传的 2.设置请求类型 , 讲你的json数据上传 3.向服务器发送数据按照下面示例代码,就差不多了 1 // 1.创建请求 ...