#!/local/bin/perl -w

use Bio::DB::GenBank;

my $gb = new Bio::DB::GenBank(
	-retrievaltype=>'tempfile', -format=>'Fasta');

# my $output_file = 'Synechococcus.seq';
# open(RE, ">$output_file") || die "Error opening file!\n";

my $seq;
$seq = $gb->get_Seq_by_id("33864539");
# print RE $seq->id, "; ";
# print RE $seq->desc(), "\n";
# print RE $seq->seq(), "\n";

# my $seqseq = "ATGTTGGCAGTG";
my $seqseq = $seq->seq();

my $target = "TTYTAYGGNAARGGNGGN";
my @matches;

foreach my $i (0..length $seqseq){
    my $ans = &dEqual($target, $seqseq, $i);
    if ($ans eq 1){
	push @matches, $i;
    }
}

print "Matches occurred at: @matches.\n";

# close RE;
exit;

####### checking if degenerate sequence is present ######
sub dEqual{
    my ($str1, $str2, $offset) = @_;
    my $match = 1;
    foreach my $i (1..length $str1){
	my $ch1 = substr($str1, $i-1, 1);
	my $ch2 = substr($str2, $offset+$i-1, 1);
	if ($ch1 eq $ch2) {
	} elsif ($ch1 eq 'N') {
	} else {$match = 0; return $match;}
	# print "Comparing at position $offset and position $i in target:$ch1;$ch2;$match;\n";
    }
    return $match;
}
