Just as a quick example, we show how to filter cells based on sample size (number of cells) and UMAP location. You can imagine a similar example with more meaningful criteria.

tidyseurat

    library(dplyr)

    ## Warning: package 'dplyr' was built under R version 4.0.3

    ##
    ## Attaching package: 'dplyr'

    ## The following objects are masked from 'package:stats':
    ##
    ##     filter, lag

    ## The following objects are masked from 'package:base':
    ##
    ##     intersect, setdiff, setequal, union

    library(Seurat)

    ## Warning: package 'Seurat' was built under R version 4.0.3

    ## Attaching SeuratObject

    library(tidyseurat)

    ## Registered S3 method overwritten by 'cli':
    ##   method     from
    ##   print.boxx spatstat

    ##
    ## Attaching package: 'tidyseurat'

    ## The following object is masked from 'package:SeuratObject':
    ##
    ##     pbmc_small

    ## The following objects are masked from 'package:dplyr':
    ##
    ##     add_count, bind_cols, bind_rows, count

    ## The following object is masked from 'package:stats':
    ##
    ##     filter

    tidy_object = pbmc_small %>% tidy()

    ## Warning: `tidy()` was deprecated in tidyseurat 0.2.0.
    ## tidyseurat says: tidy() is not needed anymore.

    seurat_object = pbmc_small

    tidy_object %>%
      add_count(file) %>%
      filter(PC_1 > 0 & n > 40)

    ## Warning: The `.drop` argument of `add_count()` is deprecated as of dplyr 1.0.0.

    ## # A Seurat-tibble abstraction: 5 x 17
    ## [90m# Transcripts=230 | Active assay=RNA | Assays=RNA[39m
    ##   cell  orig.ident nCount_RNA nFeature_RNA RNA_snn_res.0.8 letter.idents groups
    ##   <chr> <fct>           <dbl>        <int> <fct>           <fct>         <chr>
    ## 1 GGCA… SeuratPro…        172           29 0               A             g1
    ## 2 TTAC… SeuratPro…        228           39 0               A             g1
    ## 3 GAGT… SeuratPro…        527           47 0               A             g1
    ## 4 AGTC… SeuratPro…        157           29 0               A             g1
    ## 5 CTTG… SeuratPro…        233           76 1               B             g1
    ## # … with 10 more variables: RNA_snn_res.1 <fct>, file <chr>, n <int>,
    ## #   PC_1 <dbl>, PC_2 <dbl>, PC_3 <dbl>, PC_4 <dbl>, PC_5 <dbl>, tSNE_1 <dbl>,
    ## #   tSNE_2 <dbl>

Seurat

    pca_emb = Embeddings(object = seurat_object, reduction = "pca")
    cell_pca = rownames(pca_emb[pca_emb[,1]>0,])
    n =
      seurat_object@meta.data %>%
      add_count(file) %>%
      pull(n)
    seurat_object = AddMetaData( object = seurat_object, metadata = n, col.name = 'n')
    seurat_object %>%
      subset(cells = cell_pca) %>%
      subset( n < 40)

    ## # A Seurat-tibble abstraction: 5 x 17
    ## [90m# Transcripts=230 | Active assay=RNA | Assays=RNA[39m
    ##   cell  orig.ident nCount_RNA nFeature_RNA RNA_snn_res.0.8 letter.idents groups
    ##   <chr> <fct>           <dbl>        <int> <fct>           <fct>         <chr>
    ## 1 ATTC… SeuratPro…        212           38 0               A             g2
    ## 2 ATCA… SeuratPro…        168           37 0               A             g2
    ## 3 GTCA… SeuratPro…        210           33 0               A             g2
    ## 4 GACG… SeuratPro…        202           30 0               A             g2
    ## 5 GGAA… SeuratPro…        150           30 0               A             g2
    ## # … with 10 more variables: RNA_snn_res.1 <fct>, file <chr>, n <int>,
    ## #   PC_1 <dbl>, PC_2 <dbl>, PC_3 <dbl>, PC_4 <dbl>, PC_5 <dbl>, tSNE_1 <dbl>,
    ## #   tSNE_2 <dbl>