Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stream.

Similar presentations


Presentation on theme: "Stream."— Presentation transcript:

1 Stream

2 Streams temporally ordered sequence of indefinite length

3 Files

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

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

6 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.

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

8 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()])

9 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)

10 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]

11 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)

12 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())))

13 Control Statements

14 Conditionals

15 Loops

16 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':

17 Initialization of Loop Values

18 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

19 Looping Forever

20 Loops with Guard Conditions

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

22 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‘

23 Python Errors Tracebacks Runtime errors

24 Exception Handling Statements


Download ppt "Stream."

Similar presentations


Ads by Google