% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/trainSingleR.R
\name{trainSingleR}
\alias{trainSingleR}
\title{Train the SingleR classifier}
\usage{
trainSingleR(
  ref,
  labels,
  test.genes = NULL,
  genes = "de",
  sd.thresh = NULL,
  de.method = c("classic", "wilcox", "t"),
  de.n = NULL,
  de.args = list(),
  aggr.ref = FALSE,
  aggr.args = list(),
  recompute = TRUE,
  restrict = NULL,
  assay.type = "logcounts",
  check.missing = TRUE,
  hint.sce = TRUE,
  approximate = FALSE,
  num.threads = 1,
  BNPARAM = NULL,
  BPPARAM = NULL
)
}
\arguments{
\item{ref}{A numeric matrix of expression values where rows are genes and columns are reference samples (individual cells or bulk samples).
Each row should be named with the gene name.
In general, the expression values are expected to be normalized and log-transformed, see Details.

Alternatively, a \link[SummarizedExperiment]{SummarizedExperiment} object containing such a matrix.

Alternatively, a list or \link[S4Vectors]{List} of SummarizedExperiment objects or numeric matrices containing multiple references.}

\item{labels}{A character vector or factor of known labels for all samples in \code{ref}.

Alternatively, if \code{ref} is a list, \code{labels} should be a list of the same length.
Each element should contain a character vector or factor specifying the labels for the columns of the corresponding element of \code{ref}.}

\item{test.genes}{Character vector of the names of the genes in the test dataset, i.e., the row names of \code{test} in \code{\link{classifySingleR}}.
If \code{NULL}, it is assumed that the test dataset and \code{ref} have the same genes in the same row order.}

\item{genes}{A string containing \code{"de"}, indicating that markers should be calculated from \code{ref}.
For back compatibility, other string values are allowed but will be ignored with a deprecation warning.

Alternatively, if \code{ref} is \emph{not} a list, \code{genes} can be either:
\itemize{
\item A list of lists of character vectors containing DE genes between pairs of labels.
\item A list of character vectors containing marker genes for each label.
}

If \code{ref} \emph{is} a list, \code{genes} can be a list of length equal to \code{ref}.
Each element of the list should be one of the two above choices described for non-list \code{ref},
containing markers for labels in the corresponding entry of \code{ref}.}

\item{sd.thresh}{Deprecated and ignored.}

\item{de.method}{String specifying how DE genes should be detected between pairs of labels.
Defaults to \code{"classic"}, which sorts genes by the log-fold changes and takes the top \code{de.n}.
Other options are \code{"wilcox"} and \code{"t"}, see Details.
Ignored if \code{genes} is a list of markers/DE genes.}

\item{de.n}{An integer scalar specifying the number of DE genes to use when \code{genes="de"}.
If \code{de.method="classic"}, defaults to \code{500 * (2/3) ^ log2(N)} where \code{N} is the number of unique labels.
Otherwise, defaults to 10.
Ignored if \code{genes} is a list of markers/DE genes.}

\item{de.args}{Named list of additional arguments to pass to \code{\link[scrapper]{scoreMarkers}} when \code{de.method="wilcox"} or \code{"t"},
or to \code{\link{getClassicMarkers}} when \code{de.method="classic"}.
Ignored if \code{genes} is a list of markers/DE genes.}

\item{aggr.ref}{Logical scalar indicating whether references should be aggregated to pseudo-bulk samples for speed, see \code{\link{aggregateReference}}.}

\item{aggr.args}{Further arguments to pass to \code{\link{aggregateReference}} when \code{aggr.ref=TRUE}.}

\item{recompute}{Deprecated and ignored.}

\item{restrict}{A character vector of gene names to use for marker selection.
By default, all genes in \code{ref} are used.}

\item{assay.type}{An integer scalar or string specifying the assay of \code{ref} containing the relevant expression matrix,
if \code{ref} is a \link[SummarizedExperiment]{SummarizedExperiment} object (or is a list that contains one or more such objects).}

\item{check.missing}{Logical scalar indicating whether rows should be checked for missing values.
If true and any missing values are found, the rows containing these values are silently removed.}

\item{hint.sce}{Boolean indicating whether to print a hint to change \code{de.method=} when any entry of \code{ref} is a \link[SingleCellExperiment]{SingleCellExperiment}.
It will also suggest setting \code{aggr.ref=TRUE} for greater efficiency when \code{ref} contains 1000 cells or more.}

\item{approximate}{Deprecated, use \code{BNPARAM} instead.}

\item{num.threads}{Integer scalar specifying the number of threads to use for index building.}

\item{BNPARAM}{Deprecated and ignored.}

\item{BPPARAM}{Deprecated, use \code{num.threads} instead.}
}
\value{
For a single reference, a \link[S4Vectors]{List} is returned containing:
\describe{
\item{\code{built}:}{An external pointer to various indices in C++ space.
Note that this cannot be serialized and should be removed prior to any \code{\link{saveRDS}} step.}
\item{\code{ref}:}{The reference expression matrix.
This may have fewer columns than the input \code{ref} if \code{aggr.ref = TRUE}.}
\item{\code{markers}:}{A list containing \code{unique}, a character vector of all marker genes used in training;
and \code{full}, a list of list of character vectors containing the markers for each pairwise comparison between labels.}
\item{\code{labels}:}{A list containing \code{unique}, a character vector of all unique reference labels;
and \code{full}, a character vector containing the assigned label for each column in \code{ref}.}
}

For multiple references, a List of Lists is returned where each internal List corresponds to a reference in \code{ref} and has the same structure as described above.
}
\description{
Train the SingleR classifier on one or more reference datasets with known labels.
}
\details{
This function uses a training data set to select interesting features and construct nearest neighbor indices in rank space.
The resulting objects can be re-used across multiple classification steps with different test data sets via \code{\link{classifySingleR}}.
This improves efficiency by avoiding unnecessary repetition of steps during the downstream analysis.

The automatic marker detection (\code{genes="de"}) identifies genes that are differentially expressed between pairs of labels in the reference dataset.
The expression values are expected to be log-transformed and normalized.
For each pair of labels, the top \code{de.n} genes with strongest upregulation in one label are chosen as markers to distinguish it from the other label.
The exact ranking depends on the \code{de.method=} argument:
\itemize{
\item The default \code{de.method="classic"} will use \code{\link{getClassicMarkers}} to compute the median expression for each label and each gene.
Then, for each pair of labels, the top \code{de.n} genes with the largest positive differences are chosen as markers to distinguish the first label from the second.
This is intended for reference datasets derived from bulk transcriptomic data (e.g., microarrays) with a high density of non-zero values. 
It is less effective for single-cell data, where it is not uncommon to have more than 50\% zero counts for a given gene such that the median is also zero for each group.
\item \code{de.method="wilcox"} will rank genes based on the area under the curve (AUC) in each pairwise comparison between labels.
The top \code{de.n} genes with the largest AUCs above 0.5 are chosen as markers for the first label compared to the second.
This is analogous to ranking on significance in the Wilcoxon ranked sum test and is intended for use with single-cell data.
The exact calculaton is performed using the \code{\link[scrapper]{scoreMarkers}} function.
\item \code{de.method="t"} will rank genes on the Cohen's d in each pairiwse comparison.
The top \code{de.n} genes with the largest positive Cohen's d are chosen as markers for the first label compared to the second.
This is roughly analogous to ranking on significance in the t-test and is faster than the AUC.
The exact calculaton is performed using the \code{\link[scrapper]{scoreMarkers}} function.
}
Alternatively, users can detect markers externally and pass a list of markers to \code{genes} (see \dQuote{Custom gene specification}).

Classification with \code{classifySingleR} assumes that the test dataset contains all marker genes that were detected from the reference.
If the test and reference datasets do not have the same genes in the same order, we can set \code{test.genes} to the row names of the test dataset.
This will instruct \code{trainSingleR} to only consider markers that are present in the test dataset.
Any subsequent call to \code{classifySingleR} will also check that \code{test.genes} is consistent with \code{rownames(test)}.

On a similar note, if \code{restrict} is specified, marker selection will only be performed using the specified subset of genes.
This can be convenient for ignoring inappropriate genes like pseudogenes or predicted genes.
It has the same effect as filtering out undesirable rows from \code{ref} prior to calling \code{trainSingleR}.
Unlike \code{test.genes}, setting \code{restrict} does not introduce further checks on \code{rownames(test)} in \code{classifySingleR}.
}
\section{Custom gene specification}{

Rather than relying on the in-built feature selection, users can pass in their own features of interest to \code{genes}.
The function expects a named list of named lists of character vectors, with each vector containing the DE genes between a pair of labels.
For example:
\preformatted{genes <- list(
   A = list(A = character(0), B = "GENE_1", C = c("GENE_2", "GENE_3")),
   B = list(A = "GENE_100", B = character(0), C = "GENE_200"),
   C = list(A = c("GENE_4", "GENE_5"), B = "GENE_5", C = character(0))
)
}
If we consider the entry \code{genes$A$B}, this contains marker genes for label \code{"A"} against label \code{"B"}.
That is, these genes are upregulated in \code{"A"} compared to \code{"B"}.
The outer list should have one list per label, and each inner list should have one character vector per label.
(Obviously, a label cannot have markers against itself, so this is just set to \code{character(0)}.)

Alternatively, \code{genes} can be a named list of character vectors containing per-label markers.
For example:
\preformatted{genes <- list(
     A = c("GENE_1", "GENE_2", "GENE_3"),
     B = c("GENE_100", "GENE_200"),
     C = c("GENE_4", "GENE_5")
)
}
The entry \code{genes$A} represent the genes that are upregulated in \code{A} compared to some or all other labels. 
This allows the function to handle pre-defined marker lists for specific cell populations.
However, it obviously captures less information than marker sets for the pairwise comparisons.

If \code{genes} is manually passed, \code{ref} can contain the raw counts or any monotonic transformation thereof.
There is no need to supply (log-)normalized expression values for the benefit of the automatic marker detection.
Similarly, for manual \code{genes}, the values of \code{de.method}, \code{de.n} and \code{sd.thresh} have no effect.

Check out the Examples to see how manual \code{genes} can be passed to \code{trainSingleR}.
}

\section{Dealing with multiple references}{

The default \pkg{SingleR} policy for dealing with multiple references is to perform the classification for each reference separately and combine the results 
(see \code{?\link{combineRecomputedResults}} for an explanation).
To this end, if \code{ref} is a list with multiple references, marker genes are identified separately within each reference if \code{genes = NULL}.
Rank calculation and index construction is then performed within each reference separately.
The result is identical to \code{lapply}ing over a list of references and runing \code{trainSingleR} on each reference.

Alternatively, \code{genes} can still be used to explicitly specify marker genes for each label in each of multiple references.
This is achieved by passing a list of lists to \code{genes},
where each inner list corresponds to a reference in \code{ref} and can be of any format described in \dQuote{Custom feature specification}.
Thus, it is possible for \code{genes} to be - wait for it - a list (per reference) of lists (per label) of lists (per label) of character vectors.
}

\section{Aggregating single-cell references}{

It is generally unnecessary to have single-cell resolution on the reference profiles.
We can instead set \code{aggr.ref=TRUE} to aggregate per-cell references into a set of pseudo-bulk profiles using \code{\link{aggregateReference}}.
This improves classification speed while using vector quantization to preserve within-label heterogeneity and mitigate the loss of information.
Note that any aggregation is done \emph{after} marker gene detection; this ensures that the relevant tests can appropriately penalize within-label variation.
Users should also be sure to set the seed as the aggregation involves randomization.
}

\examples{
# Making up some data for a quick demonstration.
ref <- .mockRefData()

# Normalizing and log-transforming for automated marker detection.
ref <- scrapper::normalizeRnaCounts.se(ref)

trained <- trainSingleR(ref, ref$label)
trained
length(trained$markers$unique)

# Alternatively, supplying a custom set of markers from pairwise comparisons.
all.labels <- unique(ref$label)
custom.markers <- list()
for (x in all.labels) {
    current.markers <- lapply(all.labels, function(x) sample(rownames(ref), 20))
    names(current.markers) <- all.labels
    current.markers[[x]] <- character(0)
    custom.markers[[x]] <- current.markers
}
custom.trained <- trainSingleR(ref, ref$label, genes=custom.markers)

# Alternatively, supplying a custom set of markers for each label.
custom.markers <- list()
for (x in all.labels) {
    custom.markers[[x]] <- sample(rownames(ref), 20)
}
custom.trained <- trainSingleR(ref, ref$label, genes=custom.markers)

}
\seealso{
\code{\link{classifySingleR}}, where the output of this function gets used.

\code{\link{combineRecomputedResults}}, to combine results from multiple references.

\code{\link{rebuildIndex}}, to rebuild the index after external memory is invalidated.
}
\author{
Aaron Lun, based on the original \code{SingleR} code by Dvir Aran.
}
