1.1.2.1. Extraction of Atom Environments
1import sys
2
3import CDPL.Chem as Chem
4
5
6# extracts the structural environments of the atoms in the specified molecular graph and outputs them as SMILES strings
7def printEnvironments(molgraph: Chem.MolecularGraph) -> None:
8 Chem.calcImplicitHydrogenCounts(molgraph, False) # calculate implicit hydrogen counts and set corresponding property for all atoms
9 Chem.perceiveHybridizationStates(molgraph, False) # perceive atom hybridization states and set corresponding property for all atoms
10 Chem.perceiveSSSR(molgraph, False) # perceive smallest set of smallest rings and store as Chem.MolecularGraph property
11 Chem.setRingFlags(molgraph, False) # perceive cycles and set corresponding atom and bond properties
12 Chem.setAromaticityFlags(molgraph, False) # perceive aromaticity and set corresponding atom and bond properties
13
14 frag = Chem.Fragment() # for storing extracted atom environments
15
16 print('- Atom environments (radius = 3 bonds)')
17
18 for atom in molgraph.atoms:
19 Chem.getEnvironment(atom, molgraph, 3, frag) # extract environment of atom reaching out up to three bonds
20 Chem.perceiveComponents(frag, False) # perceive molecular graph components (required for SMILES generation)
21
22 smiles = Chem.generateSMILES(frag, False, False) # generate non-canonical SMILES string with explicit hydrogen atoms
23
24 print('Atom #%s: %s' % (str(molgraph.getAtomIndex(atom)), smiles))
25
26def main() -> None:
27 if len(sys.argv) < 2:
28 sys.exit('Usage: %s <mol. input file>' % sys.argv[0])
29
30 # create reader for input molecules (format specified by file extension)
31 reader = Chem.MoleculeReader(sys.argv[1])
32
33 # create an instance of the default implementation of the Chem.Molecule interface
34 mol = Chem.BasicMolecule()
35
36 # read and process molecules one after the other until the end of input has been reached
37 try:
38 while reader.read(mol):
39 try:
40 printEnvironments(mol)
41 except Exception as e:
42 sys.exit('Error: processing of molecule failed: ' + str(e))
43
44 except Exception as e: # handle exception raised in case of severe read errors
45 sys.exit('Error: reading molecule failed: ' + str(e))
46
47 sys.exit(0)
48
49if __name__ == '__main__':
50 main()