#!/usr/bin/perl

use strict;
my $suffix = "HS.txt";

my $f = "/data/mus/martin/projects/inbred/testmerge270710/case12.all.wheader.txt";

my $suffix = "CC.txt";
my $f = "/data/mus/martin/projects/inbred/testmerge270710/allCC.txt";

  open(FILE,$f) || die "Could not open $f\n";
  my $header = <FILE>;
  chomp($header);
  my ($x, $y, @s ) = split(/\s+/,$header);
  my $lastchr = undef;
  while(<FILE>) {
    chomp;
    my ( $chr, $pos, @a ) = split;
    if ( $chr ne $lastchr ) {
      close(OUT);
      my $out = "chr$chr.$suffix";
      open(OUT,">$out") || die "Could not open $out\n";
      print OUT "$header\n";
      print "$out\n";
      $lastchr = $chr;
    }
	
    my $count;
    for( my $n=0;$n<=$#a;$n++) {
      $a[$n] = $s[$n] if ( $a[$n] eq "N" );
      $count->{$a[$n]}++;
    }
    my @k = keys %$count;
    
    print OUT join( "\t", $chr, $pos, @a ) . "\n" if ( $#k > 0 );
  }

      
