#!/usr/bin/perl 
#dotplot.pl version 0.0
#author: Sonja J. Prohaska
#  date: 03.05.2005

use strict;

my $usage= << "JUS";
  usage: dotplot.pl -x=FASTA1 -y=FASTA2 [-x_ex=EXONFILE1 -y_ex=EXONFILE2 
					 -blast=PATHtoBLAST (default bl2seq)]
options: -e=  set the e-value for the blast search [default: 1.0e-05]
  needs: bl2seq
         blast2dotplot.pl
         exons2dotplot.pl
JUS

my @options = grep {substr($_, 0, 1) eq '-'} @ARGV;
my @others  = grep {substr($_, 0, 1) =~ /^[^-]/} @ARGV;

##default settings:
my $bl2seq = 'bl2seq';
my $debug  = 0 ;
my $seqx;
my $efx;
my $seqy;
my $efy;
my $e = '1.0e-05';
##----------

##setting parameters:
foreach my $option (@options){
  if    ($option eq "-d")             { $debug = 1; }
  elsif ($option =~ m/^-x=(.*)$/)     { $seqx = $1; }
  elsif ($option =~ m/^-y=(.*)$/)     { $seqy = $1; }
  elsif ($option =~ m/^-x_ex=(.*)$/)  {  $efx = $1; }
  elsif ($option =~ m/^-y_ex=(.*)$/)  {  $efy = $1; }
  elsif ($option =~ m/^-e=(.*)$/)     {    $e = $1; }
  elsif ($option =~ m/^-blast=(.*)$/) { $bl2seq = $1;}
  else  { die "Invalid command line option: \'$option\'!\n$usage"; }
}

die "Two sequence files expected!\n$usage" 
    unless (defined($seqx) & defined($seqy));

#blast the sequences:
`$bl2seq -p blastn -D1 -i $seqx -j $seqy -e $e > ttmp`;
#dotplot the blastfile:
`blast2dotplot.pl ttmp $seqx $seqy > tmp_plot1.dat`;
my $set;
open(TMP, "<tmp_plot1.dat");
while(<TMP>) {
  if ($_ =~ m/^@ s(\d*)\s/) {$set=$1+1}
}
print STDERR "$set blast hits (dotplot.pl)\n";
close TMP;

#dotplot the exonfile:
if (defined($efx) & defined($efx)) {
  `exons2dotplot.pl -x=$efx -y=$efy -s=$set >> tmp_plot1.dat`;
}

print "OUTPUT written to: tmp_plot1.dat\n";

