Difference between revisions of "Exercise 5.4"
Jump to navigation
Jump to search
(New page: Return to Week 2 <br> Exercise 5.3 in <i>Beginning Perl for Bioinformatics</i></b> <pre> </pre>) |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
Return to [[Week 2]] | Return to [[Week 2]] | ||
<br> | <br> | ||
− | Exercise 5. | + | Exercise 5.4 in <i>Beginning Perl for Bioinformatics</i></b> |
<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 16: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;