#!/bin/sh
# The next line restarts using nmrWish \
exec nmrWish "$0" -notk -- "$@"

set auto_path "[split $env(TCLPATH) :] $auto_path"
set ARGV      [concat $argv0 $argv]
set ARGC      [llength $ARGV]

proc getAtomList { resName aInfo } \
{
   global atomInfo

  set aList ""

  foreach a [split $aInfo "|"] \
     {
      set loc [string first # $a]
      set c   [string index $a 0]

      if {[strcasecmp $c H]} continue

      if {![strcasecmp $a HA3]} \
         {
          lappend aList HA1   
         } \
      elseif {$loc > 0} \
         {
          set s [lrange $a 0 [expr $loc - 1]]

          foreach i {1 2 3} \
             {
              set atomName $s$i
              if {![info exists atomInfo($resName,$atomName)]} continue
              lappend aList $atomName
             } 
         } \
      else \
         {
          lappend aList $a
         } 
     }

   return $aList
}

initResNames

set noeName   ../n15noe/test.tab
set assName   valpha.tab
set pdbName   ../dynamo/ext.pdb

set assTab  [gdbRead table -in $assName]
set noeTab  [gdbRead table -in $noeName]
set pdbTab  [gdbRead table -in $pdbName -pdb]
set seqList [getSeqList $assTab r1 rN]
set eList   [gdbGetInfo $pdbTab -parms entries]

foreach e $eList \
   {
    set resName  [gdbGet $e RESNAME]
    set atomName [gdbGet $e ATOMNAME]

    set atomInfo($resName,$atomName) 1
   }

set hnVar Y_PPM
set nVar  Z_PPM
set hVar  X_PPM

set dx    0.03
set dy    0.15
set dz    0.04

set id    1

puts "VARS   INDEX GROUP RESID_I RESNAME_I ATOMNAME_I RESID_J RESNAME_J ATOMNAME_J D_LO  D_HI"

puts "FORMAT %4d   %3d   %5d     %6s       %6s        %5d     %6s       %6s        %9.3f %9.3f"

puts ""


for {set resID $r1} {$resID <= $rN} {incr resID} \
   {
    set c   "RESID == $resID && !strcasecmp( ATOMNAME, 'HN' )"
    set eHN [gdbSelect -from $assTab -cond $c]
    if {[llength $eHN] != 1} continue

    set c  "RESID == $resID && !strcasecmp( ATOMNAME, 'N' )"
    set eN [gdbSelect -from $assTab -cond $c]
    if {[llength $eN] != 1} continue

    set hnPPM [gdbGet $eHN SHIFT]
    set nPPM  [gdbGet $eN  SHIFT]

    set c ""
    append c "near( $hnVar, $hnPPM, $dx ) && "
    append c "near( $nVar,  $nPPM,  $dy ) && "
    append c "!near( $hVar,  $hnPPM, $dz )"

    set resName  [getResName3 [lindex $seqList [expr $resID - $r1]]]
    set atomName HN

    set eList [gdbSelect -from $noeTab -cond $c]

    set fmt " %4d %2d  %4d %-4s %-4s  %4d %-4s %-4s  1.2 5.5"

    foreach e $eList \
       {
        set hPPM  [gdbGet $e $hVar]
        set aList [gdbSelect -from $assTab -cond "near( SHIFT, $hPPM, $dz )"]
        set gID   1

        foreach a $aList \
           {
            set resIDJ   [gdbGet $a RESID]
            set resNameJ [getResName3 [lindex $seqList [expr $resIDJ - $r1]]]

            foreach atomNameJ [getAtomList $resNameJ [gdbGet $a ATOMNAME]] \
               {
                puts [format $fmt $id $gID \
                             $resID $resName $atomName \
                             $resIDJ $resNameJ $atomNameJ]

               }

            incr gID
           }
       }

    incr id
   }

exit 0

