This project use DIAMOND tool as benchmark. The pipeline utilizes UniRef90 as the reference database and processes protein sequences to generate alignment results.
Goto dimond.ipynb, follow all the instructions
- You may have to download several python packages to set everything up.
- All code in there is written in
linux. For different OS, you may need to rewrite some command - The output is already generated in
/metricsand/dataset/diamond_results, you may not have to run it again
Some files are too large to be included in this repository, but you can generate them yourself following the steps above:
dataset : storing all data, including training, test and database sequences
diamond_db: The DIAMOND database folder. (create script inexperiments/dimond.ipynb)uniref90.fasta.gz: original UniRef90 data zipuniref90.dmnd: The UniRef90 database for DIAMOND to align
ec40: All the ec40 original data- including
.pkl,.csvand.fastaformats
- including
diamond_results: Storing the original test sequence and intermidiate processed filescdhit04_uniprot_sprot_2016_07.pkl: Cdhit cluster result from UDSMProt- Rest are intermidiate files from running DIAMOND
metrics : Containing methods' statisitical metrics
metrics.csv: DIAMOND benchmark report
experiments: All methods and utils
dimond: DIMOND execution file, can be download indimond.ipynb(for linux)dimond.ipynb: Pipeline for DIAMOND benchmark (All stage included, no need to download any data your self!)evaluate_ec.py: Evaluation util to compare predicted EC numbers and true EC numbers, then generate stats reportfetch_ec_improvedandfetch_ec: Fetch utils to get EC numbers from DIAMOND results. Improved version implemented async stages to run fetch simultaneously.constants: some global constants
abstracts: abstract implementation for Dataloader, FeatureEngineer and Predictor.
Diamond: Diamond version of Feature engineer and Predictor
- See
experiments/pipeline.ipynb
-
PSSM Features:
- for each input sequence:
- Parse the diamond outputs to get all sequence hits for the input which score less than 1x10^-5 (a typically good BLAST hit cut-off).
- Then put all the hit sequences in a temp fasta file.
- Create a temporary blast search database by running the makeblastdb tool over the temp fasta file.
- Then run PSI-BLAST using the input sequence as the input, the db you just created as the search db and set it to run 1 iteration and to output the PSSM (
$m \times 42$ for each sequence). - All instructions are included in
experiments/PSSM/pssm_gen_diamond.ipynb
- for each input sequence:
-
HMM Features
- Download
HmmerandPfam-A.hmmdatabase - Run
Hmmerscan through pfam - Take best (lowest) E-value, best (highest) bit score, total coverage and number of domains as final features
- All instructions are included in
experiments/HMM/HMM_gen.ipynb
- Download
- See
experiments/merge_features.ipynb
-
Data Import:
- Load physiochemical feature results from a CSV file.
- Import the training, validation, and test sets from the EC40 dataset (CSV files).
-
String Parsing and EC Vector Encoding:
- Use the
encode_ec_vectorfunction to process theeccolumn. This function:- Checks if the input is a string and attempts to parse it with
ast.literal_eval. - If parsing yields a list, the first element is taken; otherwise, the string is split using the period
.delimiter. - Each split segment is cleaned: if the segment is '-' or empty, it is converted to -1; otherwise, it attempts to convert the segment to an integer or float.
- Checks if the input is a string and attempts to parse it with
- Use the
-
List Data Conversion:
- Convert string representations of lists in the
AACandDCcolumns to actual Python lists using theconvert_str_to_listfunction.
- Convert string representations of lists in the
-
Column Expansion:
- Apply the
expand_columnfunction to the columns containing list data (ec,AAC, andDC) to expand them into multiple separate columns. Each new column is named in the formatoriginal_column_index. The original columns can be optionally dropped.
- Apply the
-
HMM Feature Merging:
- Load HMM feature CSV files for the training, validation, and test sets from the
../dataset/HMM/directory. - Merge these HMM features with the EC40 dataset using the
merge_hmmfunction, which performs a left join based on theaccessionandquery_namefields, filling any missing values with 0.
- Load HMM feature CSV files for the training, validation, and test sets from the
-
Peptide Features Integration:
- Read peptide sequence features from the
../dataset/peptides/seq_feats.csvfile. - Preprocess the peptide features by:
- Dropping redundant columns.
- Converting the string-encoded
datacolumn into Python objects usingevaland extracting the embedded dictionary. - Expanding the dictionary into separate columns and removing duplicates based on
accession.
- Merge the processed peptide features with the training, validation, and test datasets.
- Read peptide sequence features from the
-
Loading and Matrix Size Assessment:
- Load PSSM features for the training, validation, and test sets from pickle files.
- Use the
get_min_mfunction to determine the range of matrix row counts (representing sequence lengths) for each dataset.
-
Matrix Truncation and Flattening:
- Define the
truncate_and_flattenfunction to convert PSSM matrices into fixed-size representations:- If the matrix has fewer rows than the target (
2000rows was choosing according to the distribution graph), pad it with zeros.
- If it exceeds the target, truncate the matrix to the target number of rows.
- Flatten the resulting fixed-size (2000x42) matrix into a one-dimensional vector.
- Due to computational resource limitation, we deployed PCA to reduce dimension to
10% (8400)of original size.
- If the matrix has fewer rows than the target (
- Apply this function to each PSSM matrix in the training, validation, and test sets, and construct new DataFrames where each column is named
pssm_i(withibeing the vector index). - Merge these new PSSM features back into the corresponding datasets.
- Define the
- Dropping Redundant Columns:
- Remove unnecessary columns from the training, validation, and test datasets (e.g.,
Original,traintest,negative_for,mainclass_set,sprot_version,len,cluster_ID,representative,sequence,accession, andquery_name) to finalize the feature matrix for model input.
- Remove unnecessary columns from the training, validation, and test datasets (e.g.,
- Apply
MinMaxScalerto scale all features
- See
experiments/ablation_study.py
Overview
- Trains a multi-output RandomForest classifier using all available features.
- Groups features based on their type/origin (e.g., Sequential, Physiochemical, HMM, PSSM, Sequence Embedding, QSAR).
- Iteratively removes one feature group at a time, retrains the model, and evaluates performance to see how the removal of each group impacts the model.
Model: Multi-output RandomForestClassifier
- Key hyperparameters:
n_estimators=200max_depth=30min_samples_split=10min_samples_leaf=5max_features='sqrt'
- The script handles multiple EC labels (columns) simultaneously in a multi-output setting.
Feature groups include:
- Sequenctial: Columns starting with
ACC_orDC_. - Physiochemical: Includes columns like
molecular_weight,isoelectric_point,gravy,aromaticity,instability_index,aliphatic_index,boman_index. - HMM: Columns such as
E-value,score,coverage,num_domain. - PSSM: Columns starting with
pssm_. - Sequence Embedding: Columns starting with
embedded_. - QSAR: Default group for columns not falling into the above categories.
The script removes each group in turn, retrains the RandomForest model, and compares the performance to the baseline (all features).
All results are stored under ../metrics/Ablation_Results/:
-
evaluation_reports/- Contains CSV files (e.g.,
eval_report_Remove_X.csv) for each ablation experiment. - A combined file
all_ablation_eval_reports.csvaggregates all experiment results:
- Contains CSV files (e.g.,
-
confusion_matrices/- Subfolders for each experiment (e.g.,
Remove_Sequenctial/), each containing confusion matrix plots.
- Subfolders for each experiment (e.g.,
-
Ablation_Models/(Optional)- If enabled, stores trained model files (
.pkl). By default, saving models is commented out in the script to reduce disk usage.
- If enabled, stores trained model files (
- See
experiments/feature_selection.py
-
Loading Data:
The training, validation, and test datasets are loaded from pickle files. -
Feature-Label Separation:
The dataset columns starting with'ec_'are treated as the labels, while the remaining columns form the feature set. -
Scaling:
Features are scaled using aMinMaxScalerto normalize the data, which helps the subsequent feature selection methods work more effectively.
-
Purpose:
Remove features with very low variance, which are unlikely to be informative for the prediction task. -
Implementation:
AVarianceThresholdwith a threshold of0.001is applied to the combined training and validation data.Threshold # Features Remaining 0.0001 9252 0.0005 9152 0.001 8888 0.005 4437 0.01 319 This step results in a subset of features that have sufficient variation across samples.
-
Purpose:
Evaluate the relevance of each feature with respect to each output (i.e., each column in the multi-label target) and aggregate their importance. -
Implementation:
-
Outcome:
This step yields a reduced set of features that are most informative for the multi-label task.
-
Purpose:
Refine the feature subset further using Recursive Feature Elimination with Cross-Validation (RFECV) to find the optimal number of features. -
Implementation:
- A
RandomForestClassifierserves as the estimator within RFECV. - The RFECV process uses a custom scorer based on the micro-averaged F1 score, which is suitable for multi-label classification.
- The cross-validation is performed using a
PredefinedSplit, which distinguishes between training and validation samples. - The elimination process proceeds in steps (removing 5 features per iteration) until the optimal feature subset is identified.
- A
-
Outcome:
The optimal feature indices are determined and saved (100features was selected), along with the grid scores from the RFECV process, for further use in model training.
- See
experiments/model_selection.py
-
Data Import and Preparation:
- The training, validation, and test datasets are loaded from pickle files.
- Features and labels are separated, where columns starting with
'ec_'are used as labels and all other columns as features. - The feature set is filtered using a previously selected set of indices, and the data is normalized with a
MinMaxScaler.
-
Training-Validation Split:
- The training and validation datasets are concatenated to form a combined training set.
- A
PredefinedSplitis created to designate which samples are for training and which for validation.
- Cascade Structure:
- A cascade model is built for each candidate classifier. In this process, a model is trained for each output label sequentially.
- For each label:
- The model is trained only on samples with valid labels (ignoring any placeholder values such as
-1). - After training, the model predicts the label for the entire dataset.
- These predictions are appended as new features for subsequent cascade steps, thus enriching the feature set with inter-output information.
- The model is trained only on samples with valid labels (ignoring any placeholder values such as
For comparison purpose, we applied a Random Forest to perform multi-class classification and record all metrics
The following candidate models are used in the cascade training process:
- K-Nearest Neighbors (KNN)
- Logistic Regression
- Random Forest
Uses 200 trees and no maximum depth, ensuring robust performance. - CatBoost Classifier
Configured with 200 iterations and set to silent mode for training. - Support Vector Machine (SVM)
Uses probability estimates to better integrate with the cascade structure.
Each candidate model is cloned and trained within the cascade framework to handle the multi-label task.
- Model Saving:
- Once trained, the cascade models for each candidate are saved to disk as pickle files in the designated
Selected_Feature_Modelsdirectory. - This ensures that the models can be later loaded for prediction or further analysis without needing to retrain them.
- Once trained, the cascade models for each candidate are saved to disk as pickle files in the designated
- See
experiments/feature_selection.pyandexperiments/pipeline.ipynb
Before starting the evaluation, the script ensures that all necessary directories exist. This includes:
-
Models Folder:
The folderSelected_Feature_Modelscontains the saved cascade models. -
Results Base Folder:
A main folder (e.g.,../metrics) is created along with subfolders for:- Confusion Matrices: Where the confusion matrix plots for each output and model are saved.
- ROC Curves: Where the ROC curve plots for each output and model are saved.
- Evaluation Reports: Where CSV reports summarizing model performance are stored.
-
Loading ROC Data:
Previously saved ROC comparison data (e.g., fromroc_comparison_data.pkl) is loaded to support cross-model ROC analysis. -
Model File Processing:
The script iterates through all the model pickle files in the models folder. For each model file:- A model-specific subfolder is created in both the confusion matrix and ROC curves directories.
- The saved cascade models for the candidate are loaded from disk.
For each candidate model:
-
Feature Propagation:
The evaluation begins with the test feature set. For every step (i.e., for each output label in the cascade), the model:- Generates probability predictions using
predict_proba.
If the method is not available, it falls back to a default probability matrix. - Makes a label prediction using
predict. - Appends the predictions as new features for the next cascade step.
- Generates probability predictions using
-
Final Prediction Matrix:
After iterating through all cascade steps, the individual predictions are combined into a full prediction matrix (with one column per output label).
-
Evaluation Report Generation:
The custom functionevaluate_ec_predictionsis used to compute various performance metrics, including:- Exact Match Accuracy: The proportion of samples for which all predicted labels match the true labels.
- No EC Number Found / No Prediction Ratio: The fraction of samples where no valid prediction is made.
- Per-Position Metrics: For each output position, accuracy, precision, recall, and F1-score are computed (ignoring invalid labels).
-
Report Saving:
The evaluation report for each model is saved as a CSV file in the evaluation reports folder. An aggregated report is also maintained by concatenating the results from each model.
For every output position in the model’s predictions:
-
Data Filtering:
The true and predicted labels are filtered to exclude invalid labels (indicated by-1). -
Matrix Calculation and Normalization:
A confusion matrix is computed using the filtered labels. The matrix is then normalized by row sums to facilitate comparison across classes. -
Visualization:
The normalized confusion matrix is plotted using a heatmap with dynamic sizing based on the number of unique classes.
The generated plots are saved to the model-specific confusion matrix folder.
For each output position:
-
Probability Processing:
The evaluation extracts the probability estimates for the positive class from the stored probability matrices.
The true labels are binarized and filtered to ensure sufficient valid samples. -
ROC Calculation:
The micro-average ROC is computed (usingroc_curveandauc), and the resulting false positive rates, true positive rates, and AUC scores are calculated. -
Plotting and Comparison:
Individual ROC curves for each output are plotted and saved in the model-specific ROC folder.
Additionally, the ROC data for each model is aggregated in the comparison data structure to facilitate cross-model comparisons.









