Skip to content

Instantly share code, notes, and snippets.

@aurorapar
Created April 28, 2017 10:31
Show Gist options
  • Save aurorapar/80ed5642012f62a0ed37be0d795efa4f to your computer and use it in GitHub Desktop.
Save aurorapar/80ed5642012f62a0ed37be0d795efa4f to your computer and use it in GitHub Desktop.
motif finder
#not /usr/local/bin/perl
# Reports a match and position of a Motif
#
# Input:
# One motif pattern file
# One sequence file
$patternFile;
$searchFile;
displayPrompt();
printf "\nMotif pattern file: " . $patternFile . "\n" . "Sequence file: " . $patternFile . "\n";
$patternText = readFile($patternFile);
$searchText = readFile($searchFile);
print "\nMotif pattern text:\n" . $patternText . "\nSequence text:\n" . $searchText . "\n";
$start = 0;
$end = length($searchText) - length($patternText);
for($start; $start < $end; $start++)
{
$matchSequence = substr($searchText, $start, length($patternText));
if($patternText eq $matchSequence)
{
print "\nMOTIF FOUND AT POSITION " . $start;
}
}
#print $searchText =~ /$patternText/;
sub displayPrompt
{
while(not defined($patternFile))
{
$testFile = getInput("Please enter a motif pattern file.\n\t");
if(-f $testFile)
{
$patternFile = $testFile;
}
else
{
printf "\n\"$testFile\" does not exist in the current directory.\n"
}
}
while(not defined($searchFile))
{
$testFile = getInput("Please enter a sequence file.\n\t");
if(-f $testFile)
{
$searchFile = $testFile;
}
else
{
printf "\n\"$testFile\" does not exist in the current directory.\n"
}
}
}
sub getInput
{
my $prompt = shift;
$prompt = "" if not defined $prompt;
$files = "";
if(defined($patternFile))
{
$files = "\Motif pattern file: " . $patternFile . "\n";
}
else
{
$files = "\Motif pattern file: NONE\n";
}
if(defined($searchFile))
{
$files = $files . "Sequence file: " . $patternFile . "\n";
}
else
{
$files = $files . "Sequence file: NONE\n";
}
$prompt = $files . $prompt;
print($prompt);
$input = <>;
chomp($input);
return $input;
}
sub readFile
{
my $file = shift;
print "\nRead from file " . $file;
if(not -f $file)
{
printf "\n$file is not a valid file in the current directory.\n";
exit;
}
$text = "";
if (open(my $fh, '<:encoding(UTF-8)', $file))
{
while(my $line = <$fh>)
{
if(not eof)
{
chomp $line;
$text = $text . uc($line); # Uppercase
}
}
}
else
{
print "\nError reading file: " . $file;
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment