#!/usr/bin/perl
# pdb_renumber.pl 
#
# renumbers PDB residue column and
# cleans up for conversion to XPLOR format
#
# syntax: pdb_renumber.pl residue_corr input_file > 1xxx.pdb

$file1 = $ARGV[1];
$residue_change = $ARGV[0];

# loop through input_file line by line
    open (FILE1, "$file1") || die "syntax: pdb_reunmber.pl residue_corr input_file > 1xxx.pdb\n";
    while (<FILE1>) {

# split line into fields, store in array of $fld variables
    @fld = split;        #delimits by whitespace
    
# identify and print header lines without change
		if(!/ATOM/){
			print "$_";
			next;
		}

# select ATOM lines
		if(/ATOM/ && $fld[4]< 250){
		$chain = A;
		$res = $fld[4]+$residue_change;
		$~ = "PDB";
		write;
		}
                elsif(/ATOM/ && $fld[4] > 250){
                $chain = B;
                $res = $fld[4]-300+$residue_change;
                $~ = "PDB";
                write;
                }

	}

#define format for output
#ATOM      1  N   GLY A   1      14.263  -3.390  12.805  1.00  0.00

format PDB =
ATOM   @### @>>> @>> @ @##     @##.### @##.### @##.### @#.## @#.## 
$fld[1], $fld[2], $fld[3], $chain, $res $fld[5], $fld[6], $fld[7], $fld[8], $fld[9]
.
