Use RDKit from Rust #RDKit #rdkitcffi #Rust

Recently Rust is becoming popular language and I have interest Rust. There is a hurdle for me to move programming language from python to others because I would like to use RDKit from my coding environment for chemoinformatics tasks ;)

As many chemoinformaticians know that recently rdkit provides new C Foreign Function Interface (CFFI). And many language supports cffi also Rust supports it too. It means that we can use minimal function of rdkit from Rust. It sound great isn’t it.

And I found really cool project in github named ‘rdkitcffi’ which is rdkit wrapper for Rust.

So I tried to use the crate ;)

At first, install rust and install other required packages and clone rdkitcffi.

$ curl --proto '=https' --tlsv1.2 -sSf | sh

# my os is ubuntu 20.04 LTS 
$ sudo apt-get install build-essential
$ sudo apt-get install libclang-dev
$ gh repo clone chrissly31415/rdkitcffi

The of an original code defined ld_library_path as relative but to use the package as library, I modified it from relative to absolute path. And added ‘rdkitcffi_linux/linux-64/’ path to LD_LIBRARY_PATH (environment variable).

# rdkitcffi/

#from relative path
let shared_lib_dir = "./lib/rdkitcffi_linux/linux-64/";

#to absolute path
let shared_lib_dir = "/home/user/hogehoge/lib/rdkitcffi_linux/linux-64/";

Now almost there, let make new project. And add rdkitcffi in dependency of Cargo.toml. rdkitcffi is not published in so local crate is used.

$ cargo new rdkrust
$ cat rdkrust/Cargo.toml

name = "rdkrust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at

rdkitcffi = {path="/home/hogehoge/rusttest/rdkitcffi"} 

Then let’s write src/ for compound descriptor calculation from sdf.

# ./src/
use std::env;
use rdkitcffi::{Molecule, read_sdfile};

fn main() {
    let args: Vec<String> = env::args().collect();
    let sd_filename = &args[1];
    println!("filename is {}", sd_filename);
    let mut mol_opt_list: Vec<Option<Molecule>> = read_sdfile(sd_filename);
    let mut mol_list: Vec<Molecule> = mol_opt_list.into_iter().filter_map(|m| m).collect();
    mol_list.iter_mut().for_each(|m| m.remove_all_hs());
    for m in mol_list {
        let desc = m.get_descriptors();
        println!("{}", desc)

OK let’s build the code.

$ cargo build
# build rdkitcffi wrapper and sample script

After the process, main command tool is generated, the tool can calculate molecular descriptors and returns it like below.

rust_rdkit$ time ./target/debug/rdkrust cdk2.sdf 
filename is cdk2.sdf

real	0m0.085s
user	0m0.077s
sys	0m0.004s

rdkitcffi supports not only descriptor calculation but also other function of rdkit. And Rust has lots of useful package like python pandas, plotly, scikit-learn etc….

I would like to write more code with rust.

Today’s my code is uploaded my githu repo. Thanks for reading.


Published by iwatobipen

I'm medicinal chemist in mid size of pharmaceutical company. I love chemoinfo, cording, organic synthesis, my family.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: