library(ggplot2)
library(plotly)
library(dplyr)
library(colorspace)
library(dittoSeq)
library(tidyseurat)
library(tidygate)

seurat_obj <- tidyomicsWorkshop::seurat_obj

Instead of filtering using a specified threshold, the gamma delta T cells could be interactively selected from the plot using the tidygate package.

seurat_obj |>

  join_features(
    features = c("CD3D", "TRDC", "TRGC1", "TRGC2", "CD8A", "CD8B" ), shape = "wide"
  ) |>

  mutate(signature_score =
           scales::rescale(CD3D + TRDC + TRGC1+ TRGC2, to=c(0,1)) -
           scales::rescale(CD8A + CD8B, to=c(0,1))
  ) |>

  mutate(gate = gate_int(
    UMAP_1, UMAP_2,
    .size = 0.1,
    .color =signature_score
  ))

After the selection we could reload from a file the gate that was drawn, for reproducibility.

seurat_obj |>

  join_features(
    features = c("CD3D", "TRDC", "TRGC1", "TRGC2", "CD8A", "CD8B" ), shape = "wide"

  ) |>

  mutate(signature_score =
           scales::rescale(CD3D + TRDC + TRGC1+ TRGC2, to=c(0,1)) -
           scales::rescale(CD8A + CD8B, to=c(0,1))
  ) |>

  mutate(gate = gate_int(
    UMAP_1, UMAP_2,
    .size = 0.1,
    .color =signature_score,
    gate_list = tidyomicsWorkshop::gate_seurat_obj
  ))

The dataset can be filtered for just these cells using tidyverse filter.

seurat_obj_gamma_delta <-
    
  seurat_obj |>

  join_features(
    features = c("CD3D", "TRDC", "TRGC1", "TRGC2", "CD8A", "CD8B" ), shape = "wide"

  ) |>

  mutate(signature_score =
           scales::rescale(CD3D + TRDC + TRGC1+ TRGC2, to=c(0,1)) -
           scales::rescale(CD8A + CD8B, to=c(0,1))
  ) |>

  mutate(gate = gate_int(UMAP_1, UMAP_2, gate_list = tidyomicsWorkshop::gate_seurat_obj)) |>

  filter(gate == 1)