combinatorial pattern matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/lecture10.pdf ·...

37
Combinatorial Pattern Matching CS 466 Saurabh Sinha

Upload: others

Post on 19-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Combinatorial PatternMatching

CS 466Saurabh Sinha

Page 2: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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

Page 3: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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

Page 4: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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 isusually 10 to 13)

– Use l -mer repeats to potentially extend intolonger, maximal repeats

Page 5: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

l -mer Repeats (cont’d)

• There are typically many locationswhere an l -mer is repeated:

GCTTACAGATTCAGTCTTACAGATGGT

• The 4-mer TTAC starts at locations 3and 17

Page 6: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Extending l -mer Repeats

GCTTACAGATTCAGTCTTACAGATGGT

• Extend these 4-mer matches:

GCTTACAGATTCAGTCTTACAGATGGT

• Maximal repeat: CTTACAGAT

Page 7: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Maximal Repeats

• To find maximal repeats in this way, weneed ALL start locations of all l -mers inthe genome

• Hashing lets us find repeats quickly inthis manner

Page 8: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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 whichgenerated that index

– Extend l -mer repeats to maximal repeats

Page 9: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Pattern Matching

• What if, instead of finding repeats in agenome, we want to find all sequencesin a database that contain a givenpattern?

• This leads us to a different problem, thePattern Matching Problem

Page 10: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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 then-letter substring of t starting at i matches p

• Motivation: Searching database for a known pattern

Page 11: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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)

Page 12: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Generalization of problem:Multiple Pattern Matching Problem

• Goal: Given a set of patterns and a text, find all occurrences of any ofpatterns 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 pjfor 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 maximumlength of pi

Page 13: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Keyword Trees: Example

• Keyword tree:– Apple

Page 14: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Keyword Trees: Example(cont’d)

• Keyword tree:– Apple– Apropos

Page 15: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Keyword Trees: Example(cont’d)

• Keyword tree:– Apple– Apropos– Banana

Page 16: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Keyword Trees: Example(cont’d)

• Keyword tree:– Apple– Apropos– Banana– Bandana

Page 17: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Keyword Trees: Example(cont’d)

• Keyword tree:– Apple– Apropos– Banana– Bandana– Orange

Page 18: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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 havedistinct labels

– Every keyword stored canbe spelled on a path fromroot to some leaf

Page 19: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Multiple Pattern Matching: KeywordTree Approach

• Build keyword tree in O(kn) time; knis total length of all patterns

• Start “threading” at each position intext; at most n steps tell us if thereis a match here to any pi

• O(kn + nm)• Aho-Corasick algorithm: O(kn + m)

Page 20: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Aho-Corasick algorithm

Page 21: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

“Fail” edges in keyword tree

Dashed edge out of internal node if matching edge not found

Page 22: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

“Fail” edges in keyword tree

• If currently at node q representing wordL(q), find the longest proper suffix ofL(q) that is a prefix of some pattern, andgo to the node representing that prefix

• Example: node q = 5 L(q) = she; longestproper suffix that is a prefix of somepattern: “he”. Dashed edge to node q’=2

Page 23: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Automaton

• Transition among the different nodes byfollowing edges depending on nextcharacter 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 NOTCONSUME THE CHARACTER (“c”)Example: search text “ushers” with the automaton

Page 24: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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 mostone 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 levelcloser to root. Therefore total number of such“towards root” moves is <= m (you cant climb upmore than you climbed down)

Page 25: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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

Page 26: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Suffix Tree=Collapsed KeywordTrees

• Similar to keyword trees,except edges that formpaths are collapsed

– Each edge is labeledwith a substring of atext

– All internal edges haveat least two outgoingedges

– Leaves labeled by theindex of the pattern.

Page 27: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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

Page 28: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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)

Page 29: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Suffix Trees: Example

Page 30: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Multiple Pattern Matching:Summary

• Keyword and suffix trees are used to findpatterns in a text

• Keyword trees:– Build keyword tree of patterns, and thread

text through it• Suffix trees:

– Build suffix tree of text, and threadpatterns through it

Page 31: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Approximate vs. Exact PatternMatching

• So far all we’ve seen exact patternmatching algorithms

• Usually, because of mutations, it makesmuch more biological sense to findapproximate pattern matches

• Biologists often use fast heuristicapproaches (rather than localalignment) to find approximate matches

Page 32: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Heuristic Similarity Searches

• Genomes are huge: Smith-Watermanquadratic alignment algorithms are too slow

• Alignment of two sequences usually has shortidentical or highly similar fragments

• Many heuristic methods (i.e., FASTA) arebased on the same idea of filtration– Find short exact matches, and use them as

seeds for potential match extension

Page 33: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

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

Page 34: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Query Matching: Main Idea

• Approximately matching strings sharesome perfectly matching substrings.

• Instead of searching for approximatelymatching strings (difficult) search forperfectly matching substrings (easy).

Page 35: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Filtration in Query Matching

• We want all n-matches between a query anda text with up to k mismatches

• “Filter” out positions we know do not matchbetween text and query

• Potential match detection: find all matchesof l -tuples in query and text for some small l

• Potential match verification: Verify eachpotential match by extending it to the left andright, until (k + 1) mismatches are found

Page 36: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Filtration: Match Detection

• If x1…xn and y1…yn match with at most kmismatches, they must share an l -tuple thatis perfectly matched, with l = n/(k + 1)

• Break string of length n into k+1 parts, eacheach 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

Page 37: Combinatorial Pattern Matchingveda.cs.uiuc.edu/courses/fa08/cs466/lectures/Lecture10.pdf · Filtration: Match Verification •For each l -match we find, try to extend the match further

Filtration: Match Verification• For each l -match we find, try to extend the

match further to see if it is substantial

query

Extend perfectmatch oflength luntil we find anapproximatematch oflength n with kmismatchestext