; cnst_qualit (interactive)
;
; permits the output of a constraint file according to the set of 
; calibration peaks. The constraint distances are qualitatively
; defined from the calibration peaks, and the bounds given by the 
; user.
;
;

Print "Current project :"
pwd

if ($att['largest'] == 0) error "Database Empty !"
if ($dim != 2) error "Command available in 2D only !"
if (!(exist('calib_dst[]'))) error "You need first to define calibration peaks!"

if (!$arg) then
  formbox 'Qualitative constraint output' \
   'cnst_qualit $cnst_format $cnstfil $dstbg $dstmed $dbgl $dbgu $dmel $dmeu $dsml $dsmu' \
   'Format' enum 'XPLOR,DIANA' cnst_format 'XPLOR' \
   'Constraint file basename' file cnstfil cnst \
   'Large intensity bounds: lower' real dbgl 1.8 noreturn \ 
   'upper' real dbgu 2.8 \
   'Lower limit for large intensity values: ' real dstbg 2.5 \
   'Medium intensity bounds: lower' real dmel 1.8 noreturn \ 
   'upper' real dmeu 3.1 \
   'Lower limit for medium intensity value: ' real dstmed 3.0 \
   'Small intensity bounds: lower' real dsml 1.8 noreturn \  
   'upper' real dsmu 3.5 \
   *

else
  set cnst_format = $_
  set cnstfil = $_
  set dstbg = $_ 
  set dstmed = $_ 
  set dbgl = $_ 
  set dbgu = $_ 
  set dmel = $_ 
  set dmeu = $_
  set dsml = $_ 
  set dsmu = $_

; computation of the factor between intensity and (1/d**6) by
; a least-square
  set sumi2 = 0
  set sumij = 0
  set i = (nextlm("calib_dst"," "))
  while ($i s! " ")
    if ($calib_dst[$i] == 0) error ("Pb with calibration distance of peak" ; $i)
     set l = $att[$i]
     set t = (head(tail(tail($l))))
;    print ("t " ; $t ; $calib_dst[$i] ; $i)
    set sumi2 = (%+($t*$t))
    set sumij = (%+($t/($calib_dst[$i]^6)))
    set i = (nextlm("calib_dst",$i))
  endwhile
  if ($sumi2 == 0) error "Pb with calibration intensities!" 
  set factor = ($sumij/$sumi2)
; determine the intensity threshold for which the corresponding distance is 
; larger than 5.0, this value is used to reject the constraints corresponding to 
; too small intensities
  set thres = ($factor/(5.0^6))

  if ($cnst_format s= "DIANA") then
    dbopen /usr/local/gifa/macro/att/1let_3let code
;scan prim seq 
    open db/primary
    set l = <db/primary
    set i = 1
    while (!eof('db/primary'))
       set seq[$i] = (head($code[$l]))
       set i = (%+1)
       set l = <db/primary
    endwhile
    close db/primary
    dbclose code
  endif

  if ($cnst_format s= "XPLOR") then
    set cnstfil = ("constraints/" // % //".tbl")
  elsif ($cnst_format s= "DIANA") then
    set cnstfil = ("constraints/" // % //".upl")
  endif
  open $cnstfil

  initinprogress $att["LARGEST"]
  set uu = 0
  foreach i in att
    if ($i s= "LARGEST")  goto break
      inprogress $uu
      set uu = (%+1)
; get the assigned peak label
      set l = $att[$i]
      if (index($l,'unk') != 0) goto break     ; processing of the assigned peaks
      set t = (tail(tail(tail($l))))
      set s1 = ($spin[(head($t))])
      set s2 = ($spin[(head(tail($t)))])
      if ($s1 == $s2) goto break 
      set sys1 = (head(tail(tail($s1))))
      set sys2 = (head(tail(tail($s2))))
      if (($sys1 s= "unk") | ($sys2 s= "unk")) goto break 
      set r1 = (head($sys[$sys1]))
      set r2 = (head($sys[$sys2]))
      set a1 = (head(tail($s1)))
      set a2 = (head(tail($s2)))
; get the peak intensity
      set u = (head(tail(tail($l))))
      if ($u < $thres) goto break
      set p = (1/($u*$factor))
      set distmoy = ($p^(1/6))
      set distmoy = (int($distmoy*100)/100)
      if ($distmoy < $dstbg) then        ; Big intensity interval
         set varlow = $dbgl 
         set varupp = ($dbgu-$dbgl)
      elsif ($distmoy < $dstmed) then    ; Medium intensity interval
         set varlow = $dmel 
         set varupp = ($dmeu-$dmel)
      else                               ; Small intensity interval
         set varlow = $dsml 
         set varupp = ($dsmu-$dsml)
      endif
      if ($cnst_format s= "XPLOR") then
         fprint $cnstfil ('assign (resid';$r1;'and  name';$a1;')(resid';$r2;'and  name';$a2;')';$varlow;'0';$varupp)
      elsif ($cnst_format s= "DIANA") then
         fprintf $cnstfil "%-3d %3s  %4s  %-3d %3s  %4s   %5.2f" $r1 $seq[$r1] $a1 $r2 $seq[$r2] $a2 ($varlow+$varupp) *
      endif
=break
  endfor
  close $cnstfil

  inprogress $att["LARGEST"]
  alert "Constraint file written"
endif



