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

# KONI - 2004-02-09
#
# This program is documented for the case that similar problems arise with
# different data. Otherwise, it will only need to be run once to prepare the
# list of clones and chromatograms for SGN submission.

if (!$ARGV[0] || $ARGV[0] eq "help") {
  print <<EOF;

  This is a quick one-shot program to attempt a mapping of monsanto 
  chromatogram names for cPHA (Petunia) to freezer plate-well addresses.

  We assume a standard 384 -> 4x96 mapping was performed by the sequencing 
  facility and attempt to deconvolute from what appears to be 96-well plate
  numbers and well addresses. 

  The lab people left absolutely no information on what actually happened
  during sequencing. Many assumptions made here may not be valid. The output
  of this program will give us freezer plate addresses so that clones may be
  resequenced, but if any of the assumptions are wrong, the identified clone
  in the freezer will not be the one desired.

  Usage: ( use - to make this program run )

  No arguments are expected -- reads chromatogram name list from STDIN

EOF
  exit -1;
}

while(<STDIN>) {
  chomp;
  my $trace_name = $_;

  if (m/^LIB4113-([0-9]+)-[^-]+-[^-]+-([A-M][0-9]{1,2})\.gz$/) {
    my ($plate, $well) = ($1,$2);

    # Monsanto plate numbers seem to start with 1 but we need them to start
    # with 0 so the math works below.
    $plate = int($plate) - 1;

    # Map back to the Cornell 384 freezer plate address. -- Note, it is not
    # known if the mapping employed is correct. See comment below.
    my ($master_plate, $master_well) = monsanto_deconvolute($plate,$well);
    print "$trace_name\tcPHA-$master_plate-$master_well\t5\n";
  } else {
    print STDERR "Unmatched $_\n";
  }
}

# Sub-routine for a 384 -> 4 x 96 plate *reverse* mapping for "Monsanto's
# Robbin Hydra 384 well positioner".
#
# The information used to develop the algorithm below was gleaned from some
# old scripts Rutger van der Hoeven wrote and left laying about on the theory
# center SGN folder. I can't vouch for its accuracy, but I'm assuming its more
# likely to be right than our other standard deconvultion (for TIGR) which is
# quite different. (Koni)
#
#   One 384 well plate divided into quadrants of 96 wells. These quandrants 
#   are then mapped to the 4 96 well subplates apparently in this
#   counter-clockwise fashion:
#
#   +----------------------+--------------------+
#   |                      |                    |
#   |			   |			|
#   |			   |			|
#   |		3	   |		2	|
#   |			   |			|
#   |			   |			|
#   +----------------------+--------------------+
#   |                      |                    |
#   |			   |			|
#   |			   |			|
#   |		0	   |		1	|
#   |			   |			|
#   |			   |			|
#   +----------------------+--------------------+
#
sub monsanto_deconvolute {
  my ($plate, $well) = @_;

  my ($row, $col) = $well =~ m/([A-P])([0-9]{1,2})/;
  $col = int($col);
  $row = ord($row) - ord('A');

  my $subplate = $plate % 4;
  my ($source_row, $source_col);
  if ($subplate == 3 || $subplate == 0) {
    $source_col = $col;
  } else {
    $source_col = $col + 12;
  }
  if ($subplate == 3 || $subplate == 2) {
    $source_row = $row;
  } else {
    $source_row = $row + 8;
  }

  $source_row = chr(ord('A') + $source_row);

  # Freezer source plates start numbering with 1.
  my $master_plate = int($plate / 4) + 1;

  return ($master_plate, "$source_row$source_col");
}


# Included for illustrational/documentory purposes - this is a very different
# deconvolution used for mapping the sequencing TIGR pipeline back to the
# source plate addresses.
sub tigr_deconvolute {
  my ($plate, $well) = @_;

  my ($row, $col) = $well =~ m/([A-P])([0-9]{1,2})/;
  $col = int($col)-1;
  $row = ord($row) - ord('A');

  my $subplate = $plate % 4;

  # Freezer source plates start numbering with 1.
  my $master_plate = int($plate / 4) + 1;

  my $source_row = chr(ord('A') + int($subplate/2) + $row*2);
  my $source_col = ($subplate % 2) + $col*2 + 1;

  return ($master_plate, "$source_row$source_col");
}
