#!/usr/bin/perl -w
# usage: sswing     [-f]       [-c chainID] [-p score_thresh] [-k corr_coef] [-s]  
#                   fastSearch                                              outputforKiNG                                    
#                   [-u chi1] [-x chi2] [-y chi3] [-z chi4] [-r]
#                                                          one set of chi
#                   inputHpdb AminoAcidNo AminoAcidType DensityMpiFile
#                     ARGV[0]    ARGV[1]     ARGV[2]       ARGV[3] 

   use strict;
   use Cwd;
	 use Getopt::Std;

   my %options;
   my $dir = cwd();
   my $name="";
   my $prekin="";
   my $genContour="";
   my $PDBfile_ideal="idealPDB.pdb";
   my $resultFile="sswingOutput.txt";
   my $modelChainID="";

   my $PDBfile="";
   my $inputMAPfile="";
   my $residueSeqNum="";
   my $aminoAcidType="";
      
   getopts('hdfc:p:k:su:x:y:z:r',\%options);   
   $modelChainID=$options{c} if defined $options{c};

   if (defined $options{h})
   {
     print "Welcome protein side-chain fitting/refitting program\n";
     print "Usage: sswing [options] inputHpdb AminoAcidNo AminoAcidType DensityMapFile\n\n";
     print "Examples:\n";
     print "sswing  -r -s -c A -u -89 -x -57 -y -43 -z -77 0409201618_refmac1H.pdb 6 ARG cth682map.ccp4 (for single chi calculation)\n";
     print "sswing  -s -c A -u -89 -x -57 -y -43 -z -77 0409201618_refmac1H.pdb 6 ARG cth682map.ccp4 (fine search)\n";
     print "sswing  -s -c A 0409201618_refmac1H.pdb 6 ARG cth682map.ccp4 (greedy search)\n";
     print "sswing  -f -s -c A 0409201618_refmac1H.pdb 6 ARG cth682map.ccp4 (fast search)\n\n";
     print "Options:\n";
     print "-d          Debug mode: print cmd lines, leave tmp files\n";
     print "-f          Do fast search\n";
     print "-c char     Select chain ID\n";
     print "-p float    Set probe probe_score threshold(default=0.0)\n";
     print "-k float    Set correlation coefficient threshold(default=0.6)\n";
     print "-s          Set simple output for KiNG and MolProbity\n";
     print "-u float    Set chi1 for fine search\n";
     print "-x float    Set chi2 for fine search\n";
     print "-y float    Set chi3 for fine search\n";
     print "-z float    Set chi4 for fine search\n";     
     print "-r float    Set one set of chi calculation\n";
     print "            and must set -s and chi angles \n\n";
     print "Output:\n";
     print "sswingOutput.txt               clash score, correlation coefficient, chi angles\n";
     print "forKingsswingOutput.txt        sswing output for KiNG\n";
     print "sidechainPDB.pdb               new sidechain PDB file\n";
     print "sswing_residueNo[chainID].pdb  updated PDB file with new sidechain conformation\n";

     exit();
   }
   
   ($PDBfile, $residueSeqNum, $aminoAcidType, $inputMAPfile)=@ARGV;   
   $name=$PDBfile.$modelChainID.$residueSeqNum.$aminoAcidType;

   $prekin="prekin $PDBfile - -scope -show 'sc,hy' -mutate $residueSeqNum $aminoAcidType alt_";
   $prekin=$prekin." -chainID "."$modelChainID" if defined $options{c};
   $prekin=$prekin." -pdbout>$PDBfile_ideal";
   system($prekin);
           
   #produce script file
   my $outputRotscr=$name.".rotscr";
   my $pdb2rotscr = "sswingpdb2rotscr  $PDBfile $residueSeqNum $aminoAcidType  $modelChainID > $outputRotscr"; 
   system($pdb2rotscr);

   #select rotamers
   my $preGenScore ="";
   my $goFile="gofile";
   $preGenScore = "preGenScore  $PDBfile $inputMAPfile $residueSeqNum $PDBfile_ideal $outputRotscr $goFile";
   $preGenScore=$preGenScore." -c ".$modelChainID  if defined $options{c};
   $preGenScore=$preGenScore." -f "                if defined $options{f};
   $preGenScore=$preGenScore." -u ".$options{u}    if defined $options{u};
   $preGenScore=$preGenScore." -x ".$options{x}    if defined $options{x};
   $preGenScore=$preGenScore." -y ".$options{y}    if defined $options{y};
   $preGenScore=$preGenScore." -z ".$options{z}    if defined $options{z};
   $preGenScore=$preGenScore." -r "                if defined $options{r};

   print $preGenScore."\n"                          if defined $options{d};
   system($preGenScore);

   #chmod  rotamer script file
   my $rotamerRotscr=$outputRotscr."_rotamer";
   chmod (0777,$rotamerRotscr);

   #run script file and produce rotamer map
   my $rotamerMap=$name.".map_rotamer";
   my $rotamerScr="$rotamerRotscr>$rotamerMap";

   system ($rotamerScr);

   my $scoreOutput=$name.".out";
   my $genScoreResult="genScoreResult $rotamerMap $residueSeqNum $aminoAcidType";
   $genScoreResult=$genScoreResult." -p ".$options{p} if defined $options{p};
   $genScoreResult=$genScoreResult." -r "                   if defined $options{r};

   $genScoreResult=$genScoreResult." > $scoreOutput";
   system ($genScoreResult);
 
   unlink ($outputRotscr)                       unless defined $options{d};
   unlink ($rotamerMap)                         unless defined $options{d};
   unlink ($rotamerScr)                         unless defined $options{d};
   unlink ($goFile)                             unless defined $options{d};
   unlink ($rotamerRotscr)                      unless defined $options{d};

   $genContour="genContour $PDBfile $inputMAPfile $residueSeqNum $PDBfile_ideal $scoreOutput $resultFile";
   $genContour=$genContour." -c ".$modelChainID  if defined $options{c};
   $genContour=$genContour." -k ".$options{k}    if defined $options{k};
   $genContour=$genContour." -s "                if defined $options{s};
   $genContour=$genContour." -r "                if defined $options{r};
   system($genContour);

   unlink ($PDBfile_ideal)                      unless defined $options{d};
   unlink ($scoreOutput)                        unless defined $options{d};
