Compound Filtering
This module provides functions for filtering compounds based on precomputed molecular properties (number of heavy atoms, number of rings, and pattern fingerprints).
Example Use
The precompute_properties function computes properties for a large set of compounds and saves them to a JSON file. The example below shows how to use it with a subset of the n1-stock.txt file.
from pathlib import Path
from fragmentretro.utils.filter_compound import precompute_properties
DATA_PATH = Path(__file__).parent.parent / "data"
PAROUTES_PATH = DATA_PATH / "paroutes"
PRECOMPUTE_PATH = DATA_PATH / "precompute"
MOL_PROPERTIES_PATH = PRECOMPUTE_PATH / "n1_stock_properties_subset.json"
# Create the directory if it doesn't exist
PRECOMPUTE_PATH.mkdir(parents=True, exist_ok=True)
with open(PAROUTES_PATH / "n1-stock.txt") as f:
n1_stock = [line.strip() for line in f]
n1_stock_subset = n1_stock[:500]
precompute_properties(n1_stock_subset, MOL_PROPERTIES_PATH, fpSize=2048)
The CompoundFilter class is used to initialize a filter, and the get_filtered_BBs method retrieves the screened building blocks. The following example demonstrates filtering using a specific fragment SMILES string.
from fragmentretro.utils.filter_compound import CompoundFilter
from fragmentretro.utils.helpers import replace_dummy_atoms_regex
fragment_smiles_list = ["[5*]N1CCC[C@@]1([13*])C", "[4*]CCN[5*]", "[4*]C[8*]", "[*]C[*]", "[3*]O[3*]"]
fragment_smiles = fragment_smiles_list[1]
compound_filter = CompoundFilter(MOL_PROPERTIES_PATH, fpSize=2048)
filtered_indices, filtered_BBs = compound_filter.get_filtered_BBs(fragment_smiles)
Source Code
fragmentretro.utils.filter_compound
CompoundFilter
A class for filtering compounds based on precomputed molecular properties.
Source code in src/fragmentretro/utils/filter_compound.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
__init__(mol_properties_path, fpSize=2048)
Initializes the CompoundFilter with molecular properties loaded from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mol_properties_path
|
Path
|
Path to the JSON file containing molecular properties. |
required |
Source code in src/fragmentretro/utils/filter_compound.py
filter_compounds(smiles, prefiltered_indices=None)
Filters compounds based on a query SMILES string and prefiltered indices. Note that dummy atoms have to be replaced by hydrogen atoms so that we can get the minimal format for pattern fingerprint processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smiles
|
str
|
The query SMILES string. |
required |
prefiltered_indices
|
FilterIndicesType | None
|
A list of prefiltered indices. |
None
|
Returns:
| Type | Description |
|---|---|
FilterIndicesType
|
A list of indices of the compounds that pass the filter. |
Source code in src/fragmentretro/utils/filter_compound.py
get_filtered_BBs(smiles, prefiltered_indices=None)
Filters building blocks based on a query SMILES string and prefiltered indices.
This method filters the building blocks based on the properties
of the provided SMILES string and a list of prefiltered indices.
It uses the filter_compounds method to get a list of indices
that pass the filter, and then returns a set of the corresponding
canonical SMILES strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smiles
|
str
|
The query SMILES string. |
required |
prefiltered_indices
|
FilterIndicesType | None
|
A list of prefiltered indices. |
None
|
Returns:
| Type | Description |
|---|---|
FilterIndicesType
|
tuple[list[int], BBsType]: A tuple containing a list of indices |
BBsType
|
of the compounds that pass the filter and a set of canonical |
tuple[FilterIndicesType, BBsType]
|
SMILES strings of the building blocks that pass the filter. |
Source code in src/fragmentretro/utils/filter_compound.py
get_mol_properties(smiles, fpSize=2048)
Given a SMILES string, returns a dictionary containing molecular properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smiles
|
str
|
The SMILES string. |
required |
Returns:
| Type | Description |
|---|---|
MolProperties
|
A dictionary containing the following keys: - 'num_heavy_atoms': Number of heavy atoms - 'num_rings': Number of rings - 'pfp': Pattern fingerprint bits |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the SMILES string is invalid and cannot be converted to an RDKit molecule. |
Source code in src/fragmentretro/utils/filter_compound.py
precompute_properties(smiles_list, output_path, fpSize=2048)
Calculates molecular properties for a list of SMILES strings and saves them to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smiles_list
|
list[str]
|
A list of SMILES strings. |
required |
output_path
|
Path
|
The path to the output JSON file. |
required |