#!/usr/bin/perl -w

use strict;


my @alf = ('A'..'Z');
my $ialf = 0;
my $ter = 0;
my $stn = 1;
my $sqn;

my $f=$ARGV[0];
open(INP, $f) or die "Can't open file $f\n";
while( my $s=<INP> ){
    if( $s =~ /^TER/ ){ $ialf++; $ter = 1 }; 
    if( $s =~ /^ATOM/ ){
	substr($s,21,1) = $alf[$ialf];
	if( $ter ){
	    $ter = 0;
	    $stn = substr($s,22,4);
	    $stn =~ s/ //g;
	}
	$sqn = substr($s,22,4);
	$sqn =~ s/ //g;
	$sqn = $sqn-$stn+1;
	substr($s,22,4) = sprintf('%4u', $sqn);
    }
    print $s;
}
close(INP);
