#!/usr/bin/perl
#
# Name: compact.pl
# Purpose: to remove redundant sequences having the same name from a
#          gene set.
# Usage: ./compact.pl original_sequence_input > compact_sequence_output
# Created: 11/30/2006
# Copyright: (c) 2006-2009 Hui-Hsien Chou <hhchou@iastate.edu>
# License: See Picky License.pdf or visit <www.complex.iastate.edu>

use strict;
use warnings;

my %table;
my ($name, $seq);
my $count=0;

sub check_name {
  if (defined($name)) {
    if (exists $table{$name}) {
      print "$name has different sequences\n" if $table{$name} ne $seq;
    } else {
      $table{$name} = $seq;
    }
  }
}

while (<>) {
  chomp;
  if (/^>/) {
    &check_name;
    $name = $_;
    $seq = "";
    $count++;
  } else {
    $seq .= $_;
  }
}
&check_name;

print STDERR "$count input sequences\n";

$count=0;
while (($name, $seq) = each %table) {
  print "$name\n";
  for (my $i=0; $i<length($seq); $i+=70) {
    print substr($seq, $i, 70), "\n";
  }
  $count++;
}

print STDERR "$count output sequences\n";
