SlideShare a Scribd company logo
1 of 37
Download to read offline
Combinatorial Pattern
Matching
CS 466
Saurabh Sinha
Genomic Repeats
• Example of repeats:
– ATGGTCTAGGTCCTAGTGGTC
• Motivation to find them:
– Genomic rearrangements are often
associated with repeats
– Trace evolutionary secrets
– Many tumors are characterized by an
explosion of repeats
Genomic Repeats
• The problem is often more difficult:
– ATGGTCTAGGACCTAGTGTTC
• Motivation to find them:
– Genomic rearrangements are often
associated with repeats
– Trace evolutionary secrets
– Many tumors are characterized by an
explosion of repeats
l -mer Repeats
• Long repeats are difficult to find
• Short repeats are easy to find (e.g., hashing)
• Simple approach to finding long repeats:
– Find exact repeats of short l-mers (l is
usually 10 to 13)
– Use l -mer repeats to potentially extend into
longer, maximal repeats
l -mer Repeats (cont’d)
• There are typically many locations
where an l -mer is repeated:
GCTTACAGATTCAGTCTTACAGATGGT
• The 4-mer TTAC starts at locations 3
and 17
Extending l -mer Repeats
GCTTACAGATTCAGTCTTACAGATGGT
• Extend these 4-mer matches:
GCTTACAGATTCAGTCTTACAGATGGT
• Maximal repeat: CTTACAGAT
Maximal Repeats
• To find maximal repeats in this way, we
need ALL start locations of all l -mers in
the genome
• Hashing lets us find repeats quickly in
this manner
Hashing: Maximal Repeats
• To find repeats in a genome:
– For all l -mers in the genome, note the start
position and the sequence
– Generate a hash table index for each
unique l -mer sequence
– In each index of the hash table, store all
genome start locations of the l -mer which
generated that index
– Extend l -mer repeats to maximal repeats
Pattern Matching
• What if, instead of finding repeats in a
genome, we want to find all sequences
in a database that contain a given
pattern?
• This leads us to a different problem, the
Pattern Matching Problem
Pattern Matching Problem
• Goal: Find all occurrences of a pattern in a text
• Input: Pattern p = p1…pn and text t = t1…tm
• Output: All positions 1< i < (m – n + 1) such that the
n-letter substring of t starting at i matches p
• Motivation: Searching database for a known pattern
Exact Pattern Matching: Running Time
• Naïve runtime: O(nm)
• On average, it’s more like O(m)
– Why?
• Can solve problem in O(m) time ?
– Yes, we’ll see how (later)
Generalization of problem:
Multiple Pattern Matching Problem
• Goal: Given a set of patterns and a text, find all occurrences of any of
patterns in text
• Input: k patterns p1,…,pk, and text t = t1…tm
• Output: Positions 1 < i < m where substring of t starting at i matches pj
for 1 < j < k
• Motivation: Searching database for known multiple patterns
• Solution: k “pattern matching problems”: O(kmn)
• Solution: Using “Keyword trees” => O(kn+m) where n is maximum
length of pi
Keyword Trees: Example
• Keyword tree:
– Apple
Keyword Trees: Example
(cont’d)
• Keyword tree:
– Apple
– Apropos
Keyword Trees: Example
(cont’d)
• Keyword tree:
– Apple
– Apropos
– Banana
Keyword Trees: Example
(cont’d)
• Keyword tree:
– Apple
– Apropos
– Banana
– Bandana
Keyword Trees: Example
(cont’d)
• Keyword tree:
– Apple
– Apropos
– Banana
– Bandana
– Orange
Keyword Trees: Properties
– Stores a set of keywords
in a rooted labeled tree
– Each edge labeled with a
letter from an alphabet
– Any two edges coming out
of the same vertex have
distinct labels
– Every keyword stored can
be spelled on a path from
root to some leaf
Multiple Pattern Matching: Keyword
Tree Approach
• Build keyword tree in O(kn) time; kn
is total length of all patterns
• Start “threading” at each position in
text; at most n steps tell us if there
is a match here to any pi
• O(kn + nm)
• Aho-Corasick algorithm: O(kn + m)
Aho-Corasick algorithm
“Fail” edges in keyword tree
Dashed edge out of internal node if matching edge not found
“Fail” edges in keyword tree
• If currently at node q representing word
L(q), find the longest proper suffix of
L(q) that is a prefix of some pattern, and
go to the node representing that prefix
• Example: node q = 5 L(q) = she; longest
proper suffix that is a prefix of some
pattern: “he”. Dashed edge to node q’=2
Automaton
• Transition among the different nodes by
following edges depending on next
character seen (“c”)
• If outgoing edge with label “c”, follow it
• If no such edge, and are at root, stay
• If no such edge, and at non-root, follow
dashes edge (“fail” transition); DO NOT
CONSUME THE CHARACTER (“c”)
Example: search text “ushers” with the automaton
Aho-Corasick algorithm
• O(kn) to build the automaton
• O(m) to search a text of length m
• Key insight:
– For every character “consumed”, we move at most
one level deeper (away from root) in the tree.
Therefore total number of such “away from root”
moves is <= m
– Each fail transition moves us at least one level
closer to root. Therefore total number of such
“towards root” moves is <= m (you cant climb up
more than you climbed down)
Suffix tree
• Build a tree from the text
• Used if the text is expected to be the same
during several pattern queries
• Tree building is O(m) where m is the size of
the text. This is preprocessing.
• Given any pattern of length n, we can answer
if it occurs in text in O(n) time
• Suffix tree = “modified” keyword tree of all
suffixes of text
Suffix Tree=Collapsed Keyword
Trees
• Similar to keyword trees,
except edges that form
paths are collapsed
– Each edge is labeled
with a substring of a
text
– All internal edges have
at least two outgoing
edges
– Leaves labeled by the
index of the pattern.
Suffix Tree of a Text
• Suffix trees of a text is constructed for all its suffixes
ATCATG
TCATG
CATG
ATG
TG
G
quadratic Keyword
Tree
Suffix
Tree
Time is linear in the total size of all suffixes,
i.e., it is quadratic in the length of the text
Suffix Trees: Advantages
• Suffix trees build faster than keyword trees
ATCATG
TCATG
CATG
ATG
TG
G
quadratic Keyword
Tree
Suffix
Tree
linear (Weiner suffix tree algorithm)
Time to find k patterns of length n: O(m + kn)
Suffix Trees: Example
Multiple Pattern Matching:
Summary
• Keyword and suffix trees are used to find
patterns in a text
• Keyword trees:
– Build keyword tree of patterns, and thread
text through it
• Suffix trees:
– Build suffix tree of text, and thread
patterns through it
Approximate vs. Exact Pattern
Matching
• So far all we’ve seen exact pattern
matching algorithms
• Usually, because of mutations, it makes
much more biological sense to find
approximate pattern matches
• Biologists often use fast heuristic
approaches (rather than local
alignment) to find approximate matches
Heuristic Similarity Searches
• Genomes are huge: Smith-Waterman
quadratic alignment algorithms are too slow
• Alignment of two sequences usually has short
identical or highly similar fragments
• Many heuristic methods (i.e., FASTA) are
based on the same idea of filtration
– Find short exact matches, and use them as
seeds for potential match extension
Query Matching Problem
• Goal: Find all substrings of the query that
approximately match the text
• Input: Query q = q1…qw,
text t = t1…tm,
n (length of matching substrings),
k (maximum number of mismatches)
• Output: All pairs of positions (i, j) such that the
n-letter substring of q starting at i
approximately matches the
n-letter substring of t starting at j,
with at most k mismatches
Query Matching: Main Idea
• Approximately matching strings share
some perfectly matching substrings.
• Instead of searching for approximately
matching strings (difficult) search for
perfectly matching substrings (easy).
Filtration in Query Matching
• We want all n-matches between a query and
a text with up to k mismatches
• “Filter” out positions we know do not match
between text and query
• Potential match detection: find all matches
of l -tuples in query and text for some small l
• Potential match verification: Verify each
potential match by extending it to the left and
right, until (k + 1) mismatches are found
Filtration: Match Detection
• If x1…xn and y1…yn match with at most k
mismatches, they must share an l -tuple that
is perfectly matched, with l = n/(k + 1)
• Break string of length n into k+1 parts, each
each of length n/(k + 1)
– k mismatches can affect at most k of these
k+1 parts
– At least one of these k+1 parts is perfectly
matched
Filtration: Match Verification
• For each l -match we find, try to extend the
match further to see if it is substantial
query
Extend perfect
match of
length l
until we find an
approximate
match of
length n with k
mismatches
text

More Related Content

Similar to Lecture10.pdf

Tdm probabilistic models (part 2)
Tdm probabilistic  models (part  2)Tdm probabilistic  models (part  2)
Tdm probabilistic models (part 2)
KU Leuven
 

Similar to Lecture10.pdf (20)

String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Unit -I Toc.pptx
Unit -I Toc.pptxUnit -I Toc.pptx
Unit -I Toc.pptx
 
Text classification using Text kernels
Text classification using Text kernelsText classification using Text kernels
Text classification using Text kernels
 
Generating Natural-Language Text with Neural Networks
Generating Natural-Language Text with Neural NetworksGenerating Natural-Language Text with Neural Networks
Generating Natural-Language Text with Neural Networks
 
intro.ppt
intro.pptintro.ppt
intro.ppt
 
Tdm probabilistic models (part 2)
Tdm probabilistic  models (part  2)Tdm probabilistic  models (part  2)
Tdm probabilistic models (part 2)
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
Language models
Language modelsLanguage models
Language models
 
Fast Exact String Pattern-Matching Algorithm for Fixed Length Patterns
Fast Exact String Pattern-Matching Algorithm for Fixed Length PatternsFast Exact String Pattern-Matching Algorithm for Fixed Length Patterns
Fast Exact String Pattern-Matching Algorithm for Fixed Length Patterns
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
maximum parsimony.pdf
maximum parsimony.pdfmaximum parsimony.pdf
maximum parsimony.pdf
 
Unit7
Unit7Unit7
Unit7
 
lec2.pdf
lec2.pdflec2.pdf
lec2.pdf
 
Introducction to Algorithm
Introducction to AlgorithmIntroducction to Algorithm
Introducction to Algorithm
 
Elementary cryptography
Elementary cryptographyElementary cryptography
Elementary cryptography
 
Trie Data Structure
Trie Data Structure Trie Data Structure
Trie Data Structure
 
History of Cipher System
History of Cipher SystemHistory of Cipher System
History of Cipher System
 
Word2vec slide(lab seminar)
Word2vec slide(lab seminar)Word2vec slide(lab seminar)
Word2vec slide(lab seminar)
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptx
 

Recently uploaded

Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
JoseMangaJr1
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
amitlee9823
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
AroojKhan71
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
amitlee9823
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
amitlee9823
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
amitlee9823
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
only4webmaster01
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
MarinCaroMartnezBerg
 

Recently uploaded (20)

Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 

Lecture10.pdf

  • 2. Genomic Repeats • Example of repeats: – ATGGTCTAGGTCCTAGTGGTC • Motivation to find them: – Genomic rearrangements are often associated with repeats – Trace evolutionary secrets – Many tumors are characterized by an explosion of repeats
  • 3. Genomic Repeats • The problem is often more difficult: – ATGGTCTAGGACCTAGTGTTC • Motivation to find them: – Genomic rearrangements are often associated with repeats – Trace evolutionary secrets – Many tumors are characterized by an explosion of repeats
  • 4. l -mer Repeats • Long repeats are difficult to find • Short repeats are easy to find (e.g., hashing) • Simple approach to finding long repeats: – Find exact repeats of short l-mers (l is usually 10 to 13) – Use l -mer repeats to potentially extend into longer, maximal repeats
  • 5. l -mer Repeats (cont’d) • There are typically many locations where an l -mer is repeated: GCTTACAGATTCAGTCTTACAGATGGT • The 4-mer TTAC starts at locations 3 and 17
  • 6. Extending l -mer Repeats GCTTACAGATTCAGTCTTACAGATGGT • Extend these 4-mer matches: GCTTACAGATTCAGTCTTACAGATGGT • Maximal repeat: CTTACAGAT
  • 7. Maximal Repeats • To find maximal repeats in this way, we need ALL start locations of all l -mers in the genome • Hashing lets us find repeats quickly in this manner
  • 8. Hashing: Maximal Repeats • To find repeats in a genome: – For all l -mers in the genome, note the start position and the sequence – Generate a hash table index for each unique l -mer sequence – In each index of the hash table, store all genome start locations of the l -mer which generated that index – Extend l -mer repeats to maximal repeats
  • 9. Pattern Matching • What if, instead of finding repeats in a genome, we want to find all sequences in a database that contain a given pattern? • This leads us to a different problem, the Pattern Matching Problem
  • 10. Pattern Matching Problem • Goal: Find all occurrences of a pattern in a text • Input: Pattern p = p1…pn and text t = t1…tm • Output: All positions 1< i < (m – n + 1) such that the n-letter substring of t starting at i matches p • Motivation: Searching database for a known pattern
  • 11. Exact Pattern Matching: Running Time • Naïve runtime: O(nm) • On average, it’s more like O(m) – Why? • Can solve problem in O(m) time ? – Yes, we’ll see how (later)
  • 12. Generalization of problem: Multiple Pattern Matching Problem • Goal: Given a set of patterns and a text, find all occurrences of any of patterns in text • Input: k patterns p1,…,pk, and text t = t1…tm • Output: Positions 1 < i < m where substring of t starting at i matches pj for 1 < j < k • Motivation: Searching database for known multiple patterns • Solution: k “pattern matching problems”: O(kmn) • Solution: Using “Keyword trees” => O(kn+m) where n is maximum length of pi
  • 13. Keyword Trees: Example • Keyword tree: – Apple
  • 14. Keyword Trees: Example (cont’d) • Keyword tree: – Apple – Apropos
  • 15. Keyword Trees: Example (cont’d) • Keyword tree: – Apple – Apropos – Banana
  • 16. Keyword Trees: Example (cont’d) • Keyword tree: – Apple – Apropos – Banana – Bandana
  • 17. Keyword Trees: Example (cont’d) • Keyword tree: – Apple – Apropos – Banana – Bandana – Orange
  • 18. Keyword Trees: Properties – Stores a set of keywords in a rooted labeled tree – Each edge labeled with a letter from an alphabet – Any two edges coming out of the same vertex have distinct labels – Every keyword stored can be spelled on a path from root to some leaf
  • 19. Multiple Pattern Matching: Keyword Tree Approach • Build keyword tree in O(kn) time; kn is total length of all patterns • Start “threading” at each position in text; at most n steps tell us if there is a match here to any pi • O(kn + nm) • Aho-Corasick algorithm: O(kn + m)
  • 21. “Fail” edges in keyword tree Dashed edge out of internal node if matching edge not found
  • 22. “Fail” edges in keyword tree • If currently at node q representing word L(q), find the longest proper suffix of L(q) that is a prefix of some pattern, and go to the node representing that prefix • Example: node q = 5 L(q) = she; longest proper suffix that is a prefix of some pattern: “he”. Dashed edge to node q’=2
  • 23. Automaton • Transition among the different nodes by following edges depending on next character seen (“c”) • If outgoing edge with label “c”, follow it • If no such edge, and are at root, stay • If no such edge, and at non-root, follow dashes edge (“fail” transition); DO NOT CONSUME THE CHARACTER (“c”) Example: search text “ushers” with the automaton
  • 24. Aho-Corasick algorithm • O(kn) to build the automaton • O(m) to search a text of length m • Key insight: – For every character “consumed”, we move at most one level deeper (away from root) in the tree. Therefore total number of such “away from root” moves is <= m – Each fail transition moves us at least one level closer to root. Therefore total number of such “towards root” moves is <= m (you cant climb up more than you climbed down)
  • 25. Suffix tree • Build a tree from the text • Used if the text is expected to be the same during several pattern queries • Tree building is O(m) where m is the size of the text. This is preprocessing. • Given any pattern of length n, we can answer if it occurs in text in O(n) time • Suffix tree = “modified” keyword tree of all suffixes of text
  • 26. Suffix Tree=Collapsed Keyword Trees • Similar to keyword trees, except edges that form paths are collapsed – Each edge is labeled with a substring of a text – All internal edges have at least two outgoing edges – Leaves labeled by the index of the pattern.
  • 27. Suffix Tree of a Text • Suffix trees of a text is constructed for all its suffixes ATCATG TCATG CATG ATG TG G quadratic Keyword Tree Suffix Tree Time is linear in the total size of all suffixes, i.e., it is quadratic in the length of the text
  • 28. Suffix Trees: Advantages • Suffix trees build faster than keyword trees ATCATG TCATG CATG ATG TG G quadratic Keyword Tree Suffix Tree linear (Weiner suffix tree algorithm) Time to find k patterns of length n: O(m + kn)
  • 30. Multiple Pattern Matching: Summary • Keyword and suffix trees are used to find patterns in a text • Keyword trees: – Build keyword tree of patterns, and thread text through it • Suffix trees: – Build suffix tree of text, and thread patterns through it
  • 31. Approximate vs. Exact Pattern Matching • So far all we’ve seen exact pattern matching algorithms • Usually, because of mutations, it makes much more biological sense to find approximate pattern matches • Biologists often use fast heuristic approaches (rather than local alignment) to find approximate matches
  • 32. Heuristic Similarity Searches • Genomes are huge: Smith-Waterman quadratic alignment algorithms are too slow • Alignment of two sequences usually has short identical or highly similar fragments • Many heuristic methods (i.e., FASTA) are based on the same idea of filtration – Find short exact matches, and use them as seeds for potential match extension
  • 33. Query Matching Problem • Goal: Find all substrings of the query that approximately match the text • Input: Query q = q1…qw, text t = t1…tm, n (length of matching substrings), k (maximum number of mismatches) • Output: All pairs of positions (i, j) such that the n-letter substring of q starting at i approximately matches the n-letter substring of t starting at j, with at most k mismatches
  • 34. Query Matching: Main Idea • Approximately matching strings share some perfectly matching substrings. • Instead of searching for approximately matching strings (difficult) search for perfectly matching substrings (easy).
  • 35. Filtration in Query Matching • We want all n-matches between a query and a text with up to k mismatches • “Filter” out positions we know do not match between text and query • Potential match detection: find all matches of l -tuples in query and text for some small l • Potential match verification: Verify each potential match by extending it to the left and right, until (k + 1) mismatches are found
  • 36. Filtration: Match Detection • If x1…xn and y1…yn match with at most k mismatches, they must share an l -tuple that is perfectly matched, with l = n/(k + 1) • Break string of length n into k+1 parts, each each of length n/(k + 1) – k mismatches can affect at most k of these k+1 parts – At least one of these k+1 parts is perfectly matched
  • 37. Filtration: Match Verification • For each l -match we find, try to extend the match further to see if it is substantial query Extend perfect match of length l until we find an approximate match of length n with k mismatches text