David Saylor commited on
Commit ·
8a33366
1
Parent(s): c1cd9b7
added image of molecule to output
Browse filesFiles changed (3)
5 unmodified lines6789101112131411151617181 unmodified line202122232425262728293031323334353637383940414243444546474849505152535455565758596061206263646566676833 unmodified lines1021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202215 unmodified linesimport cirpyimport chemicals
from io import BytesIOfrom PIL import ImageOpsimport base64
import rdkitfrom rdkit.Chem import AllChem as Chemfrom rdkit.Chem import Descriptors,Descriptors3Dfrom rdkit.Chem import Descriptors,Descriptors3D, Draw
def ResolveChemical(chemName, IDtype):
1 unmodified line smiles = cas2smiles(chemName) mol = Chem.MolFromSmiles(smiles) Mw = Descriptors.MolWt(mol) im = ImageFromSmiles(smiles) im64 = Imageto64(im)
return (smiles, Mw, mol, im64)
#Generates an image of the molecule represented by the SMILES code given.#Returns None if the image cannot be generated. From https://github.com/ronaldo-prata/flask-test/blob/master/functions.py
def ImageFromSmiles(smiles): image = None if type(smiles) is str: try: image = Draw.MolToImage(Chem.MolFromSmiles(smiles), size=(350, 350)) except ValueError: pass return image
#Trims the image into a box, removing any excess white background.#The box cannot be smaller than 400x400. This is done due to the difference in quality in the images generated by MolToImage,# if the size is too small (300x300), big molecules are too low quality, but if the size is too big (1000*1000), small molecules appear zoomed out.
def wTrim(img): bbox = ImageOps.invert(img).getbbox()
crop = (bbox[0], bbox[1], bbox[2], bbox[3]) # if (bbox[2]-bbox[0] < 100 and bbox[3]-bbox[1] < 100): # crop = (300, 300, 700, 700) # elif (bbox[2]-bbox[0] > bbox[3]-bbox[1]): # crop = (bbox[0], bbox[0], bbox[2], bbox[2]) # else: # crop = (bbox[1], bbox[1], bbox[3], bbox[3]) return img.crop(crop)
#Converts a PIL image into its base64 representation.def Imageto64(img): img = wTrim(img) buf = BytesIO() img.save(buf, format="PNG")
return (smiles, Mw, mol) pngImageB64String = "data:image/png;base64," pngImageB64String += base64.b64encode(buf.getvalue()).decode("utf-8")
return pngImageB64String
# function to convert CAS to SMILESdef cas2smiles(cas):33 unmodified lines smiles = None return smiles
# function to convert chemical entity string to iupac namedef string2name(string): name = None origin = None # try chemicals package try: cm = chemicals.search_chemical(string) name = cm.common_name origin = 'chemicals' except KeyboardInterrupt: raise except: name = None origin = None # try pubchem for compounds if not name: try: compounds = pcp.get_compounds(string, namespace='name') c = compounds[0] #name = c.iupac_name #if not name: # have seen empty iupac_name before, try synonyms if this happens name = c.synonyms[0] origin = 'PubChem' except KeyboardInterrupt: raise except: name = None origin = None # next try cirpy if not name: try: name = cirpy.resolve(string, 'names') #name = cirpy.resolve(string, 'iupac_name') if name: origin = 'CIRPY' except KeyboardInterrupt: raise except: name = None origin = None if type(name) is list: name = name[0] # now try it as a pubchem substance if not name: try: compounds = pcp.get_substances(string, namespace='name') # sometimes there are multiple substances, and multiple synonyms per substance allsyns = [syn for c in compounds for syn in c.synonyms if cas not in syn] # choose the most common synonym fd = nltk.FreqDist(allsyns) name = fd.most_common(1)[0][0] origin = 'PubChem/substance' except KeyboardInterrupt: raise except: name = None origin = None # strip all spaces and try again... if not name: string_strip = re.sub(' ','',string) # first try pubchem for compounds try: compounds = pcp.get_compounds(string_strip, namespace='name') c = compounds[0] name = c.synonyms[0] #name = c.iupac_name origin = 'PubChem' except KeyboardInterrupt: raise except: name = None origin = None # next try cirpy if not name: try: name = cirpy.resolve(string_strip, 'names') if name: origin = 'CIRPY' except KeyboardInterrupt: raise except: name = None origin = None if type(name) is list: name = name[0] return name, origin
# function to convert name to casdef name2cas(name): cas = None # try chemicals package try: cm = chemicals.search_chemical(name) cas = cm.CASs except: cas = None # then try cirpy if not cas: try: cas = cirpy.resolve(name, 'cas') except: cas = None if type(cas) is list: cas.sort(key=lambda s: np.array(s.split('-'), dtype=int).sum()) cas = cas[0] # next try pubchem for compounds if not cas: try: compounds = pcp.get_compounds(name, namespace='name') c = compounds[0] syns = c.synonyms possible_cas = [syn for syn in syns if is_cas(syn)] # if multiple choose option with smallest sum of digits possible_cas.sort(key=lambda s: np.array(s.split('-'), dtype=int).sum()) cas = possible_cas[0] except: cas = None return cas20 unmodified lines21222324252425262739 unmodified lines676869717020 unmodified lines chemName = request.form["chemName"] IDtype = request.form["IDtype"]
smiles, MW, mol = ResolveChemical(chemName, IDtype) print(smiles,MW) smiles, MW, mol, molImage = ResolveChemical(chemName, IDtype)
amount = float(request.form["amount"]) mass = float(request.form["mass"])39 unmodified lines
return render_template('exposure_report.html', 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, smiles=smiles) MOS=MOS, chemName=chemName, image=pngImageB64String, MW=MW, smiles=smiles, molImage=molImage)6 unmodified lines7891011121314151617181920212223242526272829303132333412 unmodified lines4748495051525328295455565758596061626364656 unmodified lines<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script><link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles.css') }}">
<style>* { box-sizing: border-box;}
/* Create two equal columns that floats next to each other */.column { float: left; width: 50%; padding: 10px; vertical-align: top; align: center;}
/* Clear floats after the columns */.row:after { content: ""; display: table; clear: both;}</style>
</head>
<img src="{{ url_for('static',filename='images/FDAlogo.png') }}" style="float: left;" height="100"/>12 unmodified lines
<h2> Compound </h2>
<div class="container"><div class="row"> <div class="column"> Name = <br> <br>Molecular weight = {{MW}} <br>SMILES = {{smiles}} <br> Molecular weight = {{MW}} <br> <br> SMILES = {{smiles}} </div> <div class="column"> <img src="{{molImage}}"/> </div></div></div>
<h2> Exposure </h2>
ChemID.py+163-2
exposure_module/exposure.py+2-3
exposure_module/templates/exposure_report.html+35-2