Finding all Maximal Exact Matches (MEMs) in a single large sequence, especially with distance and size constraints, is a classic bioinformatics problem. The most efficient and widely adopted algorithms are based on enhanced suffix arrays (SA) combined with LCP arrays (Longest Common Prefix array).
Let's break down why and how:
-
Core Data Structures:
- Suffix Array (SA): An array of integers that specifies the lexicographical order of all suffixes of the input sequence
S. If S has length N, SA has N entries. Construction can be done in O(N) time (e.g., using SA-IS algorithm) or O(N log N) with simpler algorithms.
- LCP Array: An array where
LCP[i] stores the length of the longest common prefix between the suffixes S[SA[i-1]...] and S[SA[i]...]. It can be constructed in O(N) time from the SA and the original sequence (e.g., using Kasai's algorithm).
-
Finding MEMs using SA and LCP:
- A common prefix of length
L between SA[i-1] and SA[i] (i.e., LCP[i] = L) represents an exact match of length L starting at positions SA[i-1] and SA[i].
- More generally, an LCP interval
[i, j] where min(LCP[k]) for k from i+1 to j is L, and LCP[i] < L and LCP[j+1] < L (if they exist), defines a set of occurrences (SA[i], SA[i+1], ..., SA[j]) that all share a common prefix of length L. This common prefix is a candidate for a MEM.
- Maximality: A match is maximal if it cannot be extended to the left or right and still be an exact match between those specific occurrences.
- Right-maximality: Ensured by the LCP array definition. If
LCP[k] = L, the characters S[SA[k-1]+L] and S[SA[k]+L] must differ (otherwise LCP would be > L).
- Left-maximality: For a pair of occurrences at
p1 = SA[k-1] and p2 = SA[k], the match is left-maximal if S[p1-1] != S[p2-1] (assuming p1 > 0 and p2 > 0). This check needs to be performed for each pair of occurrences identified.
-
Algorithm Outline with Constraints:
a. Preprocessing:
1. Construct the Suffix Array (SA) for the input sequence S. (O(N) or O(N log N))
2. Construct the LCP Array from the SA. (O(N))
b. Iterating and Filtering for MEMs:
The key idea is to identify LCP intervals. An LCP interval (L, i, j) denotes that suffixes SA[i...j] all share a common prefix of length L, and this is maximal in the sense that LCP[i] < L (if i>0) and LCP[j+1] < L (if j<N-1). These intervals can be found by iterating through the LCP array.
A simpler approach often used in practice considers every LCP[k] value:
For each k from 1 to N-1:
1. Let L = LCP[k].
2. Size Constraint (Length of MEM):
If L <= R or L >= S, skip this LCP[k]. (The problem states >R and <S, so R < L < S).
3. Potential Occurrences: The current match of length L occurs at pos1 = SA[k-1] and pos2 = SA[k].
4. Maximality Check (Left):
* Check if pos1 == 0 or pos2 == 0.
* If not, check if S[pos1-1] == S[pos2-1]. If they are equal, this specific pair (pos1, pos2) does not form a MEM of length L because it can be extended to the left. It might be part of a longer MEM or a different MEM.
* (Right-maximality is implicitly handled by how LCP values are defined: S[pos1+L] != S[pos2+L]).
* A more robust way to ensure maximality for all occurrences within an LCP interval (L, i, j) is that the characters preceding each SA[x] (for x in i..j) must be distinct from the characters preceding other SA[y] (for y in i..j) if those other occurrences are to form a MEM with SA[x]. This is complex. A common simplification is to find "supermaximal repeats" or "shustrings," which are substrings w such that all occurrences of w are S[p_1...p_1+|w|-1], S[p_2...p_2+|w|-1], ... and for any pair (p_a, p_b), S[p_a-1] != S[p_b-1] and S[p_a+|w|] != S[p_b+|w|].
5. Handling Multiple Occurrences within an LCP Interval:
If an LCP interval [x..y] has min(LCP[k] for k in x+1..y) = L_interval, then all suffixes SA[x], ..., SA[y] share a prefix of length L_interval.
* Collect all starting positions: .
* For every pair of positions from where :
* Check if .
* Check (if ).
* If all conditions (size, distance, maximality for the pair) are met, then is a reported MEM pair.
* To make this step efficient for many occurrences in (say occurrences):
* Sort : O(m log m).
* Use a two-pointer/sliding window approach on the sorted to find pairs satisfying the distance constraint: O(m) or O(m log m) depending on implementation.
c. Output: Store the found MEMs (e.g., as tuples (start_pos1, start_pos2, length)). Ensure uniqueness if a MEM is found multiple times through different LCP entries (e.g., use a hash set of reported MEMs).
-
Complexity:
- SA + LCP construction: O(N) or O(N log N).
- Iterating through LCP array: O(N).
- For each LCP interval with
m occurrences:
- Collecting positions: O(m).
- Sorting positions (if needed for distance check): O(m log m).
- Pairwise checks with optimization: O(m) to O(m^2) in the worst case if not optimized, but can be O(m log m) or better for the distance check.
- The overall complexity is often dominated by the SA/LCP construction or by the processing of LCP intervals with many occurrences if not handled carefully. In practice, for typical biological sequences, it's efficient.
-
Alternative: Suffix Tree:
- A Suffix Tree can also be used. Each internal node in the suffix tree represents a repeated substring.
- The string depth of an internal node gives the length of the repeat.
- The leaves in the subtree rooted at an internal node give all starting positions of that repeat.
- Constraints:
- Size: Filter internal nodes by string depth.
- Distance: For each qualifying internal node, collect leaf positions and check pairwise distances.
- Maximality: Check characters preceding and succeeding the occurrences.
- Efficiency: Suffix tree construction is O(N) (Ukkonen's). Traversal is efficient. However, suffix trees can have a larger memory footprint (higher constant factor for space) than suffix arrays. Applying the distance constraint efficiently for nodes with many leaves has similar challenges as with LCP intervals.
-
Practical Tools:
- MUMmer: (Specifically
mummer or nucmer -maxmatch) is a widely used tool that finds Maximal Unique Matches (MUMs) or MEMs. It typically uses suffix trees or suffix arrays internally. You would run it on the sequence against itself.
- Specialized libraries: Many bioinformatics libraries (e.g., SeqAn in C++, sdsl-lite) provide implementations of suffix arrays, LCP arrays, and sometimes iterators for specific repeat types.
Why SA+LCP is often preferred:
- Space Efficiency: Suffix arrays (especially when compressed) and LCP arrays generally have a smaller memory footprint than suffix trees.
- Algorithmic Simplicity for LCP-based operations: Many repeat-finding algorithms can be elegantly expressed using LCP array properties.
- Mature Implementations: Highly optimized SA construction algorithms (like SA-IS) exist.
Most efficient in practice:
The "most efficient" often refers to the Suffix Array + LCP Array approach due to its good balance of time and space complexity, and the availability of highly optimized construction algorithms.
The steps to implement are:
- Concatenate your sequence with itself using a unique separator if you want to find repeats easily (e.g.,
S + $ + S where $ is lexicographically smaller than any character in S). Or, just use S and be careful with indices. For a single sequence, just use S.
- Build SA and LCP for
S.
- Iterate through LCP values. For each
LCP[k] = L such that R < L < S:
a. Occurrences are pos1 = SA[k-1] and pos2 = SA[k].
b. Check distance: X < abs(pos1 - pos2) < Y.
c. Check left-maximality: (pos1 == 0 || pos2 == 0 || S[pos1-1] != S[pos2-1]).
d. If all true, (min(pos1,pos2), max(pos1,pos2), L) is a reported MEM. Store in a set to avoid duplicates if processing LCP intervals that might generate overlapping pairs.
For finding all pairs from an LCP interval (L, i, j):
- Let
L_interval be the length defined by the interval. If not R < L_interval < S, skip.
- Collect positions
P = {SA[x] | x in [i, j]}.
- For each pair
(p_a, p_b) in P with p_a < p_b:
a. If X < (p_b - p_a) < Y:
b. If (p_a == 0 || S[p_a-1] != S[S.indexOf(SA corresponding to p_b)-1]) (this check is tricky, easier to stick to pairs from adjacent LCP entries and then expand).
A common strategy is to identify maximal repeat classes based on LCP intervals first, then check pairs of occurrences within those classes.
The algorithm by Abouelhoda, Kurtz, and Ohlebusch (2004) "Replacing suffix trees with enhanced suffix arrays" provides a framework for many suffix tree applications, including finding various types of repeats, using SA and LCP and additional precomputed tables. For your specific problem with distance constraints, iterating through LCP values and then applying pairwise checks on occurrences derived from LCP intervals (potentially with the two-pointer optimization for sorted positions) is a robust approach.