Project: s41467-024-53356-5 | Function: ab_tr_dist (r)

system: You are an expert R programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow R best practices and maintain consistent code style.
Return **only** the complete R function code.

user: Complete this R function:

**DESCRIPTION:** Computes distance matrix from antibody trajectories using cosine*euclidean distance metric. Transforms trajectories data into matrix format and calculates hybrid distance measure.

**KEY TERMS:** {
  "trajectories": "autoantibody trajectories over time",
  "fc": "fold change values",
  "d_cos": "cosine distance",
  "d_euc": "euclidean distance"
}

**CONTEXT CODE:** 
#' Compute distance matrix from antibody trajectories
#'
#'`ab_tr_dist()` computes a distance matrix using the `cosine*euclidean` distance metric.
#'
#' @param trajectories A tibble with columns `trajectory`, `t`, and `fc`. See `abtrac::trajectories` for an example.
#'
#' @return A distance matrix.
#' @export
#'
#' @examples
#' \dontrun{
#' ab_tr_dist(trajectories = trajectories)
#' }
ab_tr_dist <- function(trajectories, verbose = TRUE) {
  stop("Function implementation has been masked")
}


#' Cluster antibody trajectories
#'
#' `ab_tr_clust()` takes a distance matrix of antibody trajectories (typically the output from `ab_tr_dist()`) and assigns trajectories to `k` clusters across given `ks` using the PAM (Partitioning Around Medioids) algorithm from `cluster::pam()`.
#'
#' @param dist_mat A distance matrix.
#' @param ks A numeric vector of `k`s to iterate across.
#' @param verbose A logical specifying if progress should be printed.
#' @param ... Additional arguments to be passed to `cluster::pam()`.
#'
#' @return A tibble of `pam` objects.
#' @export
#'
#' @examples
#' \dontrun{
#' ab_tr_clust(dist_mat = dist_mat, ks = 1:20, verbose = TRUE, variant = "faster", nstart = 10)
#' }
ab_tr_clust <- function(dist_mat, ks, verbose = TRUE, ...) {
  stop("Function implementation has been masked")
}

#' Silhouette plot
#'
#' Makes a `silhouette_plot()` from a tibble of `pam` objects, typically the output of `ab_tr_clust()`. The silhouette method can be used to dplyr::select the optimal cluster number.
#'
#' @param pam_objects A tibble of `pam` objects with columns `k` (numeric) and `pam_object` (list of `pam` objects). Typically the output of `ab_tr_clust()`.
#' @param nudge_y Nudge cluster number label.
#' @param text_size Size of cluster number label.
#'
#' @return A `gg` object.
#' @export
#'
#' @examples
#' \dontrun{
#' silhouette_plot(pam_objects = pam_objects)
#' }
silhouette_plot <- function(pam_objects, nudge_y = 0.015, text_size = 8) {
  sil <-
    pam_objects %>%
    dplyr::rowwise() %>%
    dplyr::mutate(avg_sil_width = list(.data$pam_object$silinfo$avg.width)) %>%
    dplyr::select(.data$k, .data$avg_sil_width) %>%
    tidyr::unnest(.data$avg_sil_width, keep_empty = TRUE)

  p <-
    sil %>%
    ggplot2::ggplot(ggplot2::aes(x = .data$k, y = .data$avg_sil_width)) +
    ggplot2::geom_line() +
    ggplot2::geom_text(ggplot2::aes(label = .data$k), nudge_y = nudge_y, size = text_size/ggplot2::.pt) +
    ggplot2::theme_bw() +
    ggplot2::labs(x = "k", y = "Average silhouette width")

  return(p)
}

#' Extract clusters for a dplyr::selected `k`
#'
#' `extract_clusters()` extracts the cluster assignment from a tibble of `pam` objects and adds them to the original tibble of trajectories.
#'
#' @param trajectories The original tibble of trajectories used to compute the distance matrix (typically using `ab_tr_dist()`). See `abtrac::trajectories` for an example.
#' @param pam_objects A tibble of `pam` objects with columns `k` (numeric) and `pam_object` (list of `pam` objects). Typically the output of `ab_tr_clust()`
#' @param sel_k The dplyr::selected cluster number `k`, possibly dplyr::selected using the silhouette method (see `silhouette_plot()`).
#'
#' @return `trajectories` with an added column of `cluster` numbers.
#' @export
#'
#' @examples
#' \dontrun{
#' extract_clusters(trajectories = trajectories, pam_objects = pam_objects, sel_k = 7)
#' }
extract_clusters <- function(trajectories, pam_objects, sel_k) {
  pam_object <-
    pam_objects %>%
    dplyr::filter(.data$k == sel_k) %>%
    dplyr::pull(.data$pam_object) %>%
    purrr::pluck(1)

  pam_clusters <-
    pam_object$clustering %>%
    tibble::enframe(name = "trajectory", value = "cluster") %>%
    dplyr::mutate(trajectory = as.numeric(.data$trajectory))

  trajectories_clust <-
    trajectories %>%
    dplyr::full_join(pam_clusters, by = c("trajectory"))

  return(trajectories_clust)
}

#' Lineplots of clusters
#'
#' Makes a `cluster_lineplot()` to visualize the obtained clusters of trajectories.
#'
#' @param trajectories_clust Tibble of trajectories and clusters, typically the output of `extract_clusters()`. See `abtrac::trajectories` for an example of all required columns except `cluster`, which is added by `extract_clusters()`.
#' @param alpha A number (in 0:1) defining the transparency of lines.
#' @param ncol A number defining the number of columns for facetting.


Complete the function implementation replacing "stop("Not implemented")".