neurosnap.chemicals module#
Provides functions and classes related to processing chemical data.
- neurosnap.chemicals.fetch_ccd(ccd_code, fpath)[source]#
Fetches the ideal SDF (Structure Data File) for a given CCD (Chemical Component Dictionary) code and saves it to the specified file path.
This function retrieves the idealized structure of a chemical component from the RCSB Protein Data Bank (PDB) by downloading the corresponding SDF file. The downloaded file is then saved to the specified location.
- Parameters:
- Raises:
HTTPError – If the request to fetch the SDF file fails (e.g., 404 or connection error).
IOError – If there is an issue saving the SDF file to the specified file path.
Example
>>> fetch_ccd("ATP", "ATP_ideal.sdf") Fetches the ideal SDF file for the ATP molecule and saves it as "ATP_ideal.sdf".
- External Resources:
CCD Information: https://www.wwpdb.org/data/ccd
SDF File Download: https://files.rcsb.org/ligands/download/{CCD_CODE}_ideal.sdf
- neurosnap.chemicals.get_ccds(fpath='~/.cache/ccd_codes.json')[source]#
Retrieves a set of all CCD (Chemical Component Dictionary) codes from the PDB.
This function checks for a locally cached JSON file with the CCD codes. - If the file exists, it reads and returns the set of codes from the cache. - If the file does not exist, it downloads the full Chemical Component Dictionary
(in mmCIF format) from the Protein Data Bank (PDB), extracts the CCD codes, and caches them in a JSON file for future use.
- Parameters:
fpath (
str) – The path to store / cache all the stored ccd_codes as a JSON file. Default is “~/.cache/ccd_codes.json”- Returns:
- A set of all CCD codes (three-letter codes representing small molecules,
ligands, and post-translational modifications).
- Return type:
- Raises:
HTTPError – If the request to the PDB server fails.
JSONDecodeError – If the cached JSON file is corrupted.
- File Cache:
Cached file path: “.cache/ccd_codes.json”
The cache is automatically updated if it does not exist.
- External Resources:
CCD information: https://www.wwpdb.org/data/ccd
CCD data download: https://files.wwpdb.org/pub/pdb/data/monomers/components.cif
- neurosnap.chemicals.get_mol_center(mol, use_mass=False)[source]#
Computes the geometric center or center of mass of a molecule.
- Parameters:
mol (Mol) – An RDKit molecule object with 3D coordinates.
use_mass (bool, optional) – If True, computes the center of mass using atomic masses. If False, computes the simple geometric center. Defaults to False.
- Returns:
- A NumPy array of shape (3,) representing the [x, y, z] center coordinates.
Returns None if the molecule has no conformers.
- Return type:
np.ndarray
- Raises:
ValueError – If no conformer is found in the molecule.
- neurosnap.chemicals.move_ligand_to_center(ligand_sdf_path, receptor_pdb_path, output_sdf_path, use_mass=False)[source]#
Moves the center of a ligand in an SDF file to match the center of a receptor in a PDB file.
This function reads a ligand from an SDF file and a receptor from a PDB file, calculates their respective centers (center of mass or geometric center), and translates the ligand such that its center aligns with the receptor’s center. The modified ligand is then saved to a new SDF file.
- Parameters:
ligand_sdf_path (str) – Path to the input ligand SDF file.
receptor_pdb_path (str) – Path to the input receptor PDB file.
output_sdf_path (str) – Path where the adjusted ligand SDF will be saved.
use_mass (bool, optional) – If True, compute center of mass; otherwise use geometric center. Defaults to False.
- Returns:
Path to the output SDF file with the translated ligand.
- Return type:
- Raises:
ValueError – If the ligand cannot be parsed from the input SDF file.
- neurosnap.chemicals.sdf_to_smiles(fpath)[source]#
Converts molecules in an SDF file to SMILES strings.
Reads an input SDF file and extracts SMILES strings from its molecules. Invalid or unreadable molecules are skipped, with warnings logged.
- Parameters:
fpath (str) – Path to the input SDF file.
- Returns:
A list of SMILES strings corresponding to valid molecules in the SDF file.
- Return type:
List[str]
- Raises:
FileNotFoundError – If the SDF file cannot be found.
IOError – If the file cannot be read.
- neurosnap.chemicals.smiles_to_sdf(smiles, output_path)[source]#
Converts a SMILES string to an sdf file. Will overwrite existing results.
NOTE: This function does the bare minimum in terms of generating the SDF molecule. The
neurosnap.conformersmodule should be used in most cases.