6.1. MMFF94 atom charges

 1import sys
 2import os
 3
 4import CDPL.Chem as Chem
 5import CDPL.ForceField as ForceField
 6
 7
 8# calculates and outputs the MMFF94 charges of the atoms of the provided molecular graph
 9def calcAndOutputCharges(mol: Chem.Molecule) -> None:
10    Chem.calcImplicitHydrogenCounts(mol, False)  # calculate implicit hydrogen counts and set corresponding property for all atoms
11    Chem.makeHydrogenComplete(mol)               # make all implicit hydrogens explicit
12    Chem.perceiveHybridizationStates(mol, False) # perceive atom hybridization states and set corresponding property for all atoms
13    Chem.perceiveSSSR(mol, False)                # perceive smallest set of smallest rings and store as Chem.MolecularGraph property
14    Chem.setRingFlags(mol, False)                # perceive cycles and set corresponding atom and bond properties
15    Chem.setAromaticityFlags(mol, False)         # perceive aromaticity and set corresponding atom and bond properties
16  
17    ForceField.perceiveMMFF94AromaticRings(mol, False)        # perceive aromatic rings according to the MMFF94 aroamticity model and store data as Chem.MolecularGraph property
18    ForceField.assignMMFF94AtomTypes(mol, False, False)       # perceive MMFF94 atom types (tolerant mode) set corresponding property for all atoms
19    ForceField.assignMMFF94BondTypeIndices(mol, False, False) # perceive MMFF94 bond types (tolerant mode) set corresponding property for all bonds
20    ForceField.calcMMFF94AtomCharges(mol, False, False)       # calculate MMFF94 atom charges (tolerant mode) set corresponding property for all atoms
21
22    print('- MMFF94 partial charges')
23    
24    for atom in mol.atoms:
25        print('Atom #%s: %s' % (str(atom.getIndex()), str(ForceField.getMMFF94Charge(atom))))
26
27def main() -> None:
28    if len(sys.argv) < 2:
29        sys.exit('Usage: %s <input mol. file>' % sys.argv[0])
30
31    # create reader for input molecules (format specified by file extension)
32    reader = Chem.MoleculeReader(sys.argv[1]) 
33 
34    # create an instance of the default implementation of the Chem.Molecule interface
35    mol = Chem.BasicMolecule()
36    
37    # read and process molecules one after the other until the end of input has been reached
38    try:
39        while reader.read(mol):
40            try:
41                calcAndOutputCharges(mol)
42            except Exception as e:
43                sys.exit('Error: processing of molecule failed: ' + str(e))
44                
45    except Exception as e: # handle exception raised in case of severe read errors
46        sys.exit('Error: reading molecule failed: ' + str(e))
47
48    sys.exit(0)
49        
50if __name__ == '__main__':
51    main()

Download source file