#!/usr/bin/perl -w
#Derek Anderson
#Perl for Bioinformatics Project 1
use Getopt::Std;
use GD::Simple;
use strict;
sub setOpts {
#Sub that gets a filename from the command line
my $optString='f:';
my %opt;
my %configHash;
if($#ARGV >=0) {
getopts($optString,\%opt) or printError("Error on use $0 -f FILENAME");
$configHash{'myFile'}=$opt{'f'} if(defined($opt{'f'}));
}
else {
printError("Error on use $0 -f FILENAME");
}
return %configHash;
}
sub printError {
#Sub that prints a message then exits program
my $errorMsg=$_[0];
print STDERR $errorMsg."\n";
exit();
}
sub checkFile {
#Sub that checks to see if the file is on the system
my $fileName=$_[0];
printError("Please check file $fileName") if (! -f $fileName);
}
sub loadFile {
#Sub that opens a file handel called SEQ
my $ref=$_[0];
my %configHash=%$ref;
open(SEQ,'<',$configHash{'myFile'}) or printError("$! on file $configHash{'file'}");
}
sub parseFile {
#Sub that parses the fasta file
my $ref=$_[0];
my %configHash=%$ref;
while(<SEQ>) {
chomp $_;
$configHash{'String'}.=$_ if(!($_ =~ m/^>/));
}
return %configHash;
}
sub findMatch {
#Sub that finds matches in a string and saves them in an array
my $ref=$_[0];
my %configHash=%$ref;
my @match;
my @hold;
my @reg=("(TAGGT[AT]ACCTA)","(TGGGT[AT]ACCCA)","(TTGGT[AT]ACCAA)");
foreach(@reg) {
while($configHash{'String'} =~ m/$_/g) {
my $num=(pos($configHash{'String'})-11);
push(@hold,$num);
}
}
$configHash{'hold'} = \@hold;
return %configHash;
}
sub closeFile {
#Sub that closes the file handle
close SEQ;
}
sub loopTh {
#Sub that makes a png image from a fasta file
my $ref=$_[0];
my %configHash=%$ref;
my $num=0;
my $len=length($configHash{'String'});
my @hold = @{$configHash{'hold'}};
my $start =shift(@hold);
my $word=0;
my $over=20;
my $height=10;
my $img= GD::Simple->new(620,600);
$img->moveTo(10,10);
$img->penSize(15,10);
while($num <= $len) {#Loop through till there is nothing left in our string
if($num == ($start) || $over <=11) {#If we are at the start of a math or the line wraps to a new one
$img->fgcolor('yellow');
if($over >= 11 && ($word+11 > 100) ) {#If the match wraps lines
$over=0;
}
if($over <= 11) {#Print the wraped lines
$img->string(substr($configHash{'String'},$num,1));
$over++;
}
else {#If all 11 can fit on a line print it
$img->string(substr($configHash{'String'},$num,11));
$num=$num+10;
}
unless($#hold == -1) {#Get next value we found if there is any left in the array
$start =shift(@hold);
}
}
else {#if we did not find a match print it red
$img->fgcolor('red');
$img->string(substr($configHash{'String'},$num,1));
}
if($num%100==0) {#wrap to a new line
$img->fgcolor('black');
$img->moveTo(10,$height);
$img->line(599);
$img->moveTo(10,$height+5);
$height = $height + 15;
$word=0;
}
$word++;
$num++;
}
print $img->png;
}
sub init {
#sub that calls all other subs
my %configHash=setOpts();
checkFile($configHash{'myFile'});
loadFile(\%configHash);
%configHash=parseFile(\%configHash);
%configHash=findMatch(\%configHash);
loopTh(\%configHash);
closeFile();
}
init();