Call RDKit from Rust with conda env ver2 #RDKit #RDKit-sys #Rust #Chemoinformatics

To use rdkit from Rust, I introduced rdkit-sys before. And fortunately recent version of rdkit-sys cleat supports rdkit-env. It’s worth to use conda-env to build rdkit-sys because user don’t need to build rdkit from source code.

Following code is almost same as my previous post but I would like to share it.

At first, I cloned rdkit from rdkit-rs.

$ gh repo clone rdkit-rs/rdkit
$ cd rdkit

Then edit Cargo.toml. I modified dependencies part as below. Added features=[“dynamic-linking-from-conda”] option. And then added LD_LIBRARY_PATH to use it.

name = "rdkit"
version = "0.2.11"
edition = "2021"
authors = ["Xavier Lange <>"]
license = "MIT"
description = "High level RDKit functionality for rust"
# See more keys and their definitions at

bitvec = "1"
cxx = "1"
log = "0.4"
#iwatobipen modified
rdkit-sys = {version="0.2.14", features=["dynamic-linking-from-conda"]}
flate2 = "1"

env_logger = "0.9.0"

## from bash
(base) $ conda activate chemo_py310
(chemo_py310)$ cargo test # all tests will pass

Then I wrote rust_rdkit_v3

$ cargo new rust_rdkit_v3
$ cd rdkit_rust
$ vim src/
use std::env;
use std::path::PathBuf;
use rdkit;

fn main() {
    let args: Vec<String> = env::args().collect();
    let sdgz_filename = &args[1];

    println!("filename is {}", sdgz_filename);
    let mol_block_iter =
        rdkit::MolBlockIter::from_gz_file(sdgz_filename, false, false, false).unwrap();
    let mol_list = mol_block_iter.collect::<Vec<_>>();
    for m in mol_list {
        let smi = m.unwrap().as_smile();
        println!("{}", smi)

$ vim Cargo.toml
name = "rust_rdkit_v3"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at

#rdkit = "0.2.11"

After writing the code, build it.

$ cargo build --release

Now I could run code.

./target/release/rust_rdkit_v3 cdk2.sdf.gz
filename is cdk2.sdf.gz

In summary current rdkit-sys supports conda env and it makes easy to call rdkit from rust.



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 )

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: