Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python
Group practice in problem design and problem solving
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Python programs How can I run a program? Input and output.
A First Book of ANSI C Fourth Edition
Practical PHP IDIA Spring 2012 Bridget M. Blodgett.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Introduction to Python
Lists in Python.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Trinity College Dublin, The University of Dublin A Brief Introduction to Scientific Programming with Python Karsten Hokamp, PhD TCD Bioinformatics Support.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
Computer Science 101 Introduction to Programming.
Working on exercises (a few notes first). Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Working on exercises (a few notes first)‏. Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Trinity College Dublin, The University of Dublin GE3M25: Data Analysis, Class 4 Karsten Hokamp, PhD Genetics TCD, 07/12/2015
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
GE3M25: Computer Programming for Biologists Python, Class 5
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Python Let’s get started!.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Input and Output in python Josh DiCristo. How to output numbers str()  You can put any variable holding a number inside the parentheses and it will display.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Introduction to Programming
String and Lists Dr. José M. Reyes Álamo.
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Variables, Expressions, and IO
GE3M25: Data Analysis, Class 4
Introduction to Programming
String and Lists Dr. José M. Reyes Álamo.
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Fundamentals of Python: First Programs
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Computer Science
Python Strings.
Introduction to Programming
Introduction to Computer Science
Presentation transcript:

Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015

Trinity College Dublin, The University of Dublin Overview Recap Python2 vs Python3 Working with strings Reading a file Weekly task

Trinity College Dublin, The University of Dublin Python2 vs Python3 division treated as integer no parentheses required for print input expects an expression raw_input takes plain text

Trinity College Dublin, The University of Dublin Python2 vs Python3 division treated as float parentheses required for print input takes plain text

Trinity College Dublin, The University of Dublin Strings # assign a sequence string to a variable: seq = 'CTTTCACGGCTCTCTCTACAGGAATCGTATTCGGTCTCTTTTAAN' Rules for variable names:  Can contain both letters and numbers  Must begin with a letter  Can contain the underscore character  Must not clash with reserved keywords

Trinity College Dublin, The University of Dublin Strings  Strings are indexed, starting from 0  Indices are specified in square brackets, e.g. seq[0] seq[1] seq[-1]  Slices are specified via ':' and start/end are optional: seq[0:9] same as seq[:9] seq[9:1000] same as seq[9:]  A 3 rd parameter specifies an interval: seq[::2] extracts every second letter

Trinity College Dublin, The University of Dublin Exercises:  Define variable 'seq' as a 20 bp sequence string  Try to extract sub-strings using indices and slices 1.The first character 2.The last character 3.The 4 th till 9 th character 4.The last 10 characters 5.Every 3 rd character 6.What does [::-1] do?

Trinity College Dublin, The University of Dublin Functions that work on strings  len()  list()  max()  min()  print()  sorted() Give them a try!

Trinity College Dublin, The University of Dublin String Methods  Many more methods associated with strings  Try dir(seq) for an overview  These are applied as follows: variable.function(arguments), e.g. seq.find('A')  Documentation available: help(seq.isalpha)

Trinity College Dublin, The University of Dublin String Methods  Applying a method does not change the string!  Assign output to a new or the same variable to make changes permanent: rna = dna.replace('t', 'u') dna = dna.lower()

Trinity College Dublin, The University of Dublin Exercises 1. Find out what the following functions do: startswith, rindex, join, replace, upper, rjust, strip 2. Change the sequence to lower case 3. Change all 't' to 'u' and save as 'rna' 4. Add an 'n' to the end of your sequence 4. Find the first occurrence of 'n' 5. Count how many 'g' are in the sequence

Trinity College Dublin, The University of Dublin The for loop Repeating an action a defined number of times:

Trinity College Dublin, The University of Dublin The for loop Loops can be nested:

Trinity College Dublin, The University of Dublin Exercises - Loop through 'AGTC' to find occurrences of these letters in your (upper-case) sequence variable - Print out letter and number of occurrences

Trinity College Dublin, The University of Dublin Formatting Output % operator: instead of:

Trinity College Dublin, The University of Dublin % operator: Formatting Output Space holders with formatting variables

Trinity College Dublin, The University of Dublin Formatting Output Space holders: i integer f,e,E normal, 'E' notation of floating point numbers s strings % literal '%'

Trinity College Dublin, The University of Dublin Formatting Output Modifications: %x.yf overall width and digits after decimal point %5.2f  left padded with space to width of 5, precision 2 %0xi zero-padding %05i  numbers < are left-padded with 0 %-xs left align %-5s  strings will be right-padded with space to length 5

Trinity College Dublin, The University of Dublin Exercise Calculate the GC content of a DNA string stored in a variable 1. get count of 'G' 2. get count of 'C' 3. get length of string 4. calculate (G+C)/len* print result to screen (rounded to two digits after the decimal point)

Trinity College Dublin, The University of Dublin Reading from files Built-in function 'open': open( name ) open( name, mode) name = path to file mode = 'r', 'w', 'a' (read, write, append) e.g.: open('/Users/kahokamp/Downloads/test.fa', 'r') Error if file does not exist!

Trinity College Dublin, The University of Dublin Reading from files Open returns a file handle, capture in a variable: f = open('/Users/kahokamp/Downloads/test.fa', 'r') Methods for f: f.read() f.readline() f.close() read in whole content read in one line close file handle

Trinity College Dublin, The University of Dublin Reading from files file = '/Users/kahokamp/Downloads/test.fa' f = open(file, 'r') header = f.readline() print("Header: %s" +% header) for line in f: print(line) f.close() Line contains newline character at the end!

Trinity College Dublin, The University of Dublin Reading from files file = '/Users/kahokamp/Downloads/test.fa' f = open(file, 'r') line = f.readline() while line : print(line) line = f.readline() f.close() using the while loop

Trinity College Dublin, The University of Dublin Exercise Write a script that reads a FASTA sequence and reports the GC content. Start with Pseudocode and work in small steps!

Trinity College Dublin, The University of Dublin Learnt so far: String functions and methods String formatting For loop Opening a file for reading Reading line by line from a file handle

Trinity College Dublin, The University of Dublin Weekly task Read a Fastq file Shorten the DNA and quality strings to a width of 30 characters Print output to screen or to a new file Submit Python script by to Latest by next Tuesday (24 th Nov) 10 am Include loads of comments Even pseudocode will give you points!

Trinity College Dublin, The University of Dublin Don't forget to log out!