David Saylor commited on
Commit ·
8a33366
1
Parent(s): c1cd9b7
added image of molecule to output
Browse filesFiles changed (3)
5 unmodified lines678910111213141 unmodified line16171819202122232433 unmodified lines5859605 unmodified linesimport cirpyimport chemicalsimport rdkitfrom rdkit.Chem import AllChem as Chemfrom rdkit.Chem import Descriptors,Descriptors3Ddef ResolveChemical(chemName, IDtype):1 unmodified linesmiles = cas2smiles(chemName)mol = Chem.MolFromSmiles(smiles)Mw = Descriptors.MolWt(mol)return (smiles, Mw, mol)# function to convert CAS to SMILESdef cas2smiles(cas):33 unmodified linessmiles = Nonereturn smiles5 unmodified lines67891011121314151617181 unmodified line2021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676833 unmodified lines1021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202215 unmodified linesimport cirpyimport chemicalsfrom io import BytesIOfrom PIL import ImageOpsimport base64import rdkitfrom rdkit.Chem import AllChem as Chemfrom rdkit.Chem import Descriptors,Descriptors3D, Drawdef ResolveChemical(chemName, IDtype):1 unmodified linesmiles = 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.pydef ImageFromSmiles(smiles):image = Noneif type(smiles) is str:try:image = Draw.MolToImage(Chem.MolFromSmiles(smiles), size=(350, 350))except ValueError:passreturn 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")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 linessmiles = Nonereturn smiles# function to convert chemical entity string to iupac namedef string2name(string):name = Noneorigin = None# try chemicals packagetry:cm = chemicals.search_chemical(string)name = cm.common_nameorigin = 'chemicals'except KeyboardInterrupt:raiseexcept:name = Noneorigin = None# try pubchem for compoundsif 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 happensname = c.synonyms[0]origin = 'PubChem'except KeyboardInterrupt:raiseexcept:name = Noneorigin = None# next try cirpyif not name:try:name = cirpy.resolve(string, 'names')#name = cirpy.resolve(string, 'iupac_name')if name: origin = 'CIRPY'except KeyboardInterrupt:raiseexcept:name = Noneorigin = Noneif type(name) is list:name = name[0]# now try it as a pubchem substanceif not name:try:compounds = pcp.get_substances(string, namespace='name')# sometimes there are multiple substances, and multiple synonyms per substanceallsyns = [syn for c in compounds for syn in c.synonyms if cas not in syn]# choose the most common synonymfd = nltk.FreqDist(allsyns)name = fd.most_common(1)[0][0]origin = 'PubChem/substance'except KeyboardInterrupt:raiseexcept:name = Noneorigin = None# strip all spaces and try again...if not name:string_strip = re.sub(' ','',string)# first try pubchem for compoundstry:compounds = pcp.get_compounds(string_strip, namespace='name')c = compounds[0]name = c.synonyms[0]#name = c.iupac_nameorigin = 'PubChem'except KeyboardInterrupt:raiseexcept:name = Noneorigin = None# next try cirpyif not name:try:name = cirpy.resolve(string_strip, 'names')if name: origin = 'CIRPY'except KeyboardInterrupt:raiseexcept:name = Noneorigin = Noneif type(name) is list:name = name[0]return name, origin# function to convert name to casdef name2cas(name):cas = None# try chemicals packagetry:cm = chemicals.search_chemical(name)cas = cm.CASsexcept:cas = None# then try cirpyif not cas:try:cas = cirpy.resolve(name, 'cas')except:cas = Noneif type(cas) is list:cas.sort(key=lambda s: np.array(s.split('-'), dtype=int).sum())cas = cas[0]# next try pubchem for compoundsif not cas:try:compounds = pcp.get_compounds(name, namespace='name')c = compounds[0]syns = c.synonymspossible_cas = [syn for syn in syns if is_cas(syn)]# if multiple choose option with smallest sum of digitspossible_cas.sort(key=lambda s: np.array(s.split('-'), dtype=int).sum())cas = possible_cas[0]except:cas = Nonereturn cas
20 unmodified lines212223242526272839 unmodified lines6869707120 unmodified lineschemName = request.form["chemName"]IDtype = request.form["IDtype"]smiles, MW, mol = ResolveChemical(chemName, IDtype)print(smiles,MW)amount = float(request.form["amount"])mass = float(request.form["mass"])39 unmodified linesreturn 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)20 unmodified lines2122232425262739 unmodified lines6768697020 unmodified lineschemName = request.form["chemName"]IDtype = request.form["IDtype"]smiles, MW, mol, molImage = ResolveChemical(chemName, IDtype)amount = float(request.form["amount"])mass = float(request.form["mass"])39 unmodified linesreturn 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, molImage=molImage)
6 unmodified lines78910111212 unmodified lines25262728293031326 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') }}"></head><img src="{{ url_for('static',filename='images/FDAlogo.png') }}" style="float: left;" height="100"/>12 unmodified lines<h2> Compound </h2>Molecular weight = {{MW}} <br>SMILES = {{smiles}} <br><h2> Exposure </h2>6 unmodified lines7891011121314151617181920212223242526272829303132333412 unmodified lines474849505152535455565758596061626364656 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> <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