---
title: "Introduction to clustermole"
output:
  rmarkdown::html_vignette:
    keep_md: true
vignette: >
  %\VignetteIndexEntry{Introduction to clustermole}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
# reduce the minimum number of characters for the tibble column titles (default: 15)
options(pillar.min_title_chars = 10)
```

## Overview

A typical computational pipeline to process single-cell RNA sequencing (scRNA-seq) data includes clustering of cells as one of the steps.
Assignment of cell type labels to those clusters is often a time-consuming process that involves manual inspection of the cluster marker genes complemented with a detailed literature search.
This is especially challenging when unexpected or poorly described populations are present.
The clustermole R package provides methods to query thousands of human and mouse cell identity markers sourced from a variety of databases.

The clustermole package provides three primary features:

* a database of markers for thousands of cell types (`clustermole_markers`)
* cell type prediction based on marker genes (`clustermole_overlaps`)
* cell type prediction based on the full expression matrix (`clustermole_enrichment`)

## Usage

You can install clustermole from [CRAN](https://cran.r-project.org/package=clustermole).

```{r install-package, eval=FALSE}
install.packages("clustermole")
```

Load clustermole.

```{r load-package, message=FALSE}
library(clustermole)
```

### Retrieve cell type markers

You can use clustermole as a simple database and get a data frame of all cell type markers.

```{r markers}
markers <- clustermole_markers(species = "hs")
markers
```

Each row contains a gene and a cell type associated with it.
The `gene` column is the gene symbol and the `celltype_full` column contains the full cell type string, including the species and the original database. Human or mouse versions can be retrieved.

Some tools require input as a list.
To convert the markers from a data frame to a list format, you can use `gene` as the values and `celltype_full` as the grouping variable.

```{r celltypes-list}
markers_list <- split(x = markers$gene, f = markers$celltype_full)
```

Check the number of cell types in the database.

```{r celltypes-count}
length(unique(markers$celltype_full))
```

Check the cell type source databases.

```{r celltypes-dbs}
sort(unique(markers$db))
```

### Cell types based on marker genes

If you have a character vector of genes, such as cluster markers, you can compare them to known cell type markers to see if they overlap any of the known cell type markers (overrepresentation analysis).

```{r overlaps, eval=FALSE}
my_overlaps <- clustermole_overlaps(genes = my_genes_vec, species = "hs")
```

### Cell types based on expression matrix

If you have expression values, such as average expression across clusters, you can perform cell type enrichment based on a given gene expression matrix (log-transformed CPM/TPM/FPKM values).
The matrix should have genes as rows and clusters/samples as columns.
The underlying enrichment method can be changed using the `method` parameter.

```{r enrichment, eval=FALSE}
my_enrichment <- clustermole_enrichment(expr_mat = my_expr_mat, species = "hs")
```
