1
Programming in Python
Michael Schroeder Sven Schreiber
sven.schreiber@tu-dresden.de
Updates by Andreas Henschel
Lecture 3: Patterns and Functions
Slides derived from Ian Holmes, Department of Statistics, University of Oxford
Programming in Python Lecture 3: Patterns and Functions Michael - - PowerPoint PPT Presentation
Programming in Python Lecture 3: Patterns and Functions Michael Schroeder Sven Schreiber sven.schreiber@tu-dresden.de 1 Slides derived from Ian Holmes, Department of Statistics, University of Oxford Updates by Andreas Henschel Overview
1
sven.schreiber@tu-dresden.de
Updates by Andreas Henschel
Lecture 3: Patterns and Functions
Slides derived from Ian Holmes, Department of Statistics, University of Oxford
2
3
4
https://commons.wikimedia.org/wiki/Tree
5
name = ‘YBR007C’ dna = ‘TAATAAAAAACGCGTTGTCG’ if ‘ACGCGT’ in dna: print(‘%s has MCB!’ % name) 20 bases upstream of the yeast gene YBR007C The membership operator in The pattern for the MCB binding site YBR007C has MCB!
6
ACGCGT
ACGCGT ACCCGT ACACGT ACTCGT
7
8
9
10
11
>>> import re >>> pattern = re.compile('[ACGT]') >>> if pattern.match(“A"): print(“A matches") Matched >>> if pattern.match("a"): print(“a matches") >>>
successful match unsuccessful, returns None by def. case sensitive
>>> import re >>> if re.match('[ACGT]‘, “A"): print("Matched") >>> Matched
without compiling, short, but less performant A matches Matched
12
>>> pattern=re.compile("(this|that|other)", re.IGNORECASE) >>> pattern.search("Will match THIS") ## success <_sre.SRE_Match object at 0x00B52860> >>> pattern.search(“Also THat will be matched") ## success <_sre.SRE_Match object at 0x00B528A0> >>> pattern.search("Will not match ot-her") ## will return None >>>
case unsensitive search pattern Python returns a description of the match object
13
14
>>> re.sub("(red|blue|green)", "colored", "blue socks and red shoes") 'colored socks and colored shoes' >>> e,raw,frm,to = re.findall("\d+", \ "E-value: 4, \ Raw Bit Score: 165, \ Match position: 362-419") >>> print(e, raw, frm, to) 4 165 362 419
\ allows multiple line commands alternatively, construct multi-line strings using triple quotes """ …""" The result, a list of 4 strings, is assigned to 4 variables matches one or more digits Regex use without compiling
15
>>> protein=”\
MGMFFNLRSNIKKKAMDNGLSLPISRNGSSNNIKDKRSEHNSNSLKGKYRYQPRSTPSKFQLTVSITSLI\ IIAVLSLYLFISFLSGMGIGVSTQNGRSLLGSSKSSENYKTIDLEDEEYYDYDFEDIDPEVISKFDDGVQ\ HYLISQFGSEVLTPKDDEKYQRELNMLFDSTVEEYDLSNFEGAPNGLETRDHILLCIPLRNAADVLPLMF\ KHLMNLTYPHELIDLAFLVSDCSEGDTTLDALIAYSRHLQNGTLSQIFQEIDAVIDSQTKGTDKLYLKYM\ DEGYINRVHQAFSPPFHENYDKPFRSVQIFQKDFGQVIGQGFSDRHAVKVQGIRRKLMGRARNWLTANAL\ KPYHSWVYWRDADVELCPGSVIQDLMSKNYDVI”
>>> regex = "N[^P][ST]" >>> for match in re.finditer(regex, protein): print(match.group(), match.span()) NGS (26, 29) NLT (214, 217) NGT (250, 253) N[^P][ST]- the main regular expression re.finditer provides an iterator
match.group and match.span print the actual matched string and the position-tuple.
16
Courtesy of Chris Bystroff
17
Von Thomas Splettstoesser (www.scistyle.com) - self-made, based on PDB structure 1A1L, the open source molecular visualization tool PyMol and Cinema 4D, GFDL, https://commons.wikimedia.org/w/index.php?curid=3106866
18
Courtesy of Chris Bystroff
C\w{2,4}C\w{3}[LIVMFYWC]\w{8}H\w{3,5}H
hydrophobic
19
^[1-9]\w{3}$
PDB IDs
20
21
22
Syntax Example
23
Function Definition Function Calls
6 6
24
def find_max(aList): max = aList.pop() for x in aList: if x > max: max = x return max numbers = [1, 5, 1, 12, 3, 4, 6] print("Maximum: %i” % find_max(numbers)) Maximum: 12 Function declaration Function result Function body Function call
25
26
def calc(x): return (x-3)*2 calc(5) Normal function definition 4 calc1 = lambda x: (x-3)*2 calc1(5) calc2=calc1 calc2(5) Lambda function 4 4
27
map filter reduce (lambda_function, sequence) Function applied to each element ...of the given sequence Decides what to to with the result: map -> apply to each element, return modified list filter -> return list with element tested True reduce -> returns one element resulting from computation
28
map(lambda x: x*3, [1,2,3]) [3,6,9] filter(lambda x: x>=1.0, [1.2,0.5,0.7,1.3]) [1.2,1.3] filter(lambda x: x!=0, map(lambda x: x-2, [4,2,5])) [2,3] reduce(lambda x,y: x+y, (1,2,3,4)) 10 1, 2 3 x, y 2,0,3 3, 3 6 x, y 3,4 x, y
29
. ^ $ * + ? { [ ] \ | ( )
– match, search, findall, finditer etc.