Exercise 5.8D

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 -w

use strict; 

#Damian Almiron

#Ex 5-8  

#Ask 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>; 

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



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


  my $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";


    } else {


        print "I couldn\'t find it.\n";


#Exit on an empty user input 

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

#Exit the program