### Initialize Vuetify and Mount App Source: https://github.com/chembl/chembl_multitask_model/blob/main/docs/index.html This snippet shows how to create and use Vuetify, then mount the Vue application. It's typically used at the start of a Vue.js application. ```javascript createVuetify(); app.use(vuetify); // Mount the app app.mount('#app'); ``` -------------------------------- ### Install Requirements Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Installs all necessary Python packages listed in the requirements.txt file. This is crucial for resolving missing dependency errors. ```bash pip install -r requirements.txt ``` -------------------------------- ### Profile Memory Usage During Training Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Starts memory tracing using tracemalloc, runs the training process, and prints the current and peak memory usage. ```python import tracemalloc tracemalloc.start() # ... run training ... current, peak = tracemalloc.get_traced_memory() print(f'Current: {current / 1024 / 1024:.1f} MB, Peak: {peak / 1024 / 1024:.1f} MB') ``` -------------------------------- ### Train ChEMBL Multitask Model with Data File Path Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md This example shows how to train the ChEMBL multitask model, specifying the ChEMBL version and the path to the HDF5 data file. The data file must contain datasets for `fps`, `labels`, `weights`, and `target_chembl_ids`. ```bash python train_chembl_multitask.py --chembl_version 36 --data_file ./chembl_36/mt_data_36_all.h5 ``` -------------------------------- ### Vue.js App Initialization and RDKit Module Loading Source: https://github.com/chembl/chembl_multitask_model/blob/main/docs/index.html Initializes the Vue.js application and loads the RDKit WebAssembly module. This setup is required before any RDKit functions can be used. ```javascript const { createApp, ref, onMounted, computed } = Vue; const { VApp, VMain, VContainer, VRow, VCol, VSelect, VTextField, VCardActions, VBtn, VCard, VCardTitle, VSpacer, VDataTable } = window.Vuetify; document.addEventListener('DOMContentLoaded', () => { initRDKitModule().then(RDKitModule => { const app = createApp({ components: { VApp, VMain, VContainer, VRow, VCol, VSelect, VTextField, VCardActions, VBtn, VCard, VCardTitle, VSpacer, VDataTable }, setup() { // ... setup logic ... }, // ... template ... }); // Create Vuetify instance const vuetify = window.Vuetify.create({ // ... vuetify options ... }); app.use(vuetify); app.mount('#app'); }); }); ``` -------------------------------- ### Train ChEMBL Multitask Model with ChEMBL Version Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md This example demonstrates how to train the ChEMBL multitask model, specifying the ChEMBL version and the data file path. The `chembl_version` is used for output filenames, linking trained models to specific data versions. ```bash python train_chembl_multitask.py --chembl_version 36 --data_file ./data.h5 ``` -------------------------------- ### Example Training Script for ChEMBL Multitask Model Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/train_model.md This script demonstrates the steps to extract the dataset and train the ChEMBL multitask model using cross-validation. It includes parameters for ChEMBL version, data file, batch size, learning rate, epochs, and cross-validation folds. ```bash # Extract dataset python extract_format_dataset.py \ --chembl_version 36 \ --output_dir ./chembl_36/ # Train model with 5-fold cross-validation python train_chembl_multitask.py \ --chembl_version 36 \ --data_file ./chembl_36/mt_data_36_all.h5 \ --batch_size 32 \ --lr 4.0 \ --max_epochs 3 \ --cv_folds 5 \ --output_dir ./chembl_36/ ``` -------------------------------- ### Upgrade Packages from Requirements Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Upgrades all Python packages listed in requirements.txt to their latest compatible versions. Use this if specific version installations fail. ```bash pip install --upgrade -r requirements.txt ``` -------------------------------- ### Vue.js Setup Function for Target Prediction Source: https://github.com/chembl/chembl_multitask_model/blob/main/docs/index.html Sets up reactive state variables, computed properties, and methods for the Vue.js application. This includes managing SMILES input, model selection, prediction results, and molecule visualization. ```javascript const { createApp, ref, onMounted, computed } = Vue; const { VApp, VMain, VContainer, VRow, VCol, VSelect, VTextField, VCardActions, VBtn, VCard, VCardTitle, VSpacer, VDataTable } = window.Vuetify; document.addEventListener('DOMContentLoaded', () => { initRDKitModule().then(RDKitModule => { const app = createApp({ components: { VApp, VMain, VContainer, VRow, VCol, VSelect, VTextField, VCardActions, VBtn, VCard, VCardTitle, VSpacer, VDataTable }, setup() { const sortBy = ref([{ key: 'proba', order: 'desc' }]); const search = ref(''); const molSVG = ref(''); const smiles = ref('CN(Cc1cnc2nc(N)nc(N)c2n1)c1ccc(C(=O)N\[C@@H\](CCC(=O)\[O-\])C(=O)\[O-\])cc1'); const targets = ref([]); const isLoading = ref(false); const selectedModel = ref('./chembl_35_multitask_q8.onnx'); const _predictTimeout = ref(null); const models = [ { title: 'Kinase', value: './chembl_35_multitask_kinase_q8.onnx' }, { title: 'GPCR', value: './chembl_35_multitask_gpcr_q8.onnx' }, { title: 'All Targets', value: './chembl_35_multitask_q8.onnx' } ]; const headers = [ { title: 'Target ChEMBL ID', key: 'chemblid', sortable: false }, { title: 'Probability', key: 'proba', sortable: true } ]; const smileErrors = computed(() => { if (!smiles.value) return []; return is_mol_valid() ? [] : ['Invalid SMILES']; }); function is_mol_valid() { try { const mol = RDKitModule.get_mol(smiles.value); const isValid = mol.is_valid(); mol.delete(); return isValid; } catch { return false; } } function debouncedPredict() { clearTimeout(_predictTimeout.value); _predictTimeout.value = setTimeout(predict, 200); } async function predict() { targets.value = []; molSVG.value = ''; if (!smiles.value || !is_mol_valid()) return; try { isLoading.value = true; const mol = RDKitModule.get_mol(smiles.value); const svg = mol.get_svg(480, 240); const fp = mol.get_morgan_fp(JSON.stringify({ radius: 2, nBits: 1024 })); mol.delete(); molSVG.value = svg || ''; const session = await ort.InferenceSession.create(selectedModel.value, { executionProviders: ["cpu"] }); const descs = Float32Array.from(fp.split('').map(x => parseInt(x))); const tensor = new ort.Tensor('float32', descs); const results = await session.run({ input: tensor }); targets.value = Object.entries(results) .map(([target, pred]) => ({ chemblid: target, proba: pred.data[0] })) .sort((a, b) => b.proba - a.proba); } catch (error) { console.error('Prediction error:', error); } finally { isLoading.value = false; } } function downloadResults() { const csvContent = [ ["Target ChEMBL ID", "Probability"], ...targets.value.map(target => [target.chemblid, target.proba]) ].map(e => e.join(",")) .join("\n"); const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" }); const link = document.createElement("a"); const url = URL.createObjectURL(blob); link.setAttribute("href", url); link.setAttribute("download", "predictions.csv"); document.body.appendChild(link); link.click(); document.body.removeChild(link); } onMounted(() => { predict(); }); return { sortBy, search, molSVG, smiles, targets, isLoading, selectedModel, models, headers, smileErrors, debouncedPredict, predict, downloadResults, is_mol_valid }; }, // ... template ... }); // Create Vuetify instance const vuetify = window.Vuetify.create({ // ... vuetify options ... }); app.use(vuetify); app.mount('#app'); }); }); ``` -------------------------------- ### Multitask Data DataFrame Example Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/types.md Illustrates the pivoted format where each row represents a compound and columns correspond to different targets, with values indicating activity status. ```python molregno canonical_smiles CHEMBL2034 CHEMBL2041 ... 0 1 CC(C)Cc1ccc... 1 0 ... 1 2 CC(=O)Oc1cc... -1 1 ... ``` -------------------------------- ### Fingerprint Array Example Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/types.md Represents a molecular fingerprint as a 1D binary NumPy array. Each element corresponds to a bit in the fingerprint, with 1.0 indicating the bit is set. ```python np.ndarray(dtype=np.float32, shape=(fp_size,)) ``` ```python array([0., 1., 0., ..., 1., 0., 1.], dtype=float32) ``` -------------------------------- ### Enable Verbose Logging for Dataset Extraction Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Configures the Python logging module to display DEBUG level messages. Use this to get detailed output during dataset extraction. ```python import logging logging.basicConfig(level=logging.DEBUG) ``` -------------------------------- ### Predict with ChEMBL Multitask Model in Julia Source: https://github.com/chembl/chembl_multitask_model/blob/main/README.md Example of predicting bioactivity using ONNX.jl and RDKitMinimalLib.jl in Julia. Requires loading the ONNX model and target information. ```Julia import RDKitMinimalLib: get_mol, get_morgan_fp import Umlaut: play! import ONNX import JSON path = "chembl_31_multitask.onnx" targets = JSON.parsefile("targets_31.json") # dummy input dummy = rand(Float32, 1024, 1) # load the model mt_chembl = ONNX.load(path, dummy) # load molecule and calc morgan fingerprint mol = get_mol("CC(=O)Oc1ccccc1C(=O)O") fp_details = Dict{String, Any}("nBits" => 1024, "radius" => 2) mfp = get_morgan_fp(mol, fp_details) # convert the bitstring to a 1024×1 Matrix{Float32} mfp = map(x->parse(Float32,string(x)),collect(mfp)) mfp = reshape(mfp, (length(mfp), 1)) # test a molecule pred = play!(mt_chembl, mfp) pred = collect(Iterators.flatten(pred)) res = tuple.(targets, pred) res = sort(res, by=res->res[2], rev=true) ``` -------------------------------- ### Predict with ChEMBL Multitask Model in Python Source: https://github.com/chembl/chembl_multitask_model/blob/main/README.md Example of predicting bioactivity using the ONNX Runtime in Python. Requires RDKit for fingerprint calculation and ONNX Runtime for model inference. ```Python import onnxruntime import numpy as np from rdkit import Chem from rdkit.Chem import rdMolDescriptors FP_SIZE = 1024 RADIUS = 2 def calc_morgan_fp(smiles): mol = Chem.MolFromSmiles(smiles) fp = rdMolDescriptors.GetMorganFingerprintAsBitVect( mol, RADIUS, nBits=FP_SIZE) a = np.zeros((0,), dtype=np.float32) Chem.DataStructs.ConvertToNumpyArray(fp, a) return a def format_preds(preds, targets): preds = np.concatenate(preds).ravel() np_preds = [(tar, pre) for tar, pre in zip(targets, preds)] dt = [('chembl_id','|U20'), ('pred', ' { console.error('Failed to initialize RDKit:', error); alert('Failed to load molecular processing library.'); }); ``` -------------------------------- ### Handle Non-existent Output Directory Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Ensures the specified output directory exists before running the extraction script. Creates the directory if it is missing. ```bash mkdir -p ./chembl_36/ python extract_format_dataset.py --chembl_version 36 --output_dir ./chembl_36/ ``` -------------------------------- ### Configure Morgan Fingerprint Radius Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md Determines the topological radius for Morgan fingerprint generation. A radius of 2 is standard and captures the 2-hop environment around atoms. ```bash python extract_format_dataset.py --radius 2 ``` -------------------------------- ### Configure Morgan Fingerprint Size Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md Sets the number of bits for Morgan fingerprints. A standard size of 1024 offers a balance between information and memory. Ensure this matches the size used during model training. ```bash python extract_format_dataset.py --fp_size 1024 ``` -------------------------------- ### Handle Insufficient Disk Space Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Provides solutions for running out of disk space during data extraction. Recommends freeing space, using alternative directories, or reducing fingerprint size. ```bash # Solutions: # 1. Free up disk space: df -h to check # 2. Use a different output directory on another drive # 3. Reduce fingerprint size: --fp_size 512 instead of 1024 ``` -------------------------------- ### Export ChEMBL Model to ONNX Format Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/train_model.md Shows how to export a trained ChEMBLMultiTask model to the ONNX format for inference. Requires a dummy input for model tracing. ```python import torch from train_chembl_multitask import ChEMBLMultiTask # Load trained model or create new one model = ChEMBLMultiTask(n_tasks=50, fp_size=1024, lr=4.0) # Export to ONNX format dummy_input = torch.ones(1024) # Dummy input for model tracing model.to_onnx( "chembl_36_multitask.onnx", dummy_input, export_params=True, input_names=["input"], output_names=[f"CHEMBL{i}" for i in range(50)] ) ``` -------------------------------- ### Handle SQLite Database File Not Found Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Resolves issues where the downloaded ChEMBL database file is missing or corrupted. This involves clearing cache and re-downloading. ```bash # Solutions: # 1. Delete cached download: rm -rf ~/.cache/chembl_downloader/ # 2. Re-run extraction script to re-download # 3. Manually download ChEMBL and provide local path # 4. Check disk space (require 2-5 GB) ``` -------------------------------- ### Parse Command-Line Arguments for ChEMBL Training Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/train_model.md Parses command-line arguments for the ChEMBL model training script using Python's argparse. Demonstrates setting various training parameters. ```python import sys from train_chembl_multitask import parse_args sys.argv = [ 'script.py', '--chembl_version', '36', '--data_file', './data/mt_data_36_all.h5', '--batch_size', '64', '--lr', '2.0', '--max_epochs', '5', '--output_dir', './models/' ] args = parse_args() print(args.chembl_version) # 36 print(args.data_file) # ./data/mt_data_36_all.h5 print(args.batch_size) # 64 ``` -------------------------------- ### Inspect HDF5 File Structure Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Print the structure of an HDF5 file to verify the presence of required datasets like 'fps', 'labels', 'weights', and 'target_chembl_ids'. ```python import tables; f = tables.open_file('file.h5', 'r'); print(f) ``` -------------------------------- ### Provide Required Data File Argument Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Ensure the `--data_file` argument is provided when running the training script. This argument is required for the script to function. ```bash python train_chembl_multitask.py \ --chembl_version 36 \ --data_file ./chembl_36/mt_data_36_all.h5 ``` -------------------------------- ### Quick Testing Without Cross-Validation Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/README.md Perform a quick test of the model training process by setting maximum epochs to 1 and disabling cross-validation. This is useful for rapid checks. ```bash python train_chembl_multitask.py \ --chembl_version 36 \ --data_file ./data/mt_data_36_all.h5 \ --max_epochs 1 \ --cv_folds 0 \ --output_dir ./quick_test/ ``` -------------------------------- ### Model Training Arguments Namespace Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/types.md Defines arguments for model training scripts. Requires `chembl_version` and `data_file`. Optional arguments include `batch_size`, `lr`, `max_epochs`, `n_workers`, `cv_folds`, and `output_dir`. ```python argparse.Namespace( chembl_version: int, # Required data_file: str, # Required batch_size: int, # Default: 32 lr: float, # Default: 4.0 max_epochs: int, # Default: 3 n_workers: int, # Default: 6 cv_folds: int, # Default: 6 output_dir: str # Default: "./" ) ``` -------------------------------- ### Specify Output Directory for Dataset Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md Sets the directory path where all output files will be saved. The specified directory must already exist. ```bash python extract_format_dataset.py --output_dir ./chembl_36/ ``` -------------------------------- ### Use Quantized Model for Inference Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/README.md This Python script demonstrates how to load a quantized ONNX model and perform inference. It includes generating a molecular fingerprint from a SMILES string and running it through the model. ```python import onnxruntime import numpy as np from rdkit import Chem from rdkit.Chem import rdMolDescriptors # Load quantized model session = onnxruntime.InferenceSession( "chembl_36_multitask_q8.onnx", providers=['CPUExecutionProvider'] ) # Generate fingerprint mol = Chem.MolFromSmiles("CCO") # Example: ethanol fp = rdMolDescriptors.GetMorganFingerprintAsBitVect(mol, 2, nBits=1024) fp_array = np.zeros((1024,), dtype=np.float32) Chem.DataStructs.ConvertToNumpyArray(fp, fp_array) # Inference input_name = session.get_inputs()[0].name output_names = [o.name for o in session.get_outputs()] predictions = session.run(output_names, {input_name: fp_array}) # Results for target, pred in zip(output_names, predictions): print(f"{target}: {pred[0]:.2%} probability") ``` -------------------------------- ### Load Dataset and Task Weights Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/train_model.md Loads the ChEMBL dataset and extracts task weights and fingerprint size from an HDF5 file. ```python with tb.open_file(data_file, mode="r") as t_file: weights = t_file.root.weights[:] # Load task weights fp_size = t_file.root.fps.shape[1] # Extract fingerprint size ``` -------------------------------- ### Extract and Format Dataset Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md This command extracts and formats a kinase-specific dataset with relaxed criteria for model training. It specifies the ChEMBL version, protein family, number of active and inactive molecules, and the output directory. ```bash python extract_format_dataset.py \ --chembl_version 36 \ --protein_family kinase \ --active_mols 50 \ --inactive_mols 50 \ --output_dir ./kinase/ ``` -------------------------------- ### Parse Command-Line Arguments Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/extract_dataset.md Parses command-line arguments for the dataset extraction script using Python's argparse module. Demonstrates how to simulate command-line arguments and access parsed values. ```python import sys from extract_format_dataset import parse_args # Simulate command line: python script.py --chembl_version 36 --fp_size 2048 sys.argv = ['script.py', '--chembl_version', '36', '--fp_size', '2048', '--output_dir', './data/'] args = parse_args() print(args.chembl_version) # 36 print(args.fp_size) # 2048 print(args.output_dir) # ./data/ ``` -------------------------------- ### Multitask Learning Architecture Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/README.md Illustrates the shared architecture with independent output heads for multitask learning. Input is a fingerprint, processed through fully connected layers before reaching task-specific heads. ```text Input (1024-bit fingerprint) ↓ Fully Connected: 1024 → 2000 (ReLU + Dropout) ↓ Fully Connected: 2000 → 100 (ReLU) ↓ [Task 1 Head] [Task 2 Head] ... [Task N Head] ↓ ↓ ↓ Output 1 Output 2 ... Output N (0-1 probability) ``` -------------------------------- ### Set Minimum Active Compounds per Target Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md Specifies the minimum number of active compounds required for a target to be included in the dataset. A threshold of 100 is standard. ```bash python extract_format_dataset.py --active_mols 100 ``` -------------------------------- ### ChEMBLMultiTask Optimizer Configuration Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/train_model.md Configures the optimizer for the ChEMBLMultiTask model. It returns a SGD optimizer with the specified learning rate and all model parameters. ```python def configure_optimizers(self) -> torch.optim.Optimizer ``` -------------------------------- ### Download and Process ChEMBL Version 36 Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md Specifies the ChEMBL database release version to download and process. Supported versions depend on the chembl_downloader availability. ```bash python extract_format_dataset.py --chembl_version 36 ``` -------------------------------- ### Handle Empty Result Set After Filtering Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Addresses scenarios where all targets are filtered out, resulting in an empty dataset. Suggests adjusting filtering criteria or removing protein family restrictions. ```bash # Solutions: # 1. Reduce minimum molecule counts: --active_mols 50 --inactive_mols 50 # 2. Remove protein family restriction: remove --protein_family flag # 3. Use older ChEMBL version with more data # Debug: Check output CSV files: wc -l chembl_36_all_activity_data_filtered.csv ``` -------------------------------- ### Long Training with Multiple Folds Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md This command is for extended training sessions involving multiple cross-validation folds. It uses a lower learning rate and more epochs for thorough model training. ```bash python train_chembl_multitask.py \ --chembl_version 36 \ --data_file ./chembl_36/mt_data_36_all.h5 \ --batch_size 32 \ --lr 2.0 \ --max_epochs 10 \ --cv_folds 10 \ --output_dir ./chembl_36_long/ ``` -------------------------------- ### Standard Full-Dataset Extraction Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/configuration.md Performs a standard dataset extraction using ChEMBL version 36, default fingerprint size and radius, and default activity thresholds, saving output to a specified directory. ```bash python extract_format_dataset.py \ --chembl_version 36 \ --fp_size 1024 \ --radius 2 \ --active_mols 100 \ --inactive_mols 100 \ --output_dir ./chembl_36/ ``` -------------------------------- ### Python Training Step Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/train_model.md Defines a single training iteration for the ChEMBL multi-task model. It computes losses for each task, masks invalid labels, and accumulates losses. ```python def training_step( self, batch: Tuple[torch.Tensor, torch.Tensor], batch_idx: int ) -> torch.Tensor: # Computes loss for each task separately, masking invalid labels (< 0). # Accumulates losses across all tasks and logs training loss. ``` -------------------------------- ### Extract and Format ChEMBL Dataset Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/README.md Use this command to extract and format ChEMBL data for model training. It allows configuration of ChEMBL version, fingerprint size and radius, and the number of active/inactive molecules. ```bash python extract_format_dataset.py \ --chembl_version 36 \ --fp_size 1024 \ --radius 2 \ --active_mols 100 \ --inactive_mols 100 \ --output_dir ./data/ ``` -------------------------------- ### Extract and Format Dataset Source: https://github.com/chembl/chembl_multitask_model/blob/main/README.md Extracts and formats dataset from ChEMBL. Specify the ChEMBL version and output directory. ```bash python extract_format_dataset.py --chembl_version 36 --output_dir ./chembl_36/ ``` -------------------------------- ### Input Fingerprint Tensor (Single Sample) Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/types.md Defines the PyTorch tensor structure for a single molecular fingerprint, used as input during model forward passes. ```python torch.Tensor(dtype=torch.float32, shape=(fp_size,)) ``` -------------------------------- ### Extract Kinase Data and Train Kinase Model Source: https://github.com/chembl/chembl_multitask_model/blob/main/README.md Extracts kinase-specific data and trains a kinase-specific multitask model. Requires specifying ChEMBL version, output directory, and data file. ```bash python extract_format_dataset.py --chembl_version 36 --protein_family kinase --output_dir ./kinase/ && python train_chembl_multitask.py --chembl_version 36 --data_file ./kinase/mt_data_36_kinase.h5 --output_dir ./kinase/ ``` -------------------------------- ### HDF5 Compressed Array for Morgan Fingerprints Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/types.md Stores Morgan fingerprints as a compressed array (Blosc, level 5) with float32 dtype. Shape is (n_samples, fp_size). Access individual fingerprints with `file.root.fps[i]` or all with `file.root.fps[:]`. ```python pytables.carray(atom=float32, shape=(n_samples, fp_size)) ``` -------------------------------- ### Python Test Step Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/train_model.md Evaluates the model on a single batch during testing. It generates predictions, masks invalid labels, and computes various performance metrics. ```python def test_step( self, batch: Tuple[torch.Tensor, torch.Tensor], batch_idx: int ) -> dict: # Generates predictions by thresholding at 0.5, masks invalid labels, and computes all metrics across all valid predictions. ``` -------------------------------- ### Monitor Disk and Memory Usage Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Monitors disk space usage of the output directory and free memory in real-time during extraction. ```bash watch -n 1 'free -h && du -sh output_dir/' ``` -------------------------------- ### Train ChEMBL Multitask Model Source: https://github.com/chembl/chembl_multitask_model/blob/main/README.md Trains the ChEMBL Multitask model using the extracted data. Specify the ChEMBL version, data file, and output directory. ```bash python train_chembl_multitask.py --chembl_version 36 --data_file ./chembl_36/mt_data_36_all.h5 --output_dir ./chembl_36/ ``` -------------------------------- ### Train with Higher Resolution Fingerprints Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/README.md This sequence of commands first extracts data using higher resolution fingerprints (2048 bits) and then trains a model using this data. Specify the ChEMBL version, fingerprint size, and output directories. ```bash python extract_format_dataset.py \ --chembl_version 36 \ --fp_size 2048 \ --output_dir ./data_2048/ python train_chembl_multitask.py \ --chembl_version 36 \ --data_file ./data_2048/mt_data_36_all.h5 \ --output_dir ./models_2048/ ``` -------------------------------- ### Handle Network Errors During ChEMBL Download Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Addresses connection issues when downloading the ChEMBL database. Solutions include checking network, retrying, using a proxy, or pre-downloading. ```bash # Solutions: # 1. Check network connection # 2. Try again later (server may be temporarily down) # 3. Pre-download database and provide local path # 4. Use a proxy if behind corporate firewall ``` -------------------------------- ### Check Valid Labels in HDF5 File Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Use this Python script to inspect the distribution of valid labels per target in an HDF5 file. This helps diagnose issues with empty test sets where all labels might be invalid. ```python import tables with tables.open_file('file.h5', 'r') as f: labels = f.root.labels[:] print(f"Valid labels per target: {(labels >= 0).sum(axis=0)}") ``` -------------------------------- ### Train Model for One Epoch Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Trains the ChEMBL multitask model for a single epoch without cross-validation, using a specified data file. ```bash python train_chembl_multitask.py \ --data_file ./mt_data_36_all.h5 \ --max_epochs 1 \ --cv_folds 0 \ --n_workers 0 ``` -------------------------------- ### Input Fingerprint Tensor (Batch) Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/types.md Defines the PyTorch tensor structure for a batch of molecular fingerprints used as input during model forward passes. ```python torch.Tensor(dtype=torch.float32, shape=(batch_size, fp_size)) ``` -------------------------------- ### calc_fp Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/extract_dataset.md Calculates the Morgan molecular fingerprint for a given SMILES string using an RDKit fingerprint generator. ```APIDOC ## calc_fp ### Description Calculates the Morgan molecular fingerprint for a given SMILES string using an RDKit fingerprint generator. It handles invalid SMILES by returning None-like behavior. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **smiles** (str) - Required - SMILES representation of the molecule - **mfpgen** (rdkit.Chem.rdFingerprintGenerator.MorganGenerator) - Required - Initialized RDKit Morgan fingerprint generator ### Return Type `np.ndarray` (dtype=float32): A 1D array representing the molecular fingerprint. The size matches the generator configuration (typically 1024 bits). ### Behavior Converts the SMILES string to an RDKit molecule object, generates the fingerprint, and converts it to a binary numpy array. Returns None-like behavior if the SMILES is invalid. ### Request Example ```python import numpy as np from rdkit.Chem import rdFingerprintGenerator from extract_format_dataset import calc_fp # Create fingerprint generator mfpgen = rdFingerprintGenerator.GetMorganGenerator(radius=2, fpSize=1024) # Calculate fingerprint for a molecule smiles = "CC(=O)Oc1ccccc1C(=O)O" # Aspirin fp = calc_fp(smiles, mfpgen) print(f"Fingerprint shape: {fp.shape}") # (1024,) print(f"Fingerprint dtype: {fp.dtype}") # float32 print(f"Number of bits set: {np.sum(fp)}") ``` ``` -------------------------------- ### SQLAlchemy Database Engine Creation Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/types.md Creates a SQLAlchemy engine to connect to the ChEMBL SQLite database. This engine is used to establish a connection for database operations. ```python engine = create_engine("sqlite:///chembl_36.db") ``` -------------------------------- ### Verify HDF5 File Existence Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Check if the specified HDF5 data file exists at the given path. Incorrect paths or incomplete extraction can lead to this error. ```bash ls -lh ./chembl_36/*.h5 ``` -------------------------------- ### Inspect HDF5 File Contents Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md A bash command to list the contents of an HDF5 file using Python's tables library. Useful for debugging data integrity issues. ```bash # List contents python -c " import tables f = tables.open_file('data.h5', 'r') print(f) " ``` -------------------------------- ### HDF5 Compressed Array for Activity Labels Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/types.md Stores activity labels (-1, 0, or 1) as a compressed array (Blosc, level 5) with float32 dtype. Shape is (n_samples, n_targets). Access specific labels with `file.root.labels[i, j]`. ```python pytables.carray(atom=float32, shape=(n_samples, n_targets)) ``` -------------------------------- ### Extract Dataset with Minimal Data Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Extracts a minimal dataset for testing purposes, specifying ChEMBL version and the number of active and inactive molecules. ```bash python extract_format_dataset.py \ --chembl_version 36 \ --active_mols 10 --inactive_mols 10 ``` -------------------------------- ### Dataset Extraction Arguments Namespace Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/types.md Defines arguments for dataset extraction scripts. Requires `chembl_version`. Optional arguments include `fp_size`, `radius`, `active_mols`, `inactive_mols`, `output_dir`, and `protein_family`. ```python argparse.Namespace( chembl_version: int, # Required fp_size: int, # Default: 1024 radius: int, # Default: 2 active_mols: int, # Default: 100 inactive_mols: int, # Default: 100 output_dir: str, # Default: "." protein_family: Optional[str] # Default: None ) ``` -------------------------------- ### Handle SQLAlchemy Query Errors Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Addresses errors arising from database schema mismatches or SQL syntax issues. Verifies ChEMBL version compatibility and database integrity. ```bash # Solutions: # 1. Verify ChEMBL version is available and compatible # 2. Check database is not corrupted: sqlite3 chembl_36.db ".tables" # 3. Update SQL query if ChEMBL schema changed # 4. Try older ChEMBL version ``` -------------------------------- ### Train ChEMBL Multitask Model Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/README.md Train a multitask model using the extracted ChEMBL data. This command allows setting the data file, batch size, learning rate, maximum epochs, and cross-validation folds. ```bash python train_chembl_multitask.py \ --chembl_version 36 \ --data_file ./data/mt_data_36_all.h5 \ --batch_size 32 \ --lr 4.0 \ --max_epochs 3 \ --cv_folds 5 \ --output_dir ./models/ ``` -------------------------------- ### ChEMBLMultiTask Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/api-reference/train_model.md PyTorch Lightning module implementing a multitask neural network for bioactivity prediction. It defines the model architecture, forward pass, and optimizer configuration. ```APIDOC ## ChEMBLMultiTask PyTorch Lightning module implementing a multitask neural network for bioactivity prediction. ### Constructor ```python ChEMBLMultiTask(n_tasks: int, fp_size: int, lr: float, weights: Optional[List[float]] = None) ``` #### Parameters - **n_tasks** (int) - Required - Number of prediction tasks (equal to number of targets) - **fp_size** (int) - Required - Size of input fingerprint vector (typically 1024) - **lr** (float) - Required - Learning rate for SGD optimizer - **weights** (Optional[List[float]]) - Optional - Task-specific loss weights for addressing class imbalance. If provided, length must equal n_tasks. ### Architecture The model consists of: - Input layer: `fp_size` → 2000 (fully connected) - Hidden layer: 2000 → 100 (fully connected with ReLU activation) - Dropout: 25% dropout for regularization - Output heads: One independent output layer per task (100 → 1 with sigmoid) ### Properties - **n_tasks** (int) - Number of prediction tasks - **fc1** (torch.nn.Linear) - First fully connected layer - **fc2** (torch.nn.Linear) - Second fully connected layer - **dropout** (torch.nn.Dropout) - Dropout regularization layer - **criterion** (list of torch.nn.BCELoss) - Loss functions for each task - **lr** (float) - Learning rate - **test_step_outputs** (list) - Accumulated test metrics ### Methods #### `forward` ```python forward(x: torch.Tensor) -> List[torch.Tensor] ``` Forward pass through the network. #### Parameters - **x** (torch.Tensor) - Input fingerprint vector of shape (batch_size, fp_size) or (fp_size,) #### Returns List of output tensors, one per task. Each tensor has shape (batch_size, 1) or (1,) containing sigmoid-activated probabilities in range [0, 1]. #### `configure_optimizers` ```python configure_optimizers() -> torch.optim.Optimizer ``` Configures the optimizer for training. #### Returns `torch.optim.SGD` with configured learning rate and all model parameters. ``` -------------------------------- ### Handle CUDA Out of Memory Source: https://github.com/chembl/chembl_multitask_model/blob/main/_autodocs/errors.md Address GPU memory exhaustion during training by reducing batch size, fingerprint size, or worker count. Ensure CPU is used if GPU is not intended. ```bash # Example: Reduce batch size for GPU training python train_chembl_multitask.py --batch_size 8 ... ```