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

seurat_obj <- RMedicine2023tidytranscriptomics::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 = RMedicine2023tidytranscriptomics::gate_seurat_obj
  ))
## # A Seurat-tibble abstraction: 33,215 × 26
## # Features=6 | Cells=33215 | Active assay=SCT | Assays=RNA, SCT
##    .cell      file  sample_id sample Barcode batch BCB   S.Score G2M.Score Phase
##    <chr>      <chr> <chr>     <chr>  <chr>   <chr> <chr>   <dbl>     <dbl> <chr>
##  1 1_AAACGAA… ../d… SI-GA-H1… SI-GA… AAACGA… 1     BCB1…  0.0620   -0.0972 S    
##  2 1_AAACGCT… ../d… SI-GA-H1… SI-GA… AAACGC… 1     BCB1…  0.0943   -0.193  S    
##  3 1_AAACGCT… ../d… SI-GA-H1… SI-GA… AAACGC… 1     BCB1…  0.0216   -0.124  S    
##  4 1_AAAGAAC… ../d… SI-GA-H1… SI-GA… AAAGAA… 1     BCB1…  0.0598   -0.196  S    
##  5 1_AAAGGAT… ../d… SI-GA-H1… SI-GA… AAAGGA… 1     BCB1…  0.0281   -0.0540 S    
##  6 1_AAAGGGC… ../d… SI-GA-H1… SI-GA… AAAGGG… 1     BCB1…  0.0110   -0.0998 S    
##  7 1_AAAGGGC… ../d… SI-GA-H1… SI-GA… AAAGGG… 1     BCB1…  0.0341   -0.143  S    
##  8 1_AAAGTCC… ../d… SI-GA-H1… SI-GA… AAAGTC… 1     BCB1…  0.0425   -0.183  S    
##  9 1_AAAGTCC… ../d… SI-GA-H1… SI-GA… AAAGTC… 1     BCB1… -0.0289   -0.127  G1   
## 10 1_AAAGTGA… ../d… SI-GA-H1… SI-GA… AAAGTG… 1     BCB1… -0.0551   -0.102  G1   
## # ℹ 33,205 more rows
## # ℹ 16 more variables: curated_cell_type <chr>, 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>

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

  filter(gate == 1)