Difference between revisions of "Exercise 6.2"

From Earlham CS Department
Jump to navigation Jump to search
 
Line 3: Line 3:
  
  
I had some trouble with this one. I can't get the subroutine to return values. I'm not sure I understand how the arguments are read by the subroutine.
+
I'm having trouble with this one!!
  
I think this might be a good problem to use to walk through the debugger--I didn't understand everything as it was laid out in the book.
+
<pre>
 +
#!/usr/bin/perl
 +
use warnings;
 +
use strict;
 +
 
 +
#Erika Phelps
 +
#28 Sept 2009
 +
#Exercise 6.2
 +
 
 +
#Pseudocode:
 +
 
 +
#Ask for a DNA sequence from the user.
 +
#Determine the length of the DNA sequence.
 +
#Use a subroutine to calculate the percentage of each base.
 +
#Print out the returned values.
 +
 
 +
#Define variables
 +
 
 +
my $DNA = 0;
 +
my $A = "A";
 +
my $T = "T";
 +
my $G = "G";
 +
my $C = "C";
 +
my $Ap = 0;
 +
my $Tp = 0;
 +
my $Gp = 0;
 +
my $Cp = 0;
 +
 
 +
 
 +
#Obtain DNA sequence from user.
 +
 
 +
print "Please input your DNA sequence:\n";
 +
 
 +
$DNA = <STDIN>;
 +
 
 +
chomp $DNA;
 +
 
 +
#Call the subroutine for each base
 +
#The result is saved in the string for each base
 +
 
 +
$Ap = percentATGC($DNA, $A);
 +
$Tp = percentATGC($DNA, $T);
 +
$Gp = percentATGC($DNA, $G);
 +
$Cp = percentATGC($DNA, $C);
 +
 
 +
#Return the values
 +
 
 +
print "The percentage of each base in your sequence is as follows:\n";
 +
print "A = $Ap% \n";
 +
print "T = $Tp% \n";
 +
print "G = $Gp% \n";
 +
print "C = $Cp% \n\n";
 +
 
 +
exit;
 +
 
 +
####################
 +
#Subroutine#
 +
####################
 +
 
 +
sub percentATGC {
 +
#Initialize arguments and variables
 +
    my($DNA,$base) = @_; 
 +
 
 +
#Define variables
 +
    my $count  = 0;
 +
    my $countT  = 0;
 +
    my $countG  = 0;
 +
    my $countC  = 0;
 +
    my $length = 0;
 +
 
 +
    print "This is dna $DNA\n";
 +
    print "this is base $base\n";
 +
 
 +
  #Obtain a count of each base
 +
 
 +
    $count = (eval($DNA =~ tr/$base//));
 +
  # $countT = ($DNA =~ tr/Tt//);   
 +
  # $countG = ($DNA =~ tr/Gg//);
 +
  # $countC = ($DNA =~ tr/Cc//);
 +
 
 +
 
 +
  #Determine the length of the DNA sequence
 +
   
 +
  # $length = ($DNA =~ tr/ATGCatgc//);
 +
 
 +
  # my $value = $count/$length*100;
 +
   
 +
    return $count;
 +
  # return $value;
 +
}
  
<pre>
 
 
</pre>
 
</pre>

Latest revision as of 15:38, 1 October 2009

Return to Week 3 Exercise 6.2 in Beginning Perl for Bioinformatics


I'm having trouble with this one!!

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

#Erika Phelps
#28 Sept 2009
#Exercise 6.2

#Pseudocode:

#Ask for a DNA sequence from the user.
#Determine the length of the DNA sequence.
#Use a subroutine to calculate the percentage of each base.
#Print out the returned values.

#Define variables

my $DNA = 0;
my $A = "A";
my $T = "T";
my $G = "G";
my $C = "C";
my $Ap = 0;
my $Tp = 0;
my $Gp = 0;
my $Cp = 0;


#Obtain DNA sequence from user.

print "Please input your DNA sequence:\n";

$DNA = <STDIN>;

chomp $DNA;

#Call the subroutine for each base
#The result is saved in the string for each base

$Ap = percentATGC($DNA, $A);
$Tp = percentATGC($DNA, $T);
$Gp = percentATGC($DNA, $G);
$Cp = percentATGC($DNA, $C);

#Return the values

print "The percentage of each base in your sequence is as follows:\n";
print "A = $Ap% \n";
print "T = $Tp% \n";
print "G = $Gp% \n";
print "C = $Cp% \n\n";

exit;

####################
#Subroutine#
####################

sub percentATGC {
 #Initialize arguments and variables
    my($DNA,$base) = @_;  

 #Define variables
    my $count  = 0;
    my $countT  = 0;
    my $countG  = 0;
    my $countC  = 0;
    my $length = 0;

    print "This is dna $DNA\n";
    print "this is base $base\n";

  #Obtain a count of each base

    $count = (eval($DNA =~ tr/$base//));
  # $countT = ($DNA =~ tr/Tt//);    
  # $countG = ($DNA =~ tr/Gg//);
  # $countC = ($DNA =~ tr/Cc//);


   #Determine the length of the DNA sequence
    
   # $length = ($DNA =~ tr/ATGCatgc//);

   # my $value = $count/$length*100;
    
    return $count;
   # return $value;
}