#!/usr/bin/gawk -f

# This script changes residue names in pdb files according to the key
# given in the file convert.def. Cysteines are not converted by
# default. Use -c to give CYS residue numbers.


BEGIN{
  # Parse arguments:
  cmd = "convert-pdb" ;
  usage = "Usage:  "cmd" [-c cys#1,cys#2,...] PDB_FILE\n" \
    "(CYS not converted by default. Use -c to give CYS residue numbers)" ;
  if (ARGC != 2 && ARGC != 4) error("Wrong number of arguments.\n" usage) ;
  if (ARGC == 4)
  {
    if (ARGV[1] != "-c") error("Wrong command syntax.\n" usage) ;
    gsub(",", "|", ARGV[2]) ;
    cysnumb = "^(" ARGV[2] ")$" ;
    filecheck(pdb = ARGV[3]) ;
  }
  else filecheck(pdb = ARGV[1]) ;

  # Read residue name definitions (assuming 3-letter names):
  filecheck("convert.def") ;
  while (getline < "convert.def")
  {
    if ($1 ~ /^;/ || ($1 == "CYS" && cysnumb == "")) continue ;
    resX[$1] = $2 ;
    titres = titres "|" $1 ;
  }
  titres = "^(" substr(titres,2) ")$" ;

  # Read and process PDB file
  while (getline < pdb)
  {
    if ($0 !~ /^ATOM/) continue ;
    rname = substr($0,18,3) ;
    rnumb = substr($0,23,4)+0 ;
    if (rname ~ titres && (rname != "CYS" || rnumb ~ cysnumb))
      print substr($0,1,17) resX[rname] substr($0,21) ;
    else print $0 ;
  }

  exit 0 ;
}


#########################################################################
# Utilities:

# System call with return status checking:
function systemC(command)
{
  if (system(command)) error("Failed command: " command) ;
}


function filecheck(file)
{
  if (system("test -f "file))
    error("File "file" does not exist.") ;
  if (system("test -r "file))
    error("File "file" exists but is not readable.") ;
}


function warning(msg)
{
  print cmd ": Warning: " msg | "cat 1>&2" ;
}


function error(msg)
{
  print cmd ": Error: " msg | "cat 1>&2" ;
  exit 1 ;
}

