# # # The directives below enforce variable declarations and ensure your program # will be parsed to provide more feedback about possible syntax and logical errors. # use strict; # enforce variable declarations use warnings; # enable warnings # # Declare/initialize all variables used in main program body. # my ($A_count) = 0; # number of 'a' or 'A' in user-supplied sequence my ($base) = ''; # an extracted letter from the sequence my ($C_count) = 0; # number of 'c' or 'C' in user-supplied sequence my ($filename) = ''; # the name of the file containing the input sequence(s) my (@filesequences) = (); # array containing lines read from input file my ($G_count) = 0; # number of 'g' or 'G' in user-supplied sequence my ($i) = 0; # used to index into array of sequences my ($line_count) = 0; # number of lines read from input file my ($option) = 0; # the user's selected menu option my ($position) = 0; # the current position within the sequence my ($sequence) = ''; # the DNA sequence to be processed my ($T_count) = 0; # number of 't' or 'T' in user-supplied sequence # # Display welcome message and program explanation. # print "\n *****************************************************"; print "\n ** **"; print "\n ** Nucleotide Counter, version 2.0 **"; print "\n ** **"; print "\n ** Written by J.M Wagner 2014 **"; print "\n ** **"; print "\n *****************************************************"; print "\n * *"; print "\n * This program will count the number of different *"; print "\n * nucleotides present in the sequence you provide. *"; print "\n * Both uppercase and lowercase abbreviations will *"; print "\n * be counted. Input can be provided at the *"; print "\n * keyboard or can be read from a file. *"; print "\n * *"; print "\n * *"; print "\n *****************************************************"; print "\n\n\t Program options:"; print "\n\t -----------------------------"; print "\n\t 1. Enter sequence at keyboard"; print "\n\t 2. Read input sequences from a file"; print "\n\t 3. Exit"; print "\n\n Please enter your menu option>"; $option = ; # # Confirm that user has entered a valid menu option. # while (($option < 1) or ($option > 3)) { chomp $option; print "\n ERROR: The value $option is not a valid menu option."; print "\n\n Please enter your menu option>"; $option = ; } # end while # # Obtain input DNA sequence from user. # # ************************************************************************** # OPTION 1 - Obtain and process input sentence provided at keyboard. # ************************************************************************** if ($option == 1) { # # Obtain input DNA sequence from user. # print "\n\n Please enter the DNA Sequence to be processed"; print "\n >"; $sequence = ; print "\n Processed DNA sequence: $sequence"; # # Count the number of nucleotides by processing the sequence of characters # one character at a time. # for ($position = 0; $position < length($sequence); ++$position) { $base = substr($sequence, $position, 1); if (($base eq 'a') or ($base eq 'A') or ($base eq 'c') or ($base eq 'C') or ($base eq 'g') or ($base eq 'G') or ($base eq 't') or ($base eq 'T')) { if (($base eq 'a') or ($base eq 'A')) { ++$A_count; } # end if if (($base eq 'c') or ($base eq 'C')) { ++$C_count; } # end if if (($base eq 'g') or ($base eq 'G')) { ++$G_count; } # end if if (($base eq 't') or ($base eq 'T')) { ++$T_count; } # end if } # end if } # end for # # Display final results. # print "\n Number of A nucleotides: $A_count"; print "\n Number of C nucleotides: $C_count"; print "\n Number of G nucleotides: $G_count"; print "\n Number of T nucleotides: $T_count"; print "\n\n Program completed successfully.\n"; }end if # ************************************************************************** # OPTION 2 - Obtain and process input sentences provided in user file. # ************************************************************************** elsif ($option == 2) { # # Read input sequence(s) from file provided by user. # print "\n\n Please enter the filename>"; $filename = ; unless(open(SEQUENCEFILE, $filename)) { print "\n\t --> ERROR: Cannot open file: $filename"; exit; } @filesequences = ; close(SEQUENCEFILE); $line_count = @filesequences; print "\n\t --> NOTE: File opened -- $line_count lines of text read.\n"; # print @filesequences; # # Count the number of nucleotides by processing the sequence of characters # one sequence at a time. # for ($i = 0; $i < $line_count; ++$i) { # # Initialize $sequence to the next line of text from file. # $sequence = $filesequences[$i]; print "\n $i. Processed sequence: $sequence"; # # Count the number of nucleotides in the current sequence. # $position = 0; while ($position < length($sequence)) { $base = substr($sequence, $position, 1); if (($base eq 'a') or ($base eq 'A') or ($base eq 'c') or ($base eq 'C') or ($base eq 'g') or ($base eq 'G') or ($base eq 't') or ($base eq 'T')) { if (($base eq 'a') or ($base eq 'A')) { ++$A_count; } # end if if (($base eq 'c') or ($base eq 'C')) { ++$C_count; } # end if if (($base eq 'g') or ($base eq 'G')) { ++$G_count; } # end if if (($base eq 't') or ($base eq 'T')) { ++$T_count; } # end if ++$position; } # end while } # end for print "\n Number of A nucleotides: $A_count"; print "\n Number of C nucleotides: $C_count"; print "\n Number of G nucleotides: $G_count"; print "\n Number of T nucleotides: $T_count"; print "\n\n Program completed successfully.\n"; } # end elsif # # ************************************************************************** # Write final results to default output file. # ************************************************************************** $filename = "results.dat"; unless(open(OUTFILE, ">$filename")) { print "\n\t --> ERROR: Cannot open file: $filename"; exit; } print "\n\n\t --> NOTE: File opened for writing: $filename\n"; print OUTFILE "\n Total number of Adenine: $A_count"; print OUTFILE "\n Total number of Cytosine: $C_count"; print OUTFILE "\n Total number of Guanine: $G_count"; print OUTFILE "\n Total number of Thymine: $T_count"; close(OUTFILE); print "\n\n Program completed successfully.\n"; exit;