#!/usr/bin/perl

use strict;


my $mouse = $ARGV[0];
my $gname = join('', '/data/mus/wild_mus/gatk_mask_chr7/wildmus', $mouse, '_A_gatk.mask');
my $pname = join('', '/data/mus/wild_mus/python_mask_chr7/hasREFN/wildmus', $mouse, '_A_chr7.mask');


my ($i, $line, $desc, $Nbases);
my @row;
my (%masked, %called);


my @index = (0) x 152524553;


open(IN, '<', $pname) or die "Cannot open file $pname, reason $!\n";

$line = <IN>;

while ( defined($line = <IN>) ) {

    chomp($line);

    @row = split("\t", $line);
#    print("@row\n");

    for ($i=$row[1]; $i <= $row[2]; $i++) {
	$index[$i] = 1;
    }

    $desc = $row[3];
    $Nbases = ( $row[2] - $row[1] + 1 );

    if ( exists( $masked{$desc} ) ) {
        $masked{$desc} = ( $masked{$desc} + $Nbases );
    } else {
        $masked{$desc} = $Nbases;
    }
}
close(IN);

my $Nmask = 0;
for ($i=0; $i <= $#index; $i++) {
    $Nmask = ( $Nmask + $index[$i] );
}

print("chr length $#index  N masked $Nmask\n");

print("\nprint out hash of masked bases\n");
foreach my $type ( keys %masked ) {
    print("$type $masked{$type}\n");
}


open(IN, '<', $gname) or die "Cannot open file $gname, reason $!\n";

while ( defined($line = <IN>) ) {

    chomp($line);

    @row = split(' ', $line);

    $desc = $row[3];
    $Nbases = ( $row[2] - $row[1] + 1 );

    if ( $desc ne 'CALLABLE' ) {
	for ($i=$row[1]; $i <= $row[2]; $i++) {
	    $index[$i] = 1;
	}
    }

    if ( exists( $called{$desc} ) ) {
	$called{$desc} = ( $called{$desc} + $Nbases );
    } else {
	$called{$desc} = $Nbases;
    }

#    print("$desc  N bases $Nbases\n");
}
close(IN);

print("\n\nprint out hash of callable bases\n");
foreach my $type ( keys %called ) {
    print("$type $called{$type}\n");
}


my $Nmask = 0;
for ($i=0; $i <= $#index; $i++) {
    $Nmask = ( $Nmask + $index[$i] );
}

print("\n\nchr length $#index  N masked $Nmask\n");
