CPSM is an R package that provides a comprehensive computational pipeline for predicting survival probabilities and risk groups in cancer patients. It includes dedicated modules to perform key steps such as data preprocessing, training/test splitting, and normalization. CPSM enables feature selection through univariate cox-regression survival analysis, feature selection though LASSO method, and calculates a LASSO-based Prognostic Index (PI) score. It supports the development of predictive models using different feature sets and offers a suite of visualization tools, including survival curves based on predicted probabilities, barplots of predicted mean and median survival times, Kaplan-Meier (KM) plots overlaid with individual survival predictions, and nomograms for estimating 1-, 3-, 5-, and 10-year survival probabilities. Together, these functionalities make CPSM a powerful and versatile tool for survival analysis in cancer research.
Installation From BioconductorTo install this package, start R (version "4.4") and enter the code provided:
if (!requireNamespace("BiocManager", quietly = TRUE)) {
install.packages("BiocManager")
}
BiocManager::install("CPSM")
The example input data object, Example_TCGA_LGG_FPKM_data
, contains data for 184 LGG cancer samples as rows and various features as columns. Gene expression data is represented in FPKM values. The dataset includes 11 clinical and demographic features, 4 types of survival data (with both time and event information), and 19,978 protein-coding genes. The clinical and demographic features in the dataset include Age
, subtype
, gender
, race
, ajcc_pathologic_tumor_stage
, histological_type
, histological_grade
, treatment_outcome_first_course
, radiation_treatment_adjuvant
, sample_type
, and type
. The four types of survival data included are Overall Survival (OS), Progression-Free Survival (PFS), Disease-Specific Survival (DSS), and Disease-Free Survival (DFS). In the dataset, the columns labeled OS, PFS, DSS, and DFS represent event occurrences, while the columns OS.time, PFS.time, DSS.time, and DFS.time provide survival times (in days).
library(SummarizedExperiment)
set.seed(7) # set seed
data(Example_TCGA_LGG_FPKM_data, package = "CPSM")
Example_TCGA_LGG_FPKM_data
The data_process_f function converts OS time (in days) into months and removes samples where OS/OS.time information is missing.
To use this function, the input data should be provided in TSV format. Additionally, you need to define col_num
(the column number at which clinical, demographic, and survival information ends, e.g., 20), surv_time
(the name of the column that contains survival time information, e.g., OS.time
), and output
(the desired name for the output, e.g., "New_data").
data(Example_TCGA_LGG_FPKM_data, package = "CPSM")
combined_df <- cbind(
as.data.frame(colData(Example_TCGA_LGG_FPKM_data))
[, -ncol(colData(Example_TCGA_LGG_FPKM_data))],
t(as.data.frame(assay(
Example_TCGA_LGG_FPKM_data,
"expression"
)))
)
New_data <- data_process_f(combined_df, col_num = 20, surv_time = "OS.time")
str(New_data[1:10])
After data processing, the output object New_data
is generated, which contains 176 samples. This indicates that the function has removed 8 samples where OS/OS.time information was missing. Moreover, a new 21st column, OS_month
, is added to the data, containing OS time values in months.
Before proceeding further, we need to split our data into training and test subset for the purpose of feature selection and model development. Here, we need output from the previous step as an input ( which was “New_data”). Next we need to define the fraction (e.g. 0.9) by which we want to split data into training and test. Thus, fraction=0.9 will split data into 90% training and 10% as test set. Besides, we also need to provide training and set output names (e.g. train_FPKM,test_FPKM )
data(New_data, package = "CPSM")
# Call the function
result <- tr_test_f(data = New_data, fraction = 0.9)
# Access the train and test data
train_FPKM <- result$train_data
str(train_FPKM[1:10])
test_FPKM <- result$test_data
str(test_FPKM[1:10])
After the train-test split, two new output objects are generated: train_FPKM
and test_FPKM
. The train_FPKM
object contains 158 samples, while test_FPKM
contains 18 samples. This indicates that the tr_test_f
function splits the data in a 90:10 ratio.
In order to select features and develop ML models, the data must be normalized. Since the expression data is available in terms of FPKM values, the train_test_normalization_f
function will first convert the FPKM values into a log scale using the formula [log2(FPKM+1)], followed by quantile normalization. The training data will be used as the target matrix for the quantile normalization process.
For this function, you need to provide the training and test datasets obtained from the previous step (Train/Test Split). Additionally, you must specify the column number where clinical information ends (e.g., 21) in the input datasets. Finally, you need to define output names for the resulting datasets: train_clin_data
(which contains only clinical information from the training data), test_clin_data
(which contains only clinical information from the test data), train_Normalized_data_clin_data
(which contains both clinical information and normalized gene expression values for the training samples), and test_Normalized_data_clin_data
(which contains both clinical information and normalized gene expression values for the test samples).
# Step 3 - Data Normalization
# Normalize the training and test data sets
data(train_FPKM, package = "CPSM")
data(test_FPKM, package = "CPSM")
Result_N_data <- train_test_normalization_f(
train_data = train_FPKM,
test_data = test_FPKM,
col_num = 21
)
# Access the Normalized train and test data
Train_Clin <- Result_N_data$Train_Clin
Test_Clin <- Result_N_data$Test_Clin
Train_Norm_data <- Result_N_data$Train_Norm_data
Test_Norm_data <- Result_N_data$Test_Norm_data
str(Train_Clin[1:10])
str(Train_Norm_data[1:10])
After running the function, four outputs objects are generated: Train_Clin
(which contains only clinical features from the training data), Test_Clin
(which contains only clinical features from the test data), Train_Norm_data
(which includes clinical features and normalized gene expression values for the training samples), and Test_Norm_data
(which includes clinical features and normalized gene expression values for the test samples).
To create a survival model, the next step is to calculate the Prognostic Index (PI) score. The PI score is based on the expression levels of features selected by the LASSO regression model and their corresponding beta coefficients. For example, suppose five features (G1, G2, G3, G4, G5) are selected by the LASSO method, and their associated coefficients are B1, B2, B3, B4, and B5, respectively. The PI score is then computed using the following formula:
PI score = G1 * B1 + G2 * B2 + G3 * B3 + G4 * B4 + G5 * B5
For this function, you need to provide the normalized training data object (Train_Norm_data) and test data object (Test_Norm_data) obtained from the previous step (train_test_normalization_f). Additionally, you must specify the column number (col_num
) where clinical features end (e.g., 21), the number of folds (nfolds
) for the LASSO regression method (e.g., 5), and the survival time (surv_time
) and survival event (surv_event
) columns in the data (e.g., OS_month
and OS
, respectively). The LASSO regression is implemented using the glmnet
package. Finally, you need to define names of output object to store the results, which will include the selected LASSO features and their corresponding PI values.
# Step 4 - Lasso PI Score
data(Train_Norm_data, package = "CPSM")
data(Test_Norm_data, package = "CPSM")
Result_PI <- Lasso_PI_scores_f(
train_data = Train_Norm_data,
test_data = Test_Norm_data,
nfolds = 5,
col_num = 21,
surv_time = "OS_month",
surv_event = "OS"
)
Train_Lasso_key_variables <- Result_PI$Train_Lasso_key_variables
Train_PI_data <- Result_PI$Train_PI_data
Test_PI_data <- Result_PI$Test_PI_data
str(Train_PI_data[1:10])
str(Test_PI_data[1:10])
plot(Result_PI$cvfit)
The Lasso_PI_scores_f
function generates the following outputs objects:
Train_Lasso_key_variables
: A list of features selected by LASSO along with their beta coefficient values.Train_Cox_Lasso_Regression_lambda_plot
: The Lasso regression lambda plot.Train_PI_data
: This dataset contains the expression values of genes selected by LASSO along with the PI score in the last column for the training samples.Test_PI_data
: This dataset contains the expression values of genes selected by LASSO along with the PI score in the last column for the test samples.In addition to the Prognostic Index (PI) score, the Univariate_sig_features_f
function in the CPSM package allows for the selection of significant features based on univariate cox-regression survival analysis. This function identifies features with a p-value less than 0.05, which are able to stratify high-risk and low-risk survival groups. The stratification is done by using the median expression value of each feature as a cutoff.
To use this function, you need to provide the normalized training (Train_Norm_data) and test (Test_Norm_data) dataset objects, which were obtained from the previous step (train_test_normalization_f). Additionally, you must specify the column number (col_num
) where the clinical features end (e.g., 21), as well as the names of the columns containing survival time (surv_time
, e.g., OS_month
) and survival event information (surv_event
, e.g., OS
). Furthermore, you need to define output names for the resulting datasets that will contain the expression values of the selected genes. These outputs will be used to store the significant genes identified through univariate survival analysis.
# Step 4b - Univariate Survival Significant Feature Selection.
data(Train_Norm_data, package = "CPSM")
data(Test_Norm_data, package = "CPSM")
Result_Uni <- Univariate_sig_features_f(
train_data = Train_Norm_data,
test_data = Test_Norm_data,
col_num = 21,
surv_time = "OS_month",
surv_event = "OS"
)
Univariate_Suv_Sig_G_L <- Result_Uni$Univariate_Survival_Significant_genes_List
Train_Uni_sig_data <- Result_Uni$Train_Uni_sig_data
Test_Uni_sig_data <- Result_Uni$Test_Uni_sig_data
Uni_Sur_Sig_clin_List <- Result_Uni$Univariate_Survival_Significant_clin_List
Train_Uni_sig_clin_data <- Result_Uni$Train_Uni_sig_clin_data
Test_Uni_sig_clin_data <- Result_Uni$Test_Uni_sig_clin_data
str(Univariate_Suv_Sig_G_L[1:10])
The Univariate_sig_features_f
function generates the following output objects:
Univariate_Surv_Sig_G_L
: A table of univariate significant genes, along with their corresponding coefficient values, hazard ratio (HR) values, p-values, and C-Index values.Train_Uni_sig_data
: This dataset contains the expression values of the significant genes selected by univariate survival analysis for the training samples.Test_Uni_sig_data
: This dataset contains the expression values of the significant genes selected by univariate survival analysis for the test samples.After selecting significant features using LASSO or univariate survival analysis, the next step is to develop a machine learning (ML) prediction model to estimate the survival probability of patients. The MTLR_pred_model_f
function in the CPSM package provides several options for building prediction models based on different feature sets. These options include:
For this analysis, we are interested in developing a model based on the PI score (i.e., Model_type = 2).
To use this function, the following inputs are required:
Clin_Feature_List
(e.g., Key_PI_list), a list of features to be used for building the modelsurv_time
: The name of the column containing survival time in months (e.g., OS_month
)surv_event
: The name of the column containing survival event information (e.g., OS
)These inputs will allow the MTLR_pred_model_f
function to generate a prediction model for the survival probability of patients based on the provided data.
data(Train_Clin, package = "CPSM")
data(Test_Clin, package = "CPSM")
data(Key_Clin_feature_list, package = "CPSM")
Result_Model_Type1 <- MTLR_pred_model_f(
train_clin_data = Train_Clin,
test_clin_data = Test_Clin,
Model_type = 1,
train_features_data = Train_Clin,
test_features_data = Test_Clin,
Clin_Feature_List = Key_Clin_feature_list,
surv_time = "OS_month",
surv_event = "OS"
)
survCurves_data <- Result_Model_Type1$survCurves_data
mean_median_survival_tim_d <- Result_Model_Type1$mean_median_survival_time_data
survival_result_bas_on_MTLR <- Result_Model_Type1$survival_result_based_on_MTLR
Error_mat_for_Model <- Result_Model_Type1$Error_mat_for_Model
data(Train_Clin, package = "CPSM")
data(Test_Clin, package = "CPSM")
data(Train_PI_data, package = "CPSM")
data(Test_PI_data, package = "CPSM")
data(Key_PI_list, package = "CPSM")
Result_Model_Type2 <- MTLR_pred_model_f(
train_clin_data = Train_Clin,
test_clin_data = Test_Clin,
Model_type = 2,
train_features_data = Train_PI_data,
test_features_data = Test_PI_data,
Clin_Feature_List = Key_PI_list,
surv_time = "OS_month",
surv_event = "OS"
)
survCurves_data <- Result_Model_Type2$survCurves_data
mean_median_surviv_tim_da <- Result_Model_Type2$mean_median_survival_time_data
survival_result_b_on_MTLR <- Result_Model_Type2$survival_result_based_on_MTLR
Error_mat_for_Model <- Result_Model_Type2$Error_mat_for_Model
Model for Clinical features + PI
data(Train_Clin, package = "CPSM")
data(Test_Clin, package = "CPSM")
data(Train_PI_data, package = "CPSM")
data(Test_PI_data, package = "CPSM")
data(Key_Clin_features_with_PI_list, package = "CPSM")
Result_Model_Type3 <- MTLR_pred_model_f(
train_clin_data = Train_Clin,
test_clin_data = Test_Clin,
Model_type = 3,
train_features_data = Train_PI_data,
test_features_data = Test_PI_data,
Clin_Feature_List = Key_Clin_features_with_PI_list,
surv_time = "OS_month",
surv_event = "OS"
)
survCurves_data <- Result_Model_Type3$survCurves_data
mean_median_surv_tim_da <- Result_Model_Type3$mean_median_survival_time_data
survival_result_b_on_MTLR <- Result_Model_Type3$survival_result_based_on_MTLR
Error_mat_for_Model <- Result_Model_Type3$Error_mat_for_Model
Model for Univariate + Clinical features
data(Train_Clin, package = "CPSM")
data(Test_Clin, package = "CPSM")
data(Train_Uni_sig_data, package = "CPSM")
data(Test_Uni_sig_data, package = "CPSM")
data(Key_univariate_features_with_Clin_list, package = "CPSM")
Result_Model_Type5 <- MTLR_pred_model_f(
train_clin_data = Train_Clin,
test_clin_data = Test_Clin,
Model_type = 4,
train_features_data = Train_Uni_sig_data,
test_features_data = Test_Uni_sig_data,
Clin_Feature_List = Key_univariate_features_with_Clin_list,
surv_time = "OS_month",
surv_event = "OS"
)
survCurves_data <- Result_Model_Type5$survCurves_data
mean_median_surv_tim_da <- Result_Model_Type5$mean_median_survival_time_data
survival_result_b_on_MTLR <- Result_Model_Type5$survival_result_based_on_MTLR
Error_mat_for_Model <- Result_Model_Type5$Error_mat_for_Model
After implementing the MTLR_pred_model_f
function, the following outputs are generated:
To visualize the survival of patients, we use the surv_curve_plots_f
function, which generates survival curve plots based on the survCurves_data
obtained from the previous step (after running the MTLR_pred_model_f
function). This function also provides the option to highlight the survival curve of a specific patient.
The function requires two inputs:
TCGA-TQ-A8XE-01
) whose survival curve you want to highlight.# Create Survival curves/plots for individual patients
data(survCurves_data, package = "CPSM")
plots <- surv_curve_plots_f(
Surv_curve_data = survCurves_data,
selected_sample = "TCGA-TQ-A7RQ-01"
)
# Print the plots
print(plots$all_patients_plot)
print(plots$highlighted_patient_plot)
After running the function, two output plots are generated:
To visualize the predicted survival times for patients, we use the mean_median_surv_barplot_f
function, which generates bar plots for the mean and median survival times based on the data obtained from Step 5 after running the MTLR_pred_model_f
function. This function also provides the option to highlight a specific patient on the bar plot.
This function requires two inputs:
TCGA-TQ-A7RQ-01
) whose bar plot should be highlighted.data(mean_median_survival_time_data, package = "CPSM")
plots_2 <- mean_median_surv_barplot_f(
surv_mean_med_data =
mean_median_survival_time_data,
selected_sample = "TCGA-TQ-A7RQ-01"
)
# Print the plots
print(plots_2$mean_med_all_pat)
print(plots_2$highlighted_selected_pat)
<<<<<<< HEAD
After running the function, two output bar plots are generated:
These plots provide a clear comparison of the predicted survival times for all patients and the highlighted individual patient.
Step 8 – Risk-Group Prediction of Test Samples Based on Selected FeaturesTo predict the survival-based risk group of test samples (i.e., high-risk with shorter survival or low-risk with longer survival), we use the predict_survival_risk_group_f()
function provided in the CPSM package. This function implements a randomForestSRC-based prediction approach for survival risk classification. Thhis function first defines actual risk groups in the training data using the median overall survival time:
Multiple Random Survival Forest (RSF) models are then trained using different values for ntree
:
10, 20, 50, 100, 250, 500, 750, 1000.
The model with the best performance (e.g., highest accuracy) is selected automatically. This best-performing model is used to predict the risk group of test samples, along with prediction probabilities.
selected_train_data
: A data frame with normalized expression values for selected features and survival information (OS_month
, OS_event
) for the training set.selected_test_data
: A data frame with normalized expression values for the same features for the test set.Feature_List
: A character vector containing the names of selected features to be used in the model.# Load example data from CPSM package
data(Train_PI_data, package = "CPSM")
data(Test_PI_data, package = "CPSM")
data(Key_PI_list, package = "CPSM")
# Predict survival-based risk groups for test samples
Results_Risk_group_Prediction <- predict_survival_risk_group_f(
selected_train_data = Train_PI_data,
selected_test_data = Test_PI_data,
Feature_List = Key_PI_list
)
#Performance of the best model on Training and Test data
Best_model_Prediction_results<- Results_Risk_group_Prediction$misclassification_results
print(head(Best_model_Prediction_results))
#Prediction results of the best model on Training set
Test_results <- Results_Risk_group_Prediction$Test_results #Prediction resulst on Test data
print(head(Test_results))
The output is a list that includes:
User can use these results for further validation and visualization, such as overlaying test sample survival curves on the training KM plot (see next step).
Step 9 – Visual Overlay of Predicted Test Sample on Kaplan-Meier CurveTo visually evaluate how a specific test sample compares to survival risk groups defined in the training dataset, we use the km_overlay_plot_f()
function. This function overlays the predicted survival curve of a selected test sample onto the Kaplan-Meier (KM) survival plot derived from the training data. This visual comparison helps determine how closely the test sample aligns with population-level survival trends.
It requres requires following inputs
Train_results
:
A data frame containing predicted risk groups, survival times (OS_month
), event status (OS_event
), and additional training data.
Row names must correspond to sample IDs.
Test_results
:
A data frame with predicted risk groups and prediction probabilities for the test dataset.
Row names must correspond to sample IDs.
survcurve_te_data
:
A data frame with predicted survival probabilities over multiple time points for test samples (that we obtained from Step 5).
selected_sample
:
The sample ID (matching a row in Test_results
) for which the test survival curve should be plotted.
# Load example results
data(Train_results, package = "CPSM")
data(Test_results, package = "CPSM")
data(survCurves_data, package = "CPSM")
# Select a test sample to visualize
sample_id <- "TCGA-TQ-A7RQ-01"
# Generate KM overlay plot
KM_plot <- km_overlay_plot_f(
Train_results = Train_results,
Test_results = Test_results,
survcurve_te_data = survCurves_data,
selected_sample = sample_id
)
# Display plot
KM_plot
This visualization is useful for:
The Nomogram_generate_f
function in the CPSM package allows you to generate a nomogram plot based on user-defined clinical and other relevant features in the data. For example, we will generate a nomogram using six features: Age, Gender, Race, Histological Type, Sample Type, and PI score.
To create the nomogram, we need to provide the following inputs:
OS_month
).OS
).data(Train_Data_Nomogram_input, package = "CPSM")
data(feature_list_for_Nomogram, package = "CPSM")
Result_Nomogram <- Nomogram_generate_f(
data = Train_Data_Nomogram_input,
Feature_List = feature_list_for_Nomogram,
surv_time = "OS_month",
surv_event = "OS"
)
C_index_mat <- Result_Nomogram$C_index_mat
After running the function, the output is a nomogram that predicts the risk (e.g., Event risk such as death), as well as the 1-year, 3-year, 5-year, and 10-year survival probabilities for patients based on the selected features.The nomogram provides a visual representation to estimate the patient's survival outcomes over multiple time points, helping clinicians make more informed decisions.
As last part of this document, we call the function "sessionInfo()", which reports the version numbers of R and all the packages used in this session. It is good practice to always keep such a record as it will help to trace down what has happened in case that an R script ceases to work because the functions have been changed in a newer version of a package.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4