1.1.2.1. Extraction of atom environments
1import sys
2import os
3
4import CDPL.Chem as Chem
5
6
7# extracts the structural environments of the atoms in the specified molecular graph and outputs them as SMILES strings
8def printEnvironments(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 frag = Chem.Fragment() # for storing extracted atom environments
16
17 print('- Atom environments (radius = 3 bonds)')
18
19 for atom in molgraph.atoms:
20 Chem.getEnvironment(atom, molgraph, 3, frag) # extract environment of atom reaching out up to three bonds
21 Chem.perceiveComponents(frag, False) # perceive molecular graph components (required for SMILES generation)
22
23 smiles = Chem.generateSMILES(frag, False, False) # generate non-canonical SMILES string with explicit hydrogen atoms
24
25 print('Atom #%s: %s' % (str(molgraph.getAtomIndex(atom)), smiles))
26
27def main() -> None:
28 if len(sys.argv) < 2:
29 sys.exit('Usage: %s <mol. input 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 printEnvironments(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()