A Cox survival analysis framework built on Geneformer for cancer prognosis prediction using gene expression data
FTPM fine-tunes Geneformer to predict cancer survival from gene-expression tokens.
The project provides:
- Geneformer Cox models for single-cancer and pan-cancer analysis;
- 2048-token and 4096-token model variants;
- five-fold out-of-fold (OOF) risk prediction;
- forest plots, Kaplan-Meier plots, and C-index summaries combining FTPM risk, clinical variables, and tumor mutational burden (TMB);
- an MCP server for running the workflows with an AI assistant.
| Script | Scope | Tokens | Pooling |
|---|---|---|---|
FTPM_2048_brca.py |
BRCA | 2048 | Mean |
FTPM_4096_brca.py |
BRCA | 4096 | Attention |
FTPM_2048_pan-caner.py |
Pan-cancer | 2048 | Mean |
FTPM_4096_pan-caner.py |
Pan-cancer | 4096 | Attention |
FTPM_cancer_4096_brca.py |
BRCA with early stopping | 4096 | Attention |
FTPM_cancer_4096_pan-caner.py |
Pan-cancer with early stopping | 4096 | Attention |
Pan-cancer training supports:
BRCA KIRC LUSC LUAD OV GBM LAML PRAD THCA HNSC
COAD LGG STAD LIHC SKCM BLCA CESC SARC ESCA PAAD
| Script | Cox models per cancer | Output |
|---|---|---|
forest_CI_p_FTPM_TMB_clinic.py |
3 | 48 forest plots, 16 KM plots, 3 CSV tables |
forest_KM_ROC_CI_p_FTPM_TMB_clinic.py |
4 | 64 forest plots, 16 KM plots, 3 CSV tables |
Both scripts analyze 16 cancers:
BRCA KIRC LUSC LUAD OV GBM LAML PRAD
THCA HNSC COAD LGG STAD LIHC SKCM BLCA
The three common Cox models are:
baseline + TMB + FTPM
TMB + FTPM
FTPM only
The four-model script additionally fits:
baseline + FTPM, without TMB
FTPM is the categorical High/Low group derived from the OOF risk score. The
historical second filename contains ROC, but that script does not calculate
ROC curves or AUC.
Training requires a CUDA GPU. The 2048-token models typically require at least 40 GB VRAM, while the 4096-token models typically require at least 80 GB.
pip install torch transformers datasets scikit-learn scikit-survival geneformer
pip install numpy pandas scipy lifelines matplotlibGene-expression token files have no header. The training scripts transpose the loaded matrix, so input columns correspond to samples and rows correspond to token positions. Clinical files contain the event indicator, sample ID, and survival time in days.
Pan-cancer data are arranged as:
data_root/
├── TCGA-BRCA/
│ ├── BRCA_expr_token_4096_without_colnames.txt
│ └── BRCA_sur_clinic_lnc_gene_without_colnames.txt
├── TCGA-LUAD/
│ └── ...
└── ...
risk/TCGA-BRCA/OOF_risk.txt
risk/TCGA-BRCA/OOF_sample_ids.txt
tmb/TCGA-BRCA.tmb.csv
Requirements:
OOF_risk.txt: one numeric risk score per row;OOF_sample_ids.txt: one matching sample ID per row, in the same order;- clinical/TMB CSV columns:
Tumor_Sample_Barcode,time,event,age_group,gender, andTMB; timeis expected in months by the plotting scripts.
Run commands from the repository root.
python scripts/FTPM_4096_brca.py \
--expr_file /path/to/BRCA_expr_token_4096_without_colnames.txt \
--clinic_file /path/to/BRCA_sur_clinic.txt \
--pretrained_model_dir /path/to/geneformer \
--out results/brcapython scripts/FTPM_cancer_4096_pan-caner.py \
--data_root /path/to/data_root \
--pretrained_model_dir /path/to/geneformer \
--out results/pan_cancer \
--cancer_types BRCA,LUAD,KIRCpython scripts/forest_CI_p_FTPM_TMB_clinic.py \
--score-template 'risk/TCGA-{cancer}/OOF_risk.txt' \
--sid-template 'risk/TCGA-{cancer}/OOF_sample_ids.txt' \
--meta-template 'tmb/{cancer}.tmb.csv' \
--fig-dir 'results/forest_ci/forest' \
--km-dir 'results/forest_ci/km' \
--tbl-dir 'results/forest_ci/tables'python scripts/forest_KM_ROC_CI_p_FTPM_TMB_clinic.py \
--score-template 'risk/TCGA-{cancer}/OOF_risk.txt' \
--sid-template 'risk/TCGA-{cancer}/OOF_sample_ids.txt' \
--meta-template 'tmb/{cancer}.tmb.csv' \
--fig-dir 'results/forest_km/forest' \
--km-dir 'results/forest_km/km' \
--tbl-dir 'results/forest_km/tables'Use different output directories because the two analysis scripts share some output filenames.
The CSV tables are:
table1.CI_p_FTPM.csv
table1.CI_p_TMB_FTPM.csv
table1.CI_p_baseline_TMB_FTPM.csv
Install and register the MCP server:
cd ftpm_vibe
pip install -e .
claude mcp add ftmp -- python -m ftmp_mcpMain MCP tools:
| Tool | Purpose |
|---|---|
check_ftmp_installation |
Check Python, GPU, dependencies, and scripts |
run_ftmp_cox_2048_brca |
Train the 2048-token BRCA model |
run_ftmp_cox_4096_brca |
Train the 4096-token BRCA model |
run_ftmp_cox_2048_pan_cancer |
Train 2048-token pan-cancer models |
run_ftmp_cox_4096_pan_cancer |
Train 4096-token pan-cancer models |
run_ftmp_cox_cancer_4096_brca |
Train BRCA with early stopping |
run_ftmp_cox_cancer_4096_pan_cancer |
Train pan-cancer models with early stopping |
run_ftmp_forest_ci_analysis |
Run the three-model downstream analysis |
run_ftmp_forest_km_clinic_analysis |
Run the four-model downstream analysis |
search_ftmp_docs |
Search this README |
Existing ftmp_* package and tool names are retained for backward
compatibility.
Always report OOF metrics as model performance. Metrics calculated from a final model evaluated on its own training data are apparent performance and may be optimistic.
If you use FTPM, please cite the associated publication when available.
MIT