{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Convert inchi to graph and graph to inchi\n\nThis approach might be an alternative to brute force text generation as we explicitly work with graph and not trying to hack it in string\n\n[Base notebook](https://github.com/dakoner/keras-molecules/blob/dbbb790e74e406faa70b13e8be8104d9e938eba2/convert_rdkit_to_networkx.py)","metadata":{}},{"cell_type":"code","source":"!conda install -c conda-forge -y rdkit","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import networkx as nx\nimport pandas as pd\nfrom rdkit import Chem","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/bms-molecular-translation/train_labels.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mol_to_nx(mol: Chem.Mol) -> nx.Graph:\n    G = nx.Graph()\n\n    for atom in mol.GetAtoms():\n        G.add_node(atom.GetIdx(),\n                   atomic_num=atom.GetAtomicNum(),\n                   formal_charge=atom.GetFormalCharge(),\n                   chiral_tag=atom.GetChiralTag(),\n                   hybridization=atom.GetHybridization(),\n                   num_explicit_hs=atom.GetNumExplicitHs(),\n                   is_aromatic=atom.GetIsAromatic())\n    for bond in mol.GetBonds():\n        G.add_edge(bond.GetBeginAtomIdx(),\n                   bond.GetEndAtomIdx(),\n                   bond_type=bond.GetBondType())\n    return G\n\ndef nx_to_mol(G: nx.Graph) -> Chem.Mol:\n    mol = Chem.RWMol()\n    atomic_nums = nx.get_node_attributes(G, 'atomic_num')\n    chiral_tags = nx.get_node_attributes(G, 'chiral_tag')\n    formal_charges = nx.get_node_attributes(G, 'formal_charge')\n    node_is_aromatics = nx.get_node_attributes(G, 'is_aromatic')\n    node_hybridizations = nx.get_node_attributes(G, 'hybridization')\n    num_explicit_hss = nx.get_node_attributes(G, 'num_explicit_hs')\n    node_to_idx = {}\n    for node in G.nodes():\n        a=Chem.Atom(atomic_nums[node])\n        a.SetChiralTag(chiral_tags[node])\n        a.SetFormalCharge(formal_charges[node])\n        a.SetIsAromatic(node_is_aromatics[node])\n        a.SetHybridization(node_hybridizations[node])\n        a.SetNumExplicitHs(num_explicit_hss[node])\n        idx = mol.AddAtom(a)\n        node_to_idx[node] = idx\n\n    bond_types = nx.get_edge_attributes(G, 'bond_type')\n    for edge in G.edges():\n        first, second = edge\n        ifirst = node_to_idx[first]\n        isecond = node_to_idx[second]\n        bond_type = bond_types[first, second]\n        mol.AddBond(ifirst, isecond, bond_type)\n\n    Chem.SanitizeMol(mol)\n    return mol","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inch = df.InChI[0]\nmol = Chem.MolFromInchi(inch)\ngraph = mol_to_nx(mol)\nrestored_inchi = Chem.MolToInchi(nx_to_mol(graph))\nassert restored_inchi == inch\nprint(inch)\nprint(restored_inchi)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Molecular graph generation might be the next step to explore\n\nhttps://arxiv.org/abs/1802.04364\n\nhttps://github.com/wengong-jin/icml18-jtnn","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}