Astatistical theory of local alignmentalgorithms with gaps is presented. Both the linear and logarithmic phases, as well as the phase transition separating the two phases, are described in a quantitative way. Markovsequences without mutual correlations are shownto havescale-invariant alignmentstatistics. Deviationsfromscale invariance indicate the presence of mutualcorrelations detectable by alignment algorithms. Conditions are obtained for the optimal detection of a class of mutualsequencecorrelations.