```{r setup, include = FALSE}
# Runs first every time you knit. Hidden in the report (include = FALSE).
library(ggplot2)
options(HTTPUserAgent = "3MB001-course")

# error = TRUE: the report still knits when a chunk fails, and the error is
# shown in the HTML. At hand-in there must be no errors left.
knitr::opts_chunk$set(error = TRUE)

# If UniProt is down, set this to TRUE to use the course's saved copy.
use_snapshot <- FALSE
```

You only change the three places marked `___` (blanks 1–3). All other code is
given. Knit after each blank and look at the HTML.

# The proteins

Thirteen human proteins: organelle markers from Part A and a few famous proteins
from the lectures. The compartment is where each protein mainly works.

**Blank 1:** calnexin's accession is missing. Replace `___` with the UniProt
accession of calnexin from Part A, in quotes (like the others).

```{r proteins}
markers <- data.frame(
  accession = c(___, "Q15388", "P11279", "Q08379", "P20700", "P04040",
                "P05023", "P13569", "P60709", "P07437", "Q8WZ42", "P62805",
                "P04406"),
  label = c("Calnexin (CANX)", "TOM20 (TOMM20)", "LAMP1", "GM130 (GOLGA2)",
            "Lamin B1 (LMNB1)", "Catalase (CAT)", "Na/K pump (ATP1A1)",
            "CFTR", "Actin (ACTB)", "Tubulin beta (TUBB)", "Titin (TTN)",
            "Histone H4", "GAPDH"),
  compartment = c("ER", "Mitochondrion", "Lysosome", "Golgi", "Nucleus",
                  "Peroxisome", "Plasma membrane", "Plasma membrane",
                  "Cytoskeleton", "Cytoskeleton", "Cytoskeleton", "Nucleus",
                  "Cytosol")
)
markers
```

# Fetch length and mass from UniProt

This chunk is given. It asks the UniProt REST API for a table (tab-separated)
of the 13 proteins, or reads the course's saved copy if `use_snapshot` is `TRUE`.

```{r fetch}
uniprot_release <- "unknown"
retrieved <- "not yet"

if (use_snapshot) {
  snapshot_file <- "data/uniprot-proteins-2026-11-18.csv"
  if (!file.exists(snapshot_file)) {
    snapshot_file <- paste0("https://henriksson-lab.github.io/course_cellbiology/",
                            "bioinfo-protein-atlas/data/uniprot-proteins-2026-11-18.csv")
  }
  uniprot <- read.csv(snapshot_file)
  uniprot_release <- "2026_04 (course snapshot)"
  retrieved <- "2026-11-18"
} else {
  uniprot_url <- paste0(
    "https://rest.uniprot.org/uniprotkb/accessions?accessions=",
    paste(markers$accession, collapse = ","),
    "&fields=accession,gene_primary,protein_name,length,mass&format=tsv")
  uniprot <- read.delim(uniprot_url)
  headers <- curlGetHeaders(uniprot_url)
  uniprot_release <- trimws(sub(".*: ", "",
    grep("^x-uniprot-release:", headers, ignore.case = TRUE, value = TRUE)))
  retrieved <- as.character(Sys.Date())
}
```

This chunk is given too. It joins the UniProt table to our labels and adds the
mass in kDa (UniProt gives daltons).

```{r clean}
proteins <- merge(markers, uniprot, by.x = "accession", by.y = "Entry")
proteins$mass_kda <- proteins$Mass / 1000
proteins[, c("label", "compartment", "Length", "mass_kda")]
```

# Heaviest and lightest

**Blank 2:** the first line finds the row of the heaviest protein with
`which.max()`. Change `___` so that the second line finds the lightest.

```{r heaviest-lightest}
ratio <- NA   # a default, so the text below knits even before the blank is filled

heaviest <- proteins[which.max(proteins$mass_kda), ]
lightest <- proteins[which.___(proteins$mass_kda), ]
ratio <- heaviest$mass_kda / lightest$mass_kda

heaviest[, c("label", "Length", "mass_kda")]
lightest[, c("label", "Length", "mass_kda")]
```

The heaviest protein is about `r round(ratio)` times heavier than the lightest.
(This number is inline R: it is computed when you knit.)

# Plot

**Blank 3:** colour the dots by compartment. Replace `___` with the name of the
column in `proteins` that holds the compartment.

```{r mass-plot, fig.width = 7, fig.height = 4.5}
ggplot(proteins, aes(x = mass_kda, y = reorder(label, mass_kda), colour = ___)) +
  geom_point(size = 3) +
  scale_x_log10() +
  labs(x = "Protein mass (kDa, log scale)", y = NULL, colour = "Compartment",
       caption = paste0("Data: UniProtKB release ", uniprot_release,
                        ", retrieved ", retrieved)) +
  theme_minimal()
```

# Data

- **Source:** UniProtKB, <https://www.uniprot.org> (CC BY 4.0), REST API.
- **Release:** `r uniprot_release`
- **Retrieved:** `r retrieved`

```{r session-info}
sessionInfo()
```
