9/23/2015BCHB524 - 2015 - Edwards Advanced Python Data Structures BCHB524 2015 Lecture 7.

Slides:



Advertisements
Similar presentations
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Advertisements

DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Python Dictionary.
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
Lists in Python.
Introduction to Python September 26, /10/ Bioinformatics Languages Low-level, compiled languages: C, C++, Java… Pros: performance Cons:
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
9/16/2015BCHB Edwards Introduction to Python BCHB Lecture 5.
Lists and the ‘ for ’ loop. Lists Lists are an ordered collection of objects >>> data = [] >>> print data [] >>> data.append("Hello!") >>> print data.
Built-in Data Structures in Python An Introduction.
10/20/2014BCHB Edwards Advanced Python Concepts: Modules BCHB Lecture 14.
8/29/2014BCHB Edwards Introduction to Python BCHB Lecture 2.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Chapter 14 Advanced Function Topics CSC1310 Fall 2009.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
9/28/2015BCHB Edwards Basic Python Review BCHB Lecture 8.
9/21/2015BCHB Edwards Python Data Structures: Lists BCHB Lecture 6.
11/4/2015BCHB Edwards Advanced Python Concepts: Object Oriented Programming BCHB Lecture 17.
GE3M25: Computer Programming for Biologists Python, Class 5
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
9/2/2015BCHB Edwards Introduction to Python BCHB524 Lecture 1.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
11/9/2015BCHB Edwards Advanced Python Concepts: OOP & Inheritance BCHB Lecture 18.
9/11/2015BCHB Edwards Introduction to Python BCHB Lecture 3.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
DAY 3. ADVANCED PYTHON PRACTICE SANGREA SHIM TAEYOUNG LEE.
Introduction to Python
Introduction to Python
Advanced Python Idioms
Introduction to Python
Introduction to Python
Advanced Python Concepts: Modules
Advanced Python Data Structures
Introduction to Python
Introduction to Python
Advanced Python Data Structures
Introduction to Python
Introduction to Python
Lecture 10 Data Collections
Advanced Python Concepts: Object Oriented Programming
Advanced Python Concepts: OOP & Inheritance
Bryan Burlingame Halloween 2018
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Advanced Python Concepts: OOP & Inheritance
Advanced Python Concepts: Object Oriented Programming
Python Data Structures: Lists
Introduction to Python
Advanced Python Concepts: Exceptions
Introduction to Python
Advanced Python Data Structures
Advanced Python Concepts: Modules
Advanced Python Concepts: OOP & Inheritance
Introduction to Python
Advanced Python Idioms
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Introduction to Python
CHAPTER 4: Lists, Tuples and Dictionaries
Python Data Structures: Lists
Advanced Python Concepts: Exceptions
Introduction to Python
Introduction to Python
Python Data Structures: Lists
Advanced Python Idioms
Advanced Python Concepts: Modules
Bryan Burlingame Halloween 2018
Advanced Python Concepts: Object Oriented Programming
Presentation transcript:

9/23/2015BCHB Edwards Advanced Python Data Structures BCHB Lecture 7

9/23/2015BCHB Edwards2 Outline Revision of list data-structures Advanced Data-structures Dictionaries, Sets, Files Reading, parsing files (codon tables) Exercises

9/23/2015BCHB Edwards3 Data-structures: Lists Compound data-structure: Many objects in order numbered from 0 [] indicates list. Item access and iteration Same as for string, "l[i]" for item i "for item in l" for each item of the list. List modification items can be changed, added, or deleted. Range is a list String ↔ List

9/23/2015BCHB Edwards4 Python Data-structures: Dictionaries Compound data-structure, stores any number of arbitrary key-value pairs. Keys and/or value can be different types Can be empty Values can be accessed by key Keys, values, or pairs can be accessed by iteration Values can be changed Key, value pairs can be added Key, value pairs can be deleted

9/23/2015BCHB Edwards5 Dictionaries: Syntax and item access # Simple dictionary d = {'a': 1, 'b': 2, 'acdef': 3} print d # Access value using its key print d['a'] # Change value associated with a key d['acdef'] = 5 print d # Add value by assigning to a dictionary key d['newkey'] = 10 print d

9/23/2015BCHB Edwards6 Dictionaries: Iteration # Initialize d = {'a': 1, 'b': 2, 'acdef': 5, 'newkey': 10} # keys from d print d.keys() # values from d print d.values() # key-value pairs from d print d.items() # Iterate through the keys of d for k in d.keys(): print k, print # Iterate through the key-value pairs of d for k,v in d.items(): print k,"=",v, print

9/23/2015BCHB Edwards7 Dictionaries: Different from lists? # Initialize d = {} # Add some values, integer keys! d[0] = 1 d[1] = 2 d[10] = 1000 # See how the dictionary looks print d # Test whether a key is in the dictionary print "Is key 15 in d?",d.has_key(15) # Access value with key 15 with default -1 print "Value for key 15, or -1:",d.get(15,-1) # Access value with key 15 - error! print "Value for key 15:",d[15]

9/23/2015BCHB Edwards8 Python Data-structures: Sets Compound data-structure, stores any number of arbitrary distinct data-items. Data-items can be different types Can be empty Items can be accessed by iteration only. Items can be tested for membership. Items can be added Items can be deleted

9/23/2015BCHB Edwards9 Sets: Add and Test Elements # Make an empty set s = set() print s # Add an element, and then a list of elements s.add('a') s.update(['b','c','d']) print s # Test for membership print "e is in s",('e' in s) print "e is not in s",('e' not in s) print "c is in s",('c' in s)

9/23/2015BCHB Edwards10 Python Data-structures: Files Read strings from file, or Write strings to file. Get access to strings by iteration. Write by printing strings to file. Need to open and close files: Need to indicate whether we want to read or write.

9/23/2015BCHB Edwards11 Files: Reading # Open a file, store "handle" in f f = open('anthrax_sasp.nuc') # MAGIC! print ''.join(f.read().split()) # Close the file. f.close() # Slowly, now... f = open('anthrax_sasp.nuc') # Store the entire file's contents in s (as string) s = f.read() print s # Split s at whitespace sl = s.split() print sl # Join split s with nothing in between jl = ''.join(sl) print jl # Close the file f.close()

9/23/2015BCHB Edwards12 Files: Reading # Open a file f = open('anthrax_sasp.nuc') # Iterate line-by-line for line in f: print line # Close the file f.close() # Open a file f = open('anthrax_sasp.nuc') # Iterate line-by-line, and accumulate the sequence seq = "" for line in f: seq += line.strip() print "The sequence is",seq # Close the file f.close()

9/23/2015BCHB Edwards13 DNA Translation First read a codon table from a file Codon table from NCBI's on-line taxonomy resource Read line by line and use initial word to store 3 rd word appropriately. AAs = FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG Starts = ---M M M Base1 = TTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAGGGGGGGGGGGGGGGG Base2 = TTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGG Base3 = TCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAG

9/23/2015BCHB Edwards14 DNA Translation f = open('standard.code') data = {} for l in f: sl = l.split() key = sl[0] value = sl[2] data[key] = value f.close() b1 = data['Base1'] b2 = data['Base2'] b3 = data['Base3'] aa = data['AAs'] st = data['Starts'] codons = {} init = {} n = len(aa) for i in range(n): codon = b1[i] + b2[i] + b3[i] codons[codon] = aa[i] init[codon] = (st[i] == 'M')

9/23/2015BCHB Edwards15 DNA Translation f = open('anthrax_sasp.nuc') seq = ''.join(f.read().split()) f.close() seqlen = len(seq) aaseq = [] for i in range(0,seqlen,3): codon = seq[i:i+3] aa = codons[codon] aaseq.append(aa) print ''.join(aaseq)

9/23/2015BCHB Edwards Exercise 1 Using just the concepts introduced so far, find as many ways as possible to code DNA reverse complement (at least 3!) You may use any built-in function or string or list method. You may use only basic data-types and lists and dictionaries. Compare and critique each technique for robustness, speed, and correctness. 16

9/23/2015BCHB Edwards17 Exercise 2 Write a program that takes a codon table file (such as standard.code from the lecture) and a file containing nucleotide sequence (anthrax_sasp.nuc) as command-line arguments, and outputs the amino-acid sequence. Modify your program to indicate whether or not the initial codon is consistent with the codon table's start codons. Use NCBI's taxonomy resource to look up and download the correct codon table for the anthrax bacterium. Re-run your program using the correct codon table. Is the initial codon of the anthrax SASP gene a valid translation start site?

Homework 4 Due Monday, September 28. Submit using Blackboard Use only the techniques introduced so far. Make sure you can run the programs demonstrated in lecture(s). Exercises 1, 2 from Lecture 7 9/23/2015BCHB Edwards18