{"cells":[{"metadata":{},"cell_type":"markdown","source":"In this kernel, I extract new features, at molecule level, making use of pre-trained MOL2VEC model.\n\nThe algorith is based on the classical Word2Vec for NLP. I found this beatiful [project on GitHub](https://github.com/samoturk/mol2vec) which easy provides the conversion of molecules into vectors. The pyhton package gives you also all the instruments to train your own vector rapresentation of molecules (here, I make use of a pre-trained model).\n\nAll we need is the SMILE notation for our molecules. I've found a good [kernel](https://www.kaggle.com/roccomeli/easy-xyz-to-smiles-conversion) which has already implemented the creation of SMILE code.\nWe need SMILEs because we have to create the Morgan fingerprint. This is just a numeric identifier for a molecule substructure.\nWith fingerprints at our disposal we can question MOL2VEC to get the corresponding structure representation of a given molecule.   "},{"metadata":{},"cell_type":"markdown","source":"**Install the required packages**"},{"metadata":{"trusted":true},"cell_type":"code","source":"!conda install openbabel -c openbabel -y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\n!conda install --yes --prefix {sys.prefix} -c rdkit rdkit","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install git+https://github.com/samoturk/mol2vec","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom tqdm import tqdm_notebook as tqdm\n\nfrom rdkit import Chem\nfrom mol2vec.features import mol2alt_sentence\nimport pybel\n\nfrom gensim.models import Word2Vec","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Read structure files**"},{"metadata":{"trusted":true},"cell_type":"code","source":"file_dir = '../input/champs-scalar-coupling/structures/'\nmols_files=os.listdir(file_dir)\nmols_index=dict(map(reversed,enumerate(mols_files)))\nmol_name = list(mols_index.keys())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**SMILE creation**"},{"metadata":{"trusted":true},"cell_type":"code","source":"# FROM: https://www.kaggle.com/roccomeli/easy-xyz-to-smiles-conversion\n\ndef xyz_to_smiles(fname: str) -> str:\n    \n    mol = next(pybel.readfile(\"xyz\", fname))\n\n    smi = mol.write(format=\"smi\")\n\n    return smi.split()[0].strip()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"smiles = [xyz_to_smiles(file_dir + i) for i in tqdm(mol_name)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_smiles = pd.DataFrame({'molecule_name': mol_name, 'smiles': smiles})\ndf_smiles.head(11)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Extract Morgan Fingerprints**"},{"metadata":{"trusted":true},"cell_type":"code","source":"sentence = mol2alt_sentence(Chem.MolFromSmiles(df_smiles.smiles[33]), 1)\nprint('SMILE:', df_smiles.smiles[33])\nprint(sentence)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Load pretrained MOL2VEC**"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Word2Vec.load('../input/mol2vec/model_300dim.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.wv[sentence[0]]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**See you on [MEDIUM](https://medium.com/@cerlymarco)**"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}