The count of hydrogen bond donors — atoms that can donate a hydrogen bond (commonly OH and NH groups). A hydrogen bond donor (HBD) is a molecule containing a hydrogen atom that is covalently bonded to an electronegative atom, such as nitrogen (N) or oxygen (O).
NumHDonors() is the function that is used to calculate the molecular weight of a compound.
Calculate the HBD of Butane (Figure 1).
Code:
butane_smiles = "CCCC"
butane_mol = Chem.MolFromSmiles(butane_smiles)
# HBD
butane_mw = Descriptors.NumHDonors(butane_mol)
print(f"Number of Hydrogen bond donors of Butane: {butane_mw:.2f}")
Figure 1: HBD of Butane
2. Calculate the HBD of Benzene (Figure 2).
Code:
benzene_smiles = "c1ccccc1"
benzene_mol = Chem.MolFromSmiles(benzene_smiles)
# HBD
benzene_mw = Descriptors.NumHDonors(benzene_mol)
print(f"Number of Hydrogen bond donors of Benzene: {benzene_mw:.2f}")
Figure 2: HBD of Butane
3. Calculate the HBD of Aspirin (Figure 3).
Code:
aspirin_smiles = "CC(=O)OC1=CC=CC=C1C(=O)O"
aspirin_mol = Chem.MolFromSmiles(aspirin_smiles)
# HBA
aspirin_mw = Descriptors.NumHDonors(aspirin_mol)
print(f"Number of Hydrogen bond donors of Benzene: {aspirin_mw:.2f}")
Figure 3: HBD of Asipirin
4. Calculate the HBD of Butanol (Figure 4).
Code:
#Butanol
butanol_smiles = "CCCCO"
butanol_mol = Chem.MolFromSmiles(butanol_smiles)
# Calculate Log P
butanol_mw = Descriptors.NumHDonors(butanol_mol)
print(f"Number of Hydrogen bond donors of Butanol: {butanol_mw:.2f}")
Figure 4: HBA of Butanol
5. Calculate the HBA of Sodium Propionate (Figure 5).
Code:
sodiumpropionate_smiles = "CCC(=O)[O-].[Na+]"
sodiumpropionate_mol = Chem.MolFromSmiles(sodiumpropionate_smiles)
# Calculate LogP
sodiumpropionate_mw = Descriptors.NumHDonors(sodiumpropionate_mol)
print(f"Number of Hydrogen bond donors of Sodium propionate:{sodiumpropionate_mw:.2f}")
Figure 5: HBD of Sodium Propionate
The following codes will calculate the Molecular weight, LogP, HBA, and HBD for the four compounds (Butane, Benzene, Aspirin, Butanol, and Sodium Propionate). (Figure 6).
Code:
from rdkit import Chem
from rdkit.Chem import Descriptors
def calculate_molecular_properties(molecules):
"""
Calculates and prints a table of molecular properties for a given
dictionary of molecules.
Args:
molecules (dict): A dictionary where keys are molecule names (str)
and values are their SMILES strings (str).
"""
# Print table header
print(f"{'Molecule':<20} | {'Mol Weight':<12} | {'LogP':<8} | {'H Acceptors':<12} | {'H Donors':<10}")
print("-" * 80)
# Iterate through each molecule, calculate properties, and print the row
for name, smiles in molecules.items():
# Create a molecule object from the SMILES string
mol = Chem.MolFromSmiles(smiles)
# It's good practice to check if the molecule was created successfully
if mol:
# Calculate the descriptors
mol_weight = Descriptors.MolWt(mol)
mol_logp = Descriptors.MolLogP(mol)
h_acceptors = Descriptors.NumHAcceptors(mol)
h_donors = Descriptors.NumHDonors(mol)
# Print the formatted results for the current molecule
print(f"{name:<20} | {mol_weight:<12.2f} | {mol_logp:<8.2f} | {h_acceptors:<12} | {h_donors:<10}")
else:
print(f"Could not process molecule: {name} with SMILES: {smiles}")
if __name__ == '__main__':
# Dictionary of molecules with their names and SMILES strings
molecule_dict = {
"Butane": "CCCC",
"Benzene": "c1ccccc1",
"Aspirin": "CC(=O)OC1=CC=CC=C1C(=O)O",
"Butanol": "CCCCO",
"Sodium Propionate": "CCC(=O)[O-].[Na+]"
}
calculate_molecular_properties(molecule_dict)
Figure 6: RDKit Property Calculator for Butane, Benzene, Aspirin, Butanol, Sodium Propionate