Presentation is loading. Please wait.

Presentation is loading. Please wait.

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|

Similar presentations


Presentation on theme: "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|"— Presentation transcript:

1 File Handle and conditional Lecture 2

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

3 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’

4 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)

5 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

6 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…..”

7 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

8 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

9 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…..”;

10 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

11 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)

12 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”; }

13 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.

14 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.

15 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.


Download ppt "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|"

Similar presentations


Ads by Google