Liability / preprocessing scripts /MSTI Thiol Interference_preprocessing script.py
haneulpark's picture
Upload 4 files
d68447d verified
raw
history blame
2.82 kB
# 1. Load Modules
pip install rdkit
pip install molvs
import pandas as pd
import numpy as np
import rdkit
import molvs
from rdkit import Chem
standardizer = molvs.Standardizer()
fragment_remover = molvs.fragment.FragmentRemover()
# 2. Convert the SDF file from the original paper into data frame
# Before running the code, please download SDF files from the original paper
# https://pubs.acs.org/doi/10.1021/acs.jmedchem.3c00482
from rdkit.Chem import PandasTools
sdfFile = 'Thiol_training_set_curated.sdf'
dataframe = PandasTools.LoadSDF(sdfFile)
dataframe.to_csv('thiol.csv', index=False)
df = pd.read_csv('thiol.csv')
# 3. Resolve SMILES parse error
# Some of the 'Raw_SMILES' rows contain TWO SMILES separated by ';'' and, they cause SMILES parse error (which means they cannot be read)
# So we separated the SMILES and renamed the columns
df.rename(columns = {'PUBCHEM_EXT_DATASOURCE_REGID': 'REGID_1'}, inplace = True)
df.rename(columns = {'Other REGIDs': 'REGID_2'}, inplace = True)
df.insert(2, 'REGID_3', np.NaN)
df['REGID_3'] = df['REGID_2'].str.split(',').str[1]
df['REGID_2'] = df['REGID_2'].str.split(',').str[0]
df.insert(4, 'SMILES_2', np.NaN)
df.insert(5, 'SMILES_3', np.NaN)
df[['Raw_SMILES', 'SMILES_2', 'SMILES_3']] = df['Raw_SMILES'].str.split(';', expand=True)
df.rename(columns= {'Raw_SMILES' : 'SMILES_1'}, inplace = True)
# 4. Sanitize with MolVS and print problems
df['X_1'] = [ \
rdkit.Chem.MolToSmiles(
fragment_remover.remove(
standardizer.standardize(
rdkit.Chem.MolFromSmiles(
smiles))))
for smiles in df['SMILES_1']]
def process_smiles(smiles):
if pd.isna(smiles):
return None
try:
return rdkit.Chem.MolToSmiles(
fragment_remover.remove(
standardizer.standardize(
rdkit.Chem.MolFromSmiles(smiles))))
except Exception as e:
print(f"Error processing SMILES {smiles}: {e}")
return None
df['X_2'] = df['SMILES_2'].apply(process_smiles)
def process_smiles(smiles):
if pd.isna(smiles):
return None
try:
return rdkit.Chem.MolToSmiles(
fragment_remover.remove(
standardizer.standardize(
rdkit.Chem.MolFromSmiles(smiles))))
except Exception as e:
print(f"Error processing SMILES {smiles}: {e}")
return None
df['X_3'] = df['SMILES_3'].apply(process_smiles)
# 5. Rename the columns
df.rename(columns={'X_1' : 'newSMILES_1', 'X_2' : 'newSMILES_2', 'X_3' : 'newSMILES_3'}, inplace = True)
# 6. Create a file with sanitized SMILES
df[['REGID_1',
'REGID_2',
'REGID_3',
'newSMILES_1',
'newSMILES_2',
'newSMILES_3',
'log_AC50_M',
'Efficacy',
'CC-v2',
'Outcome',
'InChIKey',
'ID',
'ROMol']].to_csv('thiol_sanitized.csv', index = False)