#!/usr/bin/perl



my $bplength = 2500;

my $fastafile = $ARGV[0];
$fastafile =~ s/^.\///;

print $fastafile;

#substr($ARGV[0], 0, length($ARGV[0]) - 2);



my $exonsfile = "/homes/biertank/praktikum/chianti/oktober/exons/exons" . substr($fastafile, 0, index($fastafile, ".")) . ".txt";
my $outputfile = "/homes/biertank/praktikum/chianti/oktober/promotor/normal/length" . $bplength . "/" . substr($fastafile, 0, index($fastafile, ".")) . ".prom";


open(FILEIN, "<$exonsfile") || die ("kann net offnen datei $exonsfile.");

my $name = substr($fastafile, 0, index($fastafile, "."));

print "Open fasta: " . $fastafile . ", exon: " . $exonsfile;
print "\nJetzt kommen die Promoter: \n";


while(<FILEIN>) {

	if( (($_ =~ m/^</) || ($_ =~ m/^>/)) && !($_ =~ m/evx/) && !($_ =~ m/3end/)) {
		
		chop($_);
		
		my @fields = split(" ", $_);
		my $genename = $name . "_" . $fields[3];
		
		$genename = $genename . "_" . $fields[1] . "-" . $fields[2];
		
		my $cutoff = $fields[1] - $bplength;
		
		print $outputfile . " written\n";		
		
		@cargs = ("/u/praktikum/group5/programs/Cutter1_4.pl", $fastafile, $cutoff, $fields[1], $outputfile, $genename);
		
		system(@cargs);
	}
	
	

}


exit;
