Exercise 5.8 Modify Example 5-3 to not only find motifs by regular expressions b
ID: 669853 • Letter: E
Question
Exercise 5.8
Modify Example 5-3 to not only find motifs by regular expressions but to print
out the motif that was found. For example, if you search, using regular
expressions, for the motif EE.*EE, your program should print EETVKNDEE.
You can use the special variable $&. After a successful pattern match, this special
variable is set to hold the pattern that was matched.
Example 5.3 for exercise 5.8^^^^
#!/usr/bin/perl -w
# Searching for motifs
# Ask the user for the filename of the file containing
# the protein sequence data, and collect it from the
keyboard
print "Please type the filename of the protein sequence
data: ";
$proteinfilename = <STDIN>;
# Remove the newline from the protein filename
chomp $proteinfilename;
# open the file, or exit
unless ( open(PROTEINFILE, $proteinfilename) ) {
print "Cannot open file "$proteinfilename" ";
exit;
}
# Read the protein sequence data from the file, and store
it
# into the array variable @protein
@protein = <PROTEINFILE>;
# Close the file - we've read all the data into @protein
now.
close PROTEINFILE;
# Put the protein sequence data into a single string, as
it's easier
# to search for a motif in a string than in an array of
# lines (what if the motif occurs over a line break?)
$protein = join( '', @protein);
# Remove whitespace
$protein =~ s/s//g;
# In a loop, ask the user for a motif, search for the motif,
# and report if it was found.
# Exit if no motif is entered.
do {
print "Enter a motif to search for: ";
$motif = <STDIN>;
# Remove the newline at the end of $motif
chomp $motif;
# Look for the motif
if ( $protein =~ /$motif/ ) {
print "I found it! ";
} else {
print "I couldn't find it. ";
}
# exit on an empty user input
} until ( $motif =~ /^s*$/ );
# exit the program
exit;
Exercise 5.9
Write a program that switches two bases in a DNA string at specified positions.
(Hint: you can use the Perl functions substr or slice.
Exercise 5.10
Write a program that writes a temporary file and then deletes it. The unlink
function removes a file: just say, for example:
unlink "tmpfile";
but also check to see if unlink is successful.
Need this written in perl asap
Explanation / Answer
5.9
use strict;
use warnings;
print 'ENTER THE FILENAME OF THE DNA SEQUENCE:= ';
my $DNA_filename = <STDIN>;
chomp $DNA_filename;
unless (open(DNAFILE, $DNA_filename))
{
die 'Cannot open file "' . $DNA_filename . '"' . " ";
}
my @DNA = <DNAFILE>;
close DNAFILE;
my $DNA_string = join('', @DNA);
print " " . 'The original DNA file is:' . " " . $DNA_string . " ";
$DNA_string =~ s/s//g;
print ' enter the number ';
my $pos = <STDIN>;
if ($pos > length($DNA_string))
{
print ' no data';
}
else
{
print ' ' . substr($DNA_string, $pos, 3) . " ";
}
5.10
use File::Temp qw/ tempdir /;
$fh = tempfile( );
use File::Temp qw/ tempdir /; // conjuction with temperory directory
# or specify a directory
$dir = tempdir( CLEANUP => 1 );
($fh, $filename) = tempfile( DIR => $dir );
$template = "myprogtempXXXXXX"; # trailing Xs are changed
($fh, $filename) = tempfile( $template, DIR => $dir);
($fh, $filename) = tempfile( $template, SUFFIX => ".data");
**
use File:Temp;
my $tmp = File::Temp->new; # Will be unlinked at end of program.
while ($work_to_do)
{
print $tmp a_lot_of_stuff(); # $tmp is a filehandle
}
if ($save_it)
{
rename($tmp, $new_file); # $tmp is also a string. Move (rename) the file.
} # If you need this to work across filesystems, you
# might want to ``use File::Copy qw(move)'' instead.
exit; # $tmp will be unlinked here if it was not renamed