Spaces:
Sleeping
Sleeping
Commit
·
0bdc885
1
Parent(s):
575f5af
updated code
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ def convert_smiles_to_mol(smiles_list, checkbox):
|
|
| 13 |
if checkbox == True:
|
| 14 |
cdk = CDK(ignore_3D=False)
|
| 15 |
else:
|
| 16 |
-
cdk = CDK()
|
| 17 |
smiles_list = list(smiles_list.split(','))
|
| 18 |
try:
|
| 19 |
mols = [Chem.AddHs(Chem.MolFromSmiles(smiles)) for smiles in smiles_list]
|
|
@@ -29,7 +29,7 @@ def convert_smiles_to_mol(smiles_list, checkbox):
|
|
| 29 |
return molfile,str(e)
|
| 30 |
|
| 31 |
except Exception as e:
|
| 32 |
-
return str(e),
|
| 33 |
|
| 34 |
|
| 35 |
# Function to calculate Molecular Weight
|
|
@@ -50,7 +50,7 @@ def get_geometric_descriptors(smiles):
|
|
| 50 |
try:
|
| 51 |
mol = Chem.MolFromSmiles(smiles)
|
| 52 |
if mol is None:
|
| 53 |
-
return "Invalid SMILES string"
|
| 54 |
|
| 55 |
# Add hydrogens and compute 3D coordinates
|
| 56 |
mol = Chem.AddHs(mol)
|
|
@@ -76,7 +76,7 @@ def get_geometric_descriptors(smiles):
|
|
| 76 |
df = pd.DataFrame([geometric_descriptors])
|
| 77 |
return df.T,img
|
| 78 |
except Exception as e:
|
| 79 |
-
return str(e),
|
| 80 |
|
| 81 |
# Function to check if a substructure is present
|
| 82 |
def check_substructure(smiles, substructure_smiles):
|
|
@@ -86,7 +86,7 @@ def check_substructure(smiles, substructure_smiles):
|
|
| 86 |
|
| 87 |
# Check if the molecule is None (invalid SMILES)
|
| 88 |
if molecule is None or substructure is None:
|
| 89 |
-
return
|
| 90 |
|
| 91 |
# Use RDKit's HasSubstructMatch to check for the substructure
|
| 92 |
val = molecule.HasSubstructMatch(substructure)
|
|
@@ -100,9 +100,9 @@ def check_substructure(smiles, substructure_smiles):
|
|
| 100 |
img2 = Draw.MolToImage(sub_molecule)
|
| 101 |
return img1, img2, "Substructure is present."
|
| 102 |
except Exception as e:
|
| 103 |
-
return
|
| 104 |
else:
|
| 105 |
-
return img1,
|
| 106 |
|
| 107 |
def calculate_similarity(smiles1, smiles2):
|
| 108 |
try:
|
|
@@ -176,7 +176,7 @@ with gr.Blocks(theme='earneleh/paris') as demo:
|
|
| 176 |
weight_output = gr.TextArea(label="Geometric Values", lines=8, show_copy_button=True)
|
| 177 |
calculate_button = gr.Button("Calculate")
|
| 178 |
with gr.Column():
|
| 179 |
-
image_output = gr.Image(label="Molecular Structure", height=400, width=500)
|
| 180 |
calculate_button.click(fn=get_geometric_descriptors, inputs=smiles_input_mw,outputs=[weight_output, image_output])
|
| 181 |
|
| 182 |
with gr.Tab("Check Substructure"):
|
|
@@ -187,15 +187,15 @@ with gr.Blocks(theme='earneleh/paris') as demo:
|
|
| 187 |
substructure_output = gr.Label(label="Is Substructure Present?")
|
| 188 |
check_button = gr.Button("Check")
|
| 189 |
with gr.Column():
|
| 190 |
-
image_output1 = gr.Image(label="Molecular Structure", height=350, width=500)
|
| 191 |
-
image_output2 = gr.Image(label="Sub_Molecular Structure", height=350, width=500)
|
| 192 |
check_button.click(fn=check_substructure, inputs=[smiles_input_sub, substructure_input], outputs=[image_output1, image_output2, substructure_output])
|
| 193 |
|
| 194 |
|
| 195 |
with gr.Tab("Calculate Similarity"):
|
| 196 |
smiles_input1 = gr.Textbox(label="SMILES 1")
|
| 197 |
smiles_input2 = gr.Textbox(label="SMILES 2")
|
| 198 |
-
similarity_output = gr.
|
| 199 |
calculate_button_sim = gr.Button("Calculate Similarity")
|
| 200 |
calculate_button_sim.click(fn=calculate_similarity, inputs=[smiles_input1, smiles_input2], outputs=similarity_output)
|
| 201 |
|
|
|
|
| 13 |
if checkbox == True:
|
| 14 |
cdk = CDK(ignore_3D=False)
|
| 15 |
else:
|
| 16 |
+
cdk = CDK(ignore_3D=True)
|
| 17 |
smiles_list = list(smiles_list.split(','))
|
| 18 |
try:
|
| 19 |
mols = [Chem.AddHs(Chem.MolFromSmiles(smiles)) for smiles in smiles_list]
|
|
|
|
| 29 |
return molfile,str(e)
|
| 30 |
|
| 31 |
except Exception as e:
|
| 32 |
+
return str(e), None
|
| 33 |
|
| 34 |
|
| 35 |
# Function to calculate Molecular Weight
|
|
|
|
| 50 |
try:
|
| 51 |
mol = Chem.MolFromSmiles(smiles)
|
| 52 |
if mol is None:
|
| 53 |
+
return "Invalid SMILES string", None
|
| 54 |
|
| 55 |
# Add hydrogens and compute 3D coordinates
|
| 56 |
mol = Chem.AddHs(mol)
|
|
|
|
| 76 |
df = pd.DataFrame([geometric_descriptors])
|
| 77 |
return df.T,img
|
| 78 |
except Exception as e:
|
| 79 |
+
return str(e), None
|
| 80 |
|
| 81 |
# Function to check if a substructure is present
|
| 82 |
def check_substructure(smiles, substructure_smiles):
|
|
|
|
| 86 |
|
| 87 |
# Check if the molecule is None (invalid SMILES)
|
| 88 |
if molecule is None or substructure is None:
|
| 89 |
+
return None,None,"Invalid SMILES string provided."
|
| 90 |
|
| 91 |
# Use RDKit's HasSubstructMatch to check for the substructure
|
| 92 |
val = molecule.HasSubstructMatch(substructure)
|
|
|
|
| 100 |
img2 = Draw.MolToImage(sub_molecule)
|
| 101 |
return img1, img2, "Substructure is present."
|
| 102 |
except Exception as e:
|
| 103 |
+
return None, None, "Substructure is present."
|
| 104 |
else:
|
| 105 |
+
return img1,None,"Substructure is not present."
|
| 106 |
|
| 107 |
def calculate_similarity(smiles1, smiles2):
|
| 108 |
try:
|
|
|
|
| 176 |
weight_output = gr.TextArea(label="Geometric Values", lines=8, show_copy_button=True)
|
| 177 |
calculate_button = gr.Button("Calculate")
|
| 178 |
with gr.Column():
|
| 179 |
+
image_output = gr.Image(label="Molecular Structure", height=400, width=500, interactive=True)
|
| 180 |
calculate_button.click(fn=get_geometric_descriptors, inputs=smiles_input_mw,outputs=[weight_output, image_output])
|
| 181 |
|
| 182 |
with gr.Tab("Check Substructure"):
|
|
|
|
| 187 |
substructure_output = gr.Label(label="Is Substructure Present?")
|
| 188 |
check_button = gr.Button("Check")
|
| 189 |
with gr.Column():
|
| 190 |
+
image_output1 = gr.Image(label="Molecular Structure", height=350, width=500, interactive=True)
|
| 191 |
+
image_output2 = gr.Image(label="Sub_Molecular Structure", height=350, width=500, interactive=True)
|
| 192 |
check_button.click(fn=check_substructure, inputs=[smiles_input_sub, substructure_input], outputs=[image_output1, image_output2, substructure_output])
|
| 193 |
|
| 194 |
|
| 195 |
with gr.Tab("Calculate Similarity"):
|
| 196 |
smiles_input1 = gr.Textbox(label="SMILES 1")
|
| 197 |
smiles_input2 = gr.Textbox(label="SMILES 2")
|
| 198 |
+
similarity_output = gr.Label(label="Similarity (Tanimoto)")
|
| 199 |
calculate_button_sim = gr.Button("Calculate Similarity")
|
| 200 |
calculate_button_sim.click(fn=calculate_similarity, inputs=[smiles_input1, smiles_input2], outputs=similarity_output)
|
| 201 |
|