#!/usr/bin/perl
use warnings;
use strict;

foreach my $in (@ARGV){
    if ($in =~ /gz$/){
	open (IN," zcat $in | ") or die $!;
    }
    else{
	open (IN,$in) or die $!;
    }
    
    my $out = $in;
    $out .= '.covered.txt';

    open (OUT,'>',$out) or die $!;
    warn "Writing coverage report to $out\n\n"; sleep(1);
    
    my $count = 0 ;
    while (<IN>){
	chomp;
	++$count;
	if ($count%2500000 == 0){
	    warn "Processed $count lines already\n";
	}
	
	my ($chr,$pos,$m,$u,$context) = (split/\t/)[0,1,3,4,6];
	next unless (($m +$u) > 0); # only covered positions	
	# warn "$chr,$pos,$pos,$perc,$m,$u,$context\n"; sleep(1);

	print OUT "$_\n";
	
	### this is the coverage file format
	# my $perc =  sprintf ("%.2f",$m * 100 / ($m +$u));
	# print OUT join ("\t",$chr,$pos,$pos,$perc,$m,$u,$context)."\n";

    }


}

