#!/usr/local/bin/perl

 use strict;
 use warnings;

 # This program will take the output from synteny_pairwise.cgi for Alligator, Anolis, Boa, Chicken, Turtle and Viper (reference)
 # with Pogona (target).
 #
 # The output is a table of putative junctions between Pogona scaffolds together with the support for these junctions
 # drawn from consensus across the reference taxa.
 #
 # Briefly, two Pogona scaffolds are joined to form a 2-scaffold when they align consecutively to the reference genome
 # AND when they abut, end to end.
 #
 # Manual inspection may be required to add junctions that are obvious by eye, but missed by the strict algoritm applied
 # here.
 #
 # Author: Arthur Georges, georges@aerg.canberra.edu.au
 #
#######################################################################
# CONFIGURATION

  my $script = 'synteny_combine.cgi';
  my $version = '16-Dec-2013';
  my $out_ext = '.out';
  my $log_ext = '.log';
# Input files
  my $ano_tablefile = 'synteny_Ano-Pog.out';
  my $all_tablefile = 'synteny_All-Pog.out';
  my $boa_tablefile = 'synteny_Boa-Pog.out';
  my $gal_tablefile = 'synteny_Gal-Pog.out';
  my $tur_tablefile = 'synteny_Tur-Pog.out';
  my $vip_tablefile = 'synteny_Vip-Pog.out';
# Output file
  my $output = 'synteny.out';
  my $logfile = 'synteny.log';

  print "Running $script\n";
  print "Version $version\n";

#######################################################################

# INTIALIZE SCALARS AND ARRAYS

  my $linecount;
  my ($scaffold, $scaffold1, $scaffold2);
  my $reference;
  my $order;
  my %master_hash;
  my %ano_hash;
  my %all_hash;
  my %gal_hash;
  my %boa_hash;
  my %tur_hash;
  my %vip_hash;
  my $size;
  my ($key, $gal, $ano, $boa, $vip, $tur, $all);

 #######################################################################
 # OPEN THE OUTPUT FILE
  open (OUT, '>'."$output");
  print "Output file $output opened \n";

 # OPEN THE LOG FILE
  open (LOG, '>'."$logfile");
  print LOG "SUMMARY OF ANALYSIS TO LINK SCAFFOLDS IN POGONA BY PAIRWISE CONSENSUS \n\n";

 # OPEN THE INPUT FILES
  open (ANO, $ano_tablefile) || die "File $ano_tablefile not found\n";
  print "tablefile file $ano_tablefile opened \n";
  print LOG "tablefile file $ano_tablefile opened \n";
  print "NOTE: Assumed that the input file is sorted on reference chromosome ID and then gene location \n";
  open (ALL, $all_tablefile) || die "File $all_tablefile not found\n";
  print "tablefile file $all_tablefile opened \n";
  print LOG "tablefile file $all_tablefile opened \n";
  open (BOA, $boa_tablefile) || die "File $boa_tablefile not found\n";
  print "tablefile file $boa_tablefile opened \n";
  print LOG "tablefile file $boa_tablefile opened \n";
  open (GAL, $gal_tablefile) || die "File $gal_tablefile not found\n";
  print "tablefile file $gal_tablefile opened \n";
  print LOG "tablefile file $gal_tablefile opened \n";
  open (TUR, $tur_tablefile) || die "File $tur_tablefile not found\n";
  print "tablefile file $tur_tablefile opened \n";
  print LOG "tablefile file $tur_tablefile opened \n";
  open (VIP, $vip_tablefile) || die "File $vip_tablefile not found\n";
  print "tablefile file $vip_tablefile opened \n\n";
  print LOG "tablefile file $vip_tablefile opened \n\n";

# READ THE JOINED SCAFFOLDS INTO A HASH FOR EACH REFERENCE SPECIES

   $linecount=0;
   while (<ANO>){
    $linecount = $linecount + 1;
    # Split the line into variables
      ($scaffold1, $scaffold2, $reference, $order) = split(" ", $_);
    # Add to the hash
      $scaffold = 'SS_'.$scaffold1.'-'.$scaffold2.'_'.$order;
      $scaffold =~ s/scf//g;
      if ($scaffold1 gt $scaffold2) {
        $order = reverse($order);
        $scaffold = 'SS_'.$scaffold2.'-'.$scaffold1.'_'.$order;
        $scaffold =~ s/scf//g;
      }
      $ano_hash{$scaffold} = $reference;
      $master_hash{$scaffold} = 1;
   }
   print "$linecount records from $ano_tablefile processed\n";
   print LOG "$linecount records from $ano_tablefile processed\n";
   $size = keys %master_hash;
   print "  $size records in the combined hash tables\n";

   $linecount=0;
   while (<ALL>){
    $linecount = $linecount + 1;
    # Split the line into variables
      ($scaffold1, $scaffold2, $reference, $order) = split(" ", $_);
    # Add to the hash
      $scaffold = 'SS_'.$scaffold1.'-'.$scaffold2.'_'.$order;
      $scaffold =~ s/scf//g;
      if ($scaffold1 gt $scaffold2) {
        $order = reverse($order);
        $scaffold = 'SS_'.$scaffold2.'-'.$scaffold1.'_'.$order;
        $scaffold =~ s/scf//g;
      }
      $all_hash{$scaffold} = $reference;
      $master_hash{$scaffold} = 1;
   }
   print "$linecount records from $all_tablefile processed\n";
   print LOG "$linecount records from $all_tablefile processed\n";
   $size = keys %master_hash;
   print "  $size records in the combined hash tables\n";

   $linecount=0;
   while (<BOA>){
    $linecount = $linecount + 1;
    # Split the line into variables
      ($scaffold1, $scaffold2, $reference, $order) = split(" ", $_);
    # Add to the hash
      $scaffold = 'SS_'.$scaffold1.'-'.$scaffold2.'_'.$order;
      $scaffold =~ s/scf//g;
      if ($scaffold1 gt $scaffold2) {
        $order = reverse($order);
        $scaffold = 'SS_'.$scaffold2.'-'.$scaffold1.'_'.$order;
        $scaffold =~ s/scf//g;
      }
      $boa_hash{$scaffold} = $reference;
      $master_hash{$scaffold} = 1;
   }
   print "$linecount records from $boa_tablefile processed\n";
   print LOG "$linecount records from $boa_tablefile processed\n";
   $size = keys %master_hash;
   print "  $size records in the combined hash tables\n";

   $linecount=0;
   while (<GAL>){
    $linecount = $linecount + 1;
    # Split the line into variables
      ($scaffold1, $scaffold2, $reference, $order) = split(" ", $_);
    # Add to the hash
      $scaffold = 'SS_'.$scaffold1.'-'.$scaffold2.'_'.$order;
      $scaffold =~ s/scf//g;
      if ($scaffold1 gt $scaffold2) {
        $order = reverse($order);
        $scaffold = 'SS_'.$scaffold2.'-'.$scaffold1.'_'.$order;
        $scaffold =~ s/scf//g;
      }
      $gal_hash{$scaffold} = $reference;
      $master_hash{$scaffold} = 1;
   }
   print "$linecount records from $gal_tablefile processed\n";
   print LOG "$linecount records from $gal_tablefile processed\n";
   $size = keys %master_hash;
   print "  $size records in the combined hash tables\n";

   $linecount=0;
   while (<TUR>){
    $linecount = $linecount + 1;
    # Split the line into variables
      ($scaffold1, $scaffold2, $reference, $order) = split(" ", $_);
    # Add to the hash
      $scaffold = 'SS_'.$scaffold1.'-'.$scaffold2.'_'.$order;
      $scaffold =~ s/scf//g;
      if ($scaffold1 gt $scaffold2) {
        $order = reverse($order);
        $scaffold = 'SS_'.$scaffold2.'-'.$scaffold1.'_'.$order;
        $scaffold =~ s/scf//g;
      }
      $tur_hash{$scaffold} = $reference;
      $master_hash{$scaffold} = 1;
   }
   print "$linecount records from $tur_tablefile processed\n";
   print LOG "$linecount records from $tur_tablefile processed\n";
   $size = keys %master_hash;
   print "  $size records in the combined hash tables\n";

   $linecount=0;
   while (<VIP>){
    $linecount = $linecount + 1;
    # Split the line into variables
      ($scaffold1, $scaffold2, $reference, $order) = split(" ", $_);
    # Add to the hash
      $scaffold = 'SS_'.$scaffold1.'-'.$scaffold2.'_'.$order;
      $scaffold =~ s/scf//g;
      if ($scaffold1 gt $scaffold2) {
        $order = reverse($order);
        $scaffold = 'SS_'.$scaffold2.'-'.$scaffold1.'_'.$order;
        $scaffold =~ s/scf//g;
      }
      $vip_hash{$scaffold} = $reference;
      $master_hash{$scaffold} = 1;
   }
   print "$linecount records from $vip_tablefile processed\n";
   print LOG "$linecount records from $vip_tablefile processed\n";
   $size = keys %master_hash;
   print "  $size records in the combined hash tables\n\n";
   print LOG "  $size records in the combined hash tables\n\n";

 # FOR EACH SCAFFOLD IN THE COMBINED HASH, CHECK FOR VALUES IN THE REFERENCE HASHES

   foreach my $key ( keys %master_hash ){
     $ano='NA'; $all='NA'; $boa='NA'; $gal='NA'; $tur='NA'; $vip='NA';
     if ($ano_hash{$key}) { $ano = $ano_hash{$key} };
     if ($all_hash{$key}) { $all = $all_hash{$key} };
     if ($boa_hash{$key}) { $boa = $boa_hash{$key} };
     if ($gal_hash{$key}) { $gal = $gal_hash{$key} };
     if ($tur_hash{$key}) { $tur = $tur_hash{$key} };
     if ($vip_hash{$key}) { $vip = $vip_hash{$key} };
     print OUT "$key, $gal, $ano, $boa, $vip, $tur, $all\n";
   }

 close (OUT);
 print "Putative super-scaffold name, and spanning scaffolds for";
 print "  Gallus, Anolus, Boa, Viper, Turtle, Alligator written to output file $output\n";
 print "  NA for no overlap\n\n";
 print "outputfile file $output closed \n";
 print LOG "Putative super-scaffold name, and spanning scaffolds for";
 print LOG "  Gallus, Anolus, Boa, Viper, Turtle, Alligator written to output file\n";
 print LOG "  NA for no overlap\n";
 print LOG "Output file $output closed \n\n";


 close (ANO);
 print "tablefile file $ano_tablefile closed \n";
 close (ALL);
 print "tablefile file $all_tablefile closed \n";
 close (BOA);
 print "tablefile file $boa_tablefile closed \n";
 close (GAL);
 print "tablefile file $gal_tablefile closed \n";
 close (TUR);
 print "tablefile file $tur_tablefile closed \n";
 close (VIP);
 print "tablefile file $vip_tablefile closed \n\n";
 
 close (LOG);
 print "Log file $logfile closed \n\n";

 exit;
