3.1.1. Connectivity Properties

 1import sys
 2
 3import CDPL.Chem as Chem
 4import CDPL.MolProp as MolProp
 5
 6
 7# outputs the corresponding properties of each atom of the provided molecular graph
 8def outputProperties(molgraph: Chem.MolecularGraph) -> None:
 9    Chem.calcImplicitHydrogenCounts(molgraph, False)  # calculate implicit hydrogen counts and set corresponding property for all atoms
10    Chem.perceiveHybridizationStates(molgraph, False) # perceive atom hybridization states and set corresponding property for all atoms
11    Chem.perceiveSSSR(molgraph, False)                # perceive smallest set of smallest rings and store as Chem.MolecularGraph property
12    Chem.setRingFlags(molgraph, False)                # perceive cycles and set corresponding atom and bond properties
13    Chem.setAromaticityFlags(molgraph, False)         # perceive aromaticity and set corresponding atom and bond properties
14
15    vsepr_geom_str = { MolProp.CoordinationGeometry.UNDEF                  : 'UNDEF',
16                   MolProp.CoordinationGeometry.NONE                   : 'NONE',
17                   MolProp.CoordinationGeometry.LINEAR                 : 'LINEAR',
18                   MolProp.CoordinationGeometry.TRIGONAL_PLANAR        : 'TRIGONAL_PLANAR',
19                   MolProp.CoordinationGeometry.TETRAHEDRAL            : 'TETRAHEDRAL',
20                   MolProp.CoordinationGeometry.TRIGONAL_BIPYRAMIDAL   : 'TRIGONAL_BIPYRAMIDAL',
21                   MolProp.CoordinationGeometry.OCTAHEDRAL             : 'OCTAHEDRAL',
22                   MolProp.CoordinationGeometry.PENTAGONAL_BIPYRAMIDAL : 'PENTAGONAL_BIPYRAMIDAL',
23                   MolProp.CoordinationGeometry.SQUARE_ANTIPRISMATIC   : 'SQUARE_ANTIPRISMATIC',
24                   MolProp.CoordinationGeometry.BENT                   : 'BENT',
25                   MolProp.CoordinationGeometry.TRIGONAL_PYRAMIDAL     : 'TRIGONAL_PYRAMIDAL',
26                   MolProp.CoordinationGeometry.SQUARE_PLANAR          : 'SQUARE_PLANAR',
27                   MolProp.CoordinationGeometry.SQUARE_PYRAMIDAL       : 'SQUARE_PYRAMIDAL',
28                   MolProp.CoordinationGeometry.T_SHAPED               : 'T_SHAPED',
29                   MolProp.CoordinationGeometry.SEESAW                 : 'SEESAW',
30                   MolProp.CoordinationGeometry.PENTAGONAL_PYRAMIDAL   : 'PENTAGONAL_PYRAMIDAL',
31                   MolProp.CoordinationGeometry.PENTAGONAL_PLANAR      : 'PENTAGONAL_PLANAR' }
32    
33    for atom in molgraph.atoms:
34        print('- Atom #%s' % str(molgraph.getAtomIndex(atom)))
35        print('\tNum. connected std. hydrogens (incl. impl. H): %s' % str(MolProp.getOrdinaryHydrogenCount(atom, molgraph)))
36        print('\tNum. connected carbon atoms: %s' % str(MolProp.getExplicitAtomCount(atom, molgraph, Chem.AtomType.C)))
37        print('\tNum. connected heteroatoms: %s' % str(MolProp.getExplicitAtomCount(atom, molgraph, Chem.AtomType.HET, False)))
38        print('\tNum. connected halogens: %s' % str(MolProp.getExplicitAtomCount(atom, molgraph, Chem.AtomType.X, False)))
39        print('\tNum. connected heavy atoms: %s' % str(MolProp.getHeavyAtomCount(atom, molgraph)))
40        print('\tNum. connected chain atoms (excl. impl. H): %s' % str(MolProp.getExplicitChainAtomCount(atom, molgraph)))
41        print('\tNum. connected chain atoms (incl. impl. H): %s' % str(MolProp.getChainAtomCount(atom, molgraph)))
42        print('\tNum. connected ring atoms: %s' % str(MolProp.getRingAtomCount(atom, molgraph)))
43        print('\tNum. connected aromatic atoms: %s' % str(MolProp.getAromaticAtomCount(atom, molgraph)))
44        print('\tNum. incident bonds (excl. impl. H): %s' % str(MolProp.getExplicitBondCount(atom, molgraph)))
45        print('\tNum. incident bonds (incl. impl. H): %s' % str(MolProp.getBondCount(atom, molgraph)))
46        print('\tNum. incident single bonds (excl. impl. H): %s' % str(MolProp.getExplicitBondCount(atom, molgraph, 1)))
47        print('\tNum. incident single bonds (incl. impl. H): %s' % str(MolProp.getBondCount(atom, molgraph, 1)))
48        print('\tNum. incident double bonds: %s' % str(MolProp.getBondCount(atom, molgraph, 2)))
49        print('\tNum. incident triple bonds: %s' % str(MolProp.getBondCount(atom, molgraph, 3)))
50        print('\tNum. incident chain bonds (excl. impl. H): %s' % str(MolProp.getExplicitChainBondCount(atom, molgraph)))
51        print('\tNum. incident chain bonds (incl. impl. H): %s' % str(MolProp.getChainBondCount(atom, molgraph)))
52        print('\tNum. incident ring bonds (incl. impl. H): %s' % str(MolProp.getRingBondCount(atom, molgraph)))
53        print('\tNum. incident aromatic bonds (incl. impl. H): %s' % str(MolProp.getAromaticBondCount(atom, molgraph)))
54        print('\tNum. incident heavy atom bonds (incl. impl. H): %s' % str(MolProp.getHeavyBondCount(atom, molgraph)))
55        print('\tNum. incident rotatable bonds (incl. impl. H): %s' % str(MolProp.getRotatableBondCount(atom, molgraph, False, False)))
56        print('\tValence (excl. impl. H): %s' % str(MolProp.calcExplicitValence(atom, molgraph)))
57        print('\tValence (incl. impl. H): %s' % str(MolProp.calcValence(atom, molgraph)))
58        print('\tSteric number: %s' % str(MolProp.calcStericNumber(atom, molgraph)))
59        print('\tVSEPR coordination geometry: %s' % vsepr_geom_str[MolProp.getVSEPRCoordinationGeometry(atom, molgraph)])
60
61def main() -> None:
62    if len(sys.argv) < 2:
63        sys.exit('Usage: %s <input mol. file>' % sys.argv[0])
64
65    # create reader for input molecules (format specified by file extension)
66    reader = Chem.MoleculeReader(sys.argv[1]) 
67   
68    # create an instance of the default implementation of the Chem.Molecule interface
69    mol = Chem.BasicMolecule()
70    
71    # read and process molecules one after the other until the end of input has been reached
72    try:
73        while reader.read(mol):
74            try:
75                outputProperties(mol)
76            except Exception as e:
77                sys.exit('Error: processing of molecule failed: ' + str(e))
78                
79    except Exception as e: # handle exception raised in case of severe read errors
80        sys.exit('Error: reading molecule failed: ' + str(e))
81
82    sys.exit(0)
83        
84if __name__ == '__main__':
85    main()

Download source file