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

# 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 ChEMBL 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 question

Imatinib was designed to block the ABL kinase (BCR-ABL in chronic myeloid
leukaemia). Does it also block other kinases? ChEMBL collects IC50 values from
published experiments: the drug concentration that halves the activity of the
kinase. A **lower IC50 means a more potent drug**. We compare four kinases:
ABL1, KIT, PDGFRA and SRC.

# Fetch IC50 records from ChEMBL

This chunk is given. It asks the ChEMBL API for all imatinib (CHEMBL941) IC50
records for the four kinases, or reads the course's saved copy if
`use_snapshot` is `TRUE`. A copy is saved in `data/`, so knitting again the same
day does not ask ChEMBL again.

```{r fetch}
targets <- c(CHEMBL1862 = "ABL1", CHEMBL1936 = "KIT",
             CHEMBL2007 = "PDGFRA", CHEMBL267 = "SRC")
chembl_release <- "unknown"
retrieved <- "not yet"

if (use_snapshot) {
  snapshot_file <- "data/imatinib-ic50-2026-11-18.csv"
  if (!file.exists(snapshot_file)) {
    snapshot_file <- paste0("https://henriksson-lab.github.io/course_cellbiology/",
                            "bioinfo-drug-targets/data/imatinib-ic50-2026-11-18.csv")
  }
  activities <- read.csv(snapshot_file)
  chembl_release <- "ChEMBL_37 (course snapshot)"
  retrieved <- "2026-11-18"
} else {
  cache_file <- paste0("data/imatinib-ic50-", Sys.Date(), ".csv")
  if (file.exists(cache_file)) {
    activities <- read.csv(cache_file)
  } else {
    chembl_url <- paste0(
      "https://www.ebi.ac.uk/chembl/api/data/activity.json",
      "?molecule_chembl_id=CHEMBL941&standard_type=IC50",
      "&target_chembl_id__in=", paste(names(targets), collapse = ","),
      "&limit=1000&only=activity_id,target_chembl_id,standard_relation,",
      "standard_value,standard_units,assay_type,assay_description,document_chembl_id")
    # ChEMBL is sometimes slow: try up to three times.
    result <- NULL
    for (attempt in 1:3) {
      result <- tryCatch(jsonlite::fromJSON(chembl_url), error = function(e) NULL)
      if (!is.null(result)) break
      Sys.sleep(5)
    }
    if (is.null(result)) stop("ChEMBL did not answer: set use_snapshot <- TRUE")
    activities <- data.frame(target = unname(targets[result$activities$target_chembl_id]),
                             result$activities)
    dir.create("data", showWarnings = FALSE)
    write.csv(activities, cache_file, row.names = FALSE)
  }
  chembl_release <- jsonlite::fromJSON(
    "https://www.ebi.ac.uk/chembl/api/data/status.json")$chembl_db_version
  retrieved <- as.character(Sys.Date())
}
activities$standard_value <- as.numeric(activities$standard_value)
nrow(activities)
```

# Clean the records

Given. Keep records with a number in nM, and mark records measured on a
**mutant** kinase (the assay description says "mutant").

```{r clean}
ic50 <- activities[!is.na(activities$standard_value) &
                     activities$standard_units %in% "nM", ]
ic50$kinase_form <- ifelse(grepl("mutant", ic50$assay_description, ignore.case = TRUE),
                           "mutant", "wild type")
table(ic50$target, ic50$kinase_form)
```

# Plot every IC50 value

Example (given): a bar chart of how many records each kinase has.

```{r example-plot, fig.width = 6, fig.height = 3}
ggplot(ic50, aes(x = target)) +
  geom_bar() +
  labs(x = "Kinase", y = "IC50 records (count)",
       caption = paste("Source: ChEMBL, retrieved", retrieved))
```

**Blank 1:** make a strip plot with one point per record: the kinase on the
x axis and the IC50 value (column `standard_value`) on the y axis. Replace the
two `___` with column names (no quotes). The dashed line is 1 µM (1000 nM).

```{r strip-plot, fig.width = 7, fig.height = 4.5}
ggplot(ic50, aes(x = ___, y = ___, colour = kinase_form)) +
  geom_jitter(width = 0.15, height = 0) +
  scale_y_log10() +
  geom_hline(yintercept = 1000, linetype = "dashed") +
  labs(x = "Kinase", y = "IC50 (nM, log scale)", colour = "Kinase form",
       caption = paste("Source: ChEMBL, retrieved", retrieved))
```

# Which kinases does imatinib block below 1 µM?

Given: the median IC50 of each kinase, wild-type records only.

```{r medians}
wild_type <- ic50[ic50$kinase_form == "wild type", ]
medians <- aggregate(standard_value ~ target, data = wild_type, FUN = median)
names(medians)[2] <- "median_ic50_nm"
medians
```

Two examples of filtering and sorting in base R (given):

```{r filter-examples}
# keep rows: data[condition, ]
ic50[ic50$target == "SRC", c("target", "standard_relation", "standard_value")]

# sort rows: data[order(column), ]   (lowest first)
head(ic50[order(ic50$standard_value), c("target", "standard_value")], 3)
```

**Blank 2:** keep only the kinases with a median IC50 below 1 µM. Write the
number in nM.

**Blank 3:** sort them, lowest median first. Write the column to sort by,
as in the example (`potent$...`).

```{r potent}
potent <- medians[medians$median_ic50_nm < ___, ]
potent[order(___), ]
```

# Mutations in the mutant records

Given: the mutation names (like V600E) found in the assay descriptions of the
mutant records, most frequent first, and the median IC50 of the T315I records.

```{r mutations}
mutant <- ic50[ic50$kinase_form == "mutant", ]
mutation <- regmatches(mutant$assay_description,
                       regexpr("[A-Z][0-9]{3}[A-Z]", mutant$assay_description))
head(sort(table(mutation), decreasing = TRUE), 5)

# median IC50 (nM) of the records on the T315I mutant
median(mutant$standard_value[grepl("T315I", mutant$assay_description)])
```

# Data

- ChEMBL (<https://www.ebi.ac.uk/chembl/>), release `r chembl_release`,
  activity records for imatinib (CHEMBL941), retrieved `r retrieved`.
  Licence CC BY-SA 3.0.

# Session info

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