#!/usr/bin/perl -w

# input - HGDP Stanford genotyping date (chrxx.map and chrxx.geno), as well as a list of hapmap snp loci
# generated from get_hapmap_overlapping_snp.pl
# output - a file of overlapping snp loci between HGDP and Hapmap samples for each chromosome
# these SNPs are separated by 0.5 Mb as a minimum

use strict;

my ($fhiA, $fhia, $fhiB, $fho);
my @hgdp_marker_pool; # snps in hgdp file
my %hgdp_cor; # key - snp marker name, value - coordinate in chromosome
my %hp_cor; # key - snp marker name, value - coordiante in chromosome 
my @overlap;
my %overlap_cor; # key -overlapping snp loci, value - coordinate
my @sorted_overlap; # array in which snp loci are put in based on their coordinates - from small to large 
my @selected_marker;

for ( my $i = 22; $i >= 22; $i-- ) {
 	
	warn "start reading HGDP chr$i.\n";
	open $fhiA, "chr$i.geno" or die "cannot open hgdpchr$i geno file: $!\n";
	<$fhiA>; #skip the 1st line
	my $n = 0;
	while (my $line = <$fhiA>){
		chomp $line;
		my ($marker_id, @snp) = split /\t/, $line;
		push (@hgdp_marker_pool, $marker_id);
		$n++;
	}
	warn "$n markers read. HGDP chr$i geno file done.\n\n";
	close $fhiA;
	
	warn "start reading HGDP chr$i map.\n";
	open $fhia, "chr$i.map" or die;
	my $q = 0;
	while (my $line = <$fhia>) {
		chomp $line;
		my ($marker_id, $hgdp_chr, $hgdp_cor) = split /\t/, $line;
		$hgdp_cor{$marker_id} = $hgdp_cor;
		$q++;
	}
	warn "$q lines read. HGDP chr$i map file done.\n\n";
	close $fhia;
	
	foreach my $mark (@hgdp_marker_pool) {
		unless (exists $hgdp_cor{$mark}) {
			warn "error in hgdp map and geno file chr$i.\n";
		}
	}

	warn "start reading HapMap chr$i.\n";
	open $fhiB, "hp-chr$i.txt" || die "cannot open hpchr$i file: $!\n";
	my $m = 0;
	while (my $line = <$fhiB>){
		chomp $line;
		my ($marker_id, $hp_cor) = split /\t/, $line;
		$hp_cor{$marker_id} = $hp_cor;
		$m++;
	}
	warn "$m markers read.\nchr$i done.\n\n";	
	close $fhiB;

	foreach my $marker (keys %hgdp_cor) {
		if (exists $hp_cor{$marker}) {
			if ($hp_cor{$marker} != $hgdp_cor{$marker}) {
				warn "error: chr$i $marker coordinates in hapmap and HGDP inconsistent.\n";
			}
			else {
				push (@overlap, $marker);
			}
		}
	}	
	
	my $k = @overlap;
	print "There are $k overlapping markers in chr$i.\n";
	
	foreach my $overlap (@overlap) {
		$overlap_cor{$overlap} = $hgdp_cor{$overlap};
	}
	
	foreach my $key (sort byvalue keys %overlap_cor) {
		push (@sorted_overlap, $key);
	}	
	
	push (@selected_marker, $sorted_overlap[0]); # get the first overlapping snp loci
	
	for (my $f = 1; $f < $k; $f++) {
		if ($hgdp_cor{$sorted_overlap[$f]} > ($hgdp_cor{$selected_marker[-1]} + 500000) ) {
			push (@selected_marker, $sorted_overlap[$f]);
		}
	}	
	
	open $fho, ">selected-chr$i.txt" or die;
	for my $element (@selected_marker) {
		print $fho "$element\t$hgdp_cor{$element}\n";
	}		
	
	my $s = @selected_marker;
	print "$s markers selected based on coordinates in chr$i\n";
	close $fho;
	
	@hgdp_marker_pool = ();
	%hgdp_cor = ();
	%hp_cor = ();
	@overlap = ();	
	@sorted_overlap = ();
	%overlap_cor = ();
	@selected_marker = ();
	
}

sub byvalue {
	$overlap_cor{$a} <=> $overlap_cor{$b}
}	

	
	

