File size: 1,844 Bytes
8af1909
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pandas as pd
from rdkit import Chem
from molvs import Standardizer

# Read the CSV file
file_path = '/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Final_Project_Dataset_curration/HiTEA-master/data/cleaned_datasets/ullmann.csv'  # replace with your file path
df = pd.read_csv(file_path)

# Initialize the Standardizer from MolVS
standardizer = Standardizer()

# List the columns that contain SMILES strings
smiles_columns = ['PRODUCT_STRUCTURE', 'catalyst_1_ID_1_SMILES', 'Reactant_1_SMILES', 'reactant_2_SMILES', 'reactant_3_SMILES']  # replace with your SMILES columns

def sanitize_smiles(smiles):
    try:
        if pd.isna(smiles):
            return smiles  # Return NA or None if the original data is NA

        mol = Chem.MolFromSmiles(smiles)
        if mol:
            standardized_mol = standardizer.standardize(mol)
            sanitized_smiles = Chem.MolToSmiles(standardized_mol)
            print(f"SMILES successfully sanitized: {sanitized_smiles}")
            return sanitized_smiles
        else:
            return None
    except Exception as e:
        print(f"Error standardizing SMILES: {smiles} -> {e}")
        return None

# Apply sanitization to each SMILES column
for col in smiles_columns:
    df[col] = df[col].apply(sanitize_smiles)

# List the columns you want to keep in the output CSV
columns_to_keep = ['ID', 'Product_Yield_PCT_Area_UV', 'Solvent_1_Name', 'PRODUCT_STRUCTURE', 'catalyst_1_ID_1_SMILES', 'Reactant_1_SMILES', 'reactant_2_SMILES', 'reactant_3_SMILES']  # Replace with the columns you want

# Filter the DataFrame to keep only the specified columns
filtered_df = df[columns_to_keep]

# Save the sanitized SMILES back to a new CSV file
filtered_df.to_csv('/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Sanitized_data/ullmann.csv', index=False)