#!/usr/local/bin/perl #use strict; #use warnings; ## dotmatrix2.pl takes a query string and a search string and uses the ## dot-matrix method to find regions of alignment between the two. sub compare; ## a subrouting declaration - to be defined later my $window = 4; my $match = 4; my $similarity; my $query = "AAGCGCGTCGCGATTGACATTATAGAAGGCGC"; my $search = "ATGTTCGCGATTGACATTCAAAAGCGAGTCAG"; $query = "AAGCGCGTCGCGATTGACATTATAGAAGGCGCAG"; $search = "AAGTGCGACGCCATTCACAATATGGAACGCGCAG"; my $querylen = length($query); my $searchlen = length($search); my $queryword; my $searchword; my $i; my $j; my @table; ## Define a two-dimensional array consisting of an array of references ## to a series of anonymous arrays of scalars in the second dimension ## and initialize the two-dimensional array with zeros for ($i = 0; $i < $querylen; $i++) { for ($j = 0; $j < $searchlen; $j++) { $table[$i][$j] = 0; # perl "autovivifies" the anonymous arrays # indexed my $j!! } } ## Now step through the matrix using "sliding window" substrings of the query ## and search sequences and check off those positions where the substrings are ## identical. for ($i = 0; $i < $querylen; $i++) { for ($j = 0; $j < $searchlen; $j++) { $queryword = substr($query, $i, $window); $searchword = substr($search,$j, $window); #print $queryword, " ", $searchword, " Line \n"; $similarity = compare($queryword,$searchword); if ($similarity >= $match) { $table[$i][$j] = "\\"; } } } ## Now that we have "scored" the matrix, let's print it out and see how we did foreach (@table) { print @{$_}, " \n"; } ## compare is a simple subroutine that takes two strings ## as input and returns the number of exact matches at each position sub compare { my $similar = 0; my $string1 = shift; my $string2 = shift; my $minlength; if (length($string1) < length($string2) ) { $minlength = length($string1); } else { $minlength = length($string2); } for (my $i = 0; $i < $minlength; $i++) { if (substr($string1,$i,1) eq substr($string2, $i, 1)) { $similar++; } } #print $string1, " ", $string2, " ", $similar, " func\n"; return $similar; }