## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  echo = TRUE,
  message = FALSE,
  fig.width = 7
)

## ----libraries----------------------------------------------------------------
library(prozor)
library(ggplot2)

## ----data---------------------------------------------------------------------
file <- system.file("extdata/IDResults.txt.gz", package = "prozor")
peptides <- unique(utils::read.delim(file)$peptideSeq)
canonical <- read_fasta(system.file("p1000_db1_example/Annotation_canSeq.fasta.gz", package = "prozor"))
all_sequences <- read_fasta(system.file("p1000_db1_example/Annotation_allSeq.fasta.gz", package = "prozor"))
c(peptides = length(peptides), canonical = length(canonical), with_isoforms = length(all_sequences))

## ----coverage-----------------------------------------------------------------
annotation <- annotate_peptides(peptides, canonical)
coverage <- sequence_coverage(annotation)
head(coverage)

## ----coverage-distribution, fig.height = 3.5, fig.cap = "Sequence coverage of the proteins matched by at least one peptide."----
ggplot(coverage, aes(x = coverage)) +
  geom_histogram(binwidth = 0.05, boundary = 0, fill = "#0072B2", colour = "white") +
  scale_x_continuous(labels = function(x) paste0(100 * x, " %")) +
  labs(x = "sequence coverage", y = "proteins")

## ----best-covered-------------------------------------------------------------
best <- coverage[order(coverage$coverage, decreasing = TRUE), ]
knitr::kable(head(best, 10), digits = 2, row.names = FALSE, caption = "The ten best-covered proteins.")

## ----inference----------------------------------------------------------------
inference <- greedy_parsimony(annotation)
kept <- unique(unlist(strsplit(inference, ";", fixed = TRUE)))
coverage$inference <- ifelse(coverage$protein_id %in% kept, "kept", "dropped")
table(coverage$inference)

## ----inference-coverage, fig.height = 3, fig.cap = "Sequence coverage of the proteins kept and dropped by protein inference."----
ggplot(coverage, aes(x = coverage, y = inference)) +
  geom_boxplot(outlier.shape = NA, colour = "grey40") +
  geom_jitter(height = 0.2, width = 0, alpha = 0.4, size = 0.8, colour = "#0072B2") +
  scale_x_continuous(labels = function(x) paste0(100 * x, " %")) +
  labs(x = "sequence coverage", y = NULL)

## ----residues-----------------------------------------------------------------
residues <- residue_coverage(annotation)
well_covered <- best[best$n_peptides >= 20, ]
example <- well_covered$protein_id[1]
example

## ----plot-example, fig.height = 3, fig.cap = "Coverage of each residue: gray residues are covered by no peptide, darker blue by more peptides."----
plot_coverage(residues, example)

## ----isoforms-----------------------------------------------------------------
annotation_all <- annotate_peptides(peptides, all_sequences)
kng1 <- c("sp|P01042|KNG1_HUMAN", "sp|P01042-2|KNG1_HUMAN")
coverage_all <- sequence_coverage(annotation_all)
coverage_all[coverage_all$protein_id %in% kng1, ]

## ----isoform-peptides---------------------------------------------------------
kng1_peptides <- split(annotation_all$peptide_seq, annotation_all$protein_id)[kng1]
isoform_counts <- c(
  shared = length(intersect(kng1_peptides[[1]], kng1_peptides[[2]])),
  canonical_only = length(setdiff(kng1_peptides[[1]], kng1_peptides[[2]])),
  isoform_2_only = length(setdiff(kng1_peptides[[2]], kng1_peptides[[1]]))
)
isoform_counts

## ----isoform-plots, fig.height = 3, fig.cap = "Coverage of the canonical KNG1 sequence (top) and of isoform 2 (bottom)."----
residues_all <- residue_coverage(annotation_all)
plot_coverage(residues_all, kng1[1])
plot_coverage(residues_all, kng1[2])

## ----session-info-------------------------------------------------------------
sessionInfo()

