Created
December 2, 2023 20:56
-
-
Save 0187773933/4a70e75d17b3a22499d314c2efaf2e03 to your computer and use it in GitHub Desktop.
SMILES String Viewer Saver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import tempfile | |
from pathlib import Path | |
from PIL import Image | |
from rdkit import Chem | |
from rdkit.Chem import Draw | |
# pip install rdkit pillow | |
def show_smiles( smiles_string ): | |
with tempfile.TemporaryDirectory() as temp_dir: | |
temp_dir_posix = Path( temp_dir ) | |
with tempfile.NamedTemporaryFile( suffix=".png" , prefix=temp_dir ) as tf: | |
temp_file_path = temp_dir_posix.joinpath( tf.name ) | |
print( temp_file_path ) | |
s_fp = str( temp_file_path ) | |
molecule = Chem.MolFromSmiles( smiles_string ) | |
Draw.MolToFile( molecule , s_fp , size=( 1000 , 1000 ) ) | |
img = Image.open( s_fp ) | |
img.show() | |
def save_smiles( smiles_string , file_path ): | |
molecule = Chem.MolFromSmiles( smiles_string ) | |
Draw.MolToFile( molecule , file_path , size=( 1000 , 1000 ) ) | |
if __name__ == "__main__": | |
show_smiles( sys.argv[ 1 ] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment