Exercise 5.8

From Earlham CS Department
Jump to navigation Jump to search

Return to Week 2
Exercise 5.8 in Beginning Perl for Bioinformatics

#!/usr/bin/perl
use warnings;
use strict;

#Erika Phelps
#28 Sept 2009
#Exercise 5.8 (nodification of example5-3.pl)

#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: ";

my $proteinfilename = <STDIN>;
my $motif;

#Remove the newline from the protein filename

chomp $proteinfilename;

#Open the file, or exit

unless ( open (PROTEINFILE, $proteinfilename) ) {
    
    print "Cannot open file \"$proteinfilename\"\n\n";
    exit;
}

#Read the protein sequence data from the file, and store it
#into the array variable @protein

my @protein = <PROTEINFILE>;

#Close the file - we've read all the data into @protein now.

#Put the sequence data into a single string, as it's easier to search
#for a motif in a string than in an array of lines

my $protein = join( '' , @protein);

#Remove white space

$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 the motif:\n$&\n";
         
    } else {
        
        print "I couldn\'t find it.\n\n";
    }

#Exit on an empty user input

} until ( $motif =~ /^\s*$/ );

#Exit the program

exit;