1.1.3.2. Generating protonation states at physiological pH

 1import sys
 2import os
 3import argparse
 4
 5import CDPL.Chem as Chem
 6
 7
 8def processMolecules() -> None:
 9    args = parseArgs()
10    
11    # create reader for input molecules (format specified by file extension)
12    reader = Chem.MoleculeReader(args.in_file) 
13
14    # create writer for the output of matching molecules (format specified by file extension)
15    writer = Chem.MolecularGraphWriter(args.out_file) 
16
17    # create and initialize an instance of the class Chem.ProtonationStateStandardizer which
18    # implements the protonation state generation algorithm
19    prot_state_gen = Chem.ProtonationStateStandardizer()
20    
21    # create an instance of the default implementation of the Chem.Molecule interface
22    mol = Chem.BasicMolecule()
23    i = 1
24    
25    # read and process molecules one after the other until the end of input has been reached
26    try:
27        while reader.read(mol):
28            # compose a simple molecule identifier
29            mol_id = Chem.getName(mol).strip() 
30
31            if mol_id == '':
32                mol_id = '#' + str(i) # fallback if name is empty
33            else:
34                mol_id = '\'%s\' (#%s)' % (mol_id, str(i))
35
36            if not args.quiet:
37                print('- Processing molecule %s...' % mol_id)
38
39            try:
40                # protonate/deprotonate functional groups for phys. conditions
41                prot_state_gen.standardize(mol, Chem.ProtonationStateStandardizer.PHYSIOLOGICAL_CONDITION_STATE)
42
43                # enforce an update of the molecule components list (structure might have changed)
44                Chem.perceiveComponents(mol, True)
45                
46                # output the processed input molecule                 
47                if not writer.write(mol):   
48                    sys.exit('Error: output of molecule failed')
49                        
50            except Exception as e:
51                sys.exit('Error: processing or output of molecule %s failed: %s' % (mol_id, str(e)))
52
53            i += 1
54                
55    except Exception as e: # handle exception raised in case of severe read errors
56        sys.exit('Error: reading molecule failed: ' + str(e))
57
58    writer.close()
59    sys.exit(0)
60        
61def parseArgs() -> argparse.Namespace:
62    parser = argparse.ArgumentParser(description='Generates likely protonation states at phys. conditions for a set of\
63                                                  given input molecules and then writes the resulting structures to the\
64                                                  specified output file.')
65
66    parser.add_argument('-i',
67                        dest='in_file',
68                        required=True,
69                        metavar='<file>',
70                        help='Molecule input file')
71    parser.add_argument('-o',
72                        dest='out_file',
73                        required=True,
74                        metavar='<file>',
75                        help='Molecule output file')
76    parser.add_argument('-q',
77                        dest='quiet',
78                        required=False,
79                        action='store_true',
80                        default=False,
81                        help='Disable progress output (default: false)')
82    
83    return parser.parse_args()
84
85if __name__ == '__main__':
86    processMolecules()

Download source file