Update main.py
Browse files
main.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
from
|
|
|
|
|
|
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
|
5 |
app = FastAPI()
|
@@ -12,41 +15,50 @@ app.add_middleware(
|
|
12 |
allow_headers=["*"],
|
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 |
-
def home():
|
39 |
-
home = "Hello, Welcome to LinDB!"
|
40 |
-
return home
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
return db.find('items')
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
return
|
49 |
|
50 |
if __name__ == "__main__":
|
51 |
import uvicorn
|
52 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from rdkit import Chem
|
3 |
+
from rdkit.Chem import Descriptors
|
4 |
+
from rdkit.Chem import rdMolDescriptors
|
5 |
+
import requests
|
6 |
from fastapi.middleware.cors import CORSMiddleware
|
7 |
|
8 |
app = FastAPI()
|
|
|
15 |
allow_headers=["*"],
|
16 |
)
|
17 |
|
18 |
+
def name_to_smiles(name: str) -> str:
|
19 |
+
url = f"https://cactus.nci.nih.gov/chemical/structure/{name}/smiles"
|
20 |
+
response = requests.get(url)
|
21 |
+
if response.status_code == 200:
|
22 |
+
return response.text
|
23 |
+
return None
|
24 |
|
25 |
+
def get_molecule_info(mol):
|
26 |
+
mol_weight = Descriptors.MolWt(mol)
|
27 |
+
num_atoms = mol.GetNumAtoms()
|
28 |
+
num_bonds = mol.GetNumBonds()
|
29 |
+
mol_formula = rdMolDescriptors.CalcMolFormula(mol)
|
30 |
+
tpsa = Descriptors.TPSA(mol)
|
31 |
+
mol_logp = Descriptors.MolLogP(mol)
|
32 |
+
num_rotatable_bonds = Descriptors.NumRotatableBonds(mol)
|
33 |
|
34 |
+
return {
|
35 |
+
'molecular_weight': mol_weight,
|
36 |
+
'number_of_atoms': num_atoms,
|
37 |
+
'number_of_bonds': num_bonds,
|
38 |
+
'molecular_formula': mol_formula,
|
39 |
+
'tpsa': tpsa,
|
40 |
+
'logP': mol_logp,
|
41 |
+
'number_of_rotatable_bonds': num_rotatable_bonds
|
42 |
+
}
|
43 |
|
44 |
+
@app.get("/molecule_info/")
|
45 |
+
async def read_molecule_info(name: str):
|
46 |
+
if not name:
|
47 |
+
raise HTTPException(status_code=400, detail="No molecule name provided")
|
48 |
|
49 |
+
smiles = name_to_smiles(name)
|
50 |
+
if not smiles:
|
51 |
+
raise HTTPException(status_code=400, detail="Could not fetch SMILES string for provided name")
|
52 |
|
53 |
+
mol = Chem.MolFromSmiles(smiles)
|
|
|
|
|
|
|
54 |
|
55 |
+
if not mol:
|
56 |
+
raise HTTPException(status_code=400, detail="Molecule not recognized")
|
|
|
57 |
|
58 |
+
info = get_molecule_info(mol)
|
59 |
+
|
60 |
+
return info
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
import uvicorn
|
64 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|