#!/usr/bin/perl

use strict;

MAIN : {

    my ($motif_file, $dir) = @ARGV;
    if ((not defined $motif_file) ||
	(not defined $dir)) {
	die ("Usage: ./get_methyl.pl <motif file> <dir>\n");
    }

    my $def_hash;
    open(FILE,$motif_file);
    while (my $line = <FILE>) {
	chomp $line;
	my ($chr, $start, $end, $strand, $score, $seq) = split(/\t/,$line);
	if ($strand eq "-") {	    
	    my $base = substr($seq,3,1);
	    my $loc = $start + 3;
	    if ($base eq "C") {
		$def_hash->{$chr}->{$loc} = $line;
	    }
	} else {	    	    
	    my $base = substr($seq,13,1);
	    my $loc = $start + 13;
	    if ($base eq "G") {
                $def_hash->{$chr}->{$loc} = $line;
            }
	}
    }
    close(FILE);

    my $low_test;
    my $high_test;
    my $hash;
    my $name_def;

    opendir (DIR, $dir) or die $!;
    while (my $file = readdir(DIR)) {

	if ($file =~ m/\.gz$/) {
	    
	    print STDERR $file . "\n";

	    my $name = $file;
	    $name =~ s/allc_//;
	    $name =~ s/\.tsv\.gz//g;

	    $name_def->{$name} = 1;
	    
	    my $tag = $dir . "/" . $file;
	    open(FILE,"zcat $tag |");
	    
	    while (my $line = <FILE>) {
		chomp $line;
		
		my ($c, $loc, $strand, $context, $methyl, $cover, $test) = split(/\t/,$line);
		my $chr = "chr" . $c;
		if (defined $def_hash->{$chr}->{$loc}) {

		    if ($cover > 0) {

			$hash->{$chr}->{$loc}->{$name} = $methyl/$cover;
			
			if ($cover >= 10) {
			    if ($methyl/$cover >= 0.8) {
				$high_test->{$chr}->{$loc} = 1;
			    } 
			    if ($methyl/$cover <= 0.2) {
				$low_test->{$chr}->{$loc} = 1;
			    }
			}
			
		    }
		}


	    }
	    
	    close(FILE);

	}


    }

    my @name_array = sort {$a cmp $b} keys %$name_def;

    foreach my $chr (sort {$a cmp $b} keys %$hash) {
	foreach my $loc (sort {$a <=> $b} keys %{$hash->{$chr}}) {
	
	    if (($low_test->{$chr}->{$loc} == 1) &&
		($high_test->{$chr}->{$loc} == 1)) {
		
		print $chr . "\t" . $loc;

		foreach my $name (@name_array) {
	    
		    my $val;
		    if (defined $hash->{$chr}->{$loc}->{$name}) {
			$val = $hash->{$chr}->{$loc}->{$name};
		    } else {
			$val = "NA";
		    }

		    print "\t" . $val;

		}

		print "\n";

	    }

	}	
    }

}
