File Handle and conditional Lecture 2. A typical bioinformatics file: FASTA format Name of the following file is: DNA_sequence.fasta >gi|34529|emb|Y00477.1|

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Driving Test 1 Marking Scheme Focus on five areas to pass driving test 1.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
● Perl reference
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Lecture 2 Introduction to C Programming
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
Bioinformatics Lecture 7: Introduction to Perl. Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration.
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
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.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Script & Perl Programming Lesson 9. Shell Programming A shell script is a text file that contains Linux (UNIX) commands, which you enter using any.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Bioinformatics 生物信息学理论和实践 唐继军
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
Agenda Basic Logic Purpose if statement if / else statement
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Files Tutor: You will need ….
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Linux Administration Working with the BASH Shell.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bash Shell Scripting 10 Second Guide.
Introduction to perl programming: the minimum to know for practice!
Shell Script & Perl Programming
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
CSC 352– Unix Programming, Fall 2012
Matlab Training Session 4: Control, Flow and Functions
Perl for Bioinformatics
File Handling Programming Guides.
Control Structures: for & while Loops
Lesson 2. Control structures File IO - reading and writing Subroutines
Cmdlets “Command-lets”
Presented by, Mr. Satish Pise
CSC 352– Unix Programming, Fall, 2011
The Selection Structure
Bash Scripting CS 580U - Fall 2018.
DATA TYPES AND OPERATIONS
INTRODUCTION to PERL PART 1.
REPETITION Why Repetition?
Karan Thaker CS 265 Section 001
Presentation transcript:

File Handle and conditional Lecture 2

A typical bioinformatics file: FASTA format Name of the following file is: DNA_sequence.fasta >gi|34529|emb|Y | Human bone marrow serine protease gene (medullasin) (leukocyte neutrophil elastase gene) TTGTCAGAGCCCCAGCTGGTGTCCAGGGACTGACCGTGAGCCTGGGTGAAAGTGAGTTCCCCGTTGGAGGCA CCAGACGAGGAGAGGATGGAAGGCCTGGCCCCCAAGAATGAGCCCTGAGGTTCAGGAGCGGCTGGAGTGA GCCGCCCCCAGATCTCCGTCCAGCTGCGGGTCCCAGAGGCCTGGGTTACACTCGGAGCTCCTGGGGGAGGCC CTTGACGTGCTCAGTTCCCAAACAGGAACCCTGGGAAGGACCAGAGAAGTGCCTATTGCGCAGTGAGTGCCC GACACAGCTGCATGTGGCCGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCC GGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACAGGGCCCTGGG TAAACTGAGGCAGGCGACACAGCTGCATGTGGCCGGTATCACAGGGCCCTGGGTAAACTGAGGCAGGCGAC ACAGCTGCATGTGGCCGGTATCACGGGGCCCTGGATAAACAGAGGCAGGCGAGGCCACCCCCATCAAG…… The line in Bold is the “descriptor line” The rest are nucleotides bases The Y gene has over 5292 bp [most not shown here but can be found in file] Need to analyse such files and Perl as we will see is suited to manipulating such files. Note: FASTA files also exist for proteins

File Handling The Files associated with Perl are text files: e.g. text1.txt Files are a non-volatile form of storage stored on a hard drive, DVD, USB… In order to use a file it must be “transferred” into main memory The first step, in utilising a file requires that the file be opened (copied) to a specific location in main memory The basic type of “opening” is to open the file for reading – open MYFILE, ‘text1.txt’ or die “could not open file aborting…\n”; – MYFILE is called a file pointer (handler) and essentially is an alias for the files location – ‘text1.txt’ refers to the file : note the name is case sensitive – The above example requires the file to be in the same folder/directory as the perl program if it is not you have to insert the path (location) as well: e.g. c:\perltest\data.txt’

File Handling A file can also be opened for writing (to allow you to write data to it). – open MYFILE, ‘>text2.txt’ or die “could not open file aborting…\n”(open file for writing) – Note the > symbol before the file name tells the program to open it for writing and of course reading. – The file does not have to exist in order to use this function – However, every time you open a file for writing it over-writes the previous version A file can be open for appending (add data to existing file) – open MYFILE, ‘>>text3.txt’ or die “could not open file aborting…\n” – It this case the file does not have to exist (an empty one is created) and if it does exist the new information is added to the end of the file (appended)

File Handling: Read and Write read data from a file – $line = ; #read one line from file and assigns to variable $line – Chomp $line; Write/append data to a file – print MYFILE “Do you like computers….\n”; # write out to file – Print MYFILE “$line\n”; # write the contents of variable Close the file : – close MYFILE; read_write_append_file.pl

Conditional Statements: if The if statement is the most basic conditional statement: if the expression is true you execute the line after it; otherwise you go to the line after the if {….} statement. – print “Enter your age: ”; – $age = <>; – if ($age <= 5) #the conditional expression – { – print “You are too young to be using a computer.\n”; – } – print “continue with rest of program…..”

Conditional expression Operators == Equality $a == $b != Not equal $a != $b < Less than $a < $b > Greater than $a > $b <= Less than or equal to $a <= $b >= Greater than or equal to $a >= $b

String conditional operator eq Equality $a eq $b ne Not equal $a ne $b lt Less than $a lt $b gt Greater than $a gt $b le Less than or equal to $a le $b ge Greater than or equal to $a ge $b

Conditional Statements: if{ }else{ } This is used if there are two possible outcomes: if the conditional statement (condition) is true perform statement after if it is false the statement after the else #!/usr/bin/perl print “Enter your age: ”; #prompt user to input age $age = <>; if ($age <= 5) # the conditional statement { – print “you are to young to use a computer\n”; } else { – print “You can use a computer\n”; } Print “ continue with the rest of the program…..”;

Conditional statements: if elseif else If, elsif, else [nested if statements] Note it is elsif and not elseif #!/usr/bin/perl print “Enter your age: ”; $age = <>; if ($age <= 0) { – print “You do not exist so you can not use a computer.\n”; } elsif ($age <= 5) { print “you are a little to young to use a computer \n”; } else { print “Your are not to young to use a computer\n”; } if_else.pl

Boolean operators If the condition uses more 2 conditional expression they are connected via a Boolean operator: there are 3: – Logical AND if ($x ==$y) && (&x == ‘Y’) – logical OR if ($x = $y) || (&x = ‘Y’) – Logical NOT if ( $ = !$b)

Conditional statements: Boolean operators The If, elsif, else [nested if statements] can be written as a set of if statements with 2 conditional expression and a booolean operator. #!/usr/bin/perl print “Enter your age: ”; $age = <>; if ($age <= 0) { – print “You do not exist so you can not use a computer.\n”; } if ($age >0) && ($age <=5) # the use of a boolean operator { print “you are a little to young to use a computer \n”; } If ($age >5) { print “Your are not to young to use a computer\n”; }

Exercise 1: file handling Go to note pad++ and create a text file with a line of text called text1.txt Create and test the following programs – Program 1 Open the file for reading and print the contents to the screen – Program 2 Open a file called ‘file2.txt’ for writing: write your name and address to the file Confirms it works by writing a program to open and read the file – Program 3 Open file2.txt for appending and write the any data you want to the file, close it and open it in notepad++ to confirm it has worked.

Exercise 2: Tryptophan operon program Write a program that can simulate the Following “expression” rule: (tryptophan present no expression Tryptophan not present expression) – Ask user there is or there is not tryptophan – Validate the input: ensure only (y or n) are input by the user – Use a conditional statement to determine if the operon is transcribed or not transcribed.

Exercise 3: lac operon program Write a program that can simulate the Following “expression” rule: (no lactose or lactose and glucose: no expression; lactose only: expression – Ask user there is lactose and glucose – Use a conditional statement to determine if the operon is expressed or not expressed.