File size: 1,833 Bytes
127d226 1ebd440 5fa8a0c ac6ba74 5fa8a0c 127d226 0030454 127d226 0030454 127d226 0030454 127d226 0030454 127d226 0030454 127d226 eb1def4 127d226 0030454 127d226 5fa8a0c 127d226 |
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 |
from fastapi import FastAPI, HTTPException
from rdkit import Chem
from rdkit.Chem import Descriptors
from rdkit.Chem import rdMolDescriptors
import requests
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def name_to_smiles(name: str) -> str:
url = f"https://cactus.nci.nih.gov/chemical/structure/{name}/smiles"
response = requests.get(url)
if response.status_code == 200:
return response.text
return None
def get_molecule_info(mol):
mol_weight = Descriptors.MolWt(mol)
num_atoms = mol.GetNumAtoms()
num_bonds = mol.GetNumBonds()
mol_formula = rdMolDescriptors.CalcMolFormula(mol)
tpsa = Descriptors.TPSA(mol)
mol_logp = Descriptors.MolLogP(mol)
num_rotatable_bonds = Descriptors.NumRotatableBonds(mol)
return {
'molecular_weight': mol_weight,
'number_of_atoms': num_atoms,
'number_of_bonds': num_bonds,
'molecular_formula': mol_formula,
'tpsa': tpsa,
'logP': mol_logp,
'number_of_rotatable_bonds': num_rotatable_bonds
}
@app.get("/molecule_info/")
async def read_molecule_info(name: str):
if not name:
raise HTTPException(status_code=400, detail="No molecule name provided")
smiles = name_to_smiles(name)
if not smiles:
raise HTTPException(status_code=400, detail="Could not fetch SMILES string for provided name")
mol = Chem.MolFromSmiles(smiles)
if not mol:
raise HTTPException(status_code=400, detail="Molecule not recognized")
info = get_molecule_info(mol)
return info
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
|