---
title: "Shot Noise"
description: >
  An example for shot noise analysis.
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Shot Noise}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---


```{r, include=FALSE}
# default chunk options
knitr::opts_chunk$set(collapse = TRUE, message = FALSE, comment = "#>")
```

```{r}
# load libraries
library(isoorbi) # for Orbitrap functions
library(dplyr) # for data wrangling
```

# Model Peptide (MRFA)

This is an example for shot-noise analysis from [Kantnerová et al. (2023)](https://github.com/isoverse/2023_kantnerova_et_al).

# Data

## Load data

```{r}
# load and process data
data <- 
  # load file included in isoorbi package
  system.file(package = "isoorbi", "extdata", "testfile_shotnoise.isox") |>
  orbi_read_isox() |>
  # check data for satellite peaks
  orbi_flag_satellite_peaks() |>
  # make sure isotopocules are present in (almost) all scans, otherwise
  # shot noise analyses can be inaccurate
  orbi_flag_weak_isotopocules(min_percent = 90) |> 
  # see if there are any AGC outliers
  orbi_flag_outliers(agc_fold_cutoff = 2)
```

This indicates that there were a few satellite peaks and weak isotopocules but no outliers.

## Visualize isotopocule coverage

Show the isotopocule coverage for the different amino acids, thus highlighting which isotopocules were detected in which scans. Note that 2H was highlighted as weak for Alanine, Arginine, and Phenylalanine. This is because of the high cutoff (90%) in `orbi_flag_weak_isotopocules()` which is appropriate for shot-noise analyses but simply for inspecting ratios it would be reasonable to examine the 2H signal at least for Alanine.

```{r}
#| label: fig-mrfa-isotopocule-coverage
#| fig-cap: Isotopocule coverage
#| fig-width: 8
#| fig-height: 7
#| warning: false
data |> orbi_plot_isotopocule_coverage()
```

## Visualize satellite peaks

Show peaks that were flagged as satellite peaks.

```{r}
#| label: fig-mrfa-satellite-peaks
#| fig-cap: Satellite peaks
#| fig-width: 8
#| fig-height: 7
#| warning: false
data |> orbi_plot_satellite_peaks()
```

## Visualize intensity vs time

```{r}
#| label: fig-mrfa-intensity
#| fig-cap: Isotopocule intensity for M0 ion
#| fig-width: 8
#| fig-height: 7
#| warning: false
data |> orbi_plot_raw_data(
  isotopocules = "M0", 
  y = intensity,
  y_scale = "log"
)
```


# Shot noise

## Calculations

```{r}
# calculate ratios vs basepeak
data_w_bp <- 
  data |>
  orbi_define_basepeak("M0")

# calculate shot noise
shot_noise <-
  data_w_bp |>
  orbi_analyze_shot_noise()

# export shot noise to an Excel file
shot_noise |>
  orbi_export_data_to_excel("shot_noise.xlsx") 
```

```{r}
#| include: false
unlink("shot_noise.xlsx")
```

## Table

```{r}
# example of the first few rows of the shot-noise calculations
shot_noise |>
  arrange(compound, isotopocule, scan.no) |>
  select(compound, scan.no, time.min, isotopocule,
         ratio, ratio_rel_se.permil, shot_noise.permil) |>
  head(10) |>
  knitr::kable()
```


## Visualize ratios vs time

```{r}
#| label: fig-mrfa-ratios
#| fig-cap: Isotopocule ratios vs M0
#| fig-width: 8
#| fig-height: 7
#| warning: false
data_w_bp |> orbi_plot_raw_data(y = ratio)
```

## Visualize shot noise

```{r}
#| label: fig-mrfa-shotnoise
#| fig-cap: amino acids shotnoise
#| warning: false
#| fig-width: 8
#| fig-height: 10
shot_noise |> orbi_plot_shot_noise()
```