Exact matching algorithms like the Naive Exact Matching, Boyer-Moore Matching Algorithm and Index assisted matching can align a pattern P (sequence read) to a text T (genome).
However, in real life, the sequence read will rarely be an exact match to the genome. Two main reasons that cause a sequence read to deviate from the reference genome are 1) sequencing errors and 2) individual variations. A big part of computational genomics is to distinguish true biological variations from noises like sequencing errors.
Variations: substitution, insertion and deletion
There are 3 types of variations when comparing a sequence to a reference genome: - Substitution: one letter is substituted by another. - Insertion: there’s an extra letter in the sequence read, or a gap in reference genome. - Deletion: oppositely, there’s an extra letter in reference genome, or a gap in the sequence read.
T: GGAAAAAGAGGTAGCGGCGTTTAACAGTAG ||||||||| P: GTAACGGCG ↑ Mismatch/Substitution T: GGAAAAAGAGGTAGCG-GCGTTTAACAGTAG |||||| ||| P: GTAGCGGGCG ↑ Insertion T: GGAAAAAGAGGTAGCGGCGTTTAACAGTAG || ||||||| P: GT-GCGGCG ↑ Deletion
A substitution is also called a mismatch or mutation, and insertion and deletion sometimes are bundled and referred to as an INDEL.
Hamming and edit distance
To quantify how different two string X and Y are, we calculate the distance between the two strings. There are different types of string distances or metrics that can be used, depending on the context and purpose of the comparison.
Hamming distance: the minimal number of substitution needed to turn one string into the other.
Hamming distance only considers the cases of mutation, and requires X and Y to be of the same length.
X: GAGGTAGCGGCGTTTAAC | |||| ||| ||||||| Hamming distance=3 Y: GTGGTAACGGGGTTTAAC
Edit/Levenshtein distance: the minimal number of edits (including substitutions, insertions and deletions) needed to turn one string into the other.
When measuring edit distance, X and Y does not need to be the same length.
X: GAGGTAGCGG-GTTTAAC | |||| || ||||||| Edit distance=4 Y: GTGGTA--GGGGTTTAAC
Approximate matching with the pigeonhole principle
There’s a framework that can adapt any exact matching algorithm to solve an approximate matching problem.
First let’s think about a simple case, where up to 1 edit is allowed in pattern P for the matches to occur.
- The trick is to split P into two partitions u and v, and either u or v has to match exactly to T - the edit can only exist in one of them, hence the other has to be an exact match.
- We can then use the exact matching algorithm to find locations where either u or v is an exact match, and verify whether it leads to a true match within the 1 edit criteria.
Generally, to find matches where n edits are allowed, we can split P into n+1 partitions/ segments, search for hits where any partition is an exact match, and verify the other partitions.
This is called a pigeonhole principle: if you place 10 pigeons in 9 holes, at least 1 hole will contain 2 pigeons.
Our case is slightly different - we are placing edits in the partitions, and we have more partitions (n+1) than edits allowed (n), at least one partition will be empty (without any edits) and have to be an exact match.
So in our case there are more holes than pigeons, and if we place 8 pigeons in 9 holes, at least 1 hole will be empty.
Code example - approximate matching with Boyer-Moore
Here’s an example of adapting the Boyer-Moore algorithm to achieve approximate matching, based on the pigeonhole principle. The code is in Python, the code for the Boyer-Moore class can be found at http://d28rh4a8wq0iu5.cloudfront.net/ads1/code/bm_preproc.py