#!/usr/bin/perl
#
# Name: merge_back.pl
# Purpose: to merge oligo designed for chunks of long genomic sequences broken
#          up by break_up.pl back to target the whole genomic sequences.
# Usage: ./merge_back.pl picky_oligo_file > merged_output
# Created: 6/12/2005
# Copyright: (c) Hui-Hsien Chou <hhchou@iastate.edu>
# License: See Picky LICENSE.pdf or visit <www.complex.iastate.edu>

use strict;
use warnings;

die "Usage: ./merge_back.pl picky_oligo_file > merged_output\n" if @ARGV!=1;
my $input = $ARGV[0];
open INPUT, "$input" or die "Can't open input file $input\n";

for (my $i=0; <INPUT>; $i++) {
  if ($i) {
    chomp;
    my @fields = split/\t/;
    my @outputs;
    my %lookup;
    for (my $j=7; $j<@fields; $j+=4) {
      my ($name, $start) = split/##/, $fields[$j];
      unless (exists($lookup{$name}) && $lookup{$name}==$fields[$j+2]+$start) {
	push @outputs, $name;
	push @outputs, $fields[$j+1];
	push @outputs, $fields[$j+2]+$start;
	push @outputs, $fields[$j+3]+$start;
	$lookup{$name} = $fields[$j+2]+$start;
      }
    }
    print join("\t", @fields[0..6], @outputs), "\n";
  } else {
    print;
  }
}

