
#!/usr/bin/perl

use strict;


my @chrs = (1..19, 'X', 'Y');
print("@chrs\n");

foreach my $ch ( @chrs ) {

    print("chromosome $ch\n");

    my $fname = join('', 'SNPs/comb/filter/snps.combined.filter.phr15.chr', $ch,'.vcfstyle.txt');
    my $ofile = join('', '../vcfstyle/genotypes.combined.filtered.chr', $ch, '.vcfstyle.txt');


    open(INW, '<', $fname) or die "Cannot open vcf file $fname, reason $!\n";

    open(OUT, '>', $ofile) or die "Cannot open file $ofile for writing, reason $!\n "; 

    my ($line, $start, $trd);
    my @row;

    $line = <INW>;
    $start = substr( $line, 0, 2);

    while ( $start eq '##' ) {
	print(OUT "$line");

        $line = <INW>;
        $start = substr( $line, 0, 2);
    } # end of headers while loop               
 
    my $header_line = $line;
    print(OUT "$header_line");

    my $Ntot = 0;
    my $Nhig = 0;

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

	$Ntot++;

	@row = split("\t", $line); 
	$trd = $row[4];
	if ( $trd > 340 ) {
	    $Nhig++;
	} else {
	    print(OUT "$line");
	}
    }
    close(INW);
    close(OUT);

    print(" Total snps $Ntot  dropped for high coverage $Nhig\n");

} # end of chr loop
