1 / 16

Pattern matching

Pattern matching. We need two things:. A syntax to encode sets of (sub)strings A set of tools to search (sub)strings in strings. The Python syntax to express a set of strings. Regular expressions. ‘ ABC ’ , ‘ abc ’ , ‘ Abc ’ , ‘ aBc ’ , ‘ abC ’ , ‘ ABc ’ , ‘ aBC ’ , ‘ aBC ’.

mildredf
Télécharger la présentation

Pattern matching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pattern matching

  2. We need two things: A syntax to encode sets of (sub)strings A set of tools to search (sub)strings in strings

  3. The Python syntax to express a set of strings Regular expressions ‘ABC’, ‘abc’, ‘Abc’, ‘aBc’, ‘abC’, ‘ABc’, ‘aBC’, ‘aBC’ [Aa][Bb][Cc] Characters and metacharacters A regular expression is a string encoding a set of strings through the use of characters and metacharacters

  4. The Python regular expression syntax [ ] ^ $ \ . | * + ? { } ( ) The meaning of ‘\’ depends on whether ‘\’ is followed by a metacharacter or a character. Repetitions:* + ? { }

  5. A phosphorylation site motif R.[ST][^P] Protein names [SP]{0,1}[fhm]T{0,1}G{0,1}R 'SmTGR', 'PfTR', 'hTR', 'hGR'

  6. We first have to compile a regular expression into a Python pattern object: >>> import re >>> motif = 'A[AC]T' >>> regexp = re.compile(motif) >>> regexp <_sre.SRE_Pattern object at 0x22de0> >>> motif 'A[AC]T' >>> >>> regexp = re.compile('A[AC]T') NB:

  7. Pattern matching >>> S = 'R.[ST][^P]' >>> regexp = re.compile(S) >>>seq = ’SASRQSAMGSNKSKPKDASQRRRSLEPAENVHGAGGGAFPASQRP' >>>#Here, we use search(): >>> m1 = regexp.search(seq) >>>#This returns a Match object: >>> m1 <_sre.SRE_Match object at 0x706e8> >>>#Match object group() method: >>> m1.group() 'RQSA’

  8. Pattern matching >>> S = 'R.[ST][^P]' >>> regexp = re.compile(S) >>>seq = 'RQSAMGSNKSKPKDASQRRRSLEPAENVHGAGGGAFPASQRPSKP’ >>>#Here whe use match(): >>> m2 = regexp.match(seq) >>>#that returns a Match object >>> m2 <_sre.SRE_Match object at 0x70020> >>> m2.group() 'RQSA'

  9. group()returns the matching substring • span()returns a tuple containing the (start,end) of the match • start() returns the start positions of the match • end() returns the end positions of the match. >>> S = 'R.[ST][^P]' >>> regexp = re.compile(S) >>>seq = 'RQSAMGSNKSKPKDASQRRRSLEPAENVHGAGGGAFPASQRPSKP' >>> m1 = regexp.search(seq) >>> m1.group() 'RQSA’ >>> m2.span() (0, 4) >>> m2.start() 0 >>> m2.end() 4

  10. What if we want to find ALL matches of a regular expression and not only the first one? findall and finditer >>> S = 'R.[ST][^P]' >>> regexp = re.compile(S) >>> all = regexp.findall(seq) >>> all ['RQSA', 'RRSL', 'RPSK']

  11. An iterator is a “container” of objects that can be traversed using a for loop. In this specific case, the iterator contains a set of Python match objects Mach objects can be individually accessed using Match object methods, such as group(), span(), start() and end()

  12. >>> S = 'R.[ST][^P]' >>> regexp = re.compile(S) >>>seq = 'RQSAMGSNKSKPKDASQRRRSLEPAENVHGAGGGAFPASQRPSKP >>> iter = regexp.finditer(seq) >>> iter <callable-iterator object at 0x786d0> >>> for s in iter: ... print s.group() ... print s.span() ... print s.start() ... print s.end() ... RQSA (0, 4) 0 4 RRSL (18, 22) 18 22 RPSK (40, 44) 40 44

  13. Grouping How to divide a regular expression in subgroups matching different components of interest >>> S = 'R(.)[ST][^P]' >>> regexp = re.compile(S) >>>seq = 'RQSAMGSNKSKPKDASQRRRSLEPAENVHGAGGGAFPASQRPSKP' >>> m1 = regexp.search(seq) >>> m1.group() 'RQSA' >>> m1.group(1) 'Q' >>> S = 'R(.{0,3})[ST][^P]' >>> regexp = re.compile(S) >>> seq = 'QSAMGSNKSKPKDASQRRRSLEPAENVHGAGGGAFPASQRPSKP' >>> m1 = regexp.search(seq) >>> m1.group() 'RRRSL' >>> m1.group(1)

  14. Modifying strings The re module also provides methods to search and replace substrings sub(r,s,[c]), subn(r,s,[c]) >>> r = '\|' >>> separator = re.compile(r) >>> s = 'ATOM:CA|RES:ALA|CHAIN:B|NUMRES:166' >>> new_s = separator.sub('@', s) >>> new_s 'ATOM:CA@RES:ALA@CHAIN:B@NUMRES:166' >>> new_s = separator.sub('@', s, 2) >>> new_s 'ATOM:CA@RES:ALA@CHAIN:B|NUMRES:166' >>> >>> new_s = separator.subn('@', s) >>> new_s ('ATOM:CA@RES:ALA@CHAIN:B@NUMRES:166', 3)

  15. Modifying strings The re module also provides methods to search and replace substrings sub(r,s,[c]), subn(r,s,[c]) >>> r = '\|' >>> separator = re.compile(r) >>> s = 'ATOM:CA|RES:ALA|CHAIN:B|NUMRES:166' >>> new_s = separator.sub('@', s) >>> new_s 'ATOM:CA@RES:ALA@CHAIN:B@NUMRES:166' >>> new_s = separator.sub('@', s, 2) >>> new_s 'ATOM:CA@RES:ALA@CHAIN:B|NUMRES:166' >>> >>> new_s = separator.subn('@', s) >>> new_s ('ATOM:CA@RES:ALA@CHAIN:B@NUMRES:166', 3)

  16. Summary • Search a functional site in a protein sequence • Search a TFBS in a genomic sequence • Fetch an abstract from PubMed (urllib2) • Search a word in a text (text mining)

More Related