import pandas as pd import numpy as np from rdkit import Chem from rdkit.Chem import AllChem import h2o from h2o.estimators import H2OGradientBoostingEstimator # Initialize H2O h2o.init() def featurize_smiles(smiles): try: mol = Chem.MolFromSmiles(smiles) # Generate a binary Morgan fingerprint with a radius of 2 and 1024 bits fingerprint = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=1024) features = np.array(fingerprint) return features except Exception as e: print(f"Error featurizing SMILES: {smiles} -> {e}") return np.zeros(1024) # Return a zero vector if there's an issue # Specify the input and output column indices input_column_index = 5 # Replace with your SMILES input column index output_column_index = 3 # Replace with your output column index # Prepare a H2OFrame for the first train-test split file_path = '/Users/colestephens/Desktop/CodeFolders/ML_Dataset_Curation_Project/Split_data/homo_hydrogenation.xlsx' splits = pd.ExcelFile(file_path) # Let's use the first split as an example train_key = 'train_split_0' test_key = 'test_split_0' # Read the train and test sets train_df = pd.read_excel(splits, sheet_name=train_key) test_df = pd.read_excel(splits, sheet_name=test_key) # Determine the column names using indices input_column_name = train_df.columns[input_column_index] output_column_name = train_df.columns[output_column_index] # Featurize the SMILES columns train_features = np.array([featurize_smiles(smiles) for smiles in train_df[input_column_name]]) test_features = np.array([featurize_smiles(smiles) for smiles in test_df[input_column_name]]) # Print message after successful featurization print("Successfully converted SMILES to Morgan fingerprints for train and test sets.") # Convert to H2OFrames train_X = pd.DataFrame(train_features) test_X = pd.DataFrame(test_features) # Combine features with the target column train_h2o = h2o.H2OFrame(pd.concat([train_X, train_df[[output_column_name]]], axis=1)) test_h2o = h2o.H2OFrame(pd.concat([test_X, test_df[[output_column_name]]], axis=1)) # Set the target and features y = output_column_name X = train_h2o.columns[:-1] # All columns except the last one # Initialize and train a Gradient Boosting model gbm_model = H2OGradientBoostingEstimator( ntrees=50, # Number of trees max_depth=6, # Maximum depth learn_rate=0.1, # Learning rate seed=1 # For reproducibility ) gbm_model.train(x=X, y=y, training_frame=train_h2o) # Function to predict the output from SMILES def predict_product_from_smiles(smiles_str): # Featurize the input SMILES string features = featurize_smiles(smiles_str) # Convert to a DataFrame feature_df = pd.DataFrame([features]) # Create H2OFrame from the feature DataFrame feature_h2o = h2o.H2OFrame(feature_df) # Predict using the trained model prediction = gbm_model.predict(feature_h2o) # Retrieve the first element from the prediction column predicted_value = prediction.as_data_frame()['predict'][0] # Print the prediction result print(f"Predicted output for input SMILES '{smiles_str}': {predicted_value}") # Example usage example_smiles = 'C=C(CN1)C2=CC(C)=C(C)C=C2C1=O' # Replace with a SMILES string of your choice predict_product_from_smiles(example_smiles) # Shutdown H2O h2o.shutdown(prompt=False)