ホーム 暮らし How to Build a Python Environment for Cheminformatics Using Docker [RDKit / Py3Dmol / VScode / Cursor]

How to Build a Python Environment for Cheminformatics Using Docker [RDKit / Py3Dmol / VScode / Cursor]

三浦 拓海 三浦 拓海 • 更新: • 公開: • 読了約 18 分
目次

In this article, I will introduce how to build an environment where you can use the cheminformatics libraries I usually use.
In this day and age, I'm sure you could ask an AI to tell you, but I don't think it's right to post code in other articles without explaining the environment to run it in, so I would be happy if you could use the following steps to run the programs.

In the environment I am introducing,
in addition to basic data analysis libraries (numpy, pandas, matplotlib, seaborn, etc.), machine learning libraries such as scikit-learn, lightgbm, xgboost are also installed.
Regarding cheminformatics, it includes RDKit, Py3Dmol, PubChemPy, deepchem, mordred, etc.
At the end of the article, I will also introduce how to use the Docker environment with VScode and Cursor, but since I have installed Jupyter, you can also execute code from your browser.

Building an environment using Docker

nc6f632201216

We will use a tool called Docker to build the environment.

For information about Docker, the explanation by the author of this article is easy to understand.

Once you have installed Docker, create files by referring to the following project structure. You can name the "chem_ds_env" part whatever you like.
The src file uses a technique called mounting so that the virtual environment and local files are shared.

chem_ds_env/
├── Dockerfile
├── docker-compose.yml
├── .dockerignore
├── requirements.txt
└── src/          ← pythonスクリプトを置く場所

I have summarized the roles of each file and tool in the figure below (created with nano banana Pro m(_ _)m).

Folder creation command (for copy-pasting)
If you execute the following code on the command line (directly under chem_ds_env), the necessary project folders shown in the figure above will be created.

mkdir chem_ds_env && cd $_
mkdir .devcontainer src
touch Dockerfile docker-compose.yml .dockerignore requirements.txt 

You can connect to Jupyter Notebook by accessing "http://localhost:8888/" in your browser.

Configuration files

Once each file has been created, copy and paste the following code into each respective file.

Dockerfile

This is a file that manages the installation of packages and libraries necessary for building the environment.

# Python環境を構築
FROM python:3.11-slim
作業ディレクトリ
WORKDIR /workspace
システム依存のインストール
RUN apt-get update && apt-get install -y \
    build-essential git \
    libxrender1 libxext6 libsm6 \
 && rm -rf /var/lib/apt/lists/*
必要なパッケージをインストール
COPY requirements.txt .

RUN pip install --upgrade pip && \
    pip install --default-timeout=1000 --no-cache-dir -r requirements.txt
作業ディレクトリに移動
WORKDIR /workspace/src
デフォルトはJupyter Labを起動
CMD ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--no-browser", "--NotebookApp.token="]
requirements.txt

By using requirements.txt, you can install the necessary libraries all at once.
If there are other libraries you need, you can add them here and rebuild the environment to use them.

numpy==1.24.3
pandas==2.1.1
matplotlib==3.8.0
seaborn==0.13.0
scikit-learn==1.3.1
plotly==5.17.0
jupyterlab==4.0.7
ipykernel==6.25.2
lightgbm==4.3.0
xgboost==2.0.3
rdkit==2023.09.6
py3Dmol==2.2.0
PubChemPy==1.0.4
mordred==1.2.0
deepchem
tensorflow==2.15
docker-compose.yml

This is a file for managing the virtual environment built with Docker.

version: "3.3"
services:
  python:
    build: .
    container_name: chem_ds_env
    volumes:
      - ./src:/workspace/src  
    ports:
      - "8888:8888"          
    environment:
      - JUPYTER_ENABLE_LAB=yes
.dockerignore

.dockerignore allows you to specify files to ignore when building with Docker, similar to .gitignore. It is not strictly necessary, but it can slightly shorten the environment build time and save space in the virtual environment.

.git
pycache/
.ipynb_checkpoints/
.py[cod]
*.so
*.dylib
/data/
.vscode/
.DS_Store

Starting up the environment

Run the following command on the command line in the directory where you created the project (where the Dockerfile is located).

docker compose up -d

Once executed, the installation and environment startup will begin. Depending on your internet connection, this will take about 5 to 10 minutes.

When it finishes successfully, the "status" returned by the "docker ps -a" command will be "up".

You can stop and restart the environment by specifying the "CONTAINER ID" string, which can be checked with the "docker ps -a" command, as shown below. Be sure to stop it when not in use.

To stop the container (environment):

docker stop "コンテナID"

To restart the container (environment):

docker restart "コンテナID"

You can do it with:

As described later, this article introduces how to run it in VSCode or Cursor, but if you prefer Jupyter, you can access it by going to "http://localhost:8888" in your browser once the environment is running.

How to use the Docker environment in VSCode or Cursor

Although I will explain using VSCode images, the procedure is the same for both VSCode and Cursor. Depending on the version or language settings, the images may differ slightly, but please proceed by referring to the following.

Open VSCode and click the "> <" icon in the bottom left.

Open "Attach to Running Container".

The environment you created earlier will be displayed, so click on it.

A new window will then open, and you will see a screen like the one below.

Click the top icon on the top left, and when it says "Connected to Remote," click "Open Folder".

It will initially show "/root," but delete that and enter "workspace/src" to open the mounted folder.

Verifying if code can be executed: Displaying a 3D molecular model with py3Dmol

Open "src" and create a file named "caffeine_test.ipynb".

Paste the following code into "caffeine_test.ipynb". This will allow you to pull caffeine data from pubchempy and display it using py3Dmol.

from rdkit import Chem
from rdkit.Chem import AllChem
import pubchempy as pcp
import py3Dmol

mol = Chem.MolFromSmiles("Cn1cnc2c1c(=O)n(c(=O)n2C)C")
mol = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol)
AllChem.UFFOptimizeMolecule(mol)
3D表示(py3Dmol)
mb = Chem.MolToMolBlock(mol)
viewer = py3Dmol.view(width=400, height=300)
viewer.addModel(mb, "mol")
viewer.setStyle("stick": )
viewer.setBackgroundColor("0xeeeeee")
viewer.zoomTo()
viewer.show()

Once executed, a 3D graph of the molecule will be displayed as shown below. You have successfully run a Jupyter file in VSCode!

Conclusion

I have introduced how to build a Python environment for cheminformatics using Docker.
Docker is a highly recommended tool because it offers high reproducibility for environment setup and requires minimal effort.

It is very convenient to be able to open it in VSCode or Cursor. Please try utilizing this environment for machine learning and data analysis.

I will continue to write articles on this Note about doing data science in chemistry, focusing on RDKit, so please check them out!

Likes and follows are great motivators for me!
Thank you for reading until the end!


同じカテゴリの記事

三浦 拓海
コンテンツディレクター 三浦 拓海
プロフィールを見る →

映画と連続ドラマの公開情報を追う担当。配給会社のプレスシートと公式サイトの発表日を確認したうえで執筆します。

おすすめの関連記事