File size: 8,868 Bytes
ec4a00e
 
 
 
180de71
c87f597
 
ec4a00e
 
 
 
 
141ab09
 
ec4a00e
 
 
 
 
 
 
1acac80
 
c97ab3b
ec4a00e
 
 
 
180de71
ec4a00e
 
570b086
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec4a00e
 
570b086
 
 
 
 
 
 
 
 
 
ec4a00e
 
 
 
 
 
 
 
 
 
141ab09
ec4a00e
141ab09
ec4a00e
 
 
141ab09
570b086
 
 
 
 
 
 
 
 
 
 
141ab09
 
 
 
 
 
 
 
 
ec4a00e
 
 
 
 
 
 
 
 
 
 
 
 
 
141ab09
ec4a00e
 
 
 
1acac80
 
 
 
c97ab3b
ec4a00e
 
 
 
39ffbde
ec4a00e
570b086
 
 
 
 
 
 
 
 
 
 
1acac80
 
 
ec4a00e
 
39ffbde
ec4a00e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570b086
 
 
 
 
 
 
 
 
 
 
 
ec4a00e
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import numpy as np
from flask import render_template, request
from functions import SigFigs, Piringer, WilkeChang, SheetRelease, SheetRates, RatePlot
from functions import Piecewise, PowerLaw
from polymers import Polymers, Polymers3
from ChemID import *
from . import blueprint

import rdkit
from rdkit.Chem import AllChem as Chem

# get additional physical properties, options are: logp, rho, mp
# MP is looked up lazily only when it can affect ceramic classification.
get_properties = []
# show additional physical properties
show_properties = False
# output additional info for physical properties
debug = False
# flag for testing new polymer categories
use_new = True

#ORGANIC_ATOM_SET = {5, 6, 7, 8, 9, 15, 16, 17, 35, 53}
#METAL_ATOM_SET = set([3,4,11,12,13] + list(range(19,31+1)) + list(range(37,50+1)) + list(range(55,84+1)) + list(range(87,114+1)) + [116])

# load polymer data including Ap values
if not use_new:
    polymers, Ap = Polymers()
else:
    polymers, categories, params = Polymers3()

# load the index page for the exposure module
@blueprint.get("/exposure2_module/COU")
def COU():
    return render_template(
        "exposure2_COU.html",
        tool_title="CHRIS - Bulk Chemicals (v2)",
        nav_links=[
            ("Tool", "/exposure2"),
            ("CoU", "/exposure2_module/COU"),
            ("About", "/exposure2_module/RST"),
        ],
        footer_text="Office of Science and Engineering Laboratories 路 ",
        contact_email="david.saylor@fda.hhs.gov",
    )
@blueprint.get("/exposure2_module/RST")
def RST():
    return render_template("exposure2_RST.html",
        tool_title="CHRIS - Bulk Chemicals (v2)",
        nav_links=[
            ("Tool", "/exposure2"),
            ("CoU", "/exposure2_module/COU"),
            ("About", "/exposure2_module/RST"),
        ],
        footer_text="Office of Science and Engineering Laboratories 路 ",
        contact_email="david.saylor@fda.hhs.gov",
    )

@blueprint.route('/exposure2', methods=['GET'])
def exposure():
    return render_template('exposure2_index.html', polymers=polymers,
        tool_title="CHRIS - Bulk Chemicals (v2)",
        nav_links=[
            ("Tool", "/exposure2"),
            ("CoU", "/exposure2_module/COU"),
            ("About", "/exposure2_module/RST"),
        ],
        footer_text="Office of Science and Engineering Laboratories 路 ",
        contact_email="david.saylor@fda.hhs.gov",
    )


# build the report page for the exposure module
@blueprint.route('/exposure2', methods=['POST'])
def exp_post():

    chemName = request.form["chemName"]
    IDtype = request.form["IDtype"]

    if debug:
        iupac, cas, smiles, MW, LogP, LogP_origin, rho, rho_origin, mp, mp_origin, molImage, error = ResolveChemical(chemName, IDtype, debug=debug, get_properties=['logp','rho'])
    else:
        LogP_origin, rho_origin = '',''
        iupac, cas, smiles, MW, LogP, rho, mp, molImage, error = ResolveChemical(chemName, IDtype, get_properties=get_properties)

    if error > 0:
        return render_template('chemError.html', chemName=chemName, IDtype=IDtype, error=error,
                               error_message=ERROR_CODES.get(error, 'Unknown chemical identification error')
                               ,
                               tool_title="CHRIS - Bulk Chemicals (v2)",
                               nav_links=[
                                   ("Tool", "/exposure2"),
                                   ("CoU", "/exposure2_module/COU"),
                                   ("About", "/exposure2_module/RST"),
                               ],
                               footer_text="Office of Science and Engineering Laboratories 路 ",
                               contact_email="david.saylor@fda.hhs.gov",
                               )

    # metals/ceramics logic; only look up MP if the structural checks need it
    is_metal, is_ceramic, mp, material_mp_origin, mp_searched = ClassifyMaterial(cas, iupac, smiles)
    if debug:
        LogP_origin = f' ({LogP_origin})'
        rho_origin = f' ({rho_origin})'
        mp_origin = f' ({material_mp_origin})' if mp_searched else ''
    else:
        mp_origin = ''

    #MW = SigFigs(MW, 6)
    if 'logp' not in get_properties:
        LogP = 'Not searched'
    elif LogP is np.nan or LogP is None:
        LogP = 'Not found'
    else:
        LogP = SigFigs(LogP, 4)
    if 'rho' not in get_properties:
        rho = 'Not searched'
    elif rho is np.nan or rho is None:
        rho = 'Not found'
    else:
        rho = SigFigs(rho, 4)
    if not mp_searched:
        mp = 'Not searched'
    elif mp is np.nan or mp is None:
        mp = 'Not found'

    #ceramic = False
    #mol = Chem.MolFromSmiles(smiles)
    #atom_num_list = [a.GetAtomicNum() for a in mol.GetAtoms()]
    #is_metal = set(atom_num_list) <= METAL_ATOM_SET
    if is_metal:
        # if all atoms are metals -> this is a metal
    #if natoms == 1 and smiles != '[C]':
        # only one atom, except for carbon -> assumed metal
        # return render_template('chemError.html')
        return render_template('metalError.html', show_properties=show_properties, chemName=chemName, MW=MW, LogP=LogP, rho=rho, mp=mp, iupac=iupac,
                               cas=cas, smiles=smiles, molImage=molImage,
                               LogP_origin=LogP_origin, rho_origin=rho_origin, mp_origin=mp_origin
                               ,
                               tool_title="CHRIS - Bulk Chemicals (v2)",
                               nav_links=[
                                   ("Tool", "/exposure2"),
                                   ("CoU", "/exposure2_module/COU"),
                                   ("About", "/exposure2_module/RST"),
                               ],
                               footer_text="Office of Science and Engineering Laboratories 路 ",
                               contact_email="david.saylor@fda.hhs.gov",
                               )
    #else:
    if is_ceramic:
        MW = 1100.

    if MW < 100. and not use_new:
        return render_template('MwError.html', show_properties=show_properties, chemName=chemName, MW=MW, LogP=LogP, rho=rho, mp=mp, iupac=iupac, cas=cas, smiles=smiles, molImage=molImage,
                               LogP_origin=LogP_origin, rho_origin=rho_origin, mp_origin=mp_origin)

    amount = float(request.form["amount"])
    mass = float(request.form["mass"])
    density = float(request.form["density"])
    vol = mass / density
    polymer = request.form["polymer"]
    #pIndex = (np.where(polymers == polymer)[0])[0]
    pIndex = np.argmax(polymers == polymer)
    area = float(request.form["area"])
    exposure = request.form["exposure"]

    if exposure != "limited":
        time = 24.
    else:
        time = float(request.form["exptime"])

    if exposure != "long-term":
        TTC = 0.12
    else:
        TTC = 0.0015

    assume = np.array((request.form.get("assume1") is not None, request.form.get("assume2") is not None,
                      request.form.get("assume3") is not None, request.form.get("assume4") is not None,
                      request.form.get("assume5") is not None))

    if use_new:
        category = categories[pIndex]
        diff = Piecewise(MW, params[category])
    else:
        if not np.isnan(Ap[pIndex]):
            diff = Piringer(MW, Ap[pIndex])
        else:
            diff = WilkeChang(MW)

    release = SheetRelease(amount, vol, area, time, diff)

    MOS = TTC / release

    release = SigFigs(release, 2)
    MOS = SigFigs(MOS, 2)
    diff = SigFigs(diff, 2)

    # Generate the rate plot using matplotlib
    tarray = np.arange(1., 31., 1.)
    rates = SheetRates(amount, vol, area, tarray, diff)
    pngImageB64String = RatePlot(tarray, rates)

    return render_template('exposure2_report.html', show_properties=show_properties, polymers=polymers, pIndex=pIndex, release=release,
                           assume=assume, area=area, vol=vol, amount=amount, diff=diff, time=time, exposure=exposure, TTC=TTC,
                           MOS=MOS, chemName=chemName, image=pngImageB64String, MW=MW, LogP=LogP, rho=rho, mp=mp, iupac=iupac, cas=cas, smiles=smiles, molImage=molImage,
                           LogP_origin=LogP_origin, rho_origin=rho_origin, mp_origin=mp_origin, ceramic=is_ceramic
                           ,
                           tool_title="CHRIS - Bulk Chemicals (v2)",
                           nav_links=[
                               ("Tool", "/exposure2"),
                               ("CoU", "/exposure2_module/COU"),
                               ("About", "/exposure2_module/RST"),
                           ],
                           footer_text="Office of Science and Engineering Laboratories 路 ",
                           contact_email="david.saylor@fda.hhs.gov",
                           )