#!/usr/bin/perl

#use strict;

my $file = $ARGV[$0];
my $fprime = 2000;
my $tprime = 200;
my $line=1;

#print STDERR "ARGV: @ARGV\n";

open (IN,"<$file")  or die "Can\'t open file \"$file\"! $!\n";

my @C1=();#2-dimentional array
my @watickbrauche;

while (<IN>)  {

  my $actseqstart =0;
  my $actseqend =0;
  my $orient=0;
  my @l = split;

  # print @l->[0]."\n ";#.@l->[1]." ".@l->[2]." ".@l->[3]." ".@l->[4]." ".@l->[5]." ".@l->[6]." ".@l->[7]."\n";
  if ($l[0] eq "---") {
    if (@C1!=0) {
#      print STDERR "cluster with " . scalar(@C1) . "\n";
      if (@C1->[0][7] eq "+") {
	$orient=1;
	$actseqstart = @C1->[$line-1][2] - $fprime;
	$actseqend =  @C1->[$line-1][2] + $tprime;
      }
      else {
	$orient=2;
	$actseqstart = @C1->[$line-1][3] - $tprime;
	$actseqend =  @C1->[$line-1][3] + $fprime;
      }
      my $chrom= @C1->[0][1];
      my $tax=@C1->[0][0];
      my $db=get_genome_loc($tax);
      
#      print STDERR "fill in: $db,$actseqstart,$actseqend,$orient,$chrom\n";
      push @watickbrauche, [$tax,$db,$actseqstart,$actseqend,$orient,$chrom];
      @C1=();
    }
    else {
#      print "empty cluster\n;"
    }
  } 
  elsif ($l[0] eq "#") {
    #auskommentiert
  }    
  else {
    push @C1, [$l[0], $l[1], $l[2], $l[3], $l[4], $l[5], $l[6], $l[7]];
  }
}

#handling of last cluster
if (@C1!=0) {
#      print STDERR "cluster with " . scalar(@C1) . "\n";
  if (@C1->[0][7] eq "+") {
    $orient=1;
    $actseqstart = @C1->[$line-1][2] - $fprime;
    $actseqend =  @C1->[$line-1][2] + $tprime;
  }
  else {
    $orient=2;
    $actseqstart = @C1->[$line-1][3] - $tprime;
    $actseqend =  @C1->[$line-1][3] + $fprime;
  }
  my $chrom= @C1->[0][1];
  my $tax=@C1->[0][0];
  my $db=get_genome_loc($tax);
  
#      print STDERR "fill in: $db,$actseqstart,$actseqend,$orient,$chrom\n";
  push @watickbrauche, [$tax,$db,$actseqstart,$actseqend,$orient,$chrom];

}

for (my$i=0;$i<@watickbrauche;$i++) {
  my $newdir="./fasta-motif/";
  mkdir($newdir);
  my $seqout=$newdir . @watickbrauche->[$i][0] . "_" . $i. ".mot";
  print "for F in @watickbrauche->[$i][1]; do fastacmd -d \$F -L @watickbrauche->[$i][2],@watickbrauche->[$i][3] -S @watickbrauche->[$i][4] -s @watickbrauche->[$i][5] >> $seqout; done\n";
}

#split into directory, basename and extention
#takes: filename with or without path
#returns: array holding three elements
#         [0] path
#         [1] basename
#         [2] extentions (starting with the dot)
sub parse_filename {
my $file=shift;
  my $path="./";
  my $filename;
  my $basename;
  my $extentions;
  if ($file =~ /\//) { # file has path and filename
  #  print STDERR "has path <$file>";
    if ($file =~ /^(.*\/)([^\/]*)$/) {
      $path=$1;
      $filename=$2;
   #   print STDERR "<$path>\n";
    }
  }
  else { # file has no path
    $filename=$file;
  }
  if ($filename =~ /\./) {# has an extension
    if($filename=~ /^([^\.]+)(\..*)/) {# get basename
      $basename=$1;
      $extentions=$2;
    }
  }
  else {
    $basename=$filename;
  }
  return $path,$basename,$extentions;
}

sub get_genome_loc {
  my $tax=shift;
  my $db;  #path to species genome
  my @g=`grep $tax /scr/genomes/content.praktikum`;  #get the content.praktikum line of current species
#print @g."<----eintrag \n";
  if(@g){
    foreach(@g){
      if($_=~/ENSEMBL/){
	my @temp_db=split(/\s+/,$_);
	$db="$temp_db[4]/$temp_db[5]";
	break;
      }
      elsif($_=~/UCSC/){
	my @temp_db=split(/\s+/,$_);
	$db="$temp_db[4]/$temp_db[5]";
	break;
      } 
      elsif($_=~/OTHER/){
	
	my @temp_db=split(/\s+/,$_);
	$db="$temp_db[4]/$temp_db[5]";
      }
      elsif($_=~/NCBI/){
	
	my @temp_db=split(/\s+/,$_);
	$db="$temp_db[4]/$temp_db[5]";
      } 
    }
  }else{
    print STDERR "Species $tax has no content.praktikum entry\n";
  }
  return $db;
}
