Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab - Array Explorations - Introduction to Bioinformatics | BIF 101, Lab Reports of Bioinformatics

Material Type: Lab; Class: Intro to Bioinformatics; Subject: Bioinformatics; University: Canisius College; Term: Fall 2008;

Typology: Lab Reports

Pre 2010

Uploaded on 08/18/2009

koofers-user-a6i
koofers-user-a6i 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
BIF 101 Last Lab – Array Explorations – December 4, 2008
Goal: Finish as many of these exercises as you can. They will help boost your lab grade!
1. oneline.pl
Write a program that inputs one line from a file (as a string), chops off the carriage
return, uses a regular expression to remove punctuation, and prints out the line. You can
copy the lrr.txt file from the class web page or create a multi-line text file yourself. One
suggest is to create the file from some song lyrics – find them on the Internet.
To get input from a file into a program you need to open the file, read from the file, then
close the file. Here is an example of doing that:
# the open command names a file variable (MYINPUTFILE) and a file (here it’s lrr.txt)
open(MYINPUTFILE, "lrr.txt");
# input from a file rather than from the command line in the window use the file variable
$line = <MYINPUTFILE>;
# close the file when you’re done reading
close(MYINPUTFILE);
The regular expression will look something like this:
/[\.;:,”]//g
2. alllines.pl
Instead of just inputting one line input the entire file into an array, where each array
element is a line from the file. Then print out what you’ve read in, line by line. To read
in an entire file all at once use the following:
@lines = <MYINPUTFILE>;
The only difference from above is that you read the file into an array variable.
Now you’ll need to loop through the array called @lines and print a line at a time, you
can do this easily with a foreach loop:
foreach $line (@lines)
{
#don’t forget to chomp off the carriage return
#make sure to remove the punctuation from the line here with your regex
print “$line\n”;
}
pf2

Partial preview of the text

Download Lab - Array Explorations - Introduction to Bioinformatics | BIF 101 and more Lab Reports Bioinformatics in PDF only on Docsity!

BIF 101 Last Lab – Array Explorations – December 4, 2008

Goal : Finish as many of these exercises as you can. They will help boost your lab grade!

  1. oneline.pl

Write a program that inputs one line from a file (as a string), chops off the carriage return, uses a regular expression to remove punctuation, and prints out the line. You can copy the lrr.txt file from the class web page or create a multi-line text file yourself. One suggest is to create the file from some song lyrics – find them on the Internet.

To get input from a file into a program you need to open the file, read from the file, then close the file. Here is an example of doing that:

the open command names a file variable (MYINPUTFILE) and a file (here it’s lrr.txt)

open(MYINPUTFILE, "lrr.txt");

input from a file rather than from the command line in the window use the file variable

$line = ;

close the file when you’re done reading

close(MYINPUTFILE);

The regular expression will look something like this:

/[.;:,”]//g

  1. alllines.pl

Instead of just inputting one line input the entire file into an array, where each array element is a line from the file. Then print out what you’ve read in, line by line. To read in an entire file all at once use the following:

@lines = ;

The only difference from above is that you read the file into an array variable.

Now you’ll need to loop through the array called @lines and print a line at a time, you can do this easily with a foreach loop:

foreach $line (@lines) { #don’t forget to chomp off the carriage return #make sure to remove the punctuation from the line here with your regex print “$line\n”; }

  1. words.pl

Now you’re going to add to what you just did. Once you have grabbed the line in the foreach loop, after you get rid of the carriage return and the punctuation you’re going to turn the letters in the line to upper case using the uc function:

$line = uc($line);

then you’re going to put the words in the line into an array and print them out. To get the words into an array use the split function:

@words = split(/ /, $line);

Make sure to split on the blank character (/ /) and make sure you’ve declared the @words array at the top of the program somewhere with your other variables.

Now loop through the @words array and print one word per line using a foreach loop:

foreach $word (@words) { print “$word\n”; }

  1. counts.pl

Now you’re ready to count the number of occurrences of each word in the file using a Perl “hash”, a special array that is indexed by key values. Strings can be used as key values. Look at the program handed out in class on Tuesday that uses a hash. To declare a hash use the % prefix, for example

my %counts;

To index into a hash put the key value in curly braces, for example $counts{$key} Note that accessing an array element or hash element requires you to use the $ prefix rather than the @ (regular arrays) or % (hashes).

See if you can tally up and print the word counts with the words in alphabetical order. First try not sorting. Use the example program as a guide.