1.1.3.2. Generating Protonation States at Physiological pH

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

Download source file