Stream
Streams temporally ordered sequence of indefinite length
Files
Files Creating file objects open(path, mode) close()
Files File methods fileobj.read([count]) fileobj.readline([count]) fileobj.readlines() fileobj.write(string) fileobj.writelines(sequence)
Files : Ex1 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.
Generators an object that returns values from a series it computes
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()])
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)
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 [b1 + b2 + b3 for b1 in chars for b2 in chars for b3 in chars]
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)
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())))
Control Statements
Conditionals
Loops
Loops Simple Loop Examples def echo(): while echo1(): pass line = input('Say something: ') print('You said', line) return line def polite_echo(): while echo1() != 'bye':
Initialization of Loop Values
Initialization of Loop Values def recording_echo(): # initialize entry and lst vlst = [] # get the first input entry = echo1() # test entry while entry != 'bye': # use entry lst.append(entry) # change entry # repeat # return result return lst
Looping Forever
Loops with Guard Conditions
Iterations Iteration Statements Iteration statements all begin with the keyword for
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: 'aa2.fasta‘
Python Errors Tracebacks Runtime errors
Exception Handling Statements