\HeaderA{pairwiseAlignment}{Optimal Pairwise Alignment}{pairwiseAlignment}
\aliasA{pairwiseAlignment,character,character-method}{pairwiseAlignment}{pairwiseAlignment,character,character.Rdash.method}
\aliasA{pairwiseAlignment,character,QualityScaledXStringSet-method}{pairwiseAlignment}{pairwiseAlignment,character,QualityScaledXStringSet.Rdash.method}
\aliasA{pairwiseAlignment,character,XString-method}{pairwiseAlignment}{pairwiseAlignment,character,XString.Rdash.method}
\aliasA{pairwiseAlignment,character,XStringSet-method}{pairwiseAlignment}{pairwiseAlignment,character,XStringSet.Rdash.method}
\aliasA{pairwiseAlignment,QualityScaledXStringSet,character-method}{pairwiseAlignment}{pairwiseAlignment,QualityScaledXStringSet,character.Rdash.method}
\aliasA{pairwiseAlignment,QualityScaledXStringSet,QualityScaledXStringSet-method}{pairwiseAlignment}{pairwiseAlignment,QualityScaledXStringSet,QualityScaledXStringSet.Rdash.method}
\aliasA{pairwiseAlignment,QualityScaledXStringSet,XString-method}{pairwiseAlignment}{pairwiseAlignment,QualityScaledXStringSet,XString.Rdash.method}
\aliasA{pairwiseAlignment,QualityScaledXStringSet,XStringSet-method}{pairwiseAlignment}{pairwiseAlignment,QualityScaledXStringSet,XStringSet.Rdash.method}
\aliasA{pairwiseAlignment,XString,character-method}{pairwiseAlignment}{pairwiseAlignment,XString,character.Rdash.method}
\aliasA{pairwiseAlignment,XString,QualityScaledXStringSet-method}{pairwiseAlignment}{pairwiseAlignment,XString,QualityScaledXStringSet.Rdash.method}
\aliasA{pairwiseAlignment,XString,XString-method}{pairwiseAlignment}{pairwiseAlignment,XString,XString.Rdash.method}
\aliasA{pairwiseAlignment,XString,XStringSet-method}{pairwiseAlignment}{pairwiseAlignment,XString,XStringSet.Rdash.method}
\aliasA{pairwiseAlignment,XStringSet,character-method}{pairwiseAlignment}{pairwiseAlignment,XStringSet,character.Rdash.method}
\aliasA{pairwiseAlignment,XStringSet,QualityScaledXStringSet-method}{pairwiseAlignment}{pairwiseAlignment,XStringSet,QualityScaledXStringSet.Rdash.method}
\aliasA{pairwiseAlignment,XStringSet,XString-method}{pairwiseAlignment}{pairwiseAlignment,XStringSet,XString.Rdash.method}
\aliasA{pairwiseAlignment,XStringSet,XStringSet-method}{pairwiseAlignment}{pairwiseAlignment,XStringSet,XStringSet.Rdash.method}
\keyword{models}{pairwiseAlignment}
\keyword{methods}{pairwiseAlignment}
\begin{Description}\relax
Solves (Needleman-Wunsch) global alignment, (Smith-Waterman) local alignment,
and (ends-free) overlap alignment problems.
\end{Description}
\begin{Usage}
\begin{verbatim}
pairwiseAlignment(pattern, subject, ...)
## S4 method for signature 'XStringSet, XStringSet':
pairwiseAlignment(pattern, subject,
                  patternQuality = PhredQuality(22L), subjectQuality = PhredQuality(22L),
                  type = "global", substitutionMatrix = NULL, fuzzyMatrix = NULL,
                  gapOpening = -10, gapExtension = -4, scoreOnly = FALSE)
## S4 method for signature 'QualityScaledXStringSet,
##   QualityScaledXStringSet':
pairwiseAlignment(pattern, subject,
                  type = "global", substitutionMatrix = NULL, fuzzyMatrix = NULL, 
                  gapOpening = -10, gapExtension = -4, scoreOnly = FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{pattern}] a character vector of any length, an \code{\LinkA{XString}{XString}}, or
an \code{\LinkA{XStringSet}{XStringSet}} object.
\item[\code{subject}] a character vector of length 1 or an \code{\LinkA{XString}{XString}}
object.
\item[\code{patternQuality, subjectQuality}] objects of class
\code{\LinkA{XStringQuality}{XStringQuality}} representing the respective quality scores for
\code{pattern} and \code{subject} that are used in a quality-based method
for generating a substitution matrix. These two arguments are ignored if
\code{!is.null(substitutionMatrix)} or if its respective string set
(\code{pattern}, \code{subject}) is of class
\code{\LinkA{QualityScaledXStringSet}{QualityScaledXStringSet}}.
\item[\code{type}] type of alignment. One of \code{"global"}, \code{"local"},
\code{"overlap"}, \code{"global-local"}, and \code{"local-global"} where
\code{"global"} = align whole strings with end gap penalties,
\code{"local"} = align string fragments,
\code{"overlap"} = align whole strings without end gap penalties,
\code{"global-local"} = align whole strings with end gap penalties on
\code{pattern} and without end gap penalties on \code{subject}
\code{"local-global"} = align whole strings without end gap penalties on
\code{pattern} and with end gap penalties on \code{subject}.
\item[\code{substitutionMatrix}] substitution matrix representing the fixed
substitution scores for an alignment. It cannot be used in conjunction with
\code{patternQuality} and \code{subjectQuality} arguments.
\item[\code{fuzzyMatrix}] fuzzy match matrix for quality-based alignments. It takes
values between 0 and 1; where 0 is an unambiguous mismatch, 1 is an
unambiguous match, and values in between represent a fraction of
"matchiness". (See details section below.)
\item[\code{gapOpening}] the cost for opening a gap in the alignment.
\item[\code{gapExtension}] the incremental cost incurred along the length of the gap
in the alignment.
\item[\code{scoreOnly}] logical to denote whether or not to return just the scores of
the optimal pairwise alignment.
\item[\code{...}] optional arguments to generic function to support additional
methods.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
Quality-based alignments are based on the paper the Bioinformatics article by
Ketil Malde listed in the Reference section below. Let \eqn{\epsilon_i}{} be the
probability of an error in the base read. For \code{"Phred"} quality measures
\eqn{Q}{} in \eqn{[0, 99]}{}, these error probabilities are given by
\eqn{\epsilon_i = 10^{-Q/10}}{}. For \code{"Solexa"} quality measures \eqn{Q}{} in
\eqn{[-5, 99]}{}, they are given by \eqn{\epsilon_i = 1 - 1/(1 + 10^{-Q/10})}{}.
Assuming independence within and between base reads, the combined error
probability of a mismatch when the underlying bases do match is
\eqn{\epsilon_c = \epsilon_1 + \epsilon_2 - (n/(n-1)) * \epsilon_1 * \epsilon_2}{},
where \eqn{n}{} is the number of letters in the underlying alphabet. Using
\eqn{\epsilon_c}{}, the substitution score is given by when two bases match is given by
\eqn{b * \log_2(\gamma_{x,y} * (1 - \epsilon_c) * n + (1 - \gamma_{x,y}) * \epsilon_c * (n/(n-1)))}{},
where \eqn{b}{} is the bit-scaling for the scoring and \eqn{\gamma_{x,y}}{} is the
probability that characters \eqn{x}{} and \eqn{y}{} represents the same underlying
information (e.g. using IUPAC, \eqn{\gamma_{A,A} = 1}{} and \eqn{\gamma_{A,N} = 1/4}{}.
In the arguments listed above \code{fuzzyMatch} represents \eqn{\gamma_{x,y}}{}
and \code{patternQuality} and \code{subjectQuality} represents \eqn{\epsilon_1}{}
and \eqn{\epsilon_2}{} respectively.

If \code{scoreOnly == FALSE}, a pairwise alignment with the maximum alignment
score is returned. If more than one pairwise alignment produces the maximum
alignment score, then the alignment with the smallest initial deletion whose
mismatches occur before its insertions and deletions is chosen. For example,
if \code{pattern = "AGTA"} and \code{subject = "AACTAACTA"}, then the alignment
\code{pattern: [1] AG-TA; subject: [1] AACTA} is chosen over
\code{pattern: [1] A-GTA; subject: [1] AACTA} or
\code{pattern: [1] AG-TA; subject: [5] AACTA} if they all achieve the maximum
alignment score.
\end{Details}
\begin{Value}
If \code{scoreOnly == FALSE}, an instance of class
\code{\LinkA{PairwiseAlignedXStringSet}{PairwiseAlignedXStringSet}} or
\code{\LinkA{PairwiseAlignedFixedSubject}{PairwiseAlignedFixedSubject}} is returned.
If \code{scoreOnly == TRUE}, a numeric vector containing the scores for the
optimal pairwise alignments is returned.
\end{Value}
\begin{Note}\relax
Use \code{\LinkA{matchPattern}{matchPattern}} or \code{\LinkA{vmatchPattern}{vmatchPattern}} if you need to
find all the occurrences (eventually with indels) of a given pattern in a
reference sequence or set of sequences.

Use \code{\LinkA{matchPDict}{matchPDict}} if you need to match a (big) set of patterns
against a reference sequence.
\end{Note}
\begin{Author}\relax
P. Aboyoun and H. Pages
\end{Author}
\begin{References}\relax
R. Durbin, S. Eddy, A. Krogh, G. Mitchison, Biological Sequence Analysis, Cambridge UP 1998, sec 2.3.

B. Haubold, T. Wiehe, Introduction to Computational Biology, Birkhauser Verlag 2006, Chapter 2.

K. Malde, The effect of sequence quality on sequence alignment, Bioinformatics 2008 24(7):897-900.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{stringDist}{stringDist}},
\LinkA{PairwiseAlignedXStringSet-class}{PairwiseAlignedXStringSet.Rdash.class},
\LinkA{XStringQuality-class}{XStringQuality.Rdash.class},
\LinkA{substitution.matrices}{substitution.matrices},
\code{\LinkA{matchPattern}{matchPattern}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
  ## Nucleotide global, local, and overlap alignments
  s1 <- 
    DNAString("ACTTCACCAGCTCCCTGGCGGTAAGTTGATCAAAGGAAACGCAAAGTTTTCAAG")
  s2 <-
    DNAString("GTTTCACTACTTCCTTTCGGGTAAGTAAATATATAAATATATAAAAATATAATTTTCATC")

  # First use a fixed substitution matrix
  mat <- nucleotideSubstitutionMatrix(match = 1, mismatch = -3, baseOnly = TRUE)
  globalAlign <-
    pairwiseAlignment(s1, s2, substitutionMatrix = mat,
                      gapOpening = -5, gapExtension = -2)
  localAlign <-
    pairwiseAlignment(s1, s2, type = "local", substitutionMatrix = mat,
                      gapOpening = -5, gapExtension = -2)
  overlapAlign <-
    pairwiseAlignment(s1, s2, type = "overlap", substitutionMatrix = mat,
                      gapOpening = -5, gapExtension = -2)

  # Then use quality-based method for generating a substitution matrix
  pairwiseAlignment(s1, s2,
                    patternQuality = SolexaQuality(rep(c(22L, 12L), times = c(36, 18))),
                    subjectQuality = SolexaQuality(rep(c(22L, 12L), times = c(40, 20))),
                    scoreOnly = TRUE)

  # Now assume can't distinguish between C/T and G/A
  pairwiseAlignment(s1, s2,
                    patternQuality = SolexaQuality(rep(c(22L, 12L), times = c(36, 18))),
                    subjectQuality = SolexaQuality(rep(c(22L, 12L), times = c(40, 20))),
                    type = "local")
  mapping <- diag(4)
  dimnames(mapping) <- list(DNA_BASES, DNA_BASES)
  mapping["C", "T"] <- mapping["T", "C"] <- 1
  mapping["G", "A"] <- mapping["A", "G"] <- 1
  pairwiseAlignment(s1, s2,
                    patternQuality = SolexaQuality(rep(c(22L, 12L), times = c(36, 18))),
                    subjectQuality = SolexaQuality(rep(c(22L, 12L), times = c(40, 20))),
                    fuzzyMatrix = mapping,
                    type = "local")

  ## Amino acid global alignment
  pairwiseAlignment(AAString("PAWHEAE"), AAString("HEAGAWGHEE"),
                    substitutionMatrix = "BLOSUM50",
                    gapOpening = 0, gapExtension = -8)
\end{ExampleCode}
\end{Examples}

