#!/usr/bin/gawk -f
###########################################################################
# This file is part of meadTools, version 2.2.
# 
# Copyright (c) 2001-2019, Instituto de Tecnologia Quimica e Biologica,
# Universidade Nova de Lisboa, Portugal.
# 
# meadTools is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 2 of the License, or (at your
# option) any later version.
# 
# meadTools is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with meadTools.  If not, see <http://www.gnu.org/licenses/>.
# 
# For further details and info check the README file.
# 
# You can get meadTools at www.itqb.unl.pt/simulation
###########################################################################


############################################################################
# renumbpqr: a program to renumber .pqr files.
#
# This awk script reads a .pqr file from stdin and writes to stdout a
# new one with the atoms and residues renumbered.  This may be useful
# in several cases.  One case arises when we want to renumber things
# after cut-and-paste with an editor.  A more or less opposite
# situation occurs when, after building the .pqr file via a topology
# (which usually means having residue numbers starting at 1), we want
# to go back to the original numbering of the PDB file (eg, when the
# true N-terminus is absent from the PDB file).  In some case it may
# also be convenient to start numbering the residues at a different
# number while keeping the gaps in the original .pqr.  In all cases
# the atoms are simply renumbered sequentially, starting at 1, because
# that information is actually ignored by the programs in the MEAD
# package.
############################################################################


BEGIN{
  cmd = "renumbpqr" ;
  usage = "Usage: "cmd" [START [g]]  < INPUT_PQR  > OUTPUT_PQR\n" \
          "Options: START : first residue number (integer, default=1)\n" \
          "             g : Keep residue gaps (default=no)" ;
  if (ARGC > 3) error("Wrong number of arguments\n"usage) ;
  if (ARGC >= 2) resnumb = ARGV[1] ; else resnumb = 1 ;
  if (ARGC == 3)
  {
    if (ARGV[2] != "g") error("Wrong option "ARGV[2]"\n"usage) ;
    else gap = 1 ;
  }
  for (i = 1 ; i < ARGC ; i++) delete ARGV[i] ;
  pqrfmt = "ATOM %6d %-4s %-4s %4d     %7.3f %7.3f %7.3f %6.3f %6.3f\n" ;

  while (getline)
  {
    if (++atmnumb == 1) prev = $5 ;
    if ($5 != prev) resnumb += (gap ? $5 - prev : 1) ;
    printf pqrfmt, atmnumb, $3, $4, resnumb, $6, $7, $8, $9, $10 ;
    prev = $5 ;
  }
}

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

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