#!perl -w
use strict;
my $motif1 = shift @ARGV;
$motif1 =~ s/["']$//;
$motif1 =~ s/^["']//;
my $motif2 = shift @ARGV;
$motif2 =~ s/["']$//;
$motif2 =~ s/^["']//;
while ( my $filename = shift @ARGV ) {
my $sequence = slurp($filename);
$$sequence =~ s/\s//g; # Remove whitespace
my $motif1_loc = motif($motif1, $sequence, $filename);
my $motif2_loc = motif($motif2, $sequence, $filename);
if ($motif1_loc == -1 || $motif2_loc == -1) {
print "Both $motif1 & $motif2 not found in $filename \n";
} else {
print "Motif 1 => $motif1 & Motif 2 => $motif2 found in File => $filename \n";
print "Distance between them is: ", abs($motif1_loc - $motif2_loc), "\n";
}
}
# my $text = motif($re, $sequence, $filename);
sub motif {
my ($motif, $sequence, $filename ) = @_;
#print '$$sequence ->', $$sequence, '$motif ->', $motif, "\n";
my $pos = index ($$sequence, $motif);
#print '$pos ->', $pos, "\n";
return $pos;
}
# \$data = slurp($filename);
# slurp in all data into a single variable
# return a reference to avoid copying data
# Recommend: File::Slurp library for this
# This function was benchmarked with reading an array and reading lines
# On a slow PC, it quickly out paced arrays, and did better than lines.
sub slurp {
my ($filename) = @_;
my $inf;
my $fileTerminator = $/;
undef $/;
open($inf, "< $filename") or die("Unable to open $filename: $!");
my $buf = <$inf>;
close $inf;
$/ = $fileTerminator;
return \$buf
}