Package rdkit :: Package Chem :: Module inchi
[hide private]
[frames] | no frames]

Source Code for Module rdkit.Chem.inchi

  1  # $Id$ 
  2  # 
  3  #  Copyright (c) 2011, Novartis Institutes for BioMedical Research Inc. 
  4  #  All rights reserved. 
  5  #  
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions are 
  8  # met:  
  9  # 
 10  #     * Redistributions of source code must retain the above copyright  
 11  #       notice, this list of conditions and the following disclaimer. 
 12  #     * Redistributions in binary form must reproduce the above 
 13  #       copyright notice, this list of conditions and the following  
 14  #       disclaimer in the documentation and/or other materials provided  
 15  #       with the distribution. 
 16  #     * Neither the name of Novartis Institutes for BioMedical Research Inc.  
 17  #       nor the names of its contributors may be used to endorse or promote  
 18  #       products derived from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 23  # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 24  # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 25  # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 26  # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 27  # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 28  # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 29  # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 30  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 31  # 
 32   
 33  INCHI_AVAILABLE = True 
 34   
 35  import logging 
 36   
 37  from rdkit.Chem import rdinchi 
 38  from rdkit import RDLogger 
 39  logger = RDLogger.logger() 
 40   
 41  logLevelToLogFunctionLookup = { 
 42    logging.INFO: logger.info, 
 43    logging.DEBUG: logger.debug, 
 44    logging.WARNING: logger.warning, 
 45    logging.CRITICAL: logger.critical, 
 46    logging.ERROR: logger.error 
 47  } 
 48   
 49   
50 -class InchiReadWriteError(Exception):
51 pass
52 53
54 -def MolFromInchi(inchi, sanitize=True, removeHs=True, logLevel=None, treatWarningAsError=False):
55 """Construct a molecule from a InChI string 56 57 Keyword arguments: 58 sanitize -- set to True to enable sanitization of the molecule. Default is 59 True 60 removeHs -- set to True to remove Hydrogens from a molecule. This only 61 makes sense when sanitization is enabled 62 logLevel -- the log level used for logging logs and messages from InChI 63 API. set to None to diable the logging completely 64 treatWarningAsError -- set to True to raise an exception in case of a 65 molecule that generates warning in calling InChI API. The resultant 66 molecule and error message are part of the excpetion 67 68 Returns: 69 a rdkit.Chem.rdchem.Mol instance 70 """ 71 try: 72 mol, retcode, message, log = rdinchi.InchiToMol(inchi, sanitize, removeHs) 73 except ValueError as e: 74 logger.error(str(e)) 75 return None 76 77 if logLevel is not None: 78 if logLevel not in logLevelToLogFunctionLookup: 79 raise ValueError("Unsupported log level: %d" % logLevel) 80 log = logLevelToLogFunctionLookup[logLevel] 81 if retcode == 0: 82 log(message) 83 84 if retcode != 0: 85 if retcode == 1: 86 logger.warning(message) 87 else: 88 logger.error(message) 89 if treatWarningAsError and retcode != 0: 90 raise InchiReadWriteError(mol, message) 91 return mol
92 93
94 -def MolToInchiAndAuxInfo(mol, options="", logLevel=None, treatWarningAsError=False):
95 """Returns the standard InChI string and InChI auxInfo for a molecule 96 97 Keyword arguments: 98 logLevel -- the log level used for logging logs and messages from InChI 99 API. set to None to diable the logging completely 100 treatWarningAsError -- set to True to raise an exception in case of a 101 molecule that generates warning in calling InChI API. The resultant InChI 102 string and AuxInfo string as well as the error message are encoded in the 103 exception. 104 105 Returns: 106 a tuple of the standard InChI string and the auxInfo string returned by 107 InChI API, in that order, for the input molecule 108 """ 109 inchi, retcode, message, logs, aux = rdinchi.MolToInchi(mol, options) 110 if logLevel is not None: 111 if logLevel not in logLevelToLogFunctionLookup: 112 raise ValueError("Unsupported log level: %d" % logLevel) 113 log = logLevelToLogFunctionLookup[logLevel] 114 if retcode == 0: 115 log(message) 116 if retcode != 0: 117 if retcode == 1: 118 logger.warning(message) 119 else: 120 logger.error(message) 121 122 if treatWarningAsError and retcode != 0: 123 raise InchiReadWriteError(inchi, aux, message) 124 return inchi, aux
125 126
127 -def MolToInchi(mol, options="", logLevel=None, treatWarningAsError=False):
128 """Returns the standard InChI string for a molecule 129 130 Keyword arguments: 131 logLevel -- the log level used for logging logs and messages from InChI 132 API. set to None to diable the logging completely 133 treatWarningAsError -- set to True to raise an exception in case of a 134 molecule that generates warning in calling InChI API. The resultant InChI 135 string and AuxInfo string as well as the error message are encoded in the 136 exception. 137 138 Returns: 139 the standard InChI string returned by InChI API for the input molecule 140 """ 141 if options.find('AuxNone') == -1: 142 if options: 143 options += " /AuxNone" 144 else: 145 options += "/AuxNone" 146 147 try: 148 inchi, aux = MolToInchiAndAuxInfo(mol, options, logLevel=logLevel, 149 treatWarningAsError=treatWarningAsError) 150 except InchiReadWriteError as inst: 151 inchi, aux, message = inst.args 152 raise InchiReadWriteError(inchi, message) 153 return inchi
154 155
156 -def InchiToInchiKey(inchi):
157 """Return the InChI key for the given InChI string. Return None on error""" 158 ret = rdinchi.InchiToInchiKey(inchi) 159 if ret: 160 return ret 161 else: 162 return None
163 164 165 __all__ = ['MolToInchiAndAuxInfo', 'MolToInchi', 'MolFromInchi', 'InchiReadWriteError', 166 'InchiToInchiKey', 'INCHI_AVAILABLE'] 167