#!/usr/bin/perl

# By @ DIMITRY A SUPLATOV             

use warnings;
use strict;

my $file = $ARGV[0];

if (!$file)
{die "
 
 v 2.2 28-Jan-2010
 Prints out sequence names and thier length.
 If special symbols (\";\") are present script treats them as chain separators
 and prints the length of different chains. \"-\" sign is treated as a gap
 and deleted from total count.
 Usage: >getFastaNames.pl <filename>\n\n";}

open (F, "$file");

my @orderarray = ();

my %hash=();
my $name="";
my $seq="";
while(<F>)
    {
        chomp;
        if (/^>(.+)/) 
        {
            if (!$name and $name ne "0") {$name = $1; next;}
            my $tmp = $1;
            $seq=~s/\-//g;
            my @arr=split(//,$seq);
            if ($hash{$name}) {die "FAILED: $name printed at least twice\n\n";}
            
            push (@{$hash{$name}}, @arr); # _Append_ new lines to the hash
            push(@orderarray, $name);
            
            @arr=();
            $name = $tmp;
            $seq = "";
            next;
        }
    
        if (!$name and $name ne "0") {die "Hard!!! Sequence without a name:\n$_\n";}
        $seq.=$_;    
    }
    
if ($hash{$name}) {die "FAILED: $name printed at least twice\n\n";}
$seq=~s/\-//g;
my @arr=split(//,$seq);

push (@{$hash{$name}}, @arr); # _Append_ new lines to the hash
push(@orderarray, $name);

close(F);

print"\n";
print "$file: \n";
my $index=1;
my $aacount=0;
foreach my $key (@orderarray)
{
    $aacount=0;
    print "$index\. $key: ";
    foreach my $elm (@{$hash{$key}})
    {
        #print $elm;
        if ($elm =~ /\d|\W/)
        {
            if ($elm =~ /\s+/) {next;}
            print "$aacount $elm ";
            $aacount=0;
        }
        else {$aacount++;}
    }
    print "$aacount ";
    print "\n";
    $index++;
}
print "\n";
