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

foreach my $in (@ARGV){
    die "Check input file name settings!\n" unless ($in =~ /CX_report\.txt$/);
    open (IN,$in) or die $!;
    
    my $out = $in;
    die "Check renaming settings!\n" unless ($out =~ s/CX_report\.txt/CA.cov/);
    open (OUT,'>',$out) or die $!;
    warn "Writing CA 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 ($context =~ /^CA/); # only CA context
	next unless (($m +$u) > 0); # only covered positions	
	my $perc =  sprintf ("%.2f",$m * 100 / ($m +$u));
	# warn "$chr,$pos,$pos,$perc,$m,$u,$context\n"; sleep(1);
	print OUT join ("\t",$chr,$pos,$pos,$perc,$m,$u,$context)."\n";

    }


}

