#!/bin/env perl5
#
# $Id: checkit 972 1996-12-18 14:09:35Z hooft $
#
# Shell around WHAT IF to make different subsets of checks on a PDB file.
# This was only written for use at the PDB
#
# Usage: checkit [Name of check-set] pdbfilename
#   Leaving out the first argument runs 'NORMAL'. Case sensitive!!!!!!
#   Run without arguments to see list of check-sets.
#
# (C) Rob W.W. Hooft, EMBL, 1996.
#
# Distribute freely.
#

#
# Add Special path for scatter and xfig.
#
if (-d "/usr/software1/pdbcheck/gnu/bin") {
  $ENV{'PATH'}="/usr/software1/pdbcheck/gnu/bin:".$ENV{'PATH'}
}
#
# Where is WHAT IF?
#
for $t ("/usr/software1/pdbcheck/whatif","/nu1/hooft/p/whatif") {
  if (-f "$t/DO_WHATIF.COM" ) {
    $WHATIF=$t;
    last;
  }
}
#
# Which checks are (not) to be run? Only differences from
# the default should be indicated here. "OFF" has precedence
# over "ON". Anyway: it doesn't make sense to mention any
# check in both.
#
$OFF{'NORMAL'}="";
$ON{'NORMAL'}="";

$OFF{'NMR'}="SYM WGT XBF AXA";
$ON{'NMR'}="HNQ";

$OFF{'SLOW'}="";
$ON{'SLOW'}="HNQ";

$OFF{'QUICK'}="QUA FLP ROT";
$ON{'QUICK'}="";

$OFF{'PLOT'}="";
$ON{'PLOT'}="HST RAP BFP QPL NQP";

$OFF{'ALERT'}="FLP ROT INO PUC XBF H2O BPO CHI OME QUA HND CRD NAM MVM AXA NCS";
$ON{'ALERT'}="ZZZ";

$OFF{'AUTODEP'}=$OFF{'ALERT'}." BBC";
$ON{'AUTODEP'}="";

$OFF{'FULL'}="";
$ON{'FULL'}="";
#
# Check arguments.
#
$type=shift @ARGV;
if (defined $OFF{$type}) {
  $file=shift @ARGV;
} else {
  $file=$type;
  $type='NORMAL';
}

#
# See if the file exists.
#
unless ($file && -r $file) {
  warn "Cannot open inputfile '$file'\n" if $file;
  $opt="[".join("|",sort keys %OFF)."]";
  warn "Usage: $0 $opt pdbfilename\n";
  exit(1);
}

#
# Try remote shell if needed.
#
$HOSTNAME=`hostname`;
$REQHOST="terminator";
#unless ($HOSTNAME=~/^$REQHOST/o) {
#  $dir=`pwd`;
#  warn "Trying to run WHAT IF on $REQHOST via 'rsh'\n";
#  exec "rsh", $REQHOST, "cd $dir; $0 $type $file";
#}

#
# Check whether we can do it.
#
unless (-d $WHATIF) {
  die "WHAT IF not accessible here.\n";
}
#
# check entry type NMR - for NMR test first model for now -
#
open(ENT, "grep ^EXPDTA $file |");
$expdta = <ENT>;
close(ENT);
if($expdta =~ /NMR\,\s*\d+\s+STRUCT/) {
      $new_file = $file.".model_1";
      system ("rm -f $new_file");
      system ("egrep -v \"^ATOM|^HETATM|^TER|^MODEL|^ENDMDL|^CONECT|^END|^MASTER\" $file > $new_file");
      system ("/usr/local/bin/extract_model -f$file -m1");
      system ("cat nmr_model.dat.1 >> $new_file");
      system ("echo END >> $new_file");
      system ("rm -f nmr_model.dat.1");
      $type = "NMR";
      $file = $new_file;
}

if ($type eq 'FULL') {
  system("$WHATIF/DO_WHATIF.COM CHECK FUL $file setname yes");
  system("$WHATIF/dbdata/pdbout2html");
} else {
#
# Create list of checks...
#
  open(ORG,"<$WHATIF/dbdata/TODO.CHK");
  open(CPY,">TODO.CHK");
  while (<ORG>) {
    $chk=substr($_,0,3);
    if ($OFF{$type}=~/$chk/) {
      substr($_,4,3)="NO ";
    } elsif ($ON{$type}=~/$chk/) {
      substr($_,4,3)="YES";
    }
    print CPY;
  }
  close(CPY);
  close(ORG);
#
# ...and execute WHATIF to perform them
#
  system("$WHATIF/DO_WHATIF.COM CHECK FST $file setname yes");
  system("$WHATIF/dbdata/pdbout2html");
}
