### Install Uni-Mol Tools via PyPi Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/installation.md This command installs the `unimol_tools` package from PyPi, which is the recommended method for getting started with Uni-Mol. It provides the core functionalities of the Uni-Mol library, ensuring easy setup and access to its features. ```bash pip install unimol_tools ``` -------------------------------- ### Install Uni-Mol Tools from Source Repository Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/installation.md This set of commands outlines the process for installing Uni-Mol tools directly from its source repository. It involves installing dependencies, cloning the repository, and then performing a local installation, providing more control over the environment and allowing for development contributions. ```bash ## Dependencies installation pip install -r requirements.txt ## Clone repository git clone https://github.com/deepmodeling/Uni-Mol.git cd Uni-Mol/unimol_tools ## Install python setup.py install ``` -------------------------------- ### Install Hugging Face Hub for Model Downloads Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/installation.md This command installs the `huggingface_hub` library, which is essential for automatically downloading required Uni-Mol models at runtime. It simplifies model management and access from the Hugging Face Hub, ensuring models are available when needed. ```bash pip install huggingface_hub ``` -------------------------------- ### Example of Distributed Data Parallel (DDP) Training with MolTrain Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/quickstart.md This example demonstrates how to initialize and use the `MolTrain` class with Distributed Data Parallel (DDP) enabled. It shows how to configure `use_ddp` and `use_gpu` parameters for training across multiple GPUs. ```python from unimol_tools import MolTrain # Initialize the training class with DDP enabled if __name__ == '__main__': clf = MolTrain( task='regression', data_type='molecule', epochs=10, batch_size=16, save_path='./model_dir', remove_hs=False, target_cols='TARGET', use_ddp=True, use_gpu="all" ) pred = clf.fit(data = train_data) ``` -------------------------------- ### Set Environment Variables for Multi-Node Distributed Training Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/quickstart.md This Bash snippet illustrates how to export `MASTER_ADDR` and `MASTER_PORT` environment variables. These variables are essential for establishing communication between processes in a multi-node distributed training setup, ensuring proper coordination across different machines. ```Bash export MASTER_ADDR='localhost' export MASTER_PORT='19198' ``` -------------------------------- ### Install Uni-Mol Tools from PyPi Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_tools/README.md This command installs the Uni-Mol tools library from PyPi, ensuring you get the stable version. It's recommended for most users. ```bash pip install unimol_tools --upgrade ``` -------------------------------- ### Configure Hugging Face Hub Mirror for Faster Downloads Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/installation.md This command sets the `HF_ENDPOINT` environment variable, allowing users to specify an alternative mirror address for the Hugging Face Hub. This is particularly useful for accelerating model downloads when the default endpoint is slow, improving efficiency for users in various regions. ```bash export HF_ENDPOINT=https://hf-mirror.com ``` -------------------------------- ### Continue Training an Existing UniMol Model Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/quickstart.md This snippet demonstrates how to resume training a previously saved UniMol model. It shows an initial training phase saving to a directory, followed by a second training phase that loads the model from that directory to continue training on new data. ```python clf = MolTrain( task='regression', data_type='molecule', epochs=10, batch_size=16, save_path='./model_dir', remove_hs=False, target_cols='TARGET', ) pred = clf.fit(data = train_data) # After train a model, set load_model_dir='./model_dir' to continue training clf2 = MolTrain( task='regression', data_type='molecule', epochs=10, batch_size=16, save_path='./retrain_model_dir', remove_hs=False, target_cols='TARGET', load_model_dir='./model_dir', ) pred2 = clf.fit(data = retrain_data) ``` -------------------------------- ### Install Hugging Face Hub for Uni-Mol Models Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_tools/README.md Installing `huggingface_hub` enables automatic download and management of required Uni-Mol models from the Hugging Face Hub at runtime, which is crucial for using Uni-Mol's pre-trained models. ```bash pip install huggingface_hub ``` -------------------------------- ### Install huggingface_hub for Uni-Mol Model Downloads Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/weight.rst This command installs the `huggingface_hub` Python library. It is recommended for Uni-Mol users as it enables automatic downloading and management of required Uni-Mol models directly from the Hugging Face Hub at runtime. ```bash pip install huggingface_hub ``` -------------------------------- ### Install Uni-Mol+ Python Dependencies Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_plus/README.md This section details the installation of Uni-Mol+'s Python dependencies. It includes commands for `rdkit==2022.09.3`, `numba`, and `pandas` using pip. Users should also refer to Uni-Core's documentation for its installation. ```bash pip install rdkit==2022.09.3 pip install numba pandas ``` -------------------------------- ### Install Uni-Mol Tools from Source Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_tools/README.md This method allows installation of the latest version of Uni-Mol tools directly from the source repository. It involves installing dependencies, cloning the repository, and then installing the package. ```bash ## Dependencies installation pip install -r requirements.txt ## Clone repository git clone https://github.com/deepmodeling/Uni-Mol.git cd Uni-Mol/unimol_tools ## Install python setup.py install ``` -------------------------------- ### Train and Predict Molecule Properties with UniMol Tools Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/quickstart.md This snippet demonstrates how to train a molecule property prediction model using `unimol_tools.MolTrain` and then make predictions with `unimol_tools.MolPredict`. It supports both single and multi-tasking, accepting CSV files with SMILES and TARGET columns, or custom dictionary inputs. The example shows training a classification model and then loading a saved model for prediction. ```python from unimol_tools import MolTrain, MolPredict clf = MolTrain(task='classification', data_type='molecule', epochs=10, batch_size=16, metrics='auc', model_name='unimolv1', # avaliable: unimolv1, unimolv2 model_size='84m', # work when model_name is unimolv2. avaliable: 84m, 164m, 310m, 570m, 1.1B. ) pred = clf.fit(data = train_data) # currently support data with smiles based csv/txt file clf = MolPredict(load_model='../exp') res = clf.predict(data = test_data) ``` -------------------------------- ### Execute Uni-Mol Docking V2 Training Script Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_docking_v2/README.md Command to start the training process for Uni-Mol Docking V2. Before running, ensure that 'data_path', 'save_dir', 'finetune_mol_model', and 'finetune_pocket_model' are correctly specified within the 'train.sh' script, using pretrained molecular and pocket model weights from the Uni-Mol repo. ```bash bash train.sh ``` -------------------------------- ### Install Uni-Mol Docking V2 Dependencies Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_docking_v2/README.md Instructions to install required Python packages for Uni-Mol Docking V2, including rdkit-pypi, biopandas, tqdm, and scikit-learn. These dependencies are crucial for running the Uni-Mol Docking V2 model. ```bash pip install rdkit-pypi==2022.9.3 -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn pip install biopandas tqdm scikit-learn ``` -------------------------------- ### MolTrain Class Distributed Data Parallel (DDP) Parameters Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/quickstart.md Documentation for parameters related to Distributed Data Parallel (DDP) training within the `MolTrain` class. DDP enables training across multiple GPUs or nodes for improved performance. ```APIDOC MolTrain Parameters: use_ddp: bool, default=True Description: Whether to enable Distributed Data Parallel (DDP). use_gpu: str, default='all' Description: Specifies which GPUs to use. 'all' means all available GPUs, while '0,1,2' means using GPUs 0, 1, and 2. ``` -------------------------------- ### Generate Uni-Mol Molecule and Atomic Level Representations Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/quickstart.md This example illustrates how to obtain Uni-Mol representations for molecules using `unimol_tools.UniMolRepr`. It shows how to get both the CLS token representation and atomic-level representations for a given SMILES string, aligning with RDKit's atom indexing. ```python import numpy as np from unimol_tools import UniMolRepr # single smiles unimol representation clf = UniMolRepr(data_type='molecule', remove_hs=False, model_name='unimolv1', # avaliable: unimolv1, unimolv2 model_size='84m', # work when model_name is unimolv2. avaliable: 84m, 164m, 310m, 570m, 1.1B. ) smiles = 'c1ccc(cc1)C2=NCC(=O)Nc3c2cc(cc3)[N+](=O)[O]' smiles_list = [smiles] unimol_repr = clf.get_repr(smiles_list, return_atomic_reprs=True) # CLS token repr print(np.array(unimol_repr['cls_repr']).shape) # atomic level repr, align with rdkit mol.GetAtoms() print(np.array(unimol_repr['atomic_reprs']).shape) ``` -------------------------------- ### Python Multiprocessing `RuntimeError` Example Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/quickstart.md This snippet presents a common `RuntimeError` that occurs in Python multiprocessing contexts when the main execution logic is not encapsulated within `if __name__ == '__main__':`. This error typically arises because child processes re-import the main module, leading to premature process initiation and bootstrapping issues. ```Python RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module: if __name__ == '__main__': freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable. ``` -------------------------------- ### Install Uni-Mol Colab Dependencies Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/notebooks/unimol_binding_pose_demo.ipynb This bash script installs all necessary dependencies for the Uni-Mol binding pose prediction Colab notebook. It clones the Uni-Mol repository, installs Uni-Core, Uni-Mol, RDKit, Biopandas, and Py3Dmol, and downloads required docking data and model weights. It includes a check to prevent re-installation if dependencies are already set up. ```bash %%bash #@title Install dependencies GIT_REPO='https://github.com/deepmodeling/Uni-Mol' UNICORE_URL='https://github.com/dptech-corp/Uni-Core/releases/download/0.0.2/unicore-0.0.1+cu116torch1.13.1-cp39-cp39-linux_x86_64.whl' DOCKING_DATA_URL='https://github.com/deepmodeling/Uni-Mol/releases/download/v0.1/CASF-2016.tar.gz' DOCKING_WEIGHT_URL='https://github.com/deepmodeling/Uni-Mol/releases/download/v0.1/binding_pose_220908.pt' if [ ! -f UNIMOL_READY ]; then wget -q ${UNICORE_URL} pip3 -q install "unicore-0.0.1+cu116torch1.13.1-cp39-cp39-linux_x86_64.whl" rm -rf ./Uni-Mol git clone -b main ${GIT_REPO} pip3 install -q ./Uni-Mol/unimol pip install -q rdkit pip install -q biopandas wget -q ${DOCKING_DATA_URL} tar -xzf "CASF-2016.tar.gz" wget -q ${DOCKING_WEIGHT_URL} pip install -q py3Dmol touch UNIMOL_READY fi ``` -------------------------------- ### Initialize UniMolRepr with DDP for Molecular Representations Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/quickstart.md This Python code demonstrates how to initialize the `UniMolRepr` class, enabling Distributed Data Parallel (DDP) across specified GPUs (0 and 1) for generating molecular representations. It then shows how to retrieve both CLS token and atomic-level representations using the `get_repr` method. A key consideration is that DDP may not be optimal for small SMILES lists due to communication overhead. ```Python if __name__ == '__main__': repr_model = UniMolRepr( data_type='molecule', batch_size=32, remove_hs=False, model_name='unimolv2', model_size='84m', use_ddp=True, # Enable Distributed Data Parallel use_gpu='0,1' # Use GPU 0 and 1 ) unimol_repr = repr_model.get_repr(smiles_list, return_atomic_reprs=True) # CLS token representation print(unimol_repr['cls_repr']) # Atomic-level representation print(unimol_repr['atomic_reprs']) ``` -------------------------------- ### Run Pocket Pretraining with Uni-Mol Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This script performs pocket pretraining using Uni-Mol, configured for an 8-GPU V100 setup. It is similar to molecular pretraining but specifically tailored for pocket data, including a 'dict_name' parameter. Users should adjust 'batch_size' or 'update_freq' to match their environment's capabilities. ```bash data_path=./example_data/pocket/ # replace to your data path save_dir=./save/ # replace to your save path n_gpu=8 MASTER_PORT=10086 dict_name="dict_coarse.txt" lr=1e-4 wd=1e-4 batch_size=16 update_freq=1 masked_token_loss=1 masked_coord_loss=1 masked_dist_loss=1 x_norm_loss=0.01 delta_pair_repr_norm_loss=0.01 mask_prob=0.15 noise_type="uniform" noise=1.0 seed=1 warmup_steps=10000 max_steps=1000000 export NCCL_ASYNC_ERROR_HANDLING=1 export OMP_NUM_THREADS=1 python -m torch.distributed.launch --nproc_per_node=$n_gpu --master_port=$MASTER_PORT $(which unicore-train) $data_path --user-dir ./unimol --train-subset train --valid-subset valid \ --num-workers 8 --ddp-backend=c10d \ --task unimol_pocket --loss unimol --arch unimol_base \ --optimizer adam --adam-betas "(0.9, 0.99)" --adam-eps 1e-6 --clip-norm 1.0 --weight-decay $wd \ --lr-scheduler polynomial_decay --lr $lr --warmup-updates $warmup_steps --total-num-update $max_steps \ --update-freq $update_freq --seed $seed \ --dict-name $dict_name \ --fp16 --fp16-init-scale 4 --fp16-scale-window 256 --tensorboard-logdir $save_dir/tsb \ --max-update $max_steps --log-interval 10 --log-format simple \ --save-interval-updates 10000 --validate-interval-updates 10000 --keep-interval-updates 10 \ --masked-token-loss $masked_token_loss --masked-coord-loss $masked_coord_loss --masked-dist-loss $masked_dist_loss \ --x-norm-loss $x_norm_loss --delta-pair-repr-norm-loss $delta_pair_repr_norm_loss \ --mask-prob $mask_prob --noise-type $noise_type --noise $noise --batch-size $batch_size \ --save-dir $save_dir ``` -------------------------------- ### Generate Uni-Mol Molecule and Atom-Level Representations Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_tools/README.md This Python example shows how to obtain Uni-Mol representations for molecules and their constituent atoms using the `UniMolRepr` class. It demonstrates how to get both the CLS token representation and atomic-level representations, aligned with RDKit's atom indexing. ```python import numpy as np from unimol_tools import UniMolRepr # single smiles unimol representation clf = UniMolRepr(data_type='molecule', remove_hs=False) smiles = 'c1ccc(cc1)C2=NCC(=O)Nc3c2cc(cc3)[N+](=O)[O]' smiles_list = [smiles] unimol_repr = clf.get_repr(smiles_list, return_atomic_reprs=True) # CLS token repr print(np.array(unimol_repr['cls_repr']).shape) # atomic level repr, align with rdkit mol.GetAtoms() print(np.array(unimol_repr['atomic_reprs']).shape) ``` -------------------------------- ### Download PCQM4MV2 Dataset Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_plus/README.md This command sequence initiates the download of the PCQM4MV2 dataset. It first navigates into the 'scripts' directory and then executes the `download.sh` bash script, which fetches the necessary data files for Uni-Mol+. ```bash cd scripts bash download.sh ``` -------------------------------- ### Run Uni-Mol Interface Demo Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_docking_v2/README.md Executes the main demo script for the Uni-Mol interface. An alternative script (`demo_batch_one2one.sh`) is available for batch mode operations, as referenced in the comment. ```bash cd interface bash demo.sh # demo_batch_one2one.sh for batch mode ``` -------------------------------- ### Execute IFD Docking Workflow Scripts Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This snippet provides shell commands to download data, run training and inference scripts for IFD docking, and generate submission files. Ensure data is decompressed to `./examples/ifd_docking` before execution. ```Shell sh train_ifd.sh sh infer_ifd.sh cd ./examples/ifd_scoring && python generate_submit.py ``` -------------------------------- ### Python Project Dependency List Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol2/requirements.txt This snippet lists the essential Python packages required for the Uni-Mol project. It includes a direct installation from the Uni-Core Git repository at a stable branch and a specific version of the rdkit-pypi library. ```Python git+git://github.com/dptech-corp/Uni-Core.git@stable#egg=Uni-Core rdkit-pypi==2022.9.5 ``` -------------------------------- ### Example Usage: Calculate and Print RMSD Metrics Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_docking_v2/interface/posebuster_demo.ipynb Demonstrates how to use the `cal_rmsd_metrics` function to compute RMSD values for a set of predicted ligands and then utilize `print_result` to display the statistical summary for both basic and symmetric RMSD results. ```python rmsd_results, rmsd_sym_results = cal_rmsd_metrics(predict_dir = predict_sdf_dir, reference_dir=ligand_path, meta_info_file=meta_info_file) print_result(rmsd_results) print_result(rmsd_sym_results) ``` -------------------------------- ### Import Core Modules for Uni-Mol Project Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/notebooks/unimol_posebuster_demo.ipynb This snippet imports essential Python libraries required for the Uni-Mol project, including `os`, `pickle`, `numpy`, `pandas`, and `rdkit` for cheminformatics operations. It also includes `tqdm` for progress tracking, `multiprocessing` for parallel processing, `lmdb` for database management, `biopandas` for PDB file handling, and `sklearn` for clustering. RDKit logger output is disabled, and warnings are ignored to ensure cleaner execution. ```python import os import pickle import numpy as np import pandas as pd from rdkit import Chem, RDLogger from rdkit.Chem import AllChem from tqdm import tqdm RDLogger.DisableLog('rdApp.*') import warnings warnings.filterwarnings(action='ignore') from multiprocessing import Pool import copy import lmdb from biopandas.pdb import PandasPdb from sklearn.cluster import KMeans from rdkit.Chem.rdMolAlign import AlignMolConformers ``` -------------------------------- ### Generate RDKit Initial Conformations for QM9 Dataset Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This snippet demonstrates how to generate initial RDKit conformations for a dataset, typically as a preparatory step for conformation generation tasks. It specifies the number of threads, a reference file, and an output directory for the generated data. ```bash mode="gen_data" nthreads=20 # Num of threads reference_file="./conformation_generation/qm9/test_data_200.pkl" # Your reference file dir output_dir="./conformation_generation/qm9" # Generated initial data dir python ./unimol/utils/conf_gen_cal_metrics.py --mode $mode --nthreads $nthreads --reference-file $reference_file --output-dir $output_dir ``` -------------------------------- ### Execute Distributed Uni-Mol Model Training with PyTorch Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This command launches a distributed training job for the Uni-Mol model using `torch.distributed.launch`. It configures various training parameters such as data paths, task name, architecture, optimizer, learning rate scheduler, batch size, and logging. It also specifies the finetuning model path and best checkpoint metric. ```Shell export NCCL_ASYNC_ERROR_HANDLING=1 export OMP_NUM_THREADS=1 update_freq=`expr $batch_size / $local_batch_size` python -m torch.distributed.launch --nproc_per_node=$n_gpu --master_port=$MASTER_PORT $(which unicore-train) $data_path --task-name $task_name --user-dir ./unimol --train-subset train --valid-subset valid \ --conf-size $conf_size \ --num-workers 8 --ddp-backend=c10d \ --dict-name $dict_name \ --task mol_finetune --loss $loss_func --arch unimol_base \ --classification-head-name $task_name --num-classes $task_num \ --optimizer adam --adam-betas "(0.9, 0.99)" --adam-eps 1e-6 --clip-norm 1.0 \ --lr-scheduler polynomial_decay --lr $lr --warmup-ratio $warmup --max-epoch $epoch --batch-size $local_batch_size --pooler-dropout $dropout\ --update-freq $update_freq --seed $seed \ --fp16 --fp16-init-scale 4 --fp16-scale-window 256 \ --log-interval 100 --log-format simple \ --validate-interval 1 \ --finetune-from-model $weight_path \ --best-checkpoint-metric $metric --patience 20 \ --save-dir $save_dir --only-polar $only_polar ``` -------------------------------- ### Pull Uni-Mol Docker Image Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This command pulls the latest Uni-Mol Docker image, which includes PyTorch 1.11.0 and CUDA 11.3. It is essential for running Uni-Mol within a containerized environment, especially when utilizing GPUs. Ensure nvidia-docker-2 is installed for GPU support. ```bash docker pull dptechnology/unimol:latest-pytorch1.11.0-cuda11.3 ``` -------------------------------- ### Perform Molecular Docking Inference with Uni-Mol Model Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_docking_v2/interface/posebuster_demo.ipynb Executes the `demo.py` script for batch molecular docking inference using a pre-trained Uni-Mol model. This command specifies input and output directories, batch size, steric clash fixing, and clustering options, leveraging the previously generated meta-information file. ```bash predict_sdf_dir = f'predict_sdf_posebuster428_grid{add_size}' !python demo.py --mode batch_one2one --batch-size 8 --steric-clash-fix --conf-size 10 --cluster \ --input-batch-file $input_meta_info_file \ --output-ligand-dir $predict_sdf_dir \ --model-dir checkpoint_best.pt ``` -------------------------------- ### Archive and Download Uni-Mol Project Files Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/notebooks/unimol_binding_pose_demo.ipynb This snippet performs two key actions: first, it uses a shell command (`!zip`) to create a ZIP archive containing all files specified in `file_lists`, dynamically naming the archive based on `pdb_id`. Second, it utilizes `files.download` (a common function in environments like Google Colab) to initiate the download of the newly created ZIP file to the user's local system, facilitating easy export of processed data. ```bash !zip -j {"unimol.docking."+pdb_id}.zip {" ".join(file_lists)} ``` ```python files.download(f'{"unimol.docking."+pdb_id}.zip') ``` -------------------------------- ### Configure Hugging Face Mirror for Faster Downloads Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_tools/README.md This command sets the `HF_ENDPOINT` environment variable to use an alternative mirror for the Hugging Face Hub, which can significantly speed up model downloads if the default connection is slow. ```bash export HF_ENDPOINT=https://hf-mirror.com ``` -------------------------------- ### Run Molecular Pretraining with Uni-Mol Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This script executes molecular pretraining using Uni-Mol, configured for an 8-GPU V100 setup. It defines various hyperparameters such as data paths, learning rates, loss weights for masked tokens, coordinates, and distances, and logging settings. Users should adjust 'batch_size' or 'update_freq' based on their specific hardware environment. ```bash data_path=./example_data/molecule/ # replace to your data path save_dir=./save/ # replace to your save path n_gpu=8 MASTER_PORT=10086 lr=1e-4 wd=1e-4 batch_size=16 update_freq=1 masked_token_loss=1 masked_coord_loss=5 masked_dist_loss=10 x_norm_loss=0.01 delta_pair_repr_norm_loss=0.01 mask_prob=0.15 only_polar=0 noise_type="uniform" noise=1.0 seed=1 warmup_steps=10000 max_steps=1000000 export NCCL_ASYNC_ERROR_HANDLING=1 export OMP_NUM_THREADS=1 python -m torch.distributed.launch --nproc_per_node=$n_gpu --master_port=$MASTER_PORT $(which unicore-train) $data_path --user-dir ./unimol --train-subset train --valid-subset valid \ --num-workers 8 --ddp-backend=c10d \ --task unimol --loss unimol --arch unimol_base \ --optimizer adam --adam-betas "(0.9, 0.99)" --adam-eps 1e-6 --clip-norm 1.0 --weight-decay $wd \ --lr-scheduler polynomial_decay --lr $lr --warmup-updates $warmup_steps --total-num-update $max_steps \ --update-freq $update_freq --seed $seed \ --fp16 --fp16-init-scale 4 --fp16-scale-window 256 --tensorboard-logdir $save_dir/tsb \ --max-update $max_steps --log-interval 10 --log-format simple \ --save-interval-updates 10000 --validate-interval-updates 10000 --keep-interval-updates 10 --no-epoch-checkpoints \ --masked-token-loss $masked_token_loss --masked-coord-loss $masked_coord_loss --masked-dist-loss $masked_dist_loss \ --x-norm-loss $x_norm_loss --delta-pair-repr-norm-loss $delta_pair_repr_norm_loss \ --mask-prob $mask_prob --noise-type $noise_type --noise $noise --batch-size $batch_size \ --save-dir $save_dir --only-polar $only_polar ``` -------------------------------- ### Store Uni-Mol Data in LMDB Database Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/notebooks/unimol_binding_pose_demo.ipynb This Python function manages the creation and population of an LMDB database with processed protein-ligand data. It iterates through a list of SMILES strings for a given PDB ID, calls the `parser` function to generate the data, and then writes the serialized output to the LMDB file. It ensures the output directory exists and handles LMDB environment setup. ```python def write_lmdb(pdb_id, smiles_list, seed=42, result_dir="./results"): os.makedirs(result_dir, exist_ok=True) outputfilename = os.path.join(result_dir, pdb_id + ".lmdb") try: os.remove(outputfilename) except: pass env_new = lmdb.open( outputfilename, subdir=False, readonly=False, lock=False, readahead=False, meminit=False, max_readers=1, map_size=int(10e9), ) for i, smiles in enumerate(smiles_list): inner_output = parser(pdb_id, smiles, seed=seed) txn_write = env_new.begin(write=True) txn_write.put(f"{i}".encode("ascii"), inner_output) txn_write.commit() env_new.close() ``` -------------------------------- ### Finetuning Uni-Mol Model for Molecular Tasks Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/notebooks/unimol_mol_property_demo.ipynb This snippet demonstrates how to finetune a pre-trained Uni-Mol model for specific molecular tasks using `unicore-train`. It outlines the configuration of data paths, model weights, task parameters (e.g., classification head, number of classes), optimizer settings, and logging. The script uses `torch.distributed.launch` for distributed training. ```python data_path='./' # replace to your data path save_dir='./save_demo' # replace to your save path MASTER_PORT=10086 n_gpu=1 dict_name='dict.txt' weight_path='./weights/mol_pre_no_h_220816.pt' # replace to your ckpt path task_name='demo' # data folder name task_num=2 loss_func='finetune_cross_entropy' lr=1e-4 batch_size=32 epoch=5 dropout=0.1 warmup=0.06 local_batch_size=32 only_polar=0 # -1 all h; 0 no h conf_size=11 seed=0 metric="valid_agg_auc" update_freq=batch_size / local_batch_size !cp ../example_data/molecule/$dict_name $data_path !export NCCL_ASYNC_ERROR_HANDLING=1 !export OMP_NUM_THREADS=1 !python -m torch.distributed.launch --nproc_per_node=$n_gpu --master_port=$MASTER_PORT $(which unicore-train) $data_path --task-name $task_name --user-dir ../unimol --train-subset train --valid-subset valid \ --conf-size $conf_size \ --num-workers 8 --ddp-backend=c10d \ --dict-name $dict_name \ --task mol_finetune --loss $loss_func --arch unimol_base \ --classification-head-name $task_name --num-classes $task_num \ --optimizer adam --adam-betas '(0.9, 0.99)' --adam-eps 1e-6 --clip-norm 1.0 \ --lr-scheduler polynomial_decay --lr $lr --warmup-ratio $warmup --max-epoch $epoch --batch-size $local_batch_size --pooler-dropout $dropout\ --update-freq $update_freq --seed $seed \ --fp16 --fp16-init-scale 4 --fp16-scale-window 256 \ --log-interval 100 --log-format simple \ --validate-interval 1 --keep-last-epochs 10 \ --finetune-from-model $weight_path \ --best-checkpoint-metric $metric --patience 20 \ --save-dir $save_dir --only-polar $only_polar \ --maximize-best-checkpoint-metric # --maximize-best-checkpoint-metric, for classification task ``` -------------------------------- ### Import Core Modules for Molecular Docking Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_docking_v2/interface/posebuster_demo.ipynb Imports necessary Python libraries such as `json`, `numpy`, `pandas`, `tqdm`, `rdkit`, and `os` for molecular data processing, file operations, and progress tracking within the Uni-Mol project. ```python import json import numpy as np import pandas as pd from tqdm import tqdm from rdkit import Chem import os from typing import Optional from rdkit import Chem ``` -------------------------------- ### Docking and Calculate Metrics for Protein-ligand Binding Pose Prediction Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This command runs the docking process and calculates metrics using the inferred pose prediction file and a reference file. It specifies the number of threads and output paths for the docking results. ```bash nthreads=20 # Num of threads predict_file="./infer_pose/save_pose_test.out.pkl" # Your inference file dir reference_file="./protein_ligand_binding_pose_prediction/test.lmdb" # Your reference file dir output_path="./protein_ligand_binding_pose_prediction" # Docking results path python ./unimol/utils/docking.py --nthreads $nthreads --predict-file $predict_file --reference-file $reference_file --output-path $output_path ``` -------------------------------- ### Finetuning Uni-Mol for Molecular Conformation Generation Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This comprehensive bash script demonstrates how to finetune a Uni-Mol pretrained model on a training set for the conformation generation task. It sets up environment variables, defines various hyperparameters, and executes the `unicore-train` command using `torch.distributed.launch` for distributed training. ```bash data_path="./conformation_generation" # replace to your data path save_dir="./save_confgen" # replace to your save path n_gpu=1 MASTER_PORT=10086 dict_name="dict.txt" weight_path="./weights/checkpoint.pt" # replace to your ckpt path task_name="qm9" # or "drugs", conformation generation task name, as a part of complete data path recycles=4 coord_loss=1 distance_loss=1 beta=4.0 smooth=0.1 topN=20 lr=2e-5 batch_size=128 epoch=50 warmup=0.06 update_freq=1 export NCCL_ASYNC_ERROR_HANDLING=1 export OMP_NUM_THREADS=1 python -m torch.distributed.launch --nproc_per_node=$n_gpu --master_port=$MASTER_PORT $(which unicore-train) $data_path --task-name $task_name --user-dir ./unimol --train-subset train --valid-subset valid \ --num-workers 8 --ddp-backend=c10d \ --task mol_confG --loss mol_confG --arch mol_confG \ --optimizer adam --adam-betas "(0.9, 0.99)" --adam-eps 1e-6 --clip-norm 1.0 \ --lr-scheduler polynomial_decay --lr $lr --warmup-ratio $warmup --max-epoch $epoch --batch-size $batch_size \ --update-freq $update_freq --seed 1 \ --fp16 --fp16-init-scale 4 --fp16-scale-window 256 \ --log-interval 100 --log-format simple --tensorboard-logdir $save_dir/tsb \ --validate-interval 1 --keep-last-epochs 10 \ --keep-interval-updates 10 --best-checkpoint-metric loss --patience 50 --all-gather-list-size 102400 \ --finetune-mol-model $weight_path --save-dir $save_dir \ --coord-loss $coord_loss --distance-loss $distance_loss \ --num-recycles $recycles --beta $beta --smooth $smooth --topN $topN \ --find-unused-parameters ``` -------------------------------- ### Uni-Mol Docking V2 Inference Interface Parameters Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_docking_v2/README.md Documentation for the 'interface/demo.py' script, detailing its input and output parameters for single and various batch inference modes. It supports PDB files for protein, SDF files for ligand, and JSON files for docking grid information, with options for steric clash correction and batch processing via CSV. ```APIDOC interface/demo.py parameters: --input-protein: PDB file, absolute or relative path; list of paths in batch_one2one mode. --input-ligand: SDF file, absolute or relative path; list of paths in batch mode. --input-docking-grid: JSON file (center coordinate, box size), absolute or relative path; list of paths in batch mode. --output-ligand-name: str, the output SDF file name; list of names in batch mode. --output-ligand-dir: str, absolute or relative path. Other parameters: --input-batch-file: CSV file, used in batch mode, containing input_protein, input_ligand, input_docking_grid, and output_ligand_name. --steric-clash-fix: The predicted SDF file will be corrected for chemical detail and clash relaxation. --mode: optional values are 'single', 'batch_one2one' and 'batch_one2many'. - single: Represents one protein and one ligand as input. - batch_one2one: Represents a batch of proteins and a batch of ligands, where the relationship is one-to-one. ``` -------------------------------- ### Prepare Input Meta-Information CSV for Uni-Mol Interface Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_docking_v2/interface/posebuster_demo.ipynb This script generates a CSV file (`posebuster_428_one2one.csv`) that serves as the required input for the Uni-Mol interface. It populates a DataFrame with absolute paths to protein, ligand, and docking grid files, along with a predicted ligand name, based on the PoseBusters dataset. ```python df = pd.DataFrame(columns=['input_protein', 'input_ligand', 'input_docking_grid', 'output_ligand_name']) input_meta_info_file = 'posebuster_428_one2one.csv' predict_name_suffix = 'predict' for i, item in tqdm(enumerate(zip(pdb_ids,lig_ids))): pdbid, ligid = item single_protein_path = os.path.abspath(os.path.join(protein_path, pdbid + '.pdb')) single_ligand_path = os.path.abspath(os.path.join(ligand_path, f'{pdbid}_{ligid}.sdf')) single_grid_path = os.path.abspath(os.path.join(grid_path, pdbid + '.json')) predict_name = f'{pdbid}_{predict_name_suffix}' df.loc[i] = [single_protein_path, single_ligand_path, single_grid_path, predict_name] print(df.info()) print(df.head(3)) df.to_csv(input_meta_info_file, index= False) ``` -------------------------------- ### API Documentation for unimol_tools.utils.util Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/utils.rst This section documents the `util` module, which contains various utility methods, including padding functionalities, for the Uni-Mol tools. ```APIDOC Module: unimol_tools.utils.util Purpose: Contains some padding methods. API Reference: All public members of this module are documented here. ``` -------------------------------- ### API Documentation for unimol_tools.utils.config_handler Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/utils.rst This section documents the `config_handler` module, responsible for managing configuration input files for the Uni-Mol tools. ```APIDOC Module: unimol_tools.utils.config_handler Purpose: Manages the config input file. API Reference: All public members of this module are documented here. ``` -------------------------------- ### Switching Git Commit for Conformation Generation Reproduction Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md To reproduce the molecular conformation generation results from the paper, users should switch to a specific earlier commit. This command ensures the correct codebase version is used for subsequent steps. ```bash git checkout 37b0198cf68a349a854410a06777c2e7dacbce5e ``` -------------------------------- ### Train Uni-Mol+ Model on OC20 Dataset Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_plus/README.md Instructions for training the Uni-Mol+ model on the OC20 dataset. This involves setting data and save paths, learning rate, batch size (per GPU), and selecting the appropriate architecture before executing the training script. ```bash data_path="your_data_path" save_dir="your_save_path" lr=2e-4 batch_size=8 # per gpu batch size 8, we default use 8 GPUs export arch="unimol_plus_oc20_base" bash train_oc20.sh $data_path $save_dir $lr $batch_size ``` -------------------------------- ### Prepare for AIAC 2022 Competition Demo Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This command switches to the `ifd_demo` branch, likely to set up the environment or code specific to the AIAC 2022 Competition for predicting protein binding ability. ```bash git checkout ifd_demo ``` -------------------------------- ### Finetune Uni-Mol for Protein-ligand Binding Pose Prediction Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/README.md This command finetunes a Uni-Mol pretrained model on a training set for protein-ligand binding pose prediction. It configures various training parameters like learning rate, batch size, epochs, dropout, and uses distributed training with `torch.distributed.launch`. It saves checkpoints based on `valid_loss`. ```bash data_path="./protein_ligand_binding_pose_prediction" # replace to your data path save_dir="./save_pose" # replace to your save path n_gpu=4 MASTER_PORT=10086 finetune_mol_model="./weights/mol_checkpoint.pt" finetune_pocket_model="./weights/pocket_checkpoint.pt" lr=3e-4 batch_size=8 epoch=50 dropout=0.2 warmup=0.06 update_freq=1 dist_threshold=8.0 recycling=3 export NCCL_ASYNC_ERROR_HANDLING=1 export OMP_NUM_THREADS=1 python -m torch.distributed.launch --nproc_per_node=$n_gpu --master_port=$MASTER_PORT $(which unicore-train) $data_path --user-dir ./unimol --train-subset train --valid-subset valid \ --num-workers 8 --ddp-backend=c10d \ --task docking_pose --loss docking_pose --arch docking_pose \ --optimizer adam --adam-betas "(0.9, 0.99)" --adam-eps 1e-6 --clip-norm 1.0 \ --lr-scheduler polynomial_decay --lr $lr --warmup-ratio $warmup --max-epoch $epoch --batch-size $batch_size \ --mol-pooler-dropout $dropout --pocket-pooler-dropout $dropout \ --fp16 --fp16-init-scale 4 --fp16-scale-window 256 --update-freq $update_freq --seed 1 \ --tensorboard-logdir $save_dir/tsb \ --log-interval 100 --log-format simple \ --validate-interval 1 --keep-last-epochs 10 \ --best-checkpoint-metric valid_loss --patience 2000 --all-gather-list-size 2048000 \ --finetune-mol-model $finetune_mol_model \ --finetune-pocket-model $finetune_pocket_model \ --dist-threshold $dist_threshold --recycling $recycling \ --save-dir $save_dir \ --find-unused-parameters ``` -------------------------------- ### Conformer Module API Documentation Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/data.rst Documents the `unimol_tools.data.conformer` module, containing classes and functions related to molecular conformer generation and handling. ```APIDOC Module: unimol_tools.data.conformer Purpose: This module contains classes and functions for molecular conformer generation and handling. Members: - Classes: - Conformer (Placeholder for actual class definition and its methods/properties) - Functions: - (Placeholder for actual function definitions and their parameters/return types) ``` -------------------------------- ### Set Hugging Face Mirror for Faster Downloads Source: https://github.com/deepmodeling/uni-mol/blob/main/docs/source/weight.rst This command sets the `HF_ENDPOINT` environment variable to a specified mirror URL. This is useful for users experiencing slow downloads from the default Hugging Face Hub, allowing them to use an alternative, faster mirror for Uni-Mol model downloads. ```bash export HF_ENDPOINT=https://hf-mirror.com ``` -------------------------------- ### Construct Uni-Mol Protein and Ligand File Paths Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/notebooks/unimol_binding_pose_demo.ipynb This Python snippet dynamically generates a list of file paths for protein PDB files, multiple docked ligand SDF files, and a ground truth ligand SDF file. It leverages `os.path.join` for platform-independent path construction, accumulating all relevant paths into the `file_lists` variable for subsequent operations like archiving or analysis. ```python pdb_path = os.path.join(CASF_PATH, 'casf2016', pdb_id+'_protein.pdb') file_lists.append(pdb_path) for i in range(len(smiles_list)): ligand_path = os.path.join(results_path, "docking.{}.{}.sdf".format(pdb_id,i)) file_lists.append(ligand_path) gt_ligand_path = os.path.join(CASF_PATH,'casf2016',pdb_id+'_ligand.sdf') file_lists.append(gt_ligand_path) ``` -------------------------------- ### Preprocess OC20 IS2RE Dataset Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol_plus/README.md Instructions to clean and preprocess the downloaded OC20 IS2RE LMDB files. This involves running a Python script multiple times to split the data into train, valid, and test sets, specifying input and output paths. ```bash input_path="your_input_path" # The path to the original OC20 dataset output_path="your_output_path" python scripts/oc20_preprocess.py --input-path $input_path --out-path $output_path --split train python scripts/oc20_preprocess.py --input-path $input_path --out-path $output_path --split valid python scripts/oc20_preprocess.py --input-path $input_path --out-path $output_path --split test ``` -------------------------------- ### Evaluate Uni-Mol Docking Performance Source: https://github.com/deepmodeling/uni-mol/blob/main/unimol/notebooks/unimol_posebuster_demo.ipynb This shell script calculates docking metrics based on Uni-Mol's predicted binding poses. It takes the inference output file and the original LMDB reference file as inputs. The script then utilizes the `docking.py` utility to process the results, compare them against reference data, and output evaluation metrics to a specified directory. ```bash nthreads=8 # Num of threads predict_file=f"{results_path}/ckp_{lmdb_name}.out.pkl" # Your inference file dir reference_file=f"{outpath}/{lmdb_name}.lmdb" # Your reference file dir output_path="./unimol_repro_posebuster428" # Docking results path !python ./unimol/utils/docking.py --nthreads $nthreads --predict-file $predict_file --reference-file $reference_file --output-path $output_path ```