4.1 Revision. 4.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
CS0004: Introduction to Programming Repetition – Do Loops.
Introduction to C Programming
4.1 Reading and writing files. 4.2 Open a file for reading, and link it to a filehandle: open(IN, "
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Computer Science 1620 Loops.
6a.1 More about files: Globbing. 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
4ex.1 More loops. 4ex.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) {
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
5.1 Previously on... PERL course (let ’ s practice some more loops)
Chapter 5: Loops and Files.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Getting Started with Perl (and Excel) Biophysics 101 September 17, 2003 Griffin Weber (With material from Jon Radoff and Ivan Ovcharenko)
10.1 Sorting and Modules בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
Introduction to C Programming
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
3.1 Ifs and Loops. 3.2 Revision: variables Scalar variables can store scalar values: Variable declaration my ($priority); Numerical assignment $priority.
4.1 More loops. 4.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) { $num.
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Bioinformatics 生物信息学理论和实践 唐继军
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl Scripting II Conditionals, Logical operators, Loops, and File handles Suzi Lewis Genome Informatics.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Scripts & Functions Scripts and functions are contained in .m-files
Perl for Bioinformatics
Iteration: Beyond the Basic PERFORM
REPETITION Why Repetition?
Presentation transcript:

4.1 Revision

4.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number = ; if ($number 100) { print "ERROR: The average must be between 0 and 100.\n"; } elsif ($number > 90) { print "wow!\n"; } elsif ($number > 80) { print "well done.\n"; } else { print "oh well...\n"; } Note the indentation: a single tab in each line of new block ‘ } ’ that ends the block should be in the same indentation as where it started

4.3 if ($age = 18)... Found = in conditional, should be == at... if ($name == "Yossi")... Argument "Yossi" isn't numeric in numeric eq (==) at... Comparison operators StringNumericComparison eq==Equal ne!=Not equal lt<Less than gtgt>Greater than le>= Less than or equal to ge<= Greater than or equal to if ($age == 18){... } if ($name eq "Yossi")... if ($name ne "Yossi")... if ($name lt "n")... NumericComparison ==Equal !=Not equal <Less than >Greater than >= Less than or equal to <= Greater than or equal to

4.4 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) { $num = ; } print "correct!\n"; = ; my $name; foreach $name { print "Hello $name!\n"; }

4.5 Loops: foreach The foreach loop passes through all the elements of an array = (1,1,2,3,5); foreach my $number { $number++; } Note: The array is actually changed

4.6 Fasta format Fasta format sequence begins with a single-line description, that start with ' > ', followed by lines of sequence data that contains new-lines after a fixed number of characters: >gi| |ref|NM_ | Homo sapiens EH-domain AAACATGGCGGCGCCCTGCGCGGCTTCCCGTCGCCGCAACCGTGGGGCCGGCCCTGCCTT GGAGCGGAGCCGAAGCATCCCTTGCTGCACGCAGGGCAGAGCAGGCGAGGGCTGGGGGCC GTATAACTTATTTTATATCCATATTCAGACTATATAGAGAATATTCTATGCATCTATGAC GTGCTTAC >gi| |ref|NM_ | Pongo abelii EH-domain AGAGCTGAGCGCCTGCCCACAAACATGGCGGCGCCCTGCGCGGCTTCCCTTCGCCGGGAC CGCCTGGGGCTGCAGGATGCTGCTGCGGATGCTGAGCTGTCCGCGGGTTGGGCAGCGTCG CTGCGCGGCTTCCCTT >gi| |ref|NM_ | Xenopus tropicalis EH-domain CGGGCAAGACCACCTTCATCCGCCACCTCATAGAGCAGGACTTCCCCGGCATGAGGATCG GGCCCGAACCGGGGACTTCCTCTGCGCGCCGGCTTCCTGCCCAGCTGGCATTTAAACCAC ACATGGCGGCGCCCTGCGCGGCTTCCCGTCGCCGCAACCGTGGGGCCGGCC

4.7 Breaking out of loops next – skip to the next iteration last – skip out of the loop = ; foreach $line { if (substr($line,0,1) eq ">") { next; } if (substr($line,0,8) eq "**stop**") { last; } print($line); }

4.8 More loops

4.9 Scope of variable declaration If you declare a variable inside a loop it will only exist in that loop This is true for every { block } : my $name=""; while ($name ne "Yossi") { chomp($name = ); print "Hello $name, what is your age?\n"; my $age; $age = ; } print $name; print $age; Global symbol "$age" requires explicit package name

4.10 Never declare the same variable name twice If you declare a variable name twice, outside and inside – you are creating two distinct variables… don’t do it! my $name = "Ruti"; print "Hello $name!\n"; my $number; foreach $number (1,2,3) { my $name = "Nimrod"; print "Hello $name!\n"; } Hello Ruti! Hello Nimrod! Hello Ruti!

4.11 Never declare the same variable name twice If you declare a variable name twice, outside and inside – you are creating two distinct variables… don’t do it! my $name = "Ruti"; print "Hello $name!\n"; my $number; foreach $number (1,2,3) { $name = "Nimrod"; print "Hello $name!\n"; } Hello Ruti! Hello Nimrod!

4.12 Reminder: Uninitialized (undefined) variables If uninitialized variables are used (before assignment) awarnings is issued: my $a; print($a+3); Use of uninitialized value in addition (+) 3 my $line; print("line is $line"); Use of uninitialized value in concatenation (.) or string line is

4.13 Is this variables defined? defined check whether a variable was defined. my $a; if (defined $a){ print($a+3); } my $line = ; while (defined $line){ print "line is $line"; $line = ; } print "done!!!\n" ctrl-z to indicate end of input

4.14 Is this variables defined? defined check whether a variable was defined. my $line = ; while (defined $line){ if (substr($line,0,1) eq ">"){ print "$line"; } $line = ; }

4.15 FASTA: Analyzing complex input Assignment: Write a script that reads several DNA sequences in FASTA format, and prints for each sequence print its header and its G+C content | Obtain from the assignment:  Input  Required Output  Required processes (functions)

4.16 FASTA: Analyzing complex input Overall design: Read the FASTA file (several sequences). For each sequence: 1.Read the FASTA sequence 1.1. Read FASTA header 1.2. Read each line until next FASTA header 2.For each sequence: Do something 2.1. Compute G+C content 2.2. Print header and G+C content Let’s see how it’s done… Do something End of input? No End Start Save header Read line Header or end of input Yes Concatenate to sequence No Read line

4.17 # 1. Read FASTA sequece $fastaLine = ; while (defined $fastaLine) { # 1.1. Read FASTA header $header = substr($fastaLine,1); $fastaLine = ; # 1.2. Read sequence until next FASTA header while ((defined $fastaLine) and (substr($fastaLine,0,1) ne ">" )) { $seq.= $fastaLine; $fastaLine = ; } # 2. Do something... # 2.1 compute $gcContent print "$header: $gcContent\n"; } Do something End of input? No End Start Save header Read line Header or end of input Yes Concatenate to sequence No Read line

4.18 Class exercise 4a 1.Write a script that reads lines of names and expenses: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 6.10,24.00,7.00,8.00 END For each line print the name and the sum. Stop when you reach "END" 2.Change your script to read names and expenses on separate lines, Identify lines with numbers by a "+" sign as the first character in the string: Yossi Dana Refael END Sum the numbers while there is a '+' sign before them. Output: Yossi 27.6 Dana 27 Refael 45.1 Output: Yossi 27.6 Dana 27 Refael 45.1

4.19 Class exercise 4a 3.(Home Ex. 2 Q. 5) Write a script that reads several protein sequences in FASTA format, and prints the name and length of each sequence. Start with the example code from the last lesson. 4*.Write a script that reads several DNA sequences in FASTA format, and prints FASTA output of the sequences whose header starts with ' Chr07 '. 5*.As in Q4, but now concatenate all the sequences whose header starts with ' Chr07 '.

4.20 A bit about Reading and writing files

4.21 Open a file for reading, and link it to a filehandle: open(IN, "<EHD.fasta"); And then read lines from the filehandle, exactly like you would from : my $line = ; = ; foreach $line Every filehandle opened should be closed: close(IN); Reading files

4.22 Open a file for writing, and link it to a filehandle: open(OUT, ">EHD.analysis") NOTE: If a file by that name already exists it will be overwritten! Print to a file: print OUT "The mutation is in exon $exonNumber\n"; And don't forget to: close(IN); Writing to files no comma here

4.23 Class exercise 4b 1.Change the script for class exercise 4a.1 to read the lines from an input file (instead of reading lines from keyboard). 2.Now, in addition, write the output of the previous question to a file named "class.ex.4a1.out" (instead of printing to the screen). 3*. Change the script for class exercise 4.a3 to receive from the user two strings: 1) a name of FASTA file 2) a name of an output file. And then - read from a FASTA file given by the user, and write to an output file also supplied by the user.