
#!/usr/bin/perl

use strict;


my $subdir = $ARGV[0];
my $combset = $ARGV[1];


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


my $sumfile = $subdir.'/splitknown.'.$combset.'.sumout.txt';

open(OUS, '>', $sumfile) or die "Cannot open vcf file $sumfile, reason $!\n";
print(OUS "chr total 17str known new\n");


foreach my $ch ( @chrs ) {

    print("\nchromosome $ch\n");

    my $wm_fname = $subdir.'/SNPs.'.$combset.'.chr'.$ch.'.vcf';
    my $s17_fname = '/data/mouse.snps/17strain.data/final.chr'.$ch.'.vcf';

    print("input file $wm_fname\n");
    print("17 strains file $s17_fname\n");

    my $knownfile = $subdir.'/genotypes.'.$combset.'.known.chr'.$ch.'.vcf';
    my $newfile = $subdir.'/genotypes.'.$combset.'.new.chr'.$ch.'.vcf';
    my $known17strfile = $subdir.'/genotypes.'.$combset.'.known.17straindata.chr'.$ch.'.vcf';


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

    my ($line, $start);

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

    while ( $start eq '##' ) {
        $line = <INW>;
        $start = substr( $line, 0, 2);
    } # end of headers while loop                                                                                     
 
    my $header_line = $line;

    my ($line, $bppos, $ref, $alt, $mice_string);
    my (@row, @mice);
    my %wild;

    my $Nwild = 0;

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

	$Nwild = ( $Nwild + 1 );

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

	$bppos = $row[1];
	$wild{ $bppos }{'data'} = 'Y';
    }
    close(INW);

    print("hash of wild mouse data created\n");


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


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

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

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

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

    my $header_line = $line;
    print(OUTS "$header_line");

    my $N17 = 0;

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

	@row = split(/\t/, $line);
	$bppos = $row[1];

	if ( exists( ${$wild{ $bppos }}{'data'} ) ) {
	    $N17 = ( $N17 + 1 );
	    print(OUTS "$line");
	    $wild{ $bppos }{'known'} = 'Y';
	}

    } # end of while loop
    close(INS);
    close(OUTS);

    print("matches in 17 strain data found and printed\n");


    open(OUTK, '>', $knownfile) or die "Cannot open file $knownfile for writing, reason $!\n ";
    open(OUTN, '>', $newfile) or die "Cannot open file $newfile for writing, reason $!\n ";

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

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

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

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

    my $header_line = $line;
    print(OUTK "$header_line");
    print(OUTN "$header_line");

    my $Nknown = 0;
    my $Nnew = 0;

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

        @row = split(/\t/, $line);
        $bppos = $row[1];

	if ( exists( ${$wild{ $bppos }}{ 'known' }) ) {
	    $Nknown = ( $Nknown + 1 );
	    print(OUTK "$line");
	} else {
	    $Nnew = ( $Nnew + 1 );
	    print(OUTN "$line");
	}
    }
    close(INW);

    close(OUTK);
    close(OUTN);

    print(OUS "$ch $Nwild $N17 $Nknown $Nnew\n");

} # end of chr loop

close(OUS);
