Stream Streams temporally ordered sequence of indefinite length

  • Slides: 24
Download presentation
Stream

Stream

Streams • temporally ordered sequence of indefinite length 2

Streams • temporally ordered sequence of indefinite length 2

Files 3

Files 3

Files • Creating file objects – open(path, mode) – close() 4

Files • Creating file objects – open(path, mode) – close() 4

Files • File methods – – – fileobj. read([count]) fileobj. readlines() fileobj. write(string) fileobj.

Files • File methods – – – fileobj. read([count]) fileobj. readlines() fileobj. write(string) fileobj. writelines(sequence) 5

Files : Ex 1 • Reading FASTA sequences from a file def read_FASTA_strings(filename): file

Files : Ex 1 • Reading FASTA sequences from a file def read_FASTA_strings(filename): file = open(filename) return file. read(). split('>')[1: ] FASTA-formatted files are widely used in bioinformatics. They consist of one or more base or amino acid sequences broken up into lines of reasonable size (typically 70 characters), each preceded by a line beginning with a “>” character. That line is referred to as the sequence’s description, and it generally contains various identifiers and comments that pertain to the sequence that follows. 6

Generators • an object that returns values from a series it computes 7

Generators • an object that returns values from a series it computes 7

Collection-Related Expression Features • Comprehensions – creates a set, list, or dictionary from the

Collection-Related Expression Features • Comprehensions – creates a set, list, or dictionary from the results of evaluating an expression for each element of another collection – List comprehensions [expression for item in collection] def validate_base_sequence(base_sequence, RNAflag = False): valid_bases = 'UCAG' if RNAflag else 'TCAG' return all([(base in valid_bases) for base in base_sequence. upper()]) 8

Collection-Related Expression Features • Comprehensions – Set and dictionary comprehensions {expression for item in

Collection-Related Expression Features • Comprehensions – Set and dictionary comprehensions {expression for item in collection} {key-expression: value-expression for key, value in collection} def make_indexed_sequence_dictionary(filename): return {info[0]: seq for info, seq in read_FASTA(filename)} – Generator expressions (expression for item in collection) 9

Collection-Related Expression Features • Comprehensions – Conditional comprehensions [expression for element in collection if

Collection-Related Expression Features • Comprehensions – Conditional comprehensions [expression for element in collection if test] def dr(name): return [nm for nm in dir(name) if nm[0] != '_'] – Nested comprehensions def generate_triples(chars='TCAG'): chars = set(chars) return [b 1 + b 2 + b 3 for b 1 in chars for b 2 in chars for b 3 in chars] 10

Collection-Related Expression Features • Functional Parameters – The parameter "key" • • • max(range(3,

Collection-Related Expression Features • Functional Parameters – The parameter "key" • • • max(range(3, 7), key=abs) : 7 max(range(-7, 3), key=abs) : -7 lst = ['T', 'G', 'A', 'G', 't', 'g', 'a', 'g'] lst. sort() lst. sort(key=str. lower) 11

Collection-Related Expression Features • Anonymous functions – lambda args: expression-using-args def fn (x, y):

Collection-Related Expression Features • Anonymous functions – lambda args: expression-using-args def fn (x, y): return x*x + y*y fn = lambda x, y: x*x + y*y l = [(3, 'abc'), (5, 'ghijk'), (5, 'abcde'), (2, 'bd')] l. sort() l. sort(key=lambda seq: (len(seq), seq. lower()))) 12

Control Statements

Control Statements

Conditionals 14

Conditionals 14

Loops 15

Loops 15

Loops • Simple Loop Examples def echo(): while echo 1(): pass def echo 1():

Loops • Simple Loop Examples def echo(): while echo 1(): pass def echo 1(): line = input('Say something: ') print('You said', line) return line def polite_echo(): while echo 1() != 'bye': pass 16

Initialization of Loop Values 17

Initialization of Loop Values 17

Initialization of Loop Values def recording_echo(): # initialize entry and lst vlst = []

Initialization of Loop Values def recording_echo(): # initialize entry and lst vlst = [] # get the first input entry = echo 1() # test entry while entry != 'bye': # use entry lst. append(entry) # change entry = echo 1() # repeat # return result return lst 18

Looping Forever 19

Looping Forever 19

Loops with Guard Conditions 20

Loops with Guard Conditions 20

Iterations • Iteration Statements – Iteration statements all begin with the keyword for 21

Iterations • Iteration Statements – Iteration statements all begin with the keyword for 21

Exception Handlers def get_gi_ids(filename): with open(filename) as file: return [extract_gi_id(line) for line in file

Exception Handlers def get_gi_ids(filename): with open(filename) as file: return [extract_gi_id(line) for line in file if line[0] == '>'] IOError: [Errno 2] No such file or directory: 'aa 2. fasta‘ 22

Python Errors – Tracebacks – Runtime errors 23

Python Errors – Tracebacks – Runtime errors 23

Exception Handling Statements 24

Exception Handling Statements 24