#!/usr/bin/perl -w ################################################# # subs_count_dna.pl # Version 1 # Robert D. Cormia # UCSC Programming for Bioinformatics III # October 31, 2003 # Description - this program counts the number of bases # A, T, C, and G in the file 'new_dna_seq'. # ATAATTTGCAGCTAAGGTGCG # TTTGGGCCCACTCGAGAGGGC # CCACTTCGTACCTCGCGCTTG # The subroutine # has two arguments: a base and a sequence. # The subroutine # returns the count of the base in the sequence. # Print the resluts as: # # ATAATTTGCAGCTAAGGTGCG # A:6 T:6 C:3 G:5 # ################################################## use strict; print "\n"; # ------------------------------------------------------------- # Open 'new_dna_seq' file, if open fails, call die. # ------------------------------------------------------------- my $fname = 'new_dna_seq.txt'; open( new_dna_seq, $fname ) or die ("open() failed for '$fname'\nReason: $!\n\n"); my $line; # ------------------------------------------------------------- # While loop reads new_dna_seq.txt and calls base_counter # base_counter counts bases and prints results to screen # ------------------------------------------------------------- while ( $line = ) { chomp($line); print "$line\n"; my $base; my @bases = qw(A T C G); foreach $base (@bases) { print "$base:", &base_counter( $base, $line ), " "; } print "\n\n"; } # --------------------------------------------------------------- # This subroutine counts number of bases per sequence # --------------------------------------------------------------- sub base_counter { my ( $base, $dna_sequence ) = @_; return ( $dna_sequence =~ s/$base/$base/ig ); } # --------------------------------------------------------------- # Close the new_dna_seq file # --------------------------------------------------------------- close(new_dna_seq); print "\n"; __END__