Upload 6 files
Browse files- Scripts/Buchwald_model.py +116 -0
- Scripts/Buchwald_prediction.py +114 -0
- Scripts/Homohydrogenation_predition.py +100 -0
- Scripts/Sanitize.py +43 -0
- Scripts/Test_train_split.py +27 -0
- Scripts/homo_hydrogenation_model.py +94 -0
Scripts/Buchwald_model.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib
|
| 2 |
+
matplotlib.use('TkAgg')
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
from rdkit import Chem
|
| 7 |
+
from rdkit.Chem import AllChem
|
| 8 |
+
import h2o
|
| 9 |
+
from h2o.estimators import H2OGradientBoostingEstimator
|
| 10 |
+
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
|
| 11 |
+
|
| 12 |
+
# Initialize H2O
|
| 13 |
+
h2o.init()
|
| 14 |
+
|
| 15 |
+
def featurize_smiles(smiles):
|
| 16 |
+
try:
|
| 17 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 18 |
+
fingerprint = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=1024)
|
| 19 |
+
features = np.array(fingerprint)
|
| 20 |
+
return features
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error featurizing SMILES: {smiles} -> {e}")
|
| 23 |
+
return np.zeros(1024) # Return a zero vector if there's an issue
|
| 24 |
+
|
| 25 |
+
# Specify the input and output column indices
|
| 26 |
+
input_column_index1 = 5 # Replace with index for the first reactant SMILES
|
| 27 |
+
input_column_index2 = 6 # Replace with index for the second reactant SMILES
|
| 28 |
+
output_column_index = 3 # Replace with your output (product) column index
|
| 29 |
+
|
| 30 |
+
# Prepare a H2OFrame for the first train-test split
|
| 31 |
+
file_path = '/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Split_data/buchwald.xlsx'
|
| 32 |
+
splits = pd.ExcelFile(file_path)
|
| 33 |
+
|
| 34 |
+
# Let's use the first split as an example
|
| 35 |
+
train_key = 'train_split_0'
|
| 36 |
+
test_key = 'test_split_0'
|
| 37 |
+
|
| 38 |
+
# Read the train and test sets
|
| 39 |
+
train_df = pd.read_excel(splits, sheet_name=train_key)
|
| 40 |
+
test_df = pd.read_excel(splits, sheet_name=test_key)
|
| 41 |
+
|
| 42 |
+
# Determine the column names using indices
|
| 43 |
+
input_column_name1 = train_df.columns[input_column_index1]
|
| 44 |
+
input_column_name2 = train_df.columns[input_column_index2]
|
| 45 |
+
output_column_name = train_df.columns[output_column_index]
|
| 46 |
+
|
| 47 |
+
# Featurize the SMILES columns for both reactants and concatenate the features
|
| 48 |
+
train_features1 = np.array([featurize_smiles(smiles) for smiles in train_df[input_column_name1]])
|
| 49 |
+
train_features2 = np.array([featurize_smiles(smiles) for smiles in train_df[input_column_name2]])
|
| 50 |
+
train_features = np.hstack((train_features1, train_features2))
|
| 51 |
+
|
| 52 |
+
test_features1 = np.array([featurize_smiles(smiles) for smiles in test_df[input_column_name1]])
|
| 53 |
+
test_features2 = np.array([featurize_smiles(smiles) for smiles in test_df[input_column_name2]])
|
| 54 |
+
test_features = np.hstack((test_features1, test_features2))
|
| 55 |
+
|
| 56 |
+
# Print message after successful featurization
|
| 57 |
+
print("Successfully converted SMILES to Morgan fingerprints for train and test sets.")
|
| 58 |
+
|
| 59 |
+
# Convert to H2OFrames
|
| 60 |
+
train_X = pd.DataFrame(train_features)
|
| 61 |
+
test_X = pd.DataFrame(test_features)
|
| 62 |
+
|
| 63 |
+
# Combine features with the target column
|
| 64 |
+
train_h2o = h2o.H2OFrame(pd.concat([train_X, train_df[[output_column_name]]], axis=1))
|
| 65 |
+
test_h2o = h2o.H2OFrame(pd.concat([test_X, test_df[[output_column_name]]], axis=1))
|
| 66 |
+
|
| 67 |
+
# Set the target and features
|
| 68 |
+
y = output_column_name
|
| 69 |
+
X = train_h2o.columns[:-1] # All columns except the last one
|
| 70 |
+
|
| 71 |
+
# Initialize and train a Gradient Boosting model
|
| 72 |
+
gbm_model = H2OGradientBoostingEstimator(
|
| 73 |
+
ntrees=50,
|
| 74 |
+
max_depth=6,
|
| 75 |
+
learn_rate=0.1,
|
| 76 |
+
seed=1
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
gbm_model.train(x=X, y=y, training_frame=train_h2o)
|
| 80 |
+
|
| 81 |
+
# Evaluate model performance
|
| 82 |
+
performance = gbm_model.model_performance(test_data=test_h2o)
|
| 83 |
+
print(performance)
|
| 84 |
+
|
| 85 |
+
# Visualization: Variable Importance
|
| 86 |
+
gbm_model.varimp_plot()
|
| 87 |
+
plt.show()
|
| 88 |
+
|
| 89 |
+
# Obtain predictions and visualize the confusion matrix
|
| 90 |
+
predictions = gbm_model.predict(test_h2o).as_data_frame()
|
| 91 |
+
conf_matrix = confusion_matrix(test_df[output_column_name], predictions['predict'])
|
| 92 |
+
|
| 93 |
+
# Display confusion matrix
|
| 94 |
+
disp = ConfusionMatrixDisplay(confusion_matrix=conf_matrix)
|
| 95 |
+
disp.plot(cmap=plt.cm.Blues)
|
| 96 |
+
plt.title('Confusion Matrix')
|
| 97 |
+
plt.show()
|
| 98 |
+
|
| 99 |
+
# Function to predict the product from two input SMILES
|
| 100 |
+
def predict_product_from_smiles(smiles_str1, smiles_str2):
|
| 101 |
+
features1 = featurize_smiles(smiles_str1)
|
| 102 |
+
features2 = featurize_smiles(smiles_str2)
|
| 103 |
+
features = np.hstack((features1, features2))
|
| 104 |
+
feature_df = pd.DataFrame([features])
|
| 105 |
+
feature_h2o = h2o.H2OFrame(feature_df)
|
| 106 |
+
prediction = gbm_model.predict(feature_h2o)
|
| 107 |
+
predicted_value = prediction.as_data_frame()['predict'][0]
|
| 108 |
+
print(f"Predicted output for input SMILES '{smiles_str1}' + '{smiles_str2}': {predicted_value}")
|
| 109 |
+
|
| 110 |
+
# Example usage
|
| 111 |
+
#example_smiles1 = 'CCO' # Replace with a first reactant SMILES string
|
| 112 |
+
#example_smiles2 = 'O=O' # Replace with a second reactant SMILES string
|
| 113 |
+
#predict_product_from_smiles(example_smiles1, example_smiles2)
|
| 114 |
+
|
| 115 |
+
# Shutdown H2O
|
| 116 |
+
h2o.shutdown(prompt=False)
|
Scripts/Buchwald_prediction.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib
|
| 2 |
+
|
| 3 |
+
matplotlib.use('TkAgg')
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
from rdkit import Chem
|
| 7 |
+
from rdkit.Chem import AllChem
|
| 8 |
+
import h2o
|
| 9 |
+
from h2o.estimators import H2OGradientBoostingEstimator
|
| 10 |
+
|
| 11 |
+
# Initialize H2O
|
| 12 |
+
h2o.init()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def featurize_smiles(smiles):
|
| 16 |
+
try:
|
| 17 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 18 |
+
fingerprint = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=1024)
|
| 19 |
+
features = np.array(fingerprint)
|
| 20 |
+
return features
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error featurizing SMILES: {smiles} -> {e}")
|
| 23 |
+
return np.zeros(1024) # Return a zero vector if there's an issue
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Specify the input and output column indices
|
| 27 |
+
input_column_index1 = 5 # Replace with index for the first reactant SMILES
|
| 28 |
+
input_column_index2 = 6 # Replace with index for the second reactant SMILES
|
| 29 |
+
output_column_index = 3 # Replace with your output (product) column index
|
| 30 |
+
|
| 31 |
+
# Prepare a H2OFrame for the first train-test split
|
| 32 |
+
file_path = '/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Split_data/buchwald.xlsx'
|
| 33 |
+
splits = pd.ExcelFile(file_path)
|
| 34 |
+
|
| 35 |
+
# Let's use the first split as an example
|
| 36 |
+
train_key = 'train_split_0'
|
| 37 |
+
test_key = 'test_split_0'
|
| 38 |
+
|
| 39 |
+
# Read the train and test sets
|
| 40 |
+
train_df = pd.read_excel(splits, sheet_name=train_key)
|
| 41 |
+
test_df = pd.read_excel(splits, sheet_name=test_key)
|
| 42 |
+
|
| 43 |
+
# Determine the column names using indices
|
| 44 |
+
input_column_name1 = train_df.columns[input_column_index1]
|
| 45 |
+
input_column_name2 = train_df.columns[input_column_index2]
|
| 46 |
+
output_column_name = train_df.columns[output_column_index]
|
| 47 |
+
|
| 48 |
+
# Featurize the SMILES columns for both reactants and concatenate the features
|
| 49 |
+
train_features1 = np.array([featurize_smiles(smiles) for smiles in train_df[input_column_name1]])
|
| 50 |
+
train_features2 = np.array([featurize_smiles(smiles) for smiles in train_df[input_column_name2]])
|
| 51 |
+
train_features = np.hstack((train_features1, train_features2))
|
| 52 |
+
|
| 53 |
+
test_features1 = np.array([featurize_smiles(smiles) for smiles in test_df[input_column_name1]])
|
| 54 |
+
test_features2 = np.array([featurize_smiles(smiles) for smiles in test_df[input_column_name2]])
|
| 55 |
+
test_features = np.hstack((test_features1, test_features2))
|
| 56 |
+
|
| 57 |
+
# Print message after successful featurization
|
| 58 |
+
print("Successfully converted SMILES to Morgan fingerprints for train and test sets.")
|
| 59 |
+
|
| 60 |
+
# Convert to H2OFrames
|
| 61 |
+
train_X = pd.DataFrame(train_features)
|
| 62 |
+
test_X = pd.DataFrame(test_features)
|
| 63 |
+
|
| 64 |
+
# Combine features with the target column
|
| 65 |
+
train_h2o = h2o.H2OFrame(pd.concat([train_X, train_df[[output_column_name]]], axis=1))
|
| 66 |
+
test_h2o = h2o.H2OFrame(pd.concat([test_X, test_df[[output_column_name]]], axis=1))
|
| 67 |
+
|
| 68 |
+
# Set the target and features
|
| 69 |
+
y = output_column_name
|
| 70 |
+
X = train_h2o.columns[:-1] # All columns except the last one
|
| 71 |
+
|
| 72 |
+
# Initialize and train a Gradient Boosting model
|
| 73 |
+
gbm_model = H2OGradientBoostingEstimator(
|
| 74 |
+
ntrees=50,
|
| 75 |
+
max_depth=6,
|
| 76 |
+
learn_rate=0.1,
|
| 77 |
+
seed=1
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
gbm_model.train(x=X, y=y, training_frame=train_h2o)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Function to predict the product from two input SMILES
|
| 84 |
+
def predict_product_from_smiles(smiles_str1, smiles_str2):
|
| 85 |
+
# Featurize both input SMILES strings
|
| 86 |
+
features1 = featurize_smiles(smiles_str1)
|
| 87 |
+
features2 = featurize_smiles(smiles_str2)
|
| 88 |
+
|
| 89 |
+
# Concatenate the features
|
| 90 |
+
features = np.hstack((features1, features2))
|
| 91 |
+
|
| 92 |
+
# Convert to a DataFrame
|
| 93 |
+
feature_df = pd.DataFrame([features])
|
| 94 |
+
|
| 95 |
+
# Create H2OFrame from the feature DataFrame
|
| 96 |
+
feature_h2o = h2o.H2OFrame(feature_df)
|
| 97 |
+
|
| 98 |
+
# Predict using the trained model
|
| 99 |
+
prediction = gbm_model.predict(feature_h2o)
|
| 100 |
+
|
| 101 |
+
# Retrieve the first element from the prediction column
|
| 102 |
+
predicted_value = prediction.as_data_frame()['predict'][0]
|
| 103 |
+
|
| 104 |
+
# Print the prediction result
|
| 105 |
+
print(f"Predicted output for input SMILES '{smiles_str1}' + '{smiles_str2}': {predicted_value}")
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# Example usage
|
| 109 |
+
example_smiles1 = 'BrC1=CC=CC=C1' # Replace with a first reactant SMILES string
|
| 110 |
+
example_smiles2 = 'NC(C1=CC=CC=C1)=O' # Replace with a second reactant SMILES string
|
| 111 |
+
predict_product_from_smiles(example_smiles1, example_smiles2)
|
| 112 |
+
|
| 113 |
+
# Shutdown H2O
|
| 114 |
+
h2o.shutdown(prompt=False)
|
Scripts/Homohydrogenation_predition.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
from rdkit import Chem
|
| 4 |
+
from rdkit.Chem import AllChem
|
| 5 |
+
import h2o
|
| 6 |
+
from h2o.estimators import H2OGradientBoostingEstimator
|
| 7 |
+
|
| 8 |
+
# Initialize H2O
|
| 9 |
+
h2o.init()
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def featurize_smiles(smiles):
|
| 13 |
+
try:
|
| 14 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 15 |
+
# Generate a binary Morgan fingerprint with a radius of 2 and 1024 bits
|
| 16 |
+
fingerprint = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=1024)
|
| 17 |
+
features = np.array(fingerprint)
|
| 18 |
+
return features
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Error featurizing SMILES: {smiles} -> {e}")
|
| 21 |
+
return np.zeros(1024) # Return a zero vector if there's an issue
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Specify the input and output column indices
|
| 25 |
+
input_column_index = 5 # Replace with your SMILES input column index
|
| 26 |
+
output_column_index = 3 # Replace with your output column index
|
| 27 |
+
|
| 28 |
+
# Prepare a H2OFrame for the first train-test split
|
| 29 |
+
file_path = '/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Split_data/homo_hydrogenation.xlsx'
|
| 30 |
+
splits = pd.ExcelFile(file_path)
|
| 31 |
+
|
| 32 |
+
# Let's use the first split as an example
|
| 33 |
+
train_key = 'train_split_0'
|
| 34 |
+
test_key = 'test_split_0'
|
| 35 |
+
|
| 36 |
+
# Read the train and test sets
|
| 37 |
+
train_df = pd.read_excel(splits, sheet_name=train_key)
|
| 38 |
+
test_df = pd.read_excel(splits, sheet_name=test_key)
|
| 39 |
+
|
| 40 |
+
# Determine the column names using indices
|
| 41 |
+
input_column_name = train_df.columns[input_column_index]
|
| 42 |
+
output_column_name = train_df.columns[output_column_index]
|
| 43 |
+
|
| 44 |
+
# Featurize the SMILES columns
|
| 45 |
+
train_features = np.array([featurize_smiles(smiles) for smiles in train_df[input_column_name]])
|
| 46 |
+
test_features = np.array([featurize_smiles(smiles) for smiles in test_df[input_column_name]])
|
| 47 |
+
|
| 48 |
+
# Print message after successful featurization
|
| 49 |
+
print("Successfully converted SMILES to Morgan fingerprints for train and test sets.")
|
| 50 |
+
|
| 51 |
+
# Convert to H2OFrames
|
| 52 |
+
train_X = pd.DataFrame(train_features)
|
| 53 |
+
test_X = pd.DataFrame(test_features)
|
| 54 |
+
|
| 55 |
+
# Combine features with the target column
|
| 56 |
+
train_h2o = h2o.H2OFrame(pd.concat([train_X, train_df[[output_column_name]]], axis=1))
|
| 57 |
+
test_h2o = h2o.H2OFrame(pd.concat([test_X, test_df[[output_column_name]]], axis=1))
|
| 58 |
+
|
| 59 |
+
# Set the target and features
|
| 60 |
+
y = output_column_name
|
| 61 |
+
X = train_h2o.columns[:-1] # All columns except the last one
|
| 62 |
+
|
| 63 |
+
# Initialize and train a Gradient Boosting model
|
| 64 |
+
gbm_model = H2OGradientBoostingEstimator(
|
| 65 |
+
ntrees=50, # Number of trees
|
| 66 |
+
max_depth=6, # Maximum depth
|
| 67 |
+
learn_rate=0.1, # Learning rate
|
| 68 |
+
seed=1 # For reproducibility
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
gbm_model.train(x=X, y=y, training_frame=train_h2o)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Function to predict the output from SMILES
|
| 75 |
+
def predict_product_from_smiles(smiles_str):
|
| 76 |
+
# Featurize the input SMILES string
|
| 77 |
+
features = featurize_smiles(smiles_str)
|
| 78 |
+
|
| 79 |
+
# Convert to a DataFrame
|
| 80 |
+
feature_df = pd.DataFrame([features])
|
| 81 |
+
|
| 82 |
+
# Create H2OFrame from the feature DataFrame
|
| 83 |
+
feature_h2o = h2o.H2OFrame(feature_df)
|
| 84 |
+
|
| 85 |
+
# Predict using the trained model
|
| 86 |
+
prediction = gbm_model.predict(feature_h2o)
|
| 87 |
+
|
| 88 |
+
# Retrieve the first element from the prediction column
|
| 89 |
+
predicted_value = prediction.as_data_frame()['predict'][0]
|
| 90 |
+
|
| 91 |
+
# Print the prediction result
|
| 92 |
+
print(f"Predicted output for input SMILES '{smiles_str}': {predicted_value}")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# Example usage
|
| 96 |
+
example_smiles = 'C=C(CN1)C2=CC(C)=C(C)C=C2C1=O' # Replace with a SMILES string of your choice
|
| 97 |
+
predict_product_from_smiles(example_smiles)
|
| 98 |
+
|
| 99 |
+
# Shutdown H2O
|
| 100 |
+
h2o.shutdown(prompt=False)
|
Scripts/Sanitize.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from rdkit import Chem
|
| 3 |
+
from molvs import Standardizer
|
| 4 |
+
|
| 5 |
+
# Read the CSV file
|
| 6 |
+
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
|
| 7 |
+
df = pd.read_csv(file_path)
|
| 8 |
+
|
| 9 |
+
# Initialize the Standardizer from MolVS
|
| 10 |
+
standardizer = Standardizer()
|
| 11 |
+
|
| 12 |
+
# List the columns that contain SMILES strings
|
| 13 |
+
smiles_columns = ['PRODUCT_STRUCTURE', 'catalyst_1_ID_1_SMILES', 'Reactant_1_SMILES', 'reactant_2_SMILES', 'reactant_3_SMILES'] # replace with your SMILES columns
|
| 14 |
+
|
| 15 |
+
def sanitize_smiles(smiles):
|
| 16 |
+
try:
|
| 17 |
+
if pd.isna(smiles):
|
| 18 |
+
return smiles # Return NA or None if the original data is NA
|
| 19 |
+
|
| 20 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 21 |
+
if mol:
|
| 22 |
+
standardized_mol = standardizer.standardize(mol)
|
| 23 |
+
sanitized_smiles = Chem.MolToSmiles(standardized_mol)
|
| 24 |
+
print(f"SMILES successfully sanitized: {sanitized_smiles}")
|
| 25 |
+
return sanitized_smiles
|
| 26 |
+
else:
|
| 27 |
+
return None
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Error standardizing SMILES: {smiles} -> {e}")
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
# Apply sanitization to each SMILES column
|
| 33 |
+
for col in smiles_columns:
|
| 34 |
+
df[col] = df[col].apply(sanitize_smiles)
|
| 35 |
+
|
| 36 |
+
# List the columns you want to keep in the output CSV
|
| 37 |
+
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
|
| 38 |
+
|
| 39 |
+
# Filter the DataFrame to keep only the specified columns
|
| 40 |
+
filtered_df = df[columns_to_keep]
|
| 41 |
+
|
| 42 |
+
# Save the sanitized SMILES back to a new CSV file
|
| 43 |
+
filtered_df.to_csv('/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Sanitized_data/ullmann.csv', index=False)
|
Scripts/Test_train_split.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import xlsxwriter
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
|
| 5 |
+
# Load your prepared dataset
|
| 6 |
+
file_path = '/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Sanitized_data/homo_hydrogenation.csv' # Path to your sanitized CSV
|
| 7 |
+
df = pd.read_csv(file_path)
|
| 8 |
+
|
| 9 |
+
# Number of splits you want to create
|
| 10 |
+
n_splits = 5
|
| 11 |
+
|
| 12 |
+
# Initialize a writer to write multiple sheets
|
| 13 |
+
output_file = '/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Split_data/homo_hydrogenation.xlsx'
|
| 14 |
+
|
| 15 |
+
with pd.ExcelWriter(output_file, engine='xlsxwriter') as writer:
|
| 16 |
+
for split_index in range(n_splits):
|
| 17 |
+
# Perform the train-test split
|
| 18 |
+
train_df, test_df = train_test_split(df, test_size=0.2, random_state=split_index)
|
| 19 |
+
|
| 20 |
+
# Write each train and test split to separate sheets
|
| 21 |
+
train_sheet_name = f'train_split_{split_index}'
|
| 22 |
+
test_sheet_name = f'test_split_{split_index}'
|
| 23 |
+
|
| 24 |
+
train_df.to_excel(writer, sheet_name=train_sheet_name, index=False)
|
| 25 |
+
test_df.to_excel(writer, sheet_name=test_sheet_name, index=False)
|
| 26 |
+
|
| 27 |
+
print(f'Train-test splits saved to {output_file}')
|
Scripts/homo_hydrogenation_model.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib
|
| 2 |
+
matplotlib.use('TkAgg')
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
from rdkit import Chem
|
| 7 |
+
from rdkit.Chem import AllChem
|
| 8 |
+
import h2o
|
| 9 |
+
from h2o.estimators import H2OGradientBoostingEstimator
|
| 10 |
+
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
|
| 11 |
+
|
| 12 |
+
# Initialize H2O
|
| 13 |
+
h2o.init()
|
| 14 |
+
|
| 15 |
+
def featurize_smiles(smiles):
|
| 16 |
+
try:
|
| 17 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 18 |
+
# Generate a binary Morgan fingerprint with a radius of 2 and 1024 bits
|
| 19 |
+
fingerprint = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=1024)
|
| 20 |
+
features = np.array(fingerprint)
|
| 21 |
+
return features
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Error featurizing SMILES: {smiles} -> {e}")
|
| 24 |
+
return np.zeros(1024) # Return a zero vector if there's an issue
|
| 25 |
+
|
| 26 |
+
# Specify the input and output column indices
|
| 27 |
+
input_column_index = 5 # Replace with your SMILES input column index
|
| 28 |
+
output_column_index = 3 # Replace with your output column index
|
| 29 |
+
|
| 30 |
+
# Prepare a H2OFrame for the first train-test split
|
| 31 |
+
file_path = '/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Split_data/homo_hydrogenation.xlsx'
|
| 32 |
+
splits = pd.ExcelFile(file_path)
|
| 33 |
+
|
| 34 |
+
# Let's use the first split as an example
|
| 35 |
+
train_key = 'train_split_0'
|
| 36 |
+
test_key = 'test_split_0'
|
| 37 |
+
|
| 38 |
+
# Read the train and test sets
|
| 39 |
+
train_df = pd.read_excel(splits, sheet_name=train_key)
|
| 40 |
+
test_df = pd.read_excel(splits, sheet_name=test_key)
|
| 41 |
+
|
| 42 |
+
# Determine the column names using indices
|
| 43 |
+
input_column_name = train_df.columns[input_column_index]
|
| 44 |
+
output_column_name = train_df.columns[output_column_index]
|
| 45 |
+
|
| 46 |
+
# Featurize the SMILES columns
|
| 47 |
+
train_features = np.array([featurize_smiles(smiles) for smiles in train_df[input_column_name]])
|
| 48 |
+
test_features = np.array([featurize_smiles(smiles) for smiles in test_df[input_column_name]])
|
| 49 |
+
|
| 50 |
+
# Print message after successful featurization
|
| 51 |
+
print("Successfully converted SMILES to Morgan fingerprints for train and test sets.")
|
| 52 |
+
|
| 53 |
+
# Convert to H2OFrames
|
| 54 |
+
train_X = pd.DataFrame(train_features)
|
| 55 |
+
test_X = pd.DataFrame(test_features)
|
| 56 |
+
|
| 57 |
+
# Combine features with the target column
|
| 58 |
+
train_h2o = h2o.H2OFrame(pd.concat([train_X, train_df[[output_column_name]]], axis=1))
|
| 59 |
+
test_h2o = h2o.H2OFrame(pd.concat([test_X, test_df[[output_column_name]]], axis=1))
|
| 60 |
+
|
| 61 |
+
# Set the target and features
|
| 62 |
+
y = output_column_name
|
| 63 |
+
X = train_h2o.columns[:-1] # All columns except the last one
|
| 64 |
+
|
| 65 |
+
# Initialize and train a Gradient Boosting model
|
| 66 |
+
gbm_model = H2OGradientBoostingEstimator(
|
| 67 |
+
ntrees=50, # Number of trees
|
| 68 |
+
max_depth=6, # Maximum depth
|
| 69 |
+
learn_rate=0.1, # Learning rate
|
| 70 |
+
seed=1 # For reproducibility
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
gbm_model.train(x=X, y=y, training_frame=train_h2o)
|
| 74 |
+
|
| 75 |
+
# Evaluate model performance
|
| 76 |
+
performance = gbm_model.model_performance(test_data=test_h2o)
|
| 77 |
+
print(performance)
|
| 78 |
+
|
| 79 |
+
# Visualization: Feature Importance
|
| 80 |
+
gbm_model.varimp_plot()
|
| 81 |
+
plt.show()
|
| 82 |
+
|
| 83 |
+
# Visualization: Confusion Matrix
|
| 84 |
+
predictions = gbm_model.predict(test_h2o).as_data_frame()
|
| 85 |
+
conf_matrix = confusion_matrix(test_df[output_column_name], predictions['predict'])
|
| 86 |
+
|
| 87 |
+
# Display confusion matrix
|
| 88 |
+
disp = ConfusionMatrixDisplay(confusion_matrix=conf_matrix)
|
| 89 |
+
disp.plot(cmap=plt.cm.Blues)
|
| 90 |
+
plt.title('Confusion Matrix')
|
| 91 |
+
plt.show()
|
| 92 |
+
|
| 93 |
+
# H2O Shutdown (optional)
|
| 94 |
+
h2o.shutdown(prompt=False)
|