scientific-skills/medchem/references/api_guide.md
Comprehensive reference for all medchem modules and functions.
Filter molecules based on multiple medicinal chemistry rules.
Constructor:
RuleFilters(rule_list: List[str])
Parameters:
rule_list: List of rule names to apply. See available rules below.Methods:
__call__(mols: List[Chem.Mol], n_jobs: int = 1, progress: bool = False) -> Dict
mols: List of RDKit molecule objectsn_jobs: Number of parallel jobs (-1 uses all cores)progress: Show progress barExample:
rfilter = mc.rules.RuleFilters(rule_list=["rule_of_five", "rule_of_cns"])
results = rfilter(mols=mol_list, n_jobs=-1, progress=True)
Individual rule functions that can be applied to single molecules.
rule_of_five(mol: Union[str, Chem.Mol]) -> bool
Lipinski's Rule of Five for oral bioavailability.
Criteria:
Parameters:
mol: SMILES string or RDKit molecule objectReturns: True if molecule passes all criteria
rule_of_three(mol: Union[str, Chem.Mol]) -> bool
Rule of Three for fragment screening libraries.
Criteria:
rule_of_oprea(mol: Union[str, Chem.Mol]) -> bool
Oprea's lead-like criteria for hit-to-lead optimization.
Criteria:
rule_of_cns(mol: Union[str, Chem.Mol]) -> bool
CNS drug-likeness rules.
Criteria:
rule_of_leadlike_soft(mol: Union[str, Chem.Mol]) -> bool
Soft lead-like criteria (more permissive).
Criteria:
rule_of_leadlike_strict(mol: Union[str, Chem.Mol]) -> bool
Strict lead-like criteria (more restrictive).
Criteria:
rule_of_veber(mol: Union[str, Chem.Mol]) -> bool
Veber's rules for oral bioavailability.
Criteria:
rule_of_reos(mol: Union[str, Chem.Mol]) -> bool
Rapid Elimination Of Swill (REOS) filter.
Criteria:
rule_of_drug(mol: Union[str, Chem.Mol]) -> bool
Combined drug-likeness criteria.
Criteria:
golden_triangle(mol: Union[str, Chem.Mol]) -> bool
Golden Triangle for drug-likeness balance.
Criteria:
pains_filter(mol: Union[str, Chem.Mol]) -> bool
Pan Assay INterference compoundS (PAINS) filter.
Returns: True if molecule does NOT contain PAINS substructures
Filter for common structural alerts derived from ChEMBL and literature.
Constructor:
CommonAlertsFilters()
Methods:
__call__(mols: List[Chem.Mol], n_jobs: int = 1, progress: bool = False) -> List[Dict]
Apply common alerts filter to a list of molecules.
Returns: List of dictionaries with keys:
has_alerts: Boolean indicating if molecule has alertsalert_details: List of matched alert patternsnum_alerts: Number of alerts foundcheck_mol(mol: Chem.Mol) -> Tuple[bool, List[str]]
Check a single molecule for structural alerts.
Returns: Tuple of (has_alerts, list_of_alert_names)
Novartis NIBR medicinal chemistry filters.
Constructor:
NIBRFilters()
Methods:
__call__(mols: List[Chem.Mol], n_jobs: int = 1, progress: bool = False) -> List[bool]
Apply NIBR filters to molecules.
Returns: List of booleans (True if molecule passes)
Eli Lilly's demerit-based structural alert system (275 rules).
Constructor:
LillyDemeritsFilters()
Methods:
__call__(mols: List[Chem.Mol], n_jobs: int = 1, progress: bool = False) -> List[Dict]
Calculate Lilly demerits for molecules.
Returns: List of dictionaries with keys:
demerits: Total demerit scorepasses: Boolean (True if demerits ≤ 100)matched_patterns: List of matched patterns with scoresHigh-level functional API for common operations.
nibr_filter(mols: List[Chem.Mol], n_jobs: int = 1) -> List[bool]
Apply NIBR filters using functional API.
Parameters:
mols: List of moleculesn_jobs: Parallelization levelReturns: List of pass/fail booleans
common_alerts_filter(mols: List[Chem.Mol], n_jobs: int = 1) -> List[Dict]
Apply common alerts filter using functional API.
Returns: List of results dictionaries
lilly_demerits_filter(mols: List[Chem.Mol], n_jobs: int = 1) -> List[Dict]
Calculate Lilly demerits using functional API.
Detect specific chemical groups in molecules.
Constructor:
ChemicalGroup(groups: List[str], custom_smarts: Optional[Dict[str, str]] = None)
Parameters:
groups: List of predefined group namescustom_smarts: Dictionary mapping custom group names to SMARTS patternsPredefined Groups:
"hinge_binders": Kinase hinge binding motifs"phosphate_binders": Phosphate binding groups"michael_acceptors": Michael acceptor electrophiles"reactive_groups": General reactive functionalitiesMethods:
has_match(mols: List[Chem.Mol]) -> List[bool]
Check if molecules contain any of the specified groups.
get_matches(mol: Chem.Mol) -> Dict[str, List[Tuple]]
Get detailed match information for a single molecule.
Returns: Dictionary mapping group names to lists of atom indices
get_all_matches(mols: List[Chem.Mol]) -> List[Dict]
Get match information for all molecules.
Example:
group = mc.groups.ChemicalGroup(groups=["hinge_binders", "phosphate_binders"])
matches = group.get_all_matches(mol_list)
Access to curated chemical catalogs.
Available Catalogs:
"functional_groups": Common functional groups"protecting_groups": Protecting group structures"reagents": Common reagents"fragments": Standard fragmentsUsage:
catalog = mc.catalogs.NamedCatalogs.get("functional_groups")
matches = catalog.get_matches(mol)
Calculate molecular complexity metrics.
calculate_complexity(mol: Chem.Mol, method: str = "bertz") -> float
Calculate complexity score for a molecule.
Parameters:
mol: RDKit moleculemethod: Complexity metric ("bertz", "whitlock", "barone")Returns: Complexity score (higher = more complex)
Filter molecules by complexity threshold.
Constructor:
ComplexityFilter(max_complexity: float, method: str = "bertz")
Methods:
__call__(mols: List[Chem.Mol], n_jobs: int = 1) -> List[bool]
Filter molecules exceeding complexity threshold.
Apply custom property-based constraints.
Constructor:
Constraints(
mw_range: Optional[Tuple[float, float]] = None,
logp_range: Optional[Tuple[float, float]] = None,
tpsa_max: Optional[float] = None,
tpsa_range: Optional[Tuple[float, float]] = None,
hbd_max: Optional[int] = None,
hba_max: Optional[int] = None,
rotatable_bonds_max: Optional[int] = None,
rings_range: Optional[Tuple[int, int]] = None,
aromatic_rings_max: Optional[int] = None,
)
Parameters: All parameters are optional. Specify only the constraints needed.
Methods:
__call__(mols: List[Chem.Mol], n_jobs: int = 1) -> List[Dict]
Apply constraints to molecules.
Returns: List of dictionaries with keys:
passes: Boolean indicating if all constraints passviolations: List of constraint names that failedExample:
constraints = mc.constraints.Constraints(
mw_range=(200, 500),
logp_range=(-2, 5),
tpsa_max=140
)
results = constraints(mols=mol_list, n_jobs=-1)
Query language for complex filtering.
parse(query: str) -> Query
Parse a medchem query string into a Query object.
Query Syntax:
AND, OR, NOT<, >, <=, >=, ==, !=complexity, lilly_demerits, mw, logp, tpsarule_of_five, rule_of_cns, etc.common_alerts, nibr_filter, pains_filterExample Queries:
"rule_of_five AND NOT common_alerts"
"rule_of_cns AND complexity < 400"
"mw > 200 AND mw < 500 AND logp < 5"
"(rule_of_five OR rule_of_oprea) AND NOT pains_filter"
Methods:
apply(mols: List[Chem.Mol], n_jobs: int = 1) -> List[bool]
Apply parsed query to molecules.
Example:
query = mc.query.parse("rule_of_five AND NOT common_alerts")
results = query.apply(mols=mol_list, n_jobs=-1)
passing_mols = [mol for mol, passes in zip(mol_list, results) if passes]
Utility functions for working with molecules.
batch_process(
mols: List[Chem.Mol],
func: Callable,
n_jobs: int = 1,
progress: bool = False,
batch_size: Optional[int] = None
) -> List
Process molecules in parallel batches.
Parameters:
mols: List of moleculesfunc: Function to apply to each moleculen_jobs: Number of parallel workersprogress: Show progress barbatch_size: Size of processing batchesstandardize_mol(mol: Chem.Mol) -> Chem.Mol
Standardize molecule representation (sanitize, neutralize charges, etc.).
All filters support parallelization:
# Use all CPU cores
results = filter_object(mols=mol_list, n_jobs=-1, progress=True)
# Use specific number of cores
results = filter_object(mols=mol_list, n_jobs=4, progress=True)
import medchem as mc
# Apply multiple filters
rule_filter = mc.rules.RuleFilters(rule_list=["rule_of_five"])
alert_filter = mc.structural.CommonAlertsFilters()
lilly_filter = mc.structural.LillyDemeritsFilters()
# Get results
rule_results = rule_filter(mols=mol_list, n_jobs=-1)
alert_results = alert_filter(mols=mol_list, n_jobs=-1)
lilly_results = lilly_filter(mols=mol_list, n_jobs=-1)
# Combine criteria
passing_mols = [
mol for i, mol in enumerate(mol_list)
if rule_results[i]["passes"]
and not alert_results[i]["has_alerts"]
and lilly_results[i]["passes"]
]
import pandas as pd
import datamol as dm
import medchem as mc
# Load data
df = pd.read_csv("molecules.csv")
df["mol"] = df["smiles"].apply(dm.to_mol)
# Apply filters
rfilter = mc.rules.RuleFilters(rule_list=["rule_of_five", "rule_of_cns"])
results = rfilter(mols=df["mol"].tolist(), n_jobs=-1)
# Add results to dataframe
df["passes_ro5"] = [r["rule_of_five"] for r in results]
df["passes_cns"] = [r["rule_of_cns"] for r in results]
# Filter dataframe
filtered_df = df[df["passes_ro5"] & df["passes_cns"]]