David Saylor commited on
Commit
ec504eb
·
1 Parent(s): 4871339

added basic error handling to CAS type input

Browse files
ChemID.py CHANGED
@@ -18,14 +18,24 @@ def ResolveChemical(chemName, IDtype):
18
 
19
  if IDtype == 'CAS':
20
  cas = chemName
 
 
 
 
 
 
21
  smiles = cas2smiles(cas)
22
- name = cas2name(cas)
23
- mol = Chem.MolFromSmiles(smiles)
24
- Mw = Descriptors.MolWt(mol)
25
- im = ImageFromSmiles(smiles)
26
- im64 = Imageto64(im)
27
 
28
- return (name, cas, smiles, Mw, im64)
 
 
 
 
 
 
 
 
 
29
 
30
  #Generates an image of the molecule represented by the SMILES code given.
31
  #Returns None if the image cannot be generated. From https://github.com/ronaldo-prata/flask-test/blob/master/functions.py
@@ -54,7 +64,6 @@ def Imageto64(img):
54
  img = wTrim(img)
55
  buf = BytesIO()
56
  img.save(buf, format="PNG")
57
-
58
  pngImageB64String = "data:image/png;base64,"
59
  pngImageB64String += base64.b64encode(buf.getvalue()).decode("utf-8")
60
 
@@ -155,8 +164,6 @@ def string2name(string):
155
  # have seen empty iupac_name before, try synonyms if this happens
156
  name = c.synonyms[0]
157
  origin = 'PubChem'
158
- except KeyboardInterrupt:
159
- raise
160
  except:
161
  name = None
162
  origin = None
@@ -166,8 +173,6 @@ def string2name(string):
166
  name = cirpy.resolve(string, 'names')
167
  #name = cirpy.resolve(string, 'iupac_name')
168
  if name: origin = 'CIRPY'
169
- except KeyboardInterrupt:
170
- raise
171
  except:
172
  name = None
173
  origin = None
@@ -183,8 +188,6 @@ def string2name(string):
183
  fd = nltk.FreqDist(allsyns)
184
  name = fd.most_common(1)[0][0]
185
  origin = 'PubChem/substance'
186
- except KeyboardInterrupt:
187
- raise
188
  except:
189
  name = None
190
  origin = None
@@ -198,8 +201,6 @@ def string2name(string):
198
  name = c.synonyms[0]
199
  #name = c.iupac_name
200
  origin = 'PubChem'
201
- except KeyboardInterrupt:
202
- raise
203
  except:
204
  name = None
205
  origin = None
@@ -208,8 +209,6 @@ def string2name(string):
208
  try:
209
  name = cirpy.resolve(string_strip, 'names')
210
  if name: origin = 'CIRPY'
211
- except KeyboardInterrupt:
212
- raise
213
  except:
214
  name = None
215
  origin = None
 
18
 
19
  if IDtype == 'CAS':
20
  cas = chemName
21
+ smiles = None
22
+ name = None
23
+ Mw = None
24
+ im64 = None
25
+ error = 0
26
+
27
  smiles = cas2smiles(cas)
 
 
 
 
 
28
 
29
+ if smiles:
30
+ name = cas2name(cas)
31
+ mol = Chem.MolFromSmiles(smiles)
32
+ Mw = Descriptors.MolWt(mol)
33
+ im = ImageFromSmiles(smiles)
34
+ im64 = Imageto64(im)
35
+ else:
36
+ error = 1
37
+
38
+ return (name, cas, smiles, Mw, im64, error)
39
 
40
  #Generates an image of the molecule represented by the SMILES code given.
41
  #Returns None if the image cannot be generated. From https://github.com/ronaldo-prata/flask-test/blob/master/functions.py
 
64
  img = wTrim(img)
65
  buf = BytesIO()
66
  img.save(buf, format="PNG")
 
67
  pngImageB64String = "data:image/png;base64,"
68
  pngImageB64String += base64.b64encode(buf.getvalue()).decode("utf-8")
69
 
 
164
  # have seen empty iupac_name before, try synonyms if this happens
165
  name = c.synonyms[0]
166
  origin = 'PubChem'
 
 
167
  except:
168
  name = None
169
  origin = None
 
173
  name = cirpy.resolve(string, 'names')
174
  #name = cirpy.resolve(string, 'iupac_name')
175
  if name: origin = 'CIRPY'
 
 
176
  except:
177
  name = None
178
  origin = None
 
188
  fd = nltk.FreqDist(allsyns)
189
  name = fd.most_common(1)[0][0]
190
  origin = 'PubChem/substance'
 
 
191
  except:
192
  name = None
193
  origin = None
 
201
  name = c.synonyms[0]
202
  #name = c.iupac_name
203
  origin = 'PubChem'
 
 
204
  except:
205
  name = None
206
  origin = None
 
209
  try:
210
  name = cirpy.resolve(string_strip, 'names')
211
  if name: origin = 'CIRPY'
 
 
212
  except:
213
  name = None
214
  origin = None
exposure_module/exposure.py CHANGED
@@ -21,7 +21,10 @@ def exp_post():
21
  chemName = request.form["chemName"]
22
  IDtype = request.form["IDtype"]
23
 
24
- iupac, cas, smiles, MW, molImage = ResolveChemical(chemName, IDtype)
 
 
 
25
 
26
  amount = float(request.form["amount"])
27
  mass = float(request.form["mass"])
 
21
  chemName = request.form["chemName"]
22
  IDtype = request.form["IDtype"]
23
 
24
+ iupac, cas, smiles, MW, molImage, error = ResolveChemical(chemName, IDtype)
25
+ print(error)
26
+ if error > 0:
27
+ return render_template('chemError.html')
28
 
29
  amount = float(request.form["amount"])
30
  mass = float(request.form["mass"])
{templates → exposure_module/templates}/chemError.html RENAMED
@@ -2,7 +2,8 @@
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8">
5
- <title>CHRIS</title>
 
6
 
7
  </head>
8
 
 
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8">
5
+ <title>CHRIS-ChemError</title>
6
+ <link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles.css') }}">
7
 
8
  </head>
9
 
exposure_module/templates/exposure_report.html CHANGED
@@ -146,7 +146,7 @@ The threshold for toxicological concern (TTC) for {{exposure}} contact is {{TTC}
146
  </p>
147
 
148
  <p>
149
- <font color="red"> *Note*: This assessment assumes that {{chemName}} is not in the cohort of concern. </font>
150
  </p>
151
 
152
  {% else %}
 
146
  </p>
147
 
148
  <p>
149
+ <font color="red"> *Note*: This assessment assumes that {{iupac}} is not in the cohort of concern. </font>
150
  </p>
151
 
152
  {% else %}