1.1.2.1. Extraction of Atom Environments

The script extract_atom_envs.py extracts and outputs the structural environment of each atom of the molecules read from the specified input file.

Synopsis

python extract_atom_envs.py <file>

Code

 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.calcBasicProperties(molgraph, False)  # calculate basic molecular properties (if not yet done)
 9   
10    frag = Chem.Fragment()                     # for storing extracted atom environments
11    
12    print('- Atom environments (radius = 3 bonds)')
13    
14    for atom in molgraph.atoms:
15        Chem.getEnvironment(atom, molgraph, 3, frag)     # extract environment of atom reaching out up to three bonds
16        Chem.perceiveComponents(frag, False)             # perceive molecular graph components (required for SMILES generation)
17
18        smiles = Chem.generateSMILES(frag, False, False) # generate non-canonical SMILES string with explicit hydrogen atoms
19        
20        print('Atom #%s: %s' % (str(molgraph.getAtomIndex(atom)), smiles))
21
22def main() -> None:
23    if len(sys.argv) < 2:
24        sys.exit('Usage: %s <mol. input file>' % sys.argv[0])
25
26    # create reader for input molecules (format specified by file extension)
27    reader = Chem.MoleculeReader(sys.argv[1]) 
28    
29    # create an instance of the default implementation of the Chem.Molecule interface
30    mol = Chem.BasicMolecule()
31    
32    # read and process molecules one after the other until the end of input has been reached
33    try:
34        while reader.read(mol): 
35            try:
36                printEnvironments(mol)
37            except Exception as e:
38                sys.exit('Error: processing of molecule failed: ' + str(e))
39                
40    except Exception as e: # handle exception raised in case of severe read errors
41        sys.exit('Error: reading molecule failed: ' + str(e))
42
43    sys.exit(0)
44        
45if __name__ == '__main__':
46    main()

Download source file