#!/usr/bin/perl ################################################ # rep_pos.pl # Version 1 # Robert D. Cormia # UCSC Programming for Bioinformatics III # October 31, 2003 # Substantial help from Neil Cormia on this exercise # Welcome to Intro. to Prog. for Bioinformatics III # Class 3, Exercise 1 # 1. In this exercise we will work with the concept of counting aminoacids and # noting down the respective positions. Write a Perl program called 'rep_pos' # to count the aminoacids in a data file and note the positions as well. The # data file looks like this: # DFDKABLEGXKLRQDXVVMHMGQBA # XCGGAYABSMKERDXGLQYASHDELHFFEET # DKLZAZANXMKTIIDSVLGQTYTYQMETHERXXG # # Generate reports in the following fashion: # # Report I: # # Aminoacids: A # Pos 5: => 3 # Pos 7: => 1 # Pos 20: => 1 # Pos 25: => 1 # Aminoacids: B # Pos 6: => 1 # Pos 8: => 1 # Pos 24: => 1 # ... # #Report II: # # At position: 1 # D: => 2 # P: => 1 # X: => 1 # At position: 2 # C: => 1 # F: => 1 # K: => 1 # At position: 3 # D: => 1 # G: => 1 # L: => 1 ################################################ use strict; my $line; my $count_aa = 'count_aa.txt'; my $aa_output = 'aa_output.txt'; my @aa_string; my $aa; my %aa_hash; my ( $i, $j ); print "\n"; open( INPUT_HANDLE, $count_aa ) or die ("open() failed for 'fname'\nReason: $!\n\n"); open( OUTPUT_HANDLE, ">$aa_output" ) or die ("open() failed for '$aa_output'\nReason: $!\n\n"); while ( $line = ) { print "$line \n"; @aa_string = split ( //, $line ); # i is the position within the string for ( $i = 0 ; $i < @aa_string, $aa = $aa_string[$i] ; $i++ ) { if ( !exists( $aa_hash{$aa} ) ) { $aa_hash{$aa} = &make_array_ref(); } # end if @{ $aa_hash{$aa} }[$i]++; # print "$aa_hash{$aa}"; } } # end while # create report for ( $i = ord('A') ; $i <= ord('Z') ; $i++ ) { &report_aa( chr($i) ); } # end for # make a report subroutine sub report_aa { if ( exists( $aa_hash{ $_[0] } ) ) { my @pos = @{ $aa_hash{ $_[0] } }; print OUTPUT_HANDLE "Aminoacid: $_[0]\n"; print "Aminoacid: $_[0]\n"; for ( $j = 0 ; $j < @pos ; $j++ ) { if ( $pos[$j] > 0 ) { print OUTPUT_HANDLE "Pos $j: => $pos[$j]\n"; print "Pos $j: => $pos[$j]\n"; } } print "\n"; print OUTPUT_HANDLE "\n"; } } # end subroutine # make_array subroutine creates a reference to an array sub make_array_ref { my @array; return \@array; } __END__