Exercise 5.4

From Earlham CS Department
Revision as of 16:59, 27 September 2009 by Erika (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Return to Week 2
Exercise 5.4 in Beginning Perl for Bioinformatics

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

#Erika Phelps
#Sept 27, 2009
#Exercise 5.4

#Pseudocode:

#Ask the user to input a string of DNA
#Use the substr function to find the reverse of the string
#Print out the reverse
#End program

#Define variables

my $dna = 0;
my $base = 0;
my $reverse_dna = 0;
my $position = 0;

#Ask the user for a string of DNA

print "This program calculates the reverse complement of a strand of DNA.\n";

print "Please input a string of DNA:\n";

$dna = <STDIN>;
chomp $dna;

#Determine the reverse complement of the DNA

for ($position = 0 ; $position < length $dna ; ++$position){

    $base = substr($dna, $position, 1);

    if ( $base eq 'A' ) {

       $reverse_dna = $reverse_dna.'T';
    
    } elsif ( $base eq 'C' ) {

        $reverse_dna = $reverse_dna.'G';

    } elsif ( $base eq 'G' ) {

        $reverse_dna = $reverse_dna.'C';

    } elsif ( $base eq 'T' ) {

        $reverse_dna = $reverse_dna.'A';
    }
}

#Strip $reverse_dna of it's first character

$reverse_dna =~ s/^0*//;

#Print out the reverse complement

print "The DNA you provides is: $dna.\n";

print "The reverse complement of this string is: $reverse_dna.\n\n";

#Exit program        

exit;