1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
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
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