--- title: "Target Decoy FDR" author: "Witold Wolski" date: "`r Sys.Date()`" output: rmarkdown::html_vignette bibliography: bibliography.bib vignette: > %\VignetteIndexEntry{Target Decoy FDR} %\VignetteEncoding{UTF-8} %\VignetteEngine{knitr::rmarkdown} editor_options: chunk_output_type: console --- ```{r setup, include=FALSE} knitr::opts_chunk$set( echo = TRUE, message = FALSE ) ``` ```{r libraries} 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. ```{r targetdecoy, fig.cap = "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.", echo = FALSE, fig.width=6, fig.height=4} threshold <- 3.5 regions <- function(hits, mean) { below <- seq(0, threshold, by = 0.05) above <- seq(threshold, 10, by = 0.05) data.frame( hits = hits, score = c(below, above), density = dnorm(c(below, above), mean = mean, sd = 1), side = rep(c("below", "above"), c(length(below), length(above))) ) } dens <- rbind(regions("decoy", 3), regions("forward", 5)) classes <- c(decoy.below = "TN", decoy.above = "FP", forward.below = "FN", forward.above = "TP") dens$class <- factor(classes[paste(dens$hits, dens$side, sep = ".")], levels = c("TN", "FP", "FN", "TP")) ggplot(dens, aes(x = score, y = density)) + geom_area(aes(fill = class, group = interaction(hits, side)), position = "identity", alpha = 0.5) + geom_line(aes(colour = hits, group = hits)) + geom_vline(xintercept = threshold, linetype = "dashed") + scale_fill_manual(values = c(TN = "grey80", FP = "#D55E00", FN = "#56B4E9", TP = "#0072B2")) + scale_colour_manual(values = c(decoy = "black", forward = "#0072B2")) + labs(x = "score (larger is better)", y = "density", fill = NULL, colour = NULL) ``` ```{r} data(fdr_sample) knitr::kable(head(fdr_sample)) ``` 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. ```{r} table(grepl("^REV_", fdr_sample$protein_id)) ``` 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. ```{r fig.width=7, fig.height=6} fdr1 <- TargetDecoyFDR$new(fdr_sample$score, decoy_hit = grepl("^REV_", fdr_sample$protein_id), larger_better = FALSE) fdr1 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. ```{r} knitr::kable(head(fdr1$table)) ``` ## 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 [@Elias2007]. It is often called the false positive rate (FPR), which [@Storey2003] defines differently. Käll et al. [-@Kaell2008] 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_)." ([@Kaell2008]) The Simple FDR introduced by ([@Kaell2008]) 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)."[@Kaell2008] ```{r fig.width=7, fig.height=4} 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"[@Kaell2008]. ## 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: ```{r} (score_05_concatenated <- fdr1$score_at(q_value = 0.05, method = "fdr_concatenated")) dim(filter(fdr_sample, score <= score_05_concatenated)) (score_01_concatenated <- fdr1$score_at(q_value = 0.01, method = "fdr_concatenated")) dim(filter(fdr_sample, score <= score_01_concatenated)) (score_01_simple <- fdr1$score_at(q_value = 0.01, method = "fdr_simple")) dim(filter(fdr_sample, score <= score_01_simple)) ``` ## 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. ```{r} knitr::kable(head(data.frame(id = fdr_sample$protein_id[fdr1$table$order], fdr1$table))) ``` For convenience, pass the ID's as `id`: the table then has them, in the sorted order, in the column `id`. ```{r} 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)) ``` # Session Info ```{r} sessionInfo() ``` # References