application_vnd.jupyter.stderr_output_27.png application_vnd.jupyter.stderr_output_35.png application_vnd.jupyter.stderr_output_24.png application_vnd.jupyter.stderr_output_16.png application_vnd.jupyter.stderr_output_9.png application_vnd.jupyter.stderr_output_32.png application_vnd.jupyter.stderr_output_2.png application_vnd.jupyter.stderr_output_7.png application_vnd.jupyter.stderr_output_11.png application_vnd.jupyter.stderr_output_18.png application_vnd.jupyter.stderr_output_4.png application_vnd.jupyter.stderr_output_26.png application_vnd.jupyter.stderr_output_6.png application_vnd.jupyter.stderr_output_31.png application_vnd.jupyter.stderr_output_33.png application_vnd.jupyter.stderr_output_25.png application_vnd.jupyter.stderr_output_12.png application_vnd.jupyter.stderr_output_8.png application_vnd.jupyter.stderr_output_10.png application_vnd.jupyter.stderr_output_23.png application_vnd.jupyter.stderr_output_34.png application_vnd.jupyter.stderr_output_19.png application_vnd.jupyter.stderr_output_13.png application_vnd.jupyter.stderr_output_3.png application_vnd.jupyter.stderr_output_5.png application_vnd.jupyter.stderr_output_30.png application_vnd.jupyter.stderr_output_15.png application_vnd.jupyter.stderr_output_17.png application_vnd.jupyter.stderr_output_28.png application_vnd.jupyter.stderr_output_20.png application_vnd.jupyter.stderr_output_22.png application_vnd.jupyter.stderr_output_29.png application_vnd.jupyter.stderr_output_1.png application_vnd.jupyter.stderr_output_14.png application_vnd.jupyter.stderr_output_21.png
from rdkit import Chem

from rdkit.Chem import AllChem

from rdkit.Chem import MolFromSmiles, Descriptors, MolFromSmarts

import deepchem as dc

import numpy as np

import pandas as pd

df_train = pd.read_csv('/kaggle/input/aqueous-solubility-predictioin/train.csv')
df_test = pd.read_csv('/kaggle/input/aqueous-solubility-predictioin/test.csv')

df_train.shape

smiles_list = df_train['SMILES'][:10]

mol_list = []
for smile in smiles_list:
    mol = Chem.MolFromSmiles(smile)
    mol_list.append(mol)
img = Chem.Draw.MolsToGridImage(mol_list, molsPerRow=5)
img

df_train.columns

X = df_train.drop(columns=['ID', 'Name', 'InChI', 'InChIKey', 'SMILES', 'SD', 'Ocurrences', 'Group', 'comp_id'])

# Define a function to featurize a SMILES string
def featurize_smiles(smiles):
    mol = Chem.MolFromSmiles(smiles)
    fp = AllChem.GetMorganFingerprintAsBitVect(mol, 3, nBits=1024)
    features = np.array(list(fp.ToBitString())).astype(float)
    return features

X = np.array([featurize_smiles(smiles) for smiles in df_train['SMILES']])

X.shape

X = pd.DataFrame(X)
X['Solubility'] = df_train['Solubility']

from rdkit.Chem import MolFromSmiles, Descriptors, MolFromSmarts
def create_descriptors(smiles):
    mol = MolFromSmiles(smiles)
    mw = Descriptors.MolWt(mol)
    logp = Descriptors.MolLogP(mol)
    rotb = Descriptors.NumRotatableBonds(mol)
    arom_proportion = len(mol.GetSubstructMatches(MolFromSmarts('a'))) / Descriptors.HeavyAtomCount(mol)
    hbd = Descriptors.NumHDonors(mol)
    hba = Descriptors.NumHAcceptors(mol)
    non_carbon_proportion = len(mol.GetSubstructMatches(MolFromSmarts('[!#6]'))) / Descriptors.HeavyAtomCount(mol)
    psa = Descriptors.TPSA(mol, includeSandP=True)
    fsp3 = Descriptors.FractionCSP3(mol)
    return (mw, logp, rotb, arom_proportion, hbd, hba, non_carbon_proportion, psa, fsp3)

descriptors = [create_descriptors(smiles) for smiles in df_train['SMILES']]
col_names = ['mw', 'logp', 'rotb', 'ap', 'hbd', 'hba', 'non_cp', 'psa', 'fsp3']
train = pd.DataFrame(descriptors, columns=col_names)
train = train.join(df_train['Solubility'], how='left')

additional_col = ['MolMR', 'NumValenceElectrons', 'NumAromaticRings', 'NumSaturatedRings', 'NumAliphaticRings', 'RingCount', 'LabuteASA', 'BalabanJ', 'BertzCT']

df = train.join(df_train[additional_col], how='left')

reg = setup(df, target='Solubility', session_id=123, train_size=0.8, numeric_features=list(df.drop(columns=['Solubility']).columns), transformation=True, normalize=True)

best = create_model('et')

final_model = finalize_model(best)

descriptors = [create_descriptors(smiles) for smiles in df_test['SMILES']]
col_names = ['mw', 'logp', 'rotb', 'ap', 'hbd', 'hba', 'non_cp', 'psa', 'fsp3']
test = pd.DataFrame(descriptors, columns=col_names)

df = test.join(df_test[additional_col], how='left')

df = predict_model(final_model, data=df)

df.rename(columns={'prediction_label': 'Solubility'}, inplace=True)

smiles = ['CCC']
featurizer = dc.feat.Mol2VecFingerprint()
features = featurizer.featurize(smiles)

features = featurizer.featurize(df_train['SMILES'])

features.shape

df = pd.DataFrame(features)
df['Solubility'] = df_train['Solubility']

smiles = ['CC(=O)OC1=CC=CC=C1C(=O)O']
featurizer = dc.feat.RDKitDescriptors()
features = featurizer.featurize(smiles)

features.shape

features = featurizer.featurize(df_train['SMILES'])

df = pd.DataFrame(features)
df['Solubility'] = df_train['Solubility']

reg = setup(df, target='Solubility', session_id=123, train_size=0.8)