<jupyter_start><jupyter_text>**Extras**  **2D and 3D representation of molecules**<jupyter_code># ====================================================
# Installation
# ====================================================

# Handling formulas
!conda install -y -c rdkit rdkit
!pip install cairosvg==2.5.2

# 3D
!pip install py3Dmol #https://github.com/3dmol/3Dmol.js
!pip install pubchempy #https://github.com/mcs07/PubChemPy
# ====================================================
# Libraries
# ====================================================

import pandas as pd
import numpy as np
import random
import os
import matplotlib.pyplot as plt
import pickle
from rdkit import Chem
from rdkit.Chem import AllChem, Draw
from rdkit.Chem.Draw import rdMolDraw2D
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem.Draw import MolDrawOptions
from PIL import Image
import cairosvg
import cv2
from tqdm.auto import tqdm
import sys
tqdm.pandas()

import py3Dmol
from pubchempy import get_compounds, request, Compound, get_cids
IPythonConsole.ipython_useSVG = True

# ====================================================
# Data Loading
# ====================================================

df = pd.read_csv("../input/bms-molecular-translation/train_labels.csv").copy()
display(df)

# ====================================================
# Functions
# ====================================================

def mol_with_atom_index(mol):
    for atom in mol.GetAtoms():
        atom.SetAtomMapNum(atom.GetIdx())
    return mol

def draw_structure_2D3D(inchi, structure="3D"):
    
    mol = Chem.MolFromInchi(inchi)
    smiles = Chem.MolToSmiles(mol)
    assert inchi == Chem.MolToInchi(Chem.MolFromSmiles(smiles))
    
    if structure=="3D":
        cid_for_query = 'cid:'+str(get_cids(smiles, 'smiles')[0])

        view = py3Dmol.view(width=680, height=300, query=cid_for_query, linked=False)
        view.setStyle({'stick': {},'sphere':{'radius':0.5}})
        return view.show()
        
    else:
        return mol_with_atom_index(mol)
# ====================================================
# 3D Visualization
# ====================================================

draw_structure_2D3D(df.InChI.iloc[25], structure="3D")
# ====================================================
# 2D Visualization
# ====================================================

draw_structure_2D3D(df.InChI.iloc[25], structure="2D")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.34276342763427636, 'fraction_numerical': 0.016400164001640016, 'mean_word_length': 6.155425219941349, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '225340', 'license': 'MIT', 'n_tokens_mistral': 789, 'n_tokens_neox': 721, 'n_words': 144, 'path': 'Extras.ipynb', 'repo_name': 'Fpiotro/MOLECULAR-TRANSLATION', 'size': '2439'}