--- title: "Peptide Annotation and Protein Inference" author: "Witold Wolski" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Peptide Annotation and Protein Inference} %\VignetteEncoding{UTF-8} %\VignetteEngine{knitr::rmarkdown} editor_options: chunk_output_type: console --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, message = FALSE, fig.width = 6, fig.height = 4) ``` # Introduction This Vignette describes how the `greedy_parsimony()` function can be used to infer proteins from peptide identifications using the Occam's razor principle. The method tries to find a minimal set of proteins which can explain all the peptides identified. ```{r load-data} library(prozor) library(dplyr) library(ggplot2) file <- system.file("extdata/IDResults.txt.gz", package = "prozor") id_results <- utils::read.delim(file) head(id_results) |> knitr::kable() ``` Annotate peptide sequences with protein sequences from two FASTA files, one with reviewed entries only (sp) and the other with reviewed and TrEMBL entries (sp/tr). ```{r fig.cap = "Number of proteins in the All and Canonical database."} fasta_all <- read_fasta(system.file("p1000_db1_example/Annotation_allSeq.fasta.gz", package = "prozor")) fasta_canonical <- read_fasta(system.file("p1000_db1_example/Annotation_canSeq.fasta.gz", package = "prozor")) db_sizes <- data.frame(database = c("All", "Canonical"), proteins = c(length(fasta_all), length(fasta_canonical))) ggplot(db_sizes, aes(x = database, y = proteins)) + geom_col(fill = "#0072B2") + geom_text(aes(label = proteins), vjust = -0.3) + labs(x = NULL, y = "proteins") ``` ```{r} peptides <- unique(id_results$peptideSeq) length(peptides) annotation_all <- annotate_peptides(peptides, fasta_all) head(select(annotation_all, !protein_sequence)) |> knitr::kable() annotation_canonical <- annotate_peptides(peptides, fasta_canonical) ``` ```{r fig.cap="Number of peptide protein matches for the All and Canonical database."} matches <- data.frame(database = c("All", "Canonical"), matches = c(nrow(annotation_all), nrow(annotation_canonical))) ggplot(matches, aes(x = database, y = matches)) + geom_col(fill = "#0072B2") + geom_text(aes(label = matches), vjust = -0.3) + labs(x = NULL, y = "peptide protein matches") ``` We can see that using the larger FASTA database reduces the proportion of proteotypic peptides. A proteotypic peptide is one which matches only a single protein. ```{r proteotypic-peptides, fig.cap="Proportion of proteotypic peptides."} proteotypic <- data.frame( database = c("All", "Canonical"), percent = c( mean(table(annotation_all$peptide_seq) == 1) * 100, mean(table(annotation_canonical$peptide_seq) == 1) * 100 ) ) ggplot(proteotypic, aes(x = database, y = percent)) + geom_col(fill = "#0072B2") + geom_text(aes(label = sprintf("%.1f %%", percent)), vjust = -0.3) + scale_y_continuous(limits = c(0, 100)) + labs(x = NULL, y = "% proteotypic peptides") ``` # Protein Inference We can now identify a minimal set of proteins explaining all the peptides observed for both databases. `greedy_parsimony()` takes a table with one row per peptide-protein match; a peptide observed as several precursors gives repeated rows, which count once. ```{r} precursors <- distinct(id_results, peptideModSeq, precursorCharge, peptideSeq) # peptide protein matches as points; rows and columns sorted by their number of matches, # proteotypic peptides (one protein) in red plot_matches <- function(annotation) { pairs <- distinct(annotation, peptide = factor(peptideSeq), protein = factor(protein_id)) peptide_matches <- tabulate(pairs$peptide, nlevels(pairs$peptide)) peptide_rank <- rank(-peptide_matches, ties.method = "first") protein_rank <- rank(-tabulate(pairs$protein, nlevels(pairs$protein)), ties.method = "first") matches <- data.frame( peptide = peptide_rank[pairs$peptide], protein = protein_rank[pairs$protein], proteotypic = peptide_matches[pairs$peptide] == 1 ) ggplot(matches) + geom_point(aes(x = protein, y = peptide, colour = proteotypic), shape = 15, size = 0.5) + scale_colour_manual( name = NULL, values = c(`FALSE` = "black", `TRUE` = "red"), labels = c(`FALSE` = "shared", `TRUE` = "proteotypic") ) + scale_y_reverse() + labs(x = "proteins, most peptides first", y = "peptides, most proteins first") } ``` ## Protein Inference for database with TrEMBL identifiers ```{r greedy-trembl, fig.cap="Peptide protein matches for the All database. Rows - peptides, columns - proteins, point - peptide protein match, red - proteotypic peptide."} annotated_precursors <- inner_join( precursors, select(annotation_all, peptide_seq, protein_id), by = join_by(peptideSeq == peptide_seq), relationship = "many-to-many" ) head(annotated_precursors) |> knitr::kable() c( peptides = n_distinct(annotated_precursors$peptideSeq), proteins = n_distinct(annotated_precursors$protein_id) ) plot_matches(annotated_precursors) groups_all <- greedy_parsimony(annotated_precursors, peptide = "peptideSeq") ``` ## Protein Inference for Reviewed/Canonical database ```{r greedy-canonical, fig.cap="Peptide protein matches for the Canonical database. Rows - peptides, columns - proteins, point - peptide protein match, red - proteotypic peptide."} annotated_precursors <- inner_join( precursors, select(annotation_canonical, peptide_seq, protein_id), by = join_by(peptideSeq == peptide_seq), relationship = "many-to-many" ) c( peptides = n_distinct(annotated_precursors$peptideSeq), proteins = n_distinct(annotated_precursors$protein_id) ) plot_matches(annotated_precursors) groups_canonical <- greedy_parsimony(annotated_precursors, peptide = "peptideSeq") ``` # Conclusion We see that the number of proteins needed to explain all the peptides is practically identical for both databases. Also in practice using a database with more entries does not lead to more identified proteins. On the contrary, it might even reduce the number of proteins identified. ```{r fig.cap="Number of proteins before and after protein inference."} inference <- data.frame( database = rep(c("All", "Canonical"), each = 2), stage = factor(rep(c("before", "after"), 2), levels = c("before", "after")), proteins = c( length(unique(annotation_all$protein_id)), length(unique(groups_all)), length(unique(annotation_canonical$protein_id)), length(unique(groups_canonical)) ) ) ggplot(inference, aes(x = stage, y = proteins)) + geom_col(fill = "#0072B2") + geom_text(aes(label = proteins), vjust = -0.3) + facet_wrap(~database) + labs(x = "protein inference", y = "proteins") ``` # Session Info ```{r} sessionInfo() ```