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

seurat_obj <- LoveMangiola2022tidytranscriptomics::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 = LoveMangiola2022tidytranscriptomics::gate_seurat_obj
  ))
## # A Seurat-tibble abstraction: 33,215 × 26
## # Features=6 | Cells=33215 | Active assay=SCT | Assays=RNA, SCT
##    .cell  file  sampl…¹ sample Barcode batch BCB   S.Score G2M.S…² Phase curat…³
##    <chr>  <chr> <chr>   <chr>  <chr>   <chr> <chr>   <dbl>   <dbl> <chr> <chr>  
##  1 1_AAA… ../d… SI-GA-… SI-GA… AAACGA… 1     BCB1…  0.0620 -0.0972 S     CD4+_T…
##  2 1_AAA… ../d… SI-GA-… SI-GA… AAACGC… 1     BCB1…  0.0943 -0.193  S     CD8+_h…
##  3 1_AAA… ../d… SI-GA-… SI-GA… AAACGC… 1     BCB1…  0.0216 -0.124  S     CD4+_T…
##  4 1_AAA… ../d… SI-GA-… SI-GA… AAAGAA… 1     BCB1…  0.0598 -0.196  S     MAIT   
##  5 1_AAA… ../d… SI-GA-… SI-GA… AAAGGA… 1     BCB1…  0.0281 -0.0540 S     CD8+_h…
##  6 1_AAA… ../d… SI-GA-… SI-GA… AAAGGG… 1     BCB1…  0.0110 -0.0998 S     MAIT   
##  7 1_AAA… ../d… SI-GA-… SI-GA… AAAGGG… 1     BCB1…  0.0341 -0.143  S     CD8+_h…
##  8 1_AAA… ../d… SI-GA-… SI-GA… AAAGTC… 1     BCB1…  0.0425 -0.183  S     MAIT   
##  9 1_AAA… ../d… SI-GA-… SI-GA… AAAGTC… 1     BCB1… -0.0289 -0.127  G1    CD8+_h…
## 10 1_AAA… ../d… SI-GA-… SI-GA… AAAGTG… 1     BCB1… -0.0551 -0.102  G1    CD8+_h…
## # … with 33,205 more rows, 15 more variables: nCount_RNA <dbl>,
## #   nFeature_RNA <int>, nCount_SCT <dbl>, nFeature_SCT <int>, treatment <fct>,
## #   CD3D <dbl>, TRDC <dbl>, TRGC1 <dbl>, TRGC2 <dbl>, CD8A <dbl>, CD8B <dbl>,
## #   signature_score <dbl>, gate <int>, UMAP_1 <dbl>, UMAP_2 <dbl>, and
## #   abbreviated variable names ¹​sample_id, ²​G2M.Score, ³​curated_cell_type

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 = LoveMangiola2022tidytranscriptomics::gate_seurat_obj)) |>

  filter(gate == 1)