#!/usr/bin/awk -f
#
# pdb_setchain script
#
# This script sets the chainid
#
# Usage: see usage statement (type 'pdb_setchain' without arguments)
#
# Example:
# input lines:
# ATOM     32  CD1 LEU     2      -6.002  10.544   9.006  1.00  0.00      
#  output lines:
# ATOM     32  CD1 LEU A   2      -6.002  10.544   9.006  1.00  0.00        
# 
# Remarks: 
#  - Only ATOM and HETATM records are affected.
#  - Only the first character of the segid can be stored in the chainid. 

BEGIN { 
    if ( ARGC == 1 ) {
	print "Usage: pdb_setchain -v CHAIN=chainID inputfile > outputfile"
	print "Action:"
	print "the chainID defined as argument is copied to column 22"
	exit
    }
}

$1 == "ATOM" || $1 == "HETATM" {
    printf( "%s%1s%s\n", substr($0,1,21), CHAIN, substr($0,23) )
    next
}

{
    print $0
}
