Lecture 2 BNFO 601.

Slides:



Advertisements
Similar presentations
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Advertisements

Lecture 2 BNFO 240. Perl online references
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
Lecture 4 BNFO 235 Usman Roshan. IUPAC Nucleic Acid symbols.
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.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Lists in Python.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
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.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
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.
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.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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.
References and Data Structures
String and Lists Dr. José M. Reyes Álamo.
GCSE COMPUTER SCIENCE Practical Programming using Python
C++ Memory Management – Homework Exercises
Chapter 11 - JavaScript: Arrays
3.1 Fundamentals of algorithms
COMPUTATIONAL CONSTRUCTS
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Arrays: Checkboxes and Textareas
Containers and Lists CIS 40 – Introduction to Programming in Python
Variables, Expressions, and IO
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Engineering Innovation Center
While Loops BIS1523 – Lecture 12.
Introduction to Python
The relational operators
Perl Variables: Array Web Programming.
Introduction to Python
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Introduction to File Input and Output
© Akhilesh Bajaj, All rights reserved.
Arrays Kingdom of Saudi Arabia
4. sequence data type Rocky K. C. Chang 16 September 2018
PHP.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Introduction to TouchDevelop
String and Lists Dr. José M. Reyes Álamo.
Agenda Collating sequence / Sorting data
How to use hash tables to solve olympiad problems
For loops Taken from notes by Dr. Neil Moore
Topics Sequences Lists Copying Lists Processing Lists
Topics Basic String Operations String Slicing
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
“Everything Else”.
Topics Basic String Operations String Slicing
Topics Introduction to File Input and Output
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

Lecture 2 BNFO 601

Perl online reference https://perldoc.perl.org/ https://perldoc.perl.org/perlcheat.html

Perl variables Scalar Examples Number String $myname = “Roshan”; $year = 2006;

Number operators

Strings Perl supports very powerful operations on strings. Basic operators below.

Arrays Array variables begin with @. Example @myarray=(1,2,3,4,5); @s=(“dna”, “rna”, “protein”); print @s outputs dnarnaprotein

Array operations

Hashes Hashes are like arrays, except that they are indexed by user defined keys instead of non-negative integers. Hashes begin with % Examples %h=(‘red’, 1, ‘blue’, 2, ‘green’, 3); %h=(“firstname”, “usman”, “lastname”, “roshan”);

Input and Output printf: C-like formatted output, very powerful print: unformatted output Example: printf “My name is %s\n”, $name; printf “Today is %s %d %d\n”, $month, $day, $year;

File I/O open(IN, “in.txt”); $line=<IN>; print $line; close IN; open(OUT, “>out.txt”); print OUT “line1\n”; close OUT;

File I/O open(OUT, “>>out.txt”); print OUT “line2\n”; close OUT; open(IN, “in.txt”); @lines=<IN>; #reads all lines into array print @lines; close IN; $input=<STDIN>; #read from standard input

Control structures--- if else block if (c) { s1; s2 ;...;} { s1; s2;...;} else if (c1) { s1; s2; ...;} elsif (c2) { ... } elsif (c3) {....} else { ...};

Control structures--- if else block This is program to compute max of two Numbers. $max=0; if($x > $y){ $max = $x;} else {$max = $y;}

Control structures--- for loop for(init_exp; test_exp; iterate_exp) {s1; s2; … ;} foreach $i (@some_list) { s1; s2; ...; } Examples: for ($i=0; $i < @arr; $i++) { print $arr[$i]; } foreach $e (@arr) { print $e; }

Control structures--- while loop while (condition) { s1; s2;...; } do { s1; s2; ...;} while condition; while (condition) {s1;if(c){last;} s2;...; } #last means exit the loop while (condition) {s1;if(c){next;} s2;...; } #next means go to next iteration

Subroutines sub <subroutine_name> { } #parameters are placed in @_ <code> . return; }

Scope You can create local variables using my. Otherwise all variables are assumed global, i.e. they can be accessed and modified by any part of the code. Local variables are only accessible in the scope of the code.

Pass by value and pass by reference All variables are passed by value to subroutines. This means the original variable lying outside the subroutine scope does not get changed. You can pass arrays by reference using \. This is the same as passing the memory location of the array.

Splitting and joining strings split: splits a string by regular expression and returns array @s = split(/,/); @s = split(/\s+/); join: joins elements of array and returns a string (opposite of split) $seq=join(“”, @pieces); $seq=join(“X”, @pieces);

Searching and substitution $x =~ /$y/ ---- true if expression $y found in $x $x =~ /ATG/ --- true if open reading frame ATG found in $x $x !~ /GC/ --- true if GC not found in $x $x =~ s/T/U/g --- replace all T’s with U’s $x =~ s/g/G/g --- convert all lower case g to upper case G

DNA regular expressions Taken from Jagota’s Perl for Bioinformatics

Exercises Write a program to read in two DNA sequences and print the number of matches and mismatches. Write a program to translate a DNA string into amino acids Write a program that searches for the open reading frame in a DNA sequence.

Write a program called count Write a program called count.pl that reads in a set of strings from a file called string.txt and prints the number of A’s, B’s, C’s, and D’s. For example, if the file looks like AABCAA CDAAD CCC then your program should output A 6 B 1 C 5 D 2

Write a program to prompt the user for a Yes or No response Write a program to prompt the user for a Yes or No response. Read in the user response using the STDIN file handle and print “OK” is the user enters “Yes,” “I hear you” if the user enters “No,” and “Make up your mind!” if the user enters something other than “Yes” or “No.” Create a script called foods.pl that asks the user for their favorite foods. Ask the user to enter at least 5 foods, each separated by a space (or some other delimiter). Store their answer in a scalar. Split the scalar into an array. Once the array is created, have the script do the following: * Print the array * Print the number of elements in the array * Create an array slice from three elements of the array and print the new array Write a program to open a file containing SSNs and read in the first five records into an array.

A. Input your name into a variable called $name and then print "Hello, (your name here)". B. Input your age into a variable called $age. Have the computer print out how old you'll be eight years from now, ten years from now, twenty-four years from now. C. Input a word to a variable. Then store the first two letters of that word into another variable and print those two letters to the screen. D. Use the length function to find the length of a string. Then print the last half of the string to the screen. E. Input your firstname and lastname as two separate variables called $firstname and $lastname respectively. Concatenate them together using the dot operator '.' into a new variable called $wholename. Then print out $wholename.

A. Modify if1. pl to get an inputted number for $x A. Modify if1.pl to get an inputted number for $x. print "Enter a number: "; B.Fix if2.pl to actually get the else to happen. C. Write a program that adds two numbers and then prints out whether the sum of those two numbers is positive or negative. D. Input a number and put it into your program. Have a friend attempt to guess the number. Have your program tell your friend whether their guess was high, low, or correct. E. Enter your age and your score on the permit test (out of 15). Have the computer output whether or not you're able to get your permit based on being at least 16 years old and having a score of at least 12.

A. Write a for() loop that counts from 0 to 15 by twos and prints out the number that its on. B. Write a while() loop that counts from 0 to 15 by threes and prints out the number that it is on. C. Guessing Game revisited: Alter your guessing game to repeat itself until the user guesses the correct number. Output the number of guesses that the person made. D. Make an endless loop. If you are successful, you will need to know that control+c is the keyboard command to kill the program. E. Print out a multiplication table from 0 to 9. (Hint, use nested loops.)

A. Write a program that asks a user for five groceries, and then store that list in an array called @grocerylist. Print out the items in the array alphabetically. B. Write a program that asks the user for some numbers. Store them in an array called @numarray. Print out the numbers in the opposite order that they were entered. Stop accepting input when the user enters a letter.

A. Write a program that asks the user for five groceries, and then write them to a file in alphabetical order. (Hint: This is similar to an array exercise.) B. Write a program that reads the file wrtitten in the last ecxercise, and then prints the contents of the file to the screen in the opposite order as they appeared in the file. C. Still working on the grocery list, ask the user for the name of a file that contains their grocery list. Then display the contents of that file, and ask the user if they wish to leave the list alone, add to the list, or replace the list. If the user wishes to add to or replace the list, take the input and write it to the file, as appropriate.

Exercise 3.1 Write a program to print out the cube root of a command line argument. Remember that taking the Nth root of a number is equivalent to raising it the the 1/N power. Exercise 3.2 Given a string and a number as command line arguments, write a program to print the string out as many times as the number indicates. (You don’t need a loop.) Exercise 3.3 Write a program to add up the size (in bytes) of all the text files whose names are entered on the command line. Print out the total.

Exercise 4.1 Prompt the user for a first name and last name, and read in both names at the same time into a single variable. Assume that the input will be in the correct format with precisely one space between the names. Use index to locate the space, and use substr to parse the name. Convert the last name to UPPERCASE and the first name to Title Case. Print the results in the format “LAST, First”. Exercise 4.2 Modify Exercise 4.1 to prompt for and read in the names inside of a loop. Open a file for writing and print each name, formatted as “LAST, First” to the output file. Continue looping until the user enters the null string in response to the prompt for a name. Exercise 4.3 Print out two lines in the following format (adjusting for today’s date): Today is "Tuesday, December 8, 1998" already. It's day 342 of the year. Use localtime in a list context to determine today’s date. Assign the names of the twelve months of the year to one array and the names of the seven days of the week to another. Exercise 4.4 Using Perl’s srand and rand functions write a program to simulate a Pick 6 Lotto program. Assuming a range of numbers from 1 through 46, pick 6 numbers and store them in an array. Make sure the same number is not selected more than once. Once the array is filled with 6 random values display the 6 numbers.

Exercise 5.1 Use the range operator to fill an array with the numbers 30–40. @ary = (30..40); Compare what happens when you use the reverse operator in a list context versus a scalar context. @new = reverse @ary; # list context $var = reverse @ary; # scalar context Print out the values of the @new array and the $var scalar. Exercise 5.2 Fill an array with the names of some animals commonly found in a zoo. Experiment with some of the list functions: 1. Delete one animal from the end of the array. Print out the name of the animal deleted. 2. Add two new animals to the beginning of the array. 3. Add one new animal to the end of the array. 4. Delete one animal from the beginning of the array and add it to the end of the array. Repeat this rotation two more times. 5. Use the grep function to select all the animal names with lengths greater than 5 characters. Assign to a new array and print it. 6. Use the map function to convert each animal name to uppercase. Assign the results back to the original array. 7. Print out the entire array by deleting one element at a time from the beginning of the array until the array is emptied.

Exercise 5.3 Write a program that captures the outputs of two different commands in two separate arrays. Produce a merged listing of their respective outputs. That is, print line #1 of the first program, then line #1 of the second, then line #2 of the first program, then line #2 of the second, etc. For Unix systems, try using the commands who and w. Exercise 5.4 Modify Exercise 4.4 (Pick 6 Lotto) to sort the 6 random numbers numerically before displaying. Exercise 5.5 Fill an array with the 26 letters of the alphabet, and print the letters in random order. (Hint: Try using splice with a random subscript into the array to extract and delete one letter at a time.)

Exercise 6.1 Write a program that prints out each string entered on the command line once only. For example, $ ex6.1 red green red blue green yellow red red green blue yellow Use a hash to keep track of which strings have already been seen. Exercise 6.2 Write a program to print out a word frequency count of the strings entered on the command line, sorted from most frequently to least frequently seen string. For example, $ ex6.2 red green red blue green yellow red 3 red 2 green 1 blue 1 yellow

Exercise 6.3 Write a program to create a reverse mapping of a hash. Starting with a %phone hash, such as: %phone = ( tom => '456-7123', larry => '321-5500', sriram => '998-9469', ); that uses names as keys and phone numbers as values, build a new hash that uses the phone numbers as keys and names as values. Print out the key–value pairs of the new hash. What will happen if two or more people in the %phone hash have the same phone number? Exercise 6.4 Modify Exercise 4.4 (Pick 6 Lotto) to use a hash for keeping track of which random numbers have already been seen. Store the chosen numbers by using them as keys in the hash. Although some value must be assigned for each key to associate to, the value used is irrelevant. For example, to “store” the number 26 as a key: $pick{26} = 'foobar'; When printing out the 6 random numbers, be sure to sort the keys numerically.

Exercise 6.5 Given two lists of non-repeating elements (i.e., no list has the same element more than once), find and print the union and intersection of those lists. For example, given: @a = (1, 3, 5, 6, 7, 8); @b = (0, 2, 3, 5, 7, 9); The union of the values in the two arrays would be: (0, 1, 2, 3, 5, 6, 7, 8, 9) The intersection of the values in the two arrays would be: (3, 5, 7) Use hash variables to build the union and intersection sets.