Difference between revisions of "Exercise 5.4"

From Earlham CS Department
Jump to navigation Jump to search
 
Line 4: Line 4:
  
 
<pre>
 
<pre>
 +
#!/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;
 
</pre>
 
</pre>

Latest revision as of 17:59, 27 September 2009

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;