
#!/usr/bin/perl

use strict;


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

foreach my $ch ( @chrs ) {

    print("chromosome $ch\n");

    # males

    my $wm_fname = join('', 'vcf/genotypes.raw.filtered.males.chr', $ch,'.vcf');
    my $s17_fname = join('', '../../../mouse.snps/17strain.data/final.chr', $ch, '.vcf');

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

    my $knownfile = join('', 'vcf/genotypes.known.males.chr', $ch, '.vcf');
    my $newfile = join('', 'vcf/genotypes.new.males.chr', $ch, '.vcf');
    my $known17strfile = join('', 'vcf/genotypes.known.17straindata.males.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;

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

	@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");

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

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

	if ( exists( ${$wild{ $bppos }}{'data'} ) ) {
	    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");

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

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

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

    close(OUTK);
    close(OUTN);



    # females

    my $wm_fname = join('', 'vcf/genotypes.raw.filtered.females.chr', $ch,'.vcf');
#    my $s17_fname = join('', '../../mouse.snps/17strain.data/final.chr', $ch, '.vcf');

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

    my $knownfile = join('', 'vcf/genotypes.known.females.chr', $ch, '.vcf');
    my $newfile = join('', 'vcf/genotypes.new.females.chr', $ch, '.vcf');
    my $known17strfile = join('', 'vcf/genotypes.known.17straindata.females.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;

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

	@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");

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

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

	if ( exists( ${$wild{ $bppos }}{'data'} ) ) {
	    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");

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

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

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

    close(OUTK);
    close(OUTN);

} # end of chr loop
