! Module file: enssubavecoord
!
! CNS MODULE
! **********
!
! Authors: Gregory L. Warren and Axel T. Brunger
!
! copyright Yale University
!
! version 03/05/98
!
! Function:
!          Calculates the average coordinates and rmsd values for 
!          multi-conformer structure ensembles
!
! Requirements:
!       Use symbols initilized in the module acceptinit or initave
!
!

module {enssubavecoord}
(
   &ave=ave;                 {OUPUT: average rmsd values}
   &ave2=ave2;               {OUPUT: standard deviation rmsd values}
   &nmr=nmr;                 {INPUT: nmr restraints parameters}
   &output=output;           {INPUT: file name prefix symbols}
   &pdb=pdb;                 {INPUT: atom selections}
)

checkversion 1.3

set message ? end
evaluate ($message_old=$result)
set echo ? end
evaluate ($echo_old=$result)
if ( $log_level = verbose ) then
  set echo=on message=normal end
else
  set echo=off message=off end
end if

   evaluate ($count2=0.)
   evaluate (&output.ens.flag=false)
   while ($count2 < &output.copies) loop rmsd
      evaluate ($count2 = $count2 + 1)
      if (&nmr.ens.multi.flag=true) then
         evaluate ($chainname1 = &nmr.ens.multi.segid.1 + encode($count2))
         evaluate ($chainname2 = &nmr.ens.multi.segid.2 + encode($count2))
         evaluate (&output.ens.flag=true)
      elseif (&nmr.ens.loop.flag=true) then
         evaluate ($chainname1 = "C0")
         evaluate ($chainname2 = "C" + encode($count2))
         evaluate (&output.ens.flag=true)
      else
         evaluate ($chainname1 = "C" + encode($count2))
      end if

      if (&nmr.ens.multi.flag=true) then
         coor fit selection=((&pdb.atom.select) and (segid $chainname1 or segid $chainname2)) end
         coor rms selection=((&pdb.atom.select) and (segid $chainname1 or segid $chainname2)) end
         evaluate (&ave.rms.atom.slct =&ave.rms.atom.slct  + $rms)
         evaluate (&ave2.rms.atom.slct=&ave2.rms.atom.slct + $rms^2)
         coor rms selection=((not chemical h*) and (segid $chainname1 or segid $chainname2)) end
         evaluate (&ave.rms.atom.all  =&ave.rms.atom.all  + $rms)
         evaluate (&ave2.rms.atom.all =&ave2.rms.atom.all + $rms^2)
      elseif (&nmr.ens.loop.flag=true) then
         coor fit selection=((&pdb.atom.select) and (segid $chainname1)) end
         coor rms selection=((&pdb.atom.select) and (segid $chainname1 or segid $chainname2)) end
         evaluate (&ave.rms.atom.slct =&ave.rms.atom.slct  + $rms)
         evaluate (&ave2.rms.atom.slct=&ave2.rms.atom.slct + $rms^2)
         coor rms selection=((not chemical h*) and (segid $chainname1 or segid $chainname2)) end
         evaluate (&ave.rms.atom.all  =&ave.rms.atom.all  + $rms)
         evaluate (&ave2.rms.atom.all =&ave2.rms.atom.all + $rms^2)
      else
         coor fit selection=((&pdb.atom.select) and (segid $chainname1)) end
         coor rms selection=((&pdb.atom.select) and (segid $chainname1)) end
         evaluate (&ave.rms.atom.slct =&ave.rms.atom.slct  + $rms)
         evaluate (&ave2.rms.atom.slct=&ave2.rms.atom.slct + $rms^2)
         coor rms selection=((not chemical h*) and (segid $chainname1)) end
         evaluate (&ave.rms.atom.all  =&ave.rms.atom.all  + $rms)
         evaluate (&ave2.rms.atom.all =&ave2.rms.atom.all + $rms^2)
      end if
   end loop rmsd

set message=$message_old echo=$echo_old end