#!/usr/bin/perl
# -*-Perl-*-
# Last changed Time-stamp: <1999-06-07 21:11:06 xtof>

my %seqHash = ();

while (<>) { # read clustal file
  next if /^CLUSTAL/ || /^\s+/ || /^\n$/;
  my ($name, $seq) = split;
  $seqHash{$name} = [] if !defined $seqHash{$name};
  push @{$seqHash{$name}}, split //, $seq;
}

print_nexus_head(\%seqHash);

for (;;) {
  my $n;
  foreach (sort (keys %seqHash)) {
    $n = $_; # memorize sequence name 4 termination of for-loop
    write;
    splice(@{$seqHash{$_}}, 0, 60);
  }
  last if $#{$seqHash{$n}} < 0;
  print "\n";
}

print_nexus_tail();

sub print_nexus_head {

  my $hash_ref = shift;
  my @labels = sort (keys %{$hash_ref});
  my $ntax = $#labels+1;
  my $nchar =  $#{$seqHash{$labels[0]}}+1;
  print << "EOT";
\#NEXUS

BEGIN taxa;
DIMENSIONS ntax=$ntax;
TAXLABELS @labels;
END;
  
BEGIN characters;
DIMENSIONS nchar=$nchar;
FORMAT datatype=DNA gap=- labels interleave;
MATRIX
EOT
}

sub print_nexus_tail {

  print << "EOT";
;
END;
show st_graph
EOT
}

format STDOUT =
@<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$_, join '', @{$seqHash{$_}}
.

# End of file
