---
title: "Example Workflow for Single Cell Annotation Using CellMarker2.0"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{example-single-cell-annotation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---


```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

You can view an example script for this workflow by running the following command

```r
file.show(system.file(package = 'easybio', 'example-single-cell.R'))
```


The example marker data from pbmc3k datasets:

```{r}
library(easybio)
head(pbmc.markers)
```

```{r}
(marker <- matchCellMarker2(marker = pbmc.markers, n = 50, spc = 'Human')[, head(.SD, 2), by=cluster])
```

```{r, fig.width=7.5, fig.height=6}
plotPossibleCell(marker)
```

Explanation:

- The function matches the top 50 genes in each cluster with the CellMarker2 database.

- It then calculates the cell count for each matched cell type and prints the result, helping you identify possible cell types for the cluster.

To annotate, you can simply use the top-matched cell type:

```{r}
cl2cell <- marker[, head(.SD, 1), by = .(cluster)]
cl2cell <- setNames(cl2cell[["cell_name"]], cl2cell[["cluster"]])
cl2cell
```

Visualize marker dot plots for similar clusters:

```{r,eval=FALSE}
cls <- list(
  c(1, 5, 7), 
  c(8),
  c(3),
  c(0,2, 4, 6)
)
dotplotList <- plotSeuratDot(seuratObject, cls, marker = pbmc.markers, n = 50, spc = 'Human', topcellN = 2)
```

Explanation:

- This function searches for potential cell markers from the result of `matchCellMarker2`.

- It then uses `Seurat::DotPlot` to generate corresponding dot plots for similar clusters.

Construct a named vector for  annotation:

```{r}
cl2cell <- finsert(
  expression(
  c(1, 5) == "Monocyte",
  c(7) == "DC",
  c(8) == "megakaryocyte",
  c(3) == "B.cell",
  c(0, 2) == "Naive.CD8.T.cell",
  c(4) == "Cytotoxic.T.Cell",
  c(6) == "Natural.killer.cell",
), len = 9)
cl2cell
```

You can also directly retrieve markers:

```{r}
get_marker(spc = 'Human', cell = c('Monocyte', 'Neutrophil'), number = 5, min.count = 1)
```

or Check the  distribution of the marker directly:

```{r, fig.width=7.5, fig.height=7}
plotMarkerDistribution(mkr = "CD68")
```
