| |
|
|
| import pandas as pd |
| import numpy as np |
| from rdkit import Chem |
| from rdkit.Chem import Descriptors |
|
|
| |
| def fill_nans_iteratively(df): |
| """ |
| Iteratively fill NaN values in rows that have NaNs in all columns except |
| 'Ligand ID', 'Ligand Name', 'Ligand SMILES', and 'Protein' |
| |
| Parameters: |
| df: pandas DataFrame |
| |
| Returns: |
| DataFrame with filled values |
| """ |
| |
| |
| df_filled = df.copy() |
| |
| |
| exclude_cols = ['Ligand ID', 'Ligand Name', 'Ligand SMILES', 'Protein'] |
| |
| |
| other_cols = [col for col in df_filled.columns if col not in exclude_cols] |
| |
| |
| for i in range(1, len(df_filled)): |
| |
| |
| current_row_other_cols = df_filled.loc[df_filled.index[i], other_cols] |
| |
| if current_row_other_cols.isna().all(): |
| |
| |
| prev_row_values = df_filled.loc[df_filled.index[i-1], other_cols] |
| |
| |
| df_filled.loc[df_filled.index[i], other_cols] = prev_row_values |
| |
| return df_filled |
|
|
|
|
| def calculate_mol_weight(smiles): |
| """Helper function to calculate molecular weight from SMILES""" |
| if pd.isna(smiles) or not isinstance(smiles, str): |
| return np.nan |
| try: |
| mol = Chem.MolFromSmiles(smiles) |
| if mol is not None: |
| return Descriptors.MolWt(mol) |
| else: |
| return np.nan |
| except: |
| return np.nan |
|
|
| |
| def filter_by_molecular_weight(df, min_weight=150, max_weight=1000): |
| """ |
| Filter df by ligands within molecular weight range |
| """ |
| df['Molecular Weight'] = df['Ligand SMILES'].apply(calculate_mol_weight) |
| |
| successful = df['Molecular Weight'].notna().sum() |
| total = len(df) |
| |
| if successful == 0: |
| print("WARNING: No molecules were successfully processed!") |
| |
| print("Sample SMILES strings:") |
| print(df['Ligand Smiles'].head(10).tolist()) |
| return pd.DataFrame() |
| |
| mw_filtered_df = df[ |
| (df['Molecular Weight'].notna()) & |
| (df['Molecular Weight'] >= min_weight) & |
| (df['Molecular Weight'] <= max_weight) |
| ] |
| |
| |
| mw_filtered_df = mw_filtered_df[(mw_filtered_df['Entry ID'].notna())] |
| |
| return mw_filtered_df |
|
|
|
|
|
|
|
|
|
|