Spaces:
Running
Running
File size: 1,346 Bytes
7476d14 |
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 |
from rdkit import DataStructs, Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from PIL import Image
import io
from .main_model import ChemicalConverter
def validate_smiles2iupac(input_smiles, predicted_iupac):
converter = ChemicalConverter(mode="IUPAC2SMILES")
predicted_smiles = converter.convert(predicted_iupac)
ms = [Chem.MolFromSmiles(input_smiles), Chem.MolFromSmiles(predicted_smiles[6:])]
if None in ms:
return None
fpgen = AllChem.GetRDKitFPGenerator()
fps = [fpgen.GetFingerprint(x) for x in ms]
return DataStructs.TanimotoSimilarity(fps[0], fps[1])
def plot_mol(smiles):
# Convert the SMILES string to an RDKit molecule object
mol = Chem.MolFromSmiles(smiles)
# Use RDKit to draw the molecule to an image, with original intended size
img = Draw.MolToImage(mol, size=(185, 185))
# Create a new, blank image with the desired final size (800x190 pixels) with a white background
final_img = Image.new('RGB', (890, 185), 'white')
# Calculate the position to paste the original image onto the blank image to keep it centered
left = (890 - 185) // 2
top = (185 - 185) // 2 # This will be zero in this case but included for clarity
# Paste the original image onto the blank image
final_img.paste(img, (left, top))
return final_img |