Exercise 6.2D

From Earlham Cluster Department

Jump to: navigation, search

Return to Week 3
Exercise 6.2 in Beginning Perl for Bioinformatics</b>

#!/usr/bin/perl -w
use strict;

#Damian Almiron
#Ex 6-2


#Obtain DNA sequence from the user.
#Count the length of the DNA sequence.
#Use a subroutine to find the % for the different bases.
#Print returned values.

#Variables (why do we make them equal to zero?)refer to pg97

my $DNA = 0;
my $C = "C";
my $T = "T";
my $A = "A";
my $G = "G";
my $Cp = 0;
my $Tp = 0;
my $Ap = 0;
my $Gp = 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 (what does this mean?)

$Ap = percent($DNA, $A);
$Tp = percent($DNA, $T);
$Gp = percent($DNA, $G);
$Cp = percent($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 percent {
 #Initialize arguments and variables (how does this command work?)
    my($DNA,$base) = @_;  

 #Define variables
    my $count  = 0;
    my $countT = 0;
    my $countA  = 0;
    my $countC  = 0;
    my $countG  = 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//));
  # $countC = ($DNA =~ tr/Cc//);    
  # $countG = ($DNA =~ tr/Gg//);
  # $countA = ($DNA =~ tr/Aa//);
# $countT = ($DNA =~ tr/Tt//);


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

   # my $value = $count/$DNAlength*100;
    
    return $count;
   # return $value;
}
<pre/>
Personal tools
Namespaces
Variants
Actions
websites
wiki
this semester
Toolbox