#!/usr/bin/perl


if($#ARGV == -1) {
    print "Bitte Argument angeben: Meme Datei.\n";
    exit;
}

my $memefile = $ARGV[0];
open(MEMEFILE, '<', "$memefile");

my $strand;
my $motifnr;
my $seq;
my $tier;
my $score;
my $label;
my $nextline;
my @field;


while (<MEMEFILE>) {

    if(($_ =~ m/^MOTIF/)) {
	@field = split(" ", $_);
	$motifnr = $field[1];

    }

    if(($_ =~ m/^Sequence name\s+Strand/)) {
	
	$_ = <MEMEFILE>;

	while(<MEMEFILE>) {

	    if($_ =~ m/^\-+/) {
		last;
	    }

	    @field = split(" ", $_);
	    $tier = $field[0];
	    $start = $field[2];
	    $seq = $field[5];
	    $end = $start + length($seq) - 1;
	    $strand = $field[1];
	    $strand =~ s/\+/1/;
	    $strand =~ s/\-/0/;
	    $score = $field[3];

	    print $strand;

	    open(OUTFILE, ">>$tier.meme.data");
	    print OUTFILE "$start\t$end\t$score\t$motifnr\t$seq\tmeme\t$strand\t$tier\n";
	    close(OUTFILE);
	}
    }
   }





exit;


