library(prozor)
library(dplyr)
library(ggplot2)Target Decoy FDR
Here we describe how you can estimate the false discovery rate (FDR) when matching your spectra against a FASTA sequence database using spectra search engines such as Mascot or Comet. The sequence database contains forward sequences and the same number of reverse sequences.
The data
The data needs to have two columns, one with a label, to distinguish targets and decoys, the other with a score. Then you also need to know if a larger score or a smaller score is better. In our example data, we have two scores: score and neg_log10_score. While the score is better if it is smaller (e.g., posterior error probability), neg_log10_score, which is -log10(score), is better if it is larger.

data(fdr_sample)
knitr::kable(head(fdr_sample))| protein_id | score | neg_log10_score | |
|---|---|---|---|
| 28064 | sp|P00738|HPT_HUMAN | 0.0030141 | 2.520847 |
| 47194 | sp|P19823|ITIH2_HUMAN | 0.0008878 | 3.051662 |
| 8916 | sp|Q86WI1|PKHL1_HUMAN | 0.0350109 | 1.455797 |
| 37157 | sp|P04003|C4BPA_HUMAN | 0.0018141 | 2.741330 |
| 60963 | sp|P01876|IGHA1_HUMAN | 0.0000013 | 5.897889 |
| 89689 | sp|P01042|KNG1_HUMAN | 0.0000000 | 12.305967 |
What is also required is that the number of Targets and Decoys are the same. This is given for mass spectrometry database searches. Our dataset here is already truncated at a 5% FDR. Therefore the number of decoys is much smaller.
table(grepl("^REV_", fdr_sample$protein_id))
FALSE TRUE
39142 858
In our example, we will use the package to filter the data further for a 1% FDR.
Computing the FDR can be done by creating a TargetDecoyFDR object from the scores and the decoy flags. Printing it shows the numbers of hits and decoys and the scores at q-values of 1 % and 5 %. Its plot() method then shows the score distributions of targets and decoys (top) and the q-values of the two FDR estimates (bottom). The score axis is logarithmic because smaller scores are better.
fdr1 <- TargetDecoyFDR$new(fdr_sample$score, decoy_hit = grepl("^REV_", fdr_sample$protein_id), larger_better = FALSE)
fdr1<TargetDecoyFDR> hits: 40,000, decoys: 858, larger_better: FALSE
score at fdr_simple q-value 0.01: 0.03395, at 0.05: 0.09992
fdr1$plot()
The field table holds the results as a data frame, with the hits sorted from best to worst score. We next will briefly discuss its columns.
knitr::kable(head(fdr1$table))| order | score | decoy_hit | fdr_concatenated | fdr_simple | q_value_fdr_concatenated | q_value_fdr_simple |
|---|---|---|---|---|---|---|
| 287 | 0 | FALSE | 0 | 0 | 0 | 0 |
| 3581 | 0 | FALSE | 0 | 0 | 0 | 0 |
| 29584 | 0 | FALSE | 0 | 0 | 0 | 0 |
| 26559 | 0 | FALSE | 0 | 0 | 0 | 0 |
| 15524 | 0 | FALSE | 0 | 0 | 0 | 0 |
| 30840 | 0 | FALSE | 0 | 0 | 0 | 0 |
Types of FDR
We define
- false positives (FP) as the number of passing decoy assignments
- true positives (TP) as the number of passing forward hits.
FDR of a concatenated target-decoy search
Which is the “Fraction of incorrect assignments above score threshold”. The multiplier 2 is needed here since we assume that also the forward sequences have false assignments.
\[ FDR_{concatenated} = \frac{2 \cdot FP}{TP + FP} \]
This is taken from the reference by (Elias and Gygi 2007). It is often called the false positive rate (FPR), which (Storey and Tibshirani 2003) defines differently. Käll et al. (2008) point out that this quantity actually should be named FDR and that “Many proteomics papers incorrectly refer to this quantity as the false positive rate.” TargetDecoyFDR therefore names it fdr_concatenated.
Simple FDR
“The FDR associated with a particular score threshold is defined as the expected percentage of accepted PSMs that are incorrect, where an accepted PSM is one that scores above the threshold (Many proteomics papers incorrectly refer to this quantity as the false positive rate).” ((Käll et al. 2008))
The Simple FDR introduced by ((Käll et al. 2008)) is defined by :
\[ SimpleFDR = \frac{FP}{TP} \]
“For a given score threshold, we count the number of decoy PSMs above the threshold and the number of target PSMs above the threshold. We can now estimate the FDR by simply computing the ratio of these two values (SimpleFDR).”(Käll et al. 2008)
fdr_curves <- data.frame(
score = rep(fdr1$table$score, 2),
fdr = 100 * c(fdr1$table$fdr_simple, fdr1$table$q_value_fdr_simple),
curve = rep(c("SimpleFDR", "q-value"), each = fdr1$n)
)
ggplot(fdr_curves, aes(x = score, y = fdr, colour = curve)) +
geom_line() +
coord_cartesian(xlim = c(0, 0.002), ylim = c(0, 0.05)) +
scale_colour_manual(values = c(SimpleFDR = "black", "q-value" = "#E69F00")) +
labs(x = "score (smaller is better)", y = "FDR (%)", colour = NULL)
Although the score is getting better (smaller) the FDR may increase since the number of TP in the denominator decreases while the number of FP stays the same. Therefore Storey and Tibshirani proposed the q-value, “which in our case is defined as the minimal FDR threshold at which a given PSM is accepted”(Käll et al. 2008).
Getting the score for an FDR.
Most frequently you will need to get the score for an FDR in order to filter your data. The method score_at() returns it; the q-value is a fraction, 0.01 for 1 %. To report your data with an FDR of 1% instead of 5% you can execute this code:
(score_05_concatenated <- fdr1$score_at(q_value = 0.05, method = "fdr_concatenated"))[1] 0.09991592
dim(filter(fdr_sample, score <= score_05_concatenated))[1] 40000 3
(score_01_concatenated <- fdr1$score_at(q_value = 0.01, method = "fdr_concatenated"))[1] 0.008509834
dim(filter(fdr_sample, score <= score_01_concatenated))[1] 29180 3
(score_01_simple <- fdr1$score_at(q_value = 0.01, method = "fdr_simple"))[1] 0.03394749
dim(filter(fdr_sample, score <= score_01_simple))[1] 34876 3
The order column
Since the scores are sorted to compute the FDR, the table also has the order column, the position of each hit in the input. This column can be used to align the ID’s with the scores.
knitr::kable(head(data.frame(id = fdr_sample$protein_id[fdr1$table$order], fdr1$table)))| id | order | score | decoy_hit | fdr_concatenated | fdr_simple | q_value_fdr_concatenated | q_value_fdr_simple |
|---|---|---|---|---|---|---|---|
| sp|P04264|K2C1_HUMAN | 287 | 0 | FALSE | 0 | 0 | 0 | 0 |
| sp|P04264|K2C1_HUMAN | 3581 | 0 | FALSE | 0 | 0 | 0 | 0 |
| sp|P35527|K1C9_HUMAN | 29584 | 0 | FALSE | 0 | 0 | 0 | 0 |
| sp|P04264|K2C1_HUMAN | 26559 | 0 | FALSE | 0 | 0 | 0 | 0 |
| sp|P02768|ALBU_HUMAN | 15524 | 0 | FALSE | 0 | 0 | 0 | 0 |
| sp|P04264|K2C1_HUMAN | 30840 | 0 | FALSE | 0 | 0 | 0 | 0 |
For convenience, pass the ID’s as id: the table then has them, in the sorted order, in the column id.
fdr1 <- TargetDecoyFDR$new(
fdr_sample$score,
decoy_hit = grepl("^REV_", fdr_sample$protein_id),
larger_better = FALSE,
id = fdr_sample$protein_id
)
knitr::kable(head(fdr1$table))| order | id | score | decoy_hit | fdr_concatenated | fdr_simple | q_value_fdr_concatenated | q_value_fdr_simple |
|---|---|---|---|---|---|---|---|
| 287 | sp|P04264|K2C1_HUMAN | 0 | FALSE | 0 | 0 | 0 | 0 |
| 3581 | sp|P04264|K2C1_HUMAN | 0 | FALSE | 0 | 0 | 0 | 0 |
| 29584 | sp|P35527|K1C9_HUMAN | 0 | FALSE | 0 | 0 | 0 | 0 |
| 26559 | sp|P04264|K2C1_HUMAN | 0 | FALSE | 0 | 0 | 0 | 0 |
| 15524 | sp|P02768|ALBU_HUMAN | 0 | FALSE | 0 | 0 | 0 | 0 |
| 30840 | sp|P04264|K2C1_HUMAN | 0 | FALSE | 0 | 0 | 0 | 0 |
Session Info
sessionInfo()R version 4.6.2 (2026-10-29)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 26.04.1 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so; LAPACK version 3.12.0
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
time zone: UTC
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_4.0.4 dplyr_1.2.1 prozor_0.5.0
loaded via a namespace (and not attached):
[1] vctrs_0.7.3 cli_3.6.6 knitr_1.52 rlang_1.3.0
[5] xfun_0.62 otel_0.3.0 generics_0.1.4 S7_1.0.0
[9] jsonlite_2.0.0 labeling_0.4.3 glue_1.8.1 htmltools_0.5.9
[13] scales_1.4.0 rmarkdown_2.32 grid_4.6.2 evaluate_1.0.5
[17] tibble_3.3.1 fastmap_1.2.0 yaml_2.3.12 lifecycle_1.0.5
[21] compiler_4.6.2 RColorBrewer_1.1-3 pkgconfig_2.0.3 farver_2.1.2
[25] digest_0.6.39 R6_2.6.1 tidyselect_1.2.1 pillar_1.11.1
[29] magrittr_2.0.5 withr_3.0.3 tools_4.6.2 gtable_0.3.6