
#!/usr/bin/perl

use strict;


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


my @data_indices = (9,12,13,18,19,24,25,10,11,15,14,16,17,21,20,22,23,27,26,28);

my ($line, $start, $new_line);
my (@row, @new_row);


foreach my $ch ( @chrs ) {

    print("chromosome $ch\n");

    my $ifile = 'vcf/snps.filtered.hwe.chr'.$ch.'.vcf';
    my $ofile = 'vcf/snps.filtered.hwe.sorted.chr'.$ch.'.vcf';


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

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

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

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

	$line = <IN>;
        $start = substr( $line, 0, 2);
    } # end of headers while loop                                                                                     

    my $header_line = $line;
    chomp($header_line);
    my @header = split("\t", $header_line);
    my @new_header = (@header[0..8], @header[@data_indices]);
    my $new_header_line = join("\t", @new_header);
    print(OUT "$new_header_line\n");


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

	chomp($line);
        @row = split("\t", $line);

	@new_row = (@row[0..8], @row[@data_indices]);

	$new_line = join("\t", @new_row);
	print(OUT "$new_line\n");
    }
    close(IN);
    close(OUT);

} # end of chr loop
