LoginSignup
0
0

More than 5 years have passed since last update.

RDKitをc++で動かす ~git cloneからHello methaneまで~

Last updated at Posted at 2016-11-17

*この文章は2016.09.4バージョンを使うものとして書かれています。2017.03.01バージョンでは指定するlibの名前がいちいち異なります。

ある程度前にOpen Babelをc++で動かしていたが、RDKitのほうがpythonで数倍便利ってイメージがあるのでそちらを利用する。
…ところで、RDKitをc++で利用するのって日本語はおろか英語でもほとんど資料がないんですが…

git clone

RDKitは以下のgithubで開発をされているので、cloneしてくる。
https://github.com/rdkit/rdkit
後述の理由により、インストールしたい場所でgit cloneをするのが望ましい。

cd /path/to/install
git clone https://github.com/rdkit/rdkit.git
cd rdkit
git checkout origin/Release_2016_09 # ver 2016.09.4

cmake

実は、RDKitは-DCMAKE_INSTALL_PREFIXを指定しても全く役に立たない。
というか、make installしても移動させてくれない。
なのでインストールしたいパスでgit cloneすることを推奨している。

mkdir build
cd build
cmake ..

make

make install

library pathを通す

最後に、LIBRARY_PATHなどを通す。
以下はlinux SUSE上での例。

export RDKIT_INSTALL_PATH=/path/to/install
export LIBRARY_PATH=${RDKIT_INSTALL_PATH}/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=${RDKIT_INSTALL_PATH}/lib:$LD_LIBRARY_PATH

これでコードを書く準備が完了。

c++コード

とりあえず、methaneをsdfで読み込み、"Hello "の文字列とともにsmilesを表示させてみる。

hello_rdkit.cpp
#include <iostream>
#include <GraphMol/FileParsers/MolSupplier.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>

int main(void){
  RDKit::SDMolSupplier supplier("methane.sdf");
  RDKit::ROMol *mol;
  mol = supplier.next();
  std::cout << "Hello " << RDKit::MolToSmiles(*mol) << "!" << std::endl;
  delete mol;  //MolToSmilesは中でnew ROMolをしている
  return 0;
}

コードのコンパイル

methane.sdfを用意したうえで・・・

g++ hello_rdkit.cpp -I /path/to/install/Code -L /path/to/install/lib -lFileParsers -lSmilesParse

これを実行してやると、Hello C!と。できた。

補足

-Iは /path/to/install/includeでないことに注意。
libraryも指定する必要があるが、どうやらincludeするパスの最後のdir名を指定してやれば大丈夫っぽい。

#include <GraphMol/FileParsers/MolSupplier.h> // -lFileParsers
#include <GraphMol/ROMol.h>                   // -lGraphMol
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0