\HeaderA{matchPattern}{String searching functions}{matchPattern}
\aliasA{countPattern}{matchPattern}{countPattern}
\aliasA{countPattern,character-method}{matchPattern}{countPattern,character.Rdash.method}
\aliasA{countPattern,MaskedXString-method}{matchPattern}{countPattern,MaskedXString.Rdash.method}
\aliasA{countPattern,XString-method}{matchPattern}{countPattern,XString.Rdash.method}
\aliasA{countPattern,XStringSet-method}{matchPattern}{countPattern,XStringSet.Rdash.method}
\aliasA{countPattern,XStringViews-method}{matchPattern}{countPattern,XStringViews.Rdash.method}
\aliasA{matchDNAPattern}{matchPattern}{matchDNAPattern}
\aliasA{matchPattern,character-method}{matchPattern}{matchPattern,character.Rdash.method}
\aliasA{matchPattern,MaskedXString-method}{matchPattern}{matchPattern,MaskedXString.Rdash.method}
\aliasA{matchPattern,XString-method}{matchPattern}{matchPattern,XString.Rdash.method}
\aliasA{matchPattern,XStringSet-method}{matchPattern}{matchPattern,XStringSet.Rdash.method}
\aliasA{matchPattern,XStringViews-method}{matchPattern}{matchPattern,XStringViews.Rdash.method}
\aliasA{vcountPattern}{matchPattern}{vcountPattern}
\aliasA{vcountPattern,character-method}{matchPattern}{vcountPattern,character.Rdash.method}
\aliasA{vcountPattern,MaskedXString-method}{matchPattern}{vcountPattern,MaskedXString.Rdash.method}
\aliasA{vcountPattern,XString-method}{matchPattern}{vcountPattern,XString.Rdash.method}
\aliasA{vcountPattern,XStringSet-method}{matchPattern}{vcountPattern,XStringSet.Rdash.method}
\aliasA{vcountPattern,XStringViews-method}{matchPattern}{vcountPattern,XStringViews.Rdash.method}
\aliasA{vmatchPattern}{matchPattern}{vmatchPattern}
\aliasA{vmatchPattern,character-method}{matchPattern}{vmatchPattern,character.Rdash.method}
\aliasA{vmatchPattern,MaskedXString-method}{matchPattern}{vmatchPattern,MaskedXString.Rdash.method}
\aliasA{vmatchPattern,XString-method}{matchPattern}{vmatchPattern,XString.Rdash.method}
\aliasA{vmatchPattern,XStringSet-method}{matchPattern}{vmatchPattern,XStringSet.Rdash.method}
\aliasA{vmatchPattern,XStringViews-method}{matchPattern}{vmatchPattern,XStringViews.Rdash.method}
\keyword{methods}{matchPattern}
\begin{Description}\relax
A set of functions for finding all the occurrences (aka "matches" or "hits")
of a given pattern (typically short) in a (typically long) reference sequence
or set of reference sequences (aka the subject)
\end{Description}
\begin{Usage}
\begin{verbatim}
  matchPattern(pattern, subject,
               max.mismatch=0, min.mismatch=0, with.indels=FALSE, fixed=TRUE,
               algorithm="auto")
  countPattern(pattern, subject,
               max.mismatch=0, min.mismatch=0, with.indels=FALSE, fixed=TRUE,
               algorithm="auto")
  vmatchPattern(pattern, subject,
                max.mismatch=0, min.mismatch=0, with.indels=FALSE, fixed=TRUE,
                algorithm="auto", ...)
  vcountPattern(pattern, subject,
                max.mismatch=0, min.mismatch=0, with.indels=FALSE, fixed=TRUE,
                algorithm="auto", ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{pattern}] The pattern string.

\item[\code{subject}] An \LinkA{XString}{XString}, \LinkA{XStringViews}{XStringViews} or \LinkA{MaskedXString}{MaskedXString}
object for \code{matchPattern} and \code{countPattern}.

An \LinkA{XStringSet}{XStringSet} or \LinkA{XStringViews}{XStringViews} object for
\code{vmatchPattern} and \code{vcountPattern}.

\item[\code{max.mismatch, min.mismatch}] The maximum and minimum number of mismatching letters allowed (see
\code{?`\LinkA{lowlevel-matching}{lowlevel.Rdash.matching}`} for the details).
If non-zero, an algorithm that supports inexact matching is used.

\item[\code{with.indels}] If \code{TRUE} then indels are allowed. In that case, \code{min.mismatch}
must be \code{0} and \code{max.mismatch} is interpreted as the maximum
"edit distance" allowed between the pattern and a match.
Note that in order to avoid pollution by redundant matches,
only the "best local matches" are returned.
Roughly speaking, a "best local match" is a match that is locally
both the closest (to the pattern P) and the shortest.
More precisely, a substring S' of the subject S is a "best local match" iff:
\begin{alltt}
       (a) nedit(P, S') <= max.mismatch
       (b) for every substring S1 of S':
               nedit(P, S1) > nedit(P, S')
       (c) for every substring S2 of S that contains S':
               nedit(P, S2) >= nedit(P, S')
\end{alltt}
One nice property of "best local matches" is that their first and last
letters are guaranteed to be aligned with letters in P (i.e. they match
letters in P).

\item[\code{fixed}] If \code{TRUE} (the default), an IUPAC ambiguity code in the pattern
can only match the same code in the subject, and vice versa.
If \code{FALSE}, an IUPAC ambiguity code in the pattern can match
any letter in the subject that is associated with the code, and 
vice versa. See \code{?`\LinkA{lowlevel-matching}{lowlevel.Rdash.matching}`} for more information.

\item[\code{algorithm}] One of the following: \code{"auto"}, \code{"naive-exact"},
\code{"naive-inexact"}, \code{"boyer-moore"}, \code{"shift-or"}
or \code{"indels"}.

\item[\code{...}] Additional arguments for methods.

\end{ldescription}
\end{Arguments}
\begin{Details}\relax
Available algorithms are: ``naive exact'', ``naive inexact'',
``Boyer-Moore-like'', ``shift-or'' and ``indels''.
Not all of them can be used in all situations: restrictions
apply depending on the "search criteria" i.e. on the values of
the \code{pattern}, \code{subject}, \code{max.mismatch},
\code{min.mismatch}, \code{with.indels} and \code{fixed}
arguments.

It is important to note that the \code{algorithm} argument
is not part of the search criteria. This is because the supported
algorithms are interchangeable, that is, if 2 different algorithms
are compatible with a given search criteria, then choosing one or
the other will not affect the result (but will most likely affect
the performance). So there is no "wrong choice" of algorithm (strictly
speaking).

Using \code{algorithm="auto"} (the default) is recommended because
then the best suited algorithm will automatically be selected among
the set of algorithms that are valid for the given search criteria.
\end{Details}
\begin{Value}
An \LinkA{XStringViews}{XStringViews} object for \code{matchPattern}.

A single integer for \code{countPattern}.

An \LinkA{MIndex}{MIndex} object for \code{vmatchPattern}.

An integer vector for \code{vcountPattern}, with each element in
the vector corresponding to the number of matches in the corresponding
element of \code{subject}.
\end{Value}
\begin{Note}\relax
Use \code{\LinkA{matchPDict}{matchPDict}} if you need to match a (big) set of patterns
against a reference sequence.

Use \code{\LinkA{pairwiseAlignment}{pairwiseAlignment}} if you need to solve a (Needleman-Wunsch)
global alignment, a (Smith-Waterman) local alignment, or an (ends-free)
overlap alignment problem.
\end{Note}
\begin{SeeAlso}\relax
\LinkA{lowlevel-matching}{lowlevel.Rdash.matching},
\code{\LinkA{matchPDict}{matchPDict}},
\code{\LinkA{pairwiseAlignment}{pairwiseAlignment}},
\code{\LinkA{mismatch}{mismatch}},
\code{\LinkA{matchLRPatterns}{matchLRPatterns}},
\code{\LinkA{matchProbePair}{matchProbePair}},
\code{\LinkA{maskMotif}{maskMotif}},
\code{\LinkA{alphabetFrequency}{alphabetFrequency}},
\LinkA{XStringViews-class}{XStringViews.Rdash.class},
\LinkA{MIndex-class}{MIndex.Rdash.class}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
  ## ---------------------------------------------------------------------
  ## A. matchPattern()/countPattern()
  ## ---------------------------------------------------------------------

  ## A simple inexact matching example with a short subject:
  x <- DNAString("AAGCGCGATATG")
  m1 <- matchPattern("GCNNNAT", x)
  m1
  m2 <- matchPattern("GCNNNAT", x, fixed=FALSE)
  m2
  as.matrix(m2)

  ## With DNA sequence of yeast chromosome number 1:
  data(yeastSEQCHR1)
  yeast1 <- DNAString(yeastSEQCHR1)
  PpiI <- "GAACNNNNNCTC" # a restriction enzyme pattern
  match1.PpiI <- matchPattern(PpiI, yeast1, fixed=FALSE)
  match2.PpiI <- matchPattern(PpiI, yeast1, max.mismatch=1, fixed=FALSE)

  ## With a genome containing isolated Ns:
  library(BSgenome.Celegans.UCSC.ce2)
  chrII <- Celegans[["chrII"]]
  alphabetFrequency(chrII)
  matchPattern("N", chrII)
  matchPattern("TGGGTGTCTTT", chrII) # no match
  matchPattern("TGGGTGTCTTT", chrII, fixed=FALSE) # 1 match

  ## Using wildcards ("N") in the pattern on a genome containing N-blocks:
  library(BSgenome.Dmelanogaster.UCSC.dm3)
  chrX <- maskMotif(Dmelanogaster$chrX, "N")
  as(chrX, "XStringViews") # 4 non masked regions
  matchPattern("TTTATGNTTGGTA", chrX, fixed=FALSE)
  ## Can also be achieved with no mask:
  masks(chrX) <- NULL
  matchPattern("TTTATGNTTGGTA", chrX, fixed="subject")

  ## ---------------------------------------------------------------------
  ## B. vmatchPattern()/vcountPattern()
  ## ---------------------------------------------------------------------

  Ebox <- DNAString("CANNTG")
  subject <- Celegans$upstream5000
  mindex <- vmatchPattern(Ebox, subject, fixed=FALSE)
  count_index <- countIndex(mindex)  # Get the number of matches per
                                     # subject element.
  sum(count_index)  # Total number of matches.
  table(count_index)
  i0 <- which(count_index == max(count_index))
  subject[i0]  # The subject element with most matches.

  ## The matches in 'subject[i0]' as an IRanges object:
  mindex[[i0]]
  ## The matches in 'subject[i0]' as an XStringViews object:
  Views(subject[[i0]], mindex[[i0]])

  ## ---------------------------------------------------------------------
  ## C. WITH INDELS
  ## ---------------------------------------------------------------------
  library(BSgenome.Celegans.UCSC.ce2)
  pattern <- DNAString("ACGGACCTAATGTTATC")
  subject <- Celegans$chrI

  ## Allowing up to 2 mismatching letters doesn't give any match:
  matchPattern(pattern, subject, max.mismatch=2)

  ## But allowing up to 2 edit operations gives 3 matches:
  system.time(m <- matchPattern(pattern, subject, max.mismatch=2, with.indels=TRUE))
  m

  ## pairwiseAlignment() returns the (first) best match only:
  if (interactive()) {
    mat <- nucleotideSubstitutionMatrix(match=1, mismatch=0, baseOnly=TRUE)
    ## Note that this call to pairwiseAlignment() will need to
    ## allocate 733.5 Mb of memory (i.e. length(pattern) * length(subject)
    ## * 3 bytes).
    system.time(pwa <- pairwiseAlignment(pattern, subject, type="local",
                                         substitutionMatrix=mat,
                                         gapOpening=0, gapExtension=1))
    pwa
  }

  ## Only "best local matches" are reported:
    ## - with deletions in the subject
  subject <- BString("ACDEFxxxCDEFxxxABCE")
  matchPattern("ABCDEF", subject, max.mismatch=2, with.indels=TRUE)
  matchPattern("ABCDEF", subject, max.mismatch=2)
    ## - with insertions in the subject
  subject <- BString("AiBCDiEFxxxABCDiiFxxxAiBCDEFxxxABCiDEF")
  matchPattern("ABCDEF", subject, max.mismatch=2, with.indels=TRUE)
  matchPattern("ABCDEF", subject, max.mismatch=2)
    ## - with substitutions (note that the "best local matches" can introduce
    ##   indels and therefore be shorter than 6)
  subject <- BString("AsCDEFxxxABDCEFxxxBACDEFxxxABCEDF")
  matchPattern("ABCDEF", subject, max.mismatch=2, with.indels=TRUE)
  matchPattern("ABCDEF", subject, max.mismatch=2)
\end{ExampleCode}
\end{Examples}

