#!/usr/bin/perl -w
use strict;

use Switch;
use Data::Dumper;

# perl script for ssr data, specialized to monococcum
# note: this script could easily be generalized to n comparitive cases, using
# recursion

$Data::Dumper::Pad = "... "; 

#file handles
my $DV_ssr = "./TmDV92_ssr.txt";
my $G3_ssr = "./TmG3116_ssr.txt";
my $ortho = "./monococcum_orthologs.txt";

# hashes
my %DV;
my %G3;

#load ssr data into hash keyed by DV gene id   
#add ssr_words hash to the DV_id_gene_key
#add first ssr word as key to ssr_words hash with ssr as key and # as value
#add additional ssr words for this gene as encountered (if Gene exists)
sub pop_locus_hashes($)
{
    my $line;
    my %local_hash;
    
    open(SSR_FILE, $_[0]) or die("Could not open $_[0]"); 
        
    while (<SSR_FILE>)
    {
        $line = $_;
        chomp $line;
        my @line_ary = split('\t',$line);
        my $locus_id = $line_ary[0];
        my $ssr_word = $line_ary[3];
        my $ssr_word_count = $line_ary[4];
        
        # add new gene to hash value (array) for old gene hash key
        $local_hash{$locus_id}{"ssr_words"}{$ssr_word} = $ssr_word_count;
    }
    
    close SSR_FILE;
    
    #print "[Current Hash: " . $_[0] . "]\n";
    #print Dumper(\%local_hash) . "\n\n";
    #print "\n";
    
    return %local_hash
}

# Does this locus already exist?
#    if (!exists $DV{$locus_id})
#    {

# Read blastall results
# Foreach line splits on tab get 1st gene identifier
    #look in DV hash for gene id
        #look at each ssr_words key

        # look in G3 hash for G3ece (2nd gene identifier)
                #look in its ssr_words for current DV ssr_word key
                #if present...
                        
sub stitch_ssr_to_ortho()
{
    my $line;
    
    open(ORTHO_FILE, $ortho) or die("Could not open $ortho"); 
    
    while (<ORTHO_FILE>)
    {
        $line = $_;
        chomp $line;
        my @line_ary = split('\t',$line);
        my $DV_locus = $line_ary[0];
        my $G3_locus = $line_ary[1];
        
        #print "$DV_locus\t$G3_locus\n";
        
        if (exists $DV{$DV_locus})
        {
            foreach my $curr_DV_ssr_word (keys $DV{$DV_locus}{ssr_words}) {
                #print "$DV_locus : $curr_DV_ssr_word: " . $DV{$DV_locus}{ssr_words}{$curr_DV_ssr_word} . "\n";
                if (exists $G3{$G3_locus})
                {
                    foreach my $curr_G3_ssr_word (keys $G3{$G3_locus}{ssr_words}) {
                        if ($curr_DV_ssr_word eq $curr_G3_ssr_word) {
                            #print "$DV_locus:$curr_DV_ssr_word:" . $DV{$DV_locus}{ssr_words}{$curr_DV_ssr_word}
                            #    . "\t$G3_locus:$curr_G3_ssr_word:" . $G3{$G3_locus}{ssr_words}{$curr_G3_ssr_word} . "\n";
                            #if ($DV{$DV_locus}{ssr_words}{$curr_DV_ssr_word} == $G3{$G3_locus}{ssr_words}{$curr_G3_ssr_word}) # equivalent SSR counts 
                            if ($DV{$DV_locus}{ssr_words}{$curr_DV_ssr_word} != $G3{$G3_locus}{ssr_words}{$curr_G3_ssr_word})  # differential SSR counts
                            {
                                #if ((length($curr_DV_ssr_word) > 2)) { # dimer exclusion
                                    print "$curr_DV_ssr_word"
                                        . "\t$DV_locus\t$DV{$DV_locus}{ssr_words}{$curr_DV_ssr_word}"
                                        . "\t$G3_locus\t$G3{$G3_locus}{ssr_words}{$curr_G3_ssr_word}\n";
                                #}
                                #print "$DV_locus:$curr_DV_ssr_word:" . $DV{$DV_locus}{ssr_words}{$curr_DV_ssr_word}
                                #    . "\t$G3_locus:$curr_G3_ssr_word:" . $G3{$G3_locus}{ssr_words}{$curr_G3_ssr_word} . "\n";
                            }
                        }
                    }
                }
            }
        }
        #print "\n";
    }
    close ORTHO_FILE;
}
                        
                        
# main
%DV = pop_locus_hashes($DV_ssr);
%G3 = pop_locus_hashes($G3_ssr);

stitch_ssr_to_ortho();

exit;
