LAST TIME..THE COMMAND LINE Maybe slightly to fast.. Finder Applications Utilities Terminal The color can be adjusted and is simply a matter of taste. Per default it is black! Dr. Robert Kofler Introduction to Programming May 16, 2013 1 / 30
THE FINAL GAME Store in python folder import random print ( Welcome ) randomnumber = random. randint ( 1, 1 0 0 ) attempts =1 while ( True ) : guess= i n t ( input ( Guess the number : ) ) i f guess==randomnumber : print ( You win, a f t e r, attempts, attempts ) break e l i f guess < randomnumber : print ( The number i s l a r g e r ) e lse : print ( The number i s smaller ) attempts +=1 # l a c y man s s y n t a x f o r : a t t e m p t s = a t t e m p t s +1 print ( Game over ) # s a v e as improvedgame. py # F5 Dr. Robert Kofler Introduction to Programming May 16, 2013 2 / 30
REMINDER: UNIX COMMANDS whoami pwd : print working directory ls : list the files in my current directory cd : change directory python3.3 : start a script using the Python3 interpreter Dr. Robert Kofler Introduction to Programming May 16, 2013 3 / 30
START THE GAME FROM THE UNIX-SHELL = Enter button cd Desktop/pythonlecture (= change to the path where the script is stored) in case your folder has a different name use ls -l and cd to navigate into the folder containing your python script python3.3 finalgame.py Enjoy your game (= tell python3 to interpret your script) In case you see something weird like >>> you accidentially entered the Python command line. Type quit() to escape Dr. Robert Kofler Introduction to Programming May 16, 2013 4 / 30
START A PYTHON SCRIPT DIRECTLY FROM STUDENT python3.3 Desktop/python/finalgame.py Dr. Robert Kofler Introduction to Programming May 16, 2013 5 / 30
STARTING A PYTHON SCRIPT FROM ANYWHERE USING ABSOLUTE PATHS You are at bin and want to start the game python3.3 /Users/student/Desktop/python/finalgame.py Dr. Robert Kofler Introduction to Programming May 16, 2013 6 / 30
MINI TASK: START YOUR PYTHON SCRIPT USING ABSOLUTE PATHS python3.3 /Users/student/Desktop/python/finalgame.py Hint: use (= Tabulator) for autocomplete! type: python3.3 /U continue: python3.3 /Users/s continue: python3.3 /Users/student/D... Dr. Robert Kofler Introduction to Programming May 16, 2013 7 / 30
SUMMARY: ABSOLUTE PATH - RELATIVE PATH absolute path: always starts with root symbol / and is ALWAYS the same for any given directory, irrespective to your position in the directory tree. Is more conservative, mixing up files is more difficult. For my scripts I frequently provide absolute paths as parameters because they are unambiguous. relative path: never starts with the root symbol. It is always relative to your position. May be more convenient because usually you have to type less. Dr. Robert Kofler Introduction to Programming May 16, 2013 8 / 30
PLAYED ENOUGH Dr. Robert Kofler Introduction to Programming May 16, 2013 9 / 30
LET S GET BIOMEDICAL Dr. Robert Kofler Introduction to Programming May 16, 2013 10 / 30
DOWNLOADING THE HUMAN GENES http://drrobertkofler.wikispaces.com/pythonlecture store in your python folder Dr. Robert Kofler Introduction to Programming May 16, 2013 11 / 30
THE ABSOLUTE PATH OF FILES genes.txt: /Volumes/Temp2/genes.txt humang.txt: /Users/student/Desktop/humang.txt human-genes.txt:? Dr. Robert Kofler Introduction to Programming May 16, 2013 12 / 30
MINI TASK: FIND THE ABSOLUTE PATH OF YOUR HUMAN-GENES FILE First open a new IDLE window. Copy the absolute path as a comment to IDLE. Hint: use the command line, navigate into the folder containing your genes-file and type pwd to get the directory. You can than manually enter the rest. Hint: if you are lazy, use autocomplete Dr. Robert Kofler Introduction to Programming May 16, 2013 13 / 30
PRINTING THE CONTENT OF THE GENES FILE genepath= /Users/ r o b e r t k o f l e r /Desktop/python/human genes. g t f # we use a f o r l o o p t o i t e r a t e o v e r t h e f i l e, l i n e by l i n e for l i n e in open ( genepath, r ) : print ( l i n e ) # s a v e as g e n e p r i n t e r. py > F5 # p r e s s C t r l c t o s t o p i t Dr. Robert Kofler Introduction to Programming May 16, 2013 14 / 30
FILE OBJECT - ANOTHER OBJECT If you want to read a file, you need the following information: position of the file = absolute path read the file or write to the file? what is my current position in the file, i.e.: which line have I last read. All this information is stored in the file object! You do not have to worry about the current position in the file, you are just reading line by line. The file object is thus another object, so which objects have we met so far? Dr. Robert Kofler Introduction to Programming May 16, 2013 15 / 30
WHY OBJECTS Helps you modelling the real world. Imagine you are programming a farm-simulator (Farmville 3). You would create an object for a horse, a cow, a barn, the farmer, of course also for the farmer/innen, the pond, etc.. It thus allows you to develop the software at a very picturesque (tangible level). Instead of loops within loops, you have cows interacting with farmers (nice level ob abstraction). Dr. Robert Kofler Introduction to Programming May 16, 2013 16 / 30
WHY DO WE NEED OBJECTS? Unfortunatelly there is no escape from objects in Python: file object, range even the string.. We will not learn how to create objects (remember 1 hour lecture..) but we will learn how to use them! And remember in addition to the basic data types int, float, list, tuple we now add infinitely many different types of objects.. Dr. Robert Kofler Introduction to Programming May 16, 2013 17 / 30
FORMAL DEFINITION OF AN OBJECT Objects in object-oriented programming are essentially data structures together with their associated processing routines. A object thus has: data structures processing routines (=called methods; similar to functions) Dr. Robert Kofler Introduction to Programming May 16, 2013 18 / 30
MODELING A COW Which data-structures (which information does a cow have)? age milk yield?? any other ideas Which formal routines (what can you do with a cow)? feed() milk()?? Dr. Robert Kofler Introduction to Programming May 16, 2013 19 / 30
CLASS VS INSTANCE Class All cows adhere to the same template; the all have a age, a milk yield and so on, this template is called class. Instance One specific cow adhering to the template (class). Thus the cow named Resi is an instance, a cow named Pepi is an instance and so on. To summarize, a class cow represents the general concept of a cow, an instance of a cow is one particular cow. # C r e a t e an i n s t a n c e o f a cow Cow ( ) r e s i = Cow( 1 2, 4000) # age, m ilk y i e l d r e s i. feed ( 2 ) # g i v e 2 k i l o o f g r a s s # n o t e t h e p o i n t. # with t h e p o i n t we a r e c a l l i n g a method f o r # t h e p a r t i c u l a r i n s t a n c e milk = r e s i. milk ( ) # g e t t h e m ilk Dr. Robert Kofler Introduction to Programming May 16, 2013 20 / 30
BACK TO THE FILE OBJECT # c r e a t e an i n s t a n c e o f a f i l e o b j e c t fo=open ( /Users/student/Desktop/python/human genes. g t f, r ) # f o i s one i n s t a n c e o f t h e f i l e o b j e c t cowreader=open ( /Users/student/Desktop/cow genes. g t f, r ) # c o w r e a d e r i s a n o t h e r i n s t a n c e o f t h e f i l e o b j e c t # what a r e t h e p r o c e s s i n g r o u t i n e s (= methods )? fo. read ( ) fo. write ( ) fo. seek ( ) # Reminder : main use o f t h e f i l e o b j e c t for l i n e in fo : # do something Dr. Robert Kofler Introduction to Programming May 16, 2013 21 / 30
SUMMARY: METHOD VS FUNCTION function(): standalone; e.g.: print(), range(), int() method(): they are never alone, always only with an instance of an object; e.g.: resi.feed(2), fileobject.read(), string.split( ) what about: fileobject.feed(2)? Dr. Robert Kofler Introduction to Programming May 16, 2013 22 / 30
INSPECTING THE HUMAN-GENE FILE The human genes are stored in the widely used gtf-format. New UNIX commands head and tail Head prints the first 10 lines of a file whereas tail prints the last 10 lines of a file. move into the folder where you stored the gene file! type: head human-genes.gtf The gtf file contains information about gene s Dr. Robert Kofler Introduction to Programming May 16, 2013 23 / 30
REFRESHER: GENES AND CHROMOSOMES A gene is a region in a chromosome, usually codeing for a protein. Dr. Robert Kofler Introduction to Programming May 16, 2013 24 / 30
GENE IN THE GTF-FILE column 1: the chromosome of the gene (is a string) column 4: start position of the gene column 5: end position of the gene column 7: strand of the gene column 9: diverse information and comments, like the name of the gene Dr. Robert Kofler Introduction to Programming May 16, 2013 25 / 30
THE LENGTH OF GENES genepath= /Users/ r o b e r t k o f l e r /Desktop/python/human genes. g t f # we use a f o r l o o p t o i t e r a t e o v e r t h e f i l e, l i n e by l i n e for l i n e in open ( genepath, r ) : tmp= l i n e. s p l i t ( \ t ) # \ t = symbol f o r t a b u l a t o r s t a r t = i n t ( tmp [ 3 ] ) end= i n t ( tmp [ 4 ] ) print ( end s t a r t ) # s a v e as g e n e l e n g t h. py > F5 # p r e s s C t r l c t o s t o p i t Dr. Robert Kofler Introduction to Programming May 16, 2013 26 / 30
SPLIT(): STRING IS ACTUALLY AN OBJECT # c r e a t e a s t r i n g t e s t = the small c a t jumps the quick # s p l i t i t a t s p a c e and we g e t a l i s t a= t e s t. s p l i t ( ) print ( a ) # NOTE: a l i s t / t u p l e can c o n t a i n a n y t h ing!! # not j u s t i n t e g e r s # now we s p l i t i t a t c a t b= t e s t. s p l i t ( c a t ) print ( b ) # s a v e as s t r i n g o b j e c t. py # F5 Dr. Robert Kofler Introduction to Programming May 16, 2013 27 / 30
SPECIAL SYMBOLS IN A STRING # c r e a t e a s t r i n g t e s t = the \ t s m a l l \ ncat \njumps\ nthe \ tquick \n\n\n print ( t e s t ) # \ t = t a b u l a t o r # \n =new l i n e # r s t r i p ( ) removes something from t h e r i g h t end # we want t o g e t r i d o f t h e annoying \n\n\n t e s t = t e s t. r s t r i p ( \n ) print ( t e s t ) Dr. Robert Kofler Introduction to Programming May 16, 2013 28 / 30
QUICK DETOUR: GENE PRINTING IMPROVED genepath= /Users/ r o b e r t k o f l e r /Desktop/python/human genes. g t f # we use a f o r l o o p t o i t e r a t e o v e r t h e f i l e, l i n e by l i n e for l i n e in open ( genepath, r ) : # when r e a d i n g a f i l e e v e r y # l i n e ends with an \n > so l e t s remove i t l i n e = l i n e. r s t r i p ( \n ) print ( l i n e ) Dr. Robert Kofler Introduction to Programming May 16, 2013 29 / 30
TASK 6: X VS Y # Task6 : X vs Y # G i r l s : count t h e number o f g e n e s on t h e X chromosome # Boys : count t h e number o f g e n e s on t h e Y chromosome # example output : # X has??? g e n e s ( g i r l s ) # Y has??? g e n e s ( boys ) # Hint : use s p l i t # s a v e as t a s k 6 XvsY. py in your t a s k s f o l d e r # F5 Dr. Robert Kofler Introduction to Programming May 16, 2013 30 / 30