
#!/usr/bin/perl

use strict;


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

foreach my $ch ( @chrs ) {

    print("chromosome $ch\n");

    my $inAfile = 'vcfstyle/snps.combined.filter.missing.addDels.hwe.chr'.$ch.'.vcfstyle.txt';
    my $inKfile = 'vcfstyle/genotypes.known.chr'.$ch.'.vcfstyle.txt';
    my $inNfile = 'vcfstyle/genotypes.new.chr'.$ch.'.vcfstyle.txt';

    my $multknownfile = 'vcfstyle/snps.known.multALTs.chr'.$ch.'.vcfstyle.txt';
    my $multnewfile = 'vcfstyle/snps.new.multALTs.chr'.$ch.'.vcfstyle.txt';
    my $multallfile = 'vcfstyle/snps.all.multALTs.chr'.$ch.'.vcfstyle.txt';

    my $biknownfile = 'vcfstyle/snps.known.biALTs.chr'.$ch.'.vcfstyle.txt';
    my $binewfile = 'vcfstyle/snps.new.biALTs.chr'.$ch.'.vcfstyle.txt';
    my $biallfile = 'vcfstyle/snps.all.biALTs.chr'.$ch.'.vcfstyle.txt';



    open(INA, '<', $inAfile) or die "Cannot open vcfstyle file $inAfile, reason $!\n";

    open(OUAM, '>', $multallfile) or die "Cannot open file $multallfile for writing, reason $!\n ";
    open(OUAB, '>', $biallfile) or die "Cannot open file $biallfile for writing, reason $!\n";

    my ($line, $start);

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

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

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

    my ($line, $alt);
    my @row;


    my $Nwild = 0;

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

	$Nwild = ( $Nwild + 1 );

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

	$alt = $row[3];
	if ( length($alt) > 1 ) {
	    print(OUAM "$line");
	} else {
	    print(OUAB "$line");
	}
    }
    close(INA);
    close(OUAM);
    close(OUAB);


    open(OUKM, '>', $multknownfile) or die "Cannot open file $multknownfile for writing, reason $!\n";
    open(OUKB, '>', $biknownfile) or die "Cannot open file $biknownfile for writing, reason $!\n";

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

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

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

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

    my $header_line = $line;
    print(OUKM "$header_line");
    print(OUKB "$header_line");

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

	@row = split("\t", $line);
	$alt = $row[3];

	if ( length($alt) > 1 ) {
	    print(OUKM "$line");
	} else {
	    print(OUKB "$line");
	}

    } # end of while loop
    close(INK);
    close(OUKM);
    close(OUKB);


    open(OUNM, '>', $multnewfile) or die "Cannot open file $multnewfile for writing, reason $!\n";
    open(OUNB, '>', $binewfile) or die "Cannot open file $binewfile for writing, reason $!\n";

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

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

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

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

    my $header_line = $line;
    print(OUNM "$header_line");
    print(OUNB "$header_line");

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

        @row = split("\t", $line);
        $alt = $row[3];

	if ( length($alt) > 1 ) {
	    print(OUNM "$line");
	} else {
	    print(OUNB "$line");
	}
    }
    close(INN);
    close(OUNM);
    close(OUNB);

    print("Chromosome $ch: total number of SNPs $Nwild\n");

} # end of chr loop
