komet module

komet.MT_komet(data_set, lambda_list, mM, dM)[source]

To compair a single-task NN SVM approach to the multi-task Komet algorithm in several settings. For each protein in LCIdb considered in turn as the query protein, we performed the following experiment: a test set was built, comprising all known positive DTIs involving the query protein in LCIdb and their balanced negative DTIs. A corresponding training set is built: For Komet, it consists of all DTIs remaining in LCIdb after removal of DTIs that are in the test set, so that the query protein is orphan. The AUPR is calculated on the test set. The process is repeated for each protein in LCIdb, and the average AUPR is calculated.

Parameters:
  • data_set (pandas.DataFrame) – Dataframe containing the dataset (train) with columns ‘SMILES’, ‘Target Sequence’, and ‘Label’.

  • lambda_list (list[float]) – List of regularization parameters to use for training.

  • mM (int) – Number of molecules to use for the Nystrom approximation.

  • dM (int) – Final dimension of features for molecules.

Returns:

DataFrame with the evaluation metrics for each protein.

Return type:

pandas.DataFrame

Note: See section 7 of Supporting Information.

komet.Morgan_FP(list_smiles)[source]

Computes the Morgan fingerprints for a list of SMILES strings.

Parameters:

list_smiles (list) – A list of SMILES strings for which to compute the fingerprints.

Returns:

A tensor containing the Morgan fingerprints for the input SMILES strings.

Return type:

torch.Tensor

komet.NN_ST_SVM(data_set, mM, dM, lbda)[source]

Trains and tests a Neirest Neighbour, single-task SVM model. For each protein, we performed the following experiment: a test set was built, comprising all known positive DTIs involving the query protein in LCIdb and their balanced negative DTIs. A corresponding training set is built: the training set consists of all positive and negative DTIs involving the nearest protein, according to the LAkernel similarity. We traina linear SVM using the same molecular features as Komet. The AUPR is calculated on the test set.

Parameters:
  • data_set (pandas.DataFrame) – Dataframe containing the dataset (train) with columns ‘SMILES’, ‘Target Sequence’, and ‘Label’.

  • mM (int) – Number of molecules to use for the Nystrom approximation.

  • dM (int) – Final dimension of features for molecules.

  • lbda (float) – Regularization parameter for the SVM model.

Returns:

DataFrame with the evaluation metrics for each protein.

Return type:

pandas.DataFrame

Note: Section 7 of Supporting Information of the paper.

komet.Nystrom_X(smiles_list, S, MorganFP, V, rM, Mu, epsi)[source]

Computes the approximate features of the molecular kernel using the Nystrom approximation.

Parameters:
  • smiles_list – A list of SMILES strings for which to compute features.

  • S – Indices of the subset of samples used for the Nystrom approximation.

  • MorganFP – Precomputed Morgan Fingerprints for the dataset.

  • V – Eigenvectors of the kernel matrix.

  • rM – Rank of the approximation.

  • Mu – Eigenvalues of the kernel matrix.

  • epsi – Small regularization term added for numerical stability.

Returns:

Approximated feature matrix for the input SMILES list.

Return type:

torch.Tensor

komet.Nystrom_X_cn(mM, rM, nM, MorganFP, n_max=0)[source]

Compute the Nystrom approximation of the centered normalized feature matrix.

Parameters:
  • mM (int) – Number of molecule landmarks

  • rM (int) – Number of dimensions to keep after SVD for molecule features

  • nM (int) – Total number of molecules.

  • MorganFP (numpy.ndarray) – Matrix of Morgan fingerprints of shape (nM, fingerprint_length).

  • n_max (int, optional) – Maximum number of molecules to consider for the approximation, defaults to nM.

Returns:

The centered normalized feature matrix.

Return type:

torch.Tensor

Notes:

This function computes the Nystrom approximation of the feature matrix using the given Morgan fingerprints. It first selects a random subset S of size mM from the total nM molecules. It then computes the kernel matrix K using the selected subset. The approximate feature matrix is computed using Singular Value Decomposition (SVD) on K. Finally, the features are normalized by centering and dividing by their L2 norm.

The input MorganFP should be a numpy array with shape (nM, fingerprint_length).

komet.SVM_bfgs(X_cn, Y_cn, y, I, J, lamb, niter=50)[source]

Trains an SVM model using the L-BFGS optimization algorithm to minimize the loss function.

Parameters:
  • X_cn – Feature matrix for compounds.

  • Y_cn – Feature matrix for targets.

  • y – Interaction labels.

  • I – Indices of molecules.

  • J – Indices of proteins.

  • lamb – Regularization parameter.

  • niter – Number of iterations for the L-BFGS optimization algorithm.

Returns:

Optimized weight and bias parameters for the SVM model, and the history of the loss function.

Return type:

tuple[torch.Tensor, torch.Tensor, list[float]]

komet.add_indfasta(df)[source]

Adds an index column for each unique FASTA sequence in the dataframe.

Parameters:

df – Dataframe containing protein sequences.

Returns:

The updated dataframe with an ‘indfasta’ column and the array of unique FASTA sequences.

Return type:

tuple[pd.DataFrame, np.ndarray]

komet.add_indsmiles(df)[source]

Adds a column to the dataframe with indices for each unique SMILES string.

Parameters:

df (pd.DataFrame) – The dataframe to be processed.

Returns:

A tuple containing the processed dataframe with an added column for SMILES indices and the array of unique SMILES strings.

Return type:

tuple(pd.DataFrame, np.ndarray)

komet.compute_proba(w_bfgs, b_bfgs, s, t, X_cn, Y_cn, I, J)[source]

Computes probabilities using the trained weights and Platt scaling parameters.

Parameters:
  • w_bfgs – Optimized weights from the SVM model for compounds.

  • b_bfgs – Optimized bias from the SVM model.

  • s – Optimized scaling parameter from Platt scaling.

  • t – Optimized offset parameter from Platt scaling.

  • X_cn – Feature matrix for compounds.

  • Y_cn – Feature matrix for targets.

  • I – Indices of molecules.

  • J – Indices of proteins.

Returns:

A tuple containing the margin values, predicted labels, and predicted probabilities.

Return type:

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

komet.compute_proba_Platt_Scalling(w_bfgs, X_cn, Y_cn, y, I, J, niter=20)[source]

Computes probability estimates for interaction predictions using Platt scaling.

Parameters:
  • w_bfgs – Optimized weights from the SVM model.

  • X_cn – Feature matrix for compounds.

  • Y_cn – Feature matrix for targets.

  • y – Interaction labels.

  • I – Indices of molecules.

  • J – Indices of proteins.

  • niter – Number of iterations for the L-BFGS optimization algorithm.

Returns:

Optimized parameters ‘s’ and ‘t’ for Platt scaling, and the history of the loss function.

Return type:

tuple[torch.Tensor, torch.Tensor,list[float]]

komet.load_datas(df)[source]

Loads interaction data including indices of molecule and protein pairs along with their labels.

Parameters:

df – Dataframe containing the interaction data.

Returns:

Tensors of molecule indices, protein indices, and interaction labels.

Return type:

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

komet.load_df(name, dataset_dir='data')[source]

Loads a dataframe from a CSV file, cleans up SMILES strings that cannot be read by RDKit, and returns the cleaned dataframe.

Parameters:
  • name (str) – The name of the file (with extension) to be loaded. If the file is a zip archive, it will be extracted first.

  • dataset_dir (str, optional) – The directory where the dataset is located, by default “data”.

Returns:

The cleaned dataframe with SMILES strings that RDKit can read.

Return type:

pd.DataFrame

komet.make_CV_train_test(load_data, S, save_data, nb_folds=5)[source]

Loads the interaction data from a CSV file, preprocesses the data to generate numerical indices for unique smiles (molecules) and fasta (proteins), and splits the data into cross-validation training and testing datasets based on the specified split type.

Parameters:
  • load_data (str) – Path to the input CSV file containing the interaction data : LCIdb_v2.csv (or after process_LCIdb with other thresholds) or Chembl.csv

  • S (str) – Specifies the split type, which determines how the training and testing datasets are created. Options are “full” (S1), “unseen_drug” (S2), “unseen_target” (S3), and “Orphan” (S4).

  • save_data (str) – Path to the directory where the output train and test sets will be saved.

  • nb_folds (int, optional) – Number of folds for cross-validation, defaults to 5.

Returns:

A tuple containing lists of DataFrames representing the training and testing datasets for each fold.

Note: The function preprocesses the input data to create unique numerical indices for each unique molecule and protein, which are then used to create a pivot table representing the interaction matrix. The data is split into training and testing sets using the appropriate split function based on the specified split type (full, unseen_drug, unseen_target, Orphan)

komet.make_CV_train_test_Orphan(df, nb_folds, path_mkdir)[source]

Splits the input DataFrame into cross-validation training and testing datasets, ensuring that the proteins and molecules in the test set are not present in the train set. The function performs K-fold cross-validation with the specified number of folds.

Parameters:
  • df (pandas.DataFrame) – Input data containing the columns ‘indfasta’, ‘indsmiles’, and ‘score’.

  • nb_folds (int) – Number of folds for cross-validation.

  • path_mkdir (str) – Path to the directory where the output train and test sets will be saved as pickle files.

Returns:

A tuple containing lists of DataFrames representing the training and testing datasets for each fold.

Return type:

(list[pandas.DataFrame], list[pandas.DataFrame])

Note: The function first converts the DataFrame into a numpy matrix where rows correspond to ‘indfasta’ (protein indices), columns to ‘indsmiles’ (drug indices), and cell values to ‘score’ (interaction score). Positive (interaction score = 1) and negative (interaction score = 0) interactions are identified and distributed into the folds. NaN values in ‘score’ are treated as unknown interactions and handled separately. The folds are created such that the proteins and molecules in the test set are not present in the train set.

komet.make_CV_train_test_full(df, nb_folds, path_mkdir)[source]

Splits the input DataFrame into cross-validation training and testing datasets, ensuring the same proportion of positive (interaction score = 1) and negative (interaction score = 0) interactions in each set. The function performs K-fold cross-validation with the specified number of folds.

Parameters:
  • df (pandas.DataFrame) – Input data containing the columns ‘indfasta’, ‘indsmiles’, and ‘score’.

  • nb_folds (int) – Number of folds for cross-validation.

  • path_mkdir (str) – Path to the directory where the output train and test sets will be saved as pickle files.

Returns:

A tuple containing lists of DataFrames representing the training and testing datasets for each fold.

Return type:

(list[pandas.DataFrame], list[pandas.DataFrame])

Note: The function first converts the DataFrame into a numpy matrix where rows correspond to ‘indfasta’ (protein indices), columns to ‘indsmiles’ (drug indices), and cell values to ‘score’ (interaction score). Positive (interaction score = 1) and negative (interaction score = 0) interactions are identified and distributed into the folds. NaN values in ‘score’ are treated as unknown interactions and handled separately. The function then constructs balanced training and testing datasets for each fold, ensuring that the same proportion of positive and negative interactions is maintained in both sets.

komet.make_CV_train_test_unseen_drug(df, nb_folds, path_mkdir)[source]

Splits the input DataFrame into cross-validation training and testing datasets, ensuring that the molecules in the test set are not present in the train set. The function performs K-fold cross-validation with the specified number of folds.

Parameters:
  • df (pandas.DataFrame) – Input data containing the columns ‘indfasta’, ‘indsmiles’, and ‘score’.

  • nb_folds (int) – Number of folds for cross-validation.

  • path_mkdir (str) – Path to the directory where the output train and test sets will be saved as pickle files.

Returns:

A tuple containing lists of DataFrames representing the training and testing datasets for each fold.

Return type:

(list[pandas.DataFrame], list[pandas.DataFrame])

Note: The function first converts the DataFrame into a numpy matrix where rows correspond to ‘indfasta’ (protein indices), columns to ‘indsmiles’ (drug indices), and cell values to ‘score’ (interaction score). Positive (interaction score = 1) and negative (interaction score = 0) interactions are identified and distributed into the folds. NaN values in ‘score’ are treated as unknown interactions and handled separately. The folds are created such that the molecules in the test set are not present in the train set.

komet.make_CV_train_test_unseen_target(df, nb_folds, path_mkdir)[source]

Splits the input DataFrame into cross-validation training and testing datasets, ensuring that the proteins in the test set are not present in the train set. The function performs K-fold cross-validation with the specified number of folds.

Parameters:
  • df (pandas.DataFrame) – Input data containing the columns ‘indfasta’, ‘indsmiles’, and ‘score’.

  • nb_folds (int) – Number of folds for cross-validation.

  • path_mkdir (str) – Path to the directory where the output train and test sets will be saved as pickle files.

Returns:

A tuple containing lists of DataFrames representing the training and testing datasets for each fold.

Return type:

(list[pandas.DataFrame], list[pandas.DataFrame])

Note: The function first converts the DataFrame into a numpy matrix where rows correspond to ‘indfasta’ (protein indices), columns to ‘indsmiles’ (drug indices), and cell values to ‘score’ (interaction score). Positive (interaction score = 1) and negative (interaction score = 0) interactions are identified and distributed into the folds. NaN values in ‘score’ are treated as unknown interactions and handled separately. The folds are created such that the proteins in the test set are not present in the train set. The function returns the training and testing datasets for each fold as lists of DataFrames.

komet.make_train_test_val_S1(df, train_ratio=0.8, test_ratio=0.12)[source]

Splits the input DataFrame into training, testing, and validation datasets. The function first converts the DataFrame into a numpy matrix where rows correspond to ‘indfasta’ (protein indices), columns to ‘indsmiles’ (drug indices), and cell values to ‘score’ (interaction score). It then identifies positive (interaction score = 1) and negative (interaction score = 0) interactions and distributes them into training, testing, and validation sets according to the specified ratios. The split is done ensuring that each set contains a balanced proportion of positive and unknown interactions.

Parameters:
  • df (pandas.DataFrame) – Input data containing the columns ‘indfasta’, ‘indsmiles’, and ‘score’.

  • train_ratio (float, optional) – The proportion of the dataset to be used for the training set, defaults to 0.8.

  • test_ratio (float, optional) – The proportion of the dataset to be used for the testing set, defaults to 0.12.

Returns:

A tuple of numpy arrays representing the training, testing, and validation datasets respectively.

Return type:

(numpy.ndarray, numpy.ndarray, numpy.ndarray)

Note: The remaining portion of the dataset not allocated to training or testing is used for validation. This function assumes that the DataFrame’s ‘score’ column contains binary values (1 for interaction and 0 for no interaction). NaN values in ‘score’ are treated as unknown interactions and are handled separately.

komet.make_train_test_val_S2(df)[source]

Splits the interaction data into distinct training, testing, and validation datasets, ensuring molecules in the test set are not in the training set and molecules in the validation set are not in the training set. It processes input data to form interaction matrices and categorizes these interactions into positive, negative, and unknown based on their presence, absence, or uncertainty in the dataset.

Parameters:

df (pandas.DataFrame) – The input data containing the columns ‘indfasta’, ‘indsmiles’, and ‘score’, representing interaction data between proteins and molecules.

Returns:

A tuple of numpy.ndarrays representing the training, testing, and validation datasets. Each set contains arrays of interactions labeled with indices and interaction scores, with drugs and proteins split according to specified criteria to ensure separation between training, testing, and validation sets.

Return type:

tuple

The function performs the following steps: - Convert the DataFrame to a numpy array of interaction scores. - Determine the unique proteins and drugs, assigning them to train, test, and validation groups based on specified proportions. - For each group, create datasets of positive and negative interactions, handling missing values as unknown interactions. - Ensure there is no overlap of molecules between the training set and either the testing or validation sets. - Return the prepared datasets for further processing or model training.

komet.make_train_test_val_S3(df)[source]

Splits the interaction matrix into training, testing, and validation sets, ensuring there is no overlap between the proteins in the training set and those in the testing or validation sets. The split is based on the initial distribution of proteins and molecules in the interaction matrix.

Parameters:

df (pandas.DataFrame) – The input DataFrame containing the columns ‘indfasta’ for proteins, ‘indsmiles’ for molecules, and ‘score’ for their interaction scores.

Returns:

A tuple containing the training, testing, and validation sets, each as a numpy.ndarray with the first column representing the index of proteins, the second column the index of molecules, and the third column the interaction scores (1 for interaction, 0 for no interaction, and np.nan for unknown interactions).

Return type:

tuple

The function performs the following operations: - Converts the DataFrame to a numpy array representing the interaction scores. - Randomly shuffles and splits proteins and molecules into distinct groups for training, testing, and validation based on predefined ratios. - Creates interaction datasets for each set, ensuring proteins in the test set are not in the train set, and proteins in the val set are not in the train set, thereby preventing data leakage. - Handles missing values and ensures the final datasets are balanced in terms of positive and negative interactions.

komet.make_train_test_val_S4(df)[source]

Splits the interaction matrix into balanced training, testing, and validation sets ensuring there is no overlap between the proteins and molecules in the training set compared to those in the testing and validation sets. This aims to create distinct and balanced sets for robust model evaluation.

Parameters:

df (pandas.DataFrame) – Input DataFrame containing interaction data with ‘indfasta’ for proteins, ‘indsmiles’ for molecules, and ‘score’ for interaction scores.

Returns:

A tuple containing the training, testing, and validation datasets. Each dataset consists of a numpy array with three columns: protein indices, molecule indices, and interaction scores. The interaction scores are 1 for positive interactions, 0 for negative interactions, and np.nan for unknown interactions.

Return type:

tuple

The function executes the following major steps: - Converts the DataFrame to a numpy array to represent the interaction scores. - Splits proteins and molecules into distinct groups for training, testing, and validation based on predefined criteria, ensuring there’s no overlap between the sets for proteins and molecules. - Constructs interaction datasets for each set by maintaining a balance between positive and negative interactions and properly handling unknown interactions. - Ensures the testing and validation sets are balanced and do not contain any proteins or molecules present in the training set, thus avoiding data leakage and ensuring the model’s generalizability.

komet.make_trains_full(load_data, save_data, nb_trains=5)[source]

Loads the interaction data from a CSV file, preprocesses the data to generate numerical indices for unique smiles (molecules) and fasta (proteins), and make nb_train training datasets on full data by chosing different negative interactions each time.

Parameters:
  • load_data (str) – Path to the input CSV file containing the interaction data : LCIdb_v2.csv (or after process_LCIdb with other thresholds) or Chembl.csv

  • save_data (str) – Path to the directory where the output train sets will be saved.

  • nb_trains (int, optional) – Number of different trains, defaults to 5.

Returns:

A tuple containing lists of DataFrames representing the training datasets for each fold.

Note: The function preprocesses the input data to create unique numerical indices for each unique molecule and protein, which are then used to create a pivot table representing the interaction matrix. Choose different negative interactions each time.

komet.predict_drug_profile(train, smiles_drug, mM=3000, dM=1000, lamb=1e-06)[source]

Predicts the interaction profile of a given drug (specified by its SMILES representation) against a set of proteins by training Komet 5 times. Each time, Komet uses different training datasets (different choices of negatives) and different molecular landmarks.

Parameters:
  • train (list[pandas.DataFrame]) – List of training datasets, each represented as a DataFrame.

  • smiles_drug (str) – SMILES representation of the drug to predict interactions for.

  • mM (int, optional) – Number of molecules to use for the Nystrom approximation, defaults to 3000.

  • dM (int, optional) – Final dimension of features for molecules, defaults to 1000.

  • lamb (float, optional) – Regularization parameter for the SVM model, defaults to 1e-6.

Returns:

A DataFrame with the predicted interaction profile for each protein.

Return type:

pandas.DataFrame

Note: This function expects precomputed protein kernels and dictionaries mapping protein sequences to indices. It uses Morgan fingerprints for molecule features and applies Nystrom approximation to the molecule kernel. The protein features are computed using SVD. The function trains an SVM model for each training dataset and predicts the interaction probabilities for the specified drug.

komet.predict_protein_profile(train, fasta_protein, mM=3000, dM=1000, lamb=1e-06)[source]

Predicts the interaction profile of a given protein (specified by its FASTA sequence) against a set of drugs using the KOMET model. The function trains Komet 5 times on different training datasets and different molecular landmarks to predict the interaction profile for the specified protein.

Parameters:
  • train (list[pandas.DataFrame]) – List of training datasets, each represented as a DataFrame.

  • fasta_protein (str) – FASTA sequence of the protein to predict interactions for.

  • mM (int, optional) – Number of molecules to use for the Nystrom approximation, defaults to 3000.

  • dM (int, optional) – Final dimension of features for molecules, defaults to 1000.

  • lamb (float, optional) – Regularization parameter for the SVM model, defaults to 1e-6.

Returns:

A DataFrame with the predicted interaction profile for each drug.

Return type:

pandas.DataFrame

Note: This function expects precomputed protein kernels and dictionaries mapping protein sequences to indices. It uses Morgan fingerprints for molecule features and applies Nystrom approximation to the molecule kernel. The protein features are computed using SVD. The function trains an SVM model for each training dataset and predicts the interaction probabilities for the specified protein.

komet.process_LCIdb(name_file, data_dir='./', max_length_fasta=1000, bioactivity_choice='checkand1database', min_weight=100, max_weight=900, interaction_plus=1e-07, interaction_minus=0.0001)[source]

Processes data from a given ligand-chemical interaction database file and performs various data cleaning and transformation steps.

Parameters:
  • name_file (str) – The name of the file containing the database information.

  • data_dir (str, optional) – The directory where the database file and the file prot_uniprot_fasta_all.csv are located, defaults to “./”.

  • max_length_fasta (int, optional) – The maximum length of FASTA sequences to be included, defaults to 1000.

  • bioactivity_choice (str, optional) – The type of bioactivity data to include, defaults to ‘checkand1database’.

  • min_weight (int, optional) – The minimum molecular weight of the ligands to be included, defaults to 100.

  • max_weight (int, optional) – The maximum molecular weight of the ligands to be included, defaults to 900.

  • interaction_plus (float, optional) – The threshold for positive interaction, defaults to 1e-7.

  • interaction_minus (float, optional) – The threshold for negative interaction, defaults to 1e-4.

Returns:

A pandas DataFrame containing the processed and filtered data.

Return type:

pandas.DataFrame

The function performs the following steps: - Reads the database and filters based on activity type (pIC50, pKd, pKi) and units (negative logarithm). - Merges with protein FASTA sequence data and filters based on sequence length and molecular weight. - Processes bioactivity data based on the selected type and calculates mean, min, and max bioactivity values. - Aggregates data based on SMILES representation and FASTA sequence, and calculates interaction scores. - Saves the processed data to a new CSV file.

komet.results(y, y_pred, proba_pred)[source]

Computes and returns various performance metrics of the model including accuracy, AUC, average precision, optimal threshold for classification, accuracy at the best threshold, confusion matrix, and false positive rate.

Parameters:
  • y – True labels.

  • y_pred – Predicted labels before applying Platt scaling.

  • proba_pred – Predicted probabilities after applying Platt scaling.

Returns:

A tuple containing accuracy before Platt scaling, AUC, average precision, optimal threshold, accuracy at optimal threshold, confusion matrix, and false positive rate at the optimal threshold.

Return type:

tuple[float, float, float, float, float, np.ndarray, float]