Target Decoy FDR

library(prozor)
library(dplyr)
library(ggplot2)

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.

Score distributions of decoy (black line) and forward (blue line) hits; the dashed line is the score threshold. TN true negative, FP false positive, FN false negative, TP true positive.

Score distributions of decoy (black line) and forward (blue line) hits; the dashed line is the score threshold. TN true negative, FP false positive, FN false negative, TP true positive.

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.

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=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Etc/UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] dplyr_1.2.1    ggplot2_4.0.4  prozor_0.5.0   rmarkdown_2.32
## 
## loaded via a namespace (and not attached):
##  [1] vctrs_0.7.3           cli_3.6.6             knitr_1.52           
##  [4] rlang_1.3.0           xfun_0.62             otel_0.3.0           
##  [7] generics_0.1.4        S7_1.0.0              jsonlite_2.0.0       
## [10] AhoCorasickTrie_0.1.4 labeling_0.4.3        glue_1.8.1           
## [13] buildtools_1.0.0      htmltools_0.5.9       maketools_1.3.2      
## [16] sys_3.4.3             sass_0.5.0            scales_1.4.0         
## [19] grid_4.6.2            tibble_3.3.1          evaluate_1.0.5       
## [22] jquerylib_0.1.4       fastmap_1.2.0         yaml_2.3.12          
## [25] lifecycle_1.0.5       compiler_4.6.2        RColorBrewer_1.1-3   
## [28] Rcpp_1.1.2            pkgconfig_2.0.3       farver_2.1.2         
## [31] digest_0.6.39         R6_2.6.1              tidyselect_1.2.1     
## [34] pillar_1.11.1         magrittr_2.0.5        bslib_0.12.0         
## [37] withr_3.0.3           tools_4.6.2           gtable_0.3.6         
## [40] cachem_1.1.0

References

Elias, Joshua E, and Steven P Gygi. 2007. “Target-Decoy Search Strategy for Increased Confidence in Large-Scale Protein Identifications by Mass Spectrometry.” Nature Methods 4 (3): 207–14.
Käll, Lukas, John D Storey, Michael J MacCoss, and William Stafford Noble. 2008. “Assigning Significance to Peptides Identified by Tandem Mass Spectrometry Using Decoy Databases.” Journal of Proteome Research 7 (1): 29–34.
Storey, John D, and Robert Tibshirani. 2003. “Statistical Significance for Genomewide Studies.” Proceedings of the National Academy of Sciences 100 (16): 9440–45.