{+ file: ncs.def  +}
{+ directory: auxiliary +}
{+ description: Defines non-crystallographic constraints or restraints +} 
{+ authors: Axel T. Brunger, and Paul D. Adams +}
{+ copyright: Yale University +}

{- begin block parameter definition -} define(

{============================ NCS type ===============================}

{* NCS type *}
{+ choice: "strict" "restrain" +}
{===>} ncs_type="restrain";

{===================== strict NCS (Skew matrix) ======================}

{* Skew matrix *}

{* use the skew matrix *}
{+ choice: true false +}
{===>} skew_use=false;

{* real-space skew matrix *}
{===>} skew_matrix=( 1 0 0 )
                   ( 0 1 0 )
                   ( 0 0 1 );

{* real-space skew vector *}
{===>} skew_vector=( 0 0 0 );

{=============== strict NCS (X-ray and nonbonded terms) ==============}

{* these operators are used to generate the complete asymmetric unit for
   structure factor and nonbonded energy calculations *}

{* NCS operator 1 - the identity operator *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_1=true;

{* real-space rotation matrix *}
{===>} ncs_matrix_1=( 1 0 0 )
                    ( 0 1 0 )
                    ( 0 0 1 );

{* real-space translation vector *}
{===>} ncs_vector_1=( 0 0 0 );

{* NCS operator 2 *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_2=true;

{* real-space rotation matrix *}
{===>} ncs_matrix_2=(  0.24218  0.24708 -0.93824 )
                    (  0.30835 -0.93649 -0.16702 )
                    ( -0.91993 -0.24886 -0.30299 );

{* real-space translation vector *}
{===>} ncs_vector_2=( 35.02347   32.17915   55.41964 );

{* NCS operator 3 *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_3=false;

{* real-space rotation matrix *}
{===>} ncs_matrix_3=( 1 0 0 )
                    ( 0 1 0 )
                    ( 0 0 1 );

{* real-space translation vector *}
{===>} ncs_vector_3=( 0 0 0 );

{* NCS operator 4 *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_4=false;

{* real-space rotation matrix *}
{===>} ncs_matrix_4=( 1 0 0 )
                    ( 0 1 0 )
                    ( 0 0 1 );

{* real-space translation vector *}
{===>} ncs_vector_4=( 0 0 0 );

{* NCS operator 5 *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_5=false;

{* real-space rotation matrix *}
{===>} ncs_matrix_5=( 1 0 0 )
                    ( 0 1 0 )
                    ( 0 0 1 );

{* real-space translation vector *}
{===>} ncs_vector_5=( 0 0 0 );

{* NCS operator 6 *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_6=false;

{* real-space rotation matrix *}
{===>} ncs_matrix_6=( 1 0 0 )
                    ( 0 1 0 )
                    ( 0 0 1 );

{* real-space translation vector *}
{===>} ncs_vector_6=( 0 0 0 );

{* NCS operator 7 *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_7=false;

{* real-space rotation matrix *}
{===>} ncs_matrix_7=( 1 0 0 )
                    ( 0 1 0 )
                    ( 0 0 1 );

{* real-space translation vector *}
{===>} ncs_vector_7=( 0 0 0 );

{* NCS operator 8 *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_8=false;

{* real-space rotation matrix *}
{===>} ncs_matrix_8=( 1 0 0 )
                    ( 0 1 0 )
                    ( 0 0 1 );

{* real-space translation vector *}
{===>} ncs_vector_8=( 0 0 0 );

{* NCS operator 9 *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_9=false;

{* real-space rotation matrix *}
{===>} ncs_matrix_9=( 1 0 0 )
                    ( 0 1 0 )
                    ( 0 0 1 );

{* real-space translation vector *}
{===>} ncs_vector_9=( 0 0 0 );

{* NCS operator 10 *}

{* use this operator *}
{+ choice: true false +}
{===>} ncs_op_10=false;

{* real-space rotation matrix *}
{===>} ncs_matrix_10=( 1 0 0 )
                     ( 0 1 0 )
                     ( 0 0 1 );

{* real-space translation vector *}
{===>} ncs_vector_10=( 0 0 0 );

{================= strict NCS (nonbonded term only) ==================}

{* these operators can be used to generate the full nonbonded 
   environment around the protomer *}

{* NCS nonbonded operator 1 *}

{* use this operator *}
{+ choice: true false +}
{===>} nb_ncs_op_1=false;

{* real-space rotation matrix *}
{===>} nb_ncs_matrix_1=( 1 0 0 )
                       ( 0 1 0 )
                       ( 0 0 1 );

{* real-space translation vector *}
{===>} nb_ncs_vector_1=( 0 0 0 );

{* NCS nonbonded operator 2 *}

{* use this operator *}
{+ choice: true false +}
{===>} nb_ncs_op_2=false;

{* real-space rotation matrix *}
{===>} nb_ncs_matrix_2=( 1 0 0 )
                       ( 0 1 0 )
                       ( 0 0 1 );

{* real-space translation vector *}
{===>} nb_ncs_vector_2=( 0 0 0 );

{* NCS nonbonded operator 3 *}

{* use this operator *}
{+ choice: true false +}
{===>} nb_ncs_op_3=false;

{* real-space rotation matrix *}
{===>} nb_ncs_matrix_3=( 1 0 0 )
                       ( 0 1 0 )
                       ( 0 0 1 );

{* real-space translation vector *}
{===>} nb_ncs_vector_3=( 0 0 0 );

{* NCS nonbonded operator 4 *}

{* use this operator *}
{+ choice: true false +}
{===>} nb_ncs_op_4=false;

{* real-space rotation matrix *}
{===>} nb_ncs_matrix_4=( 1 0 0 )
                       ( 0 1 0 )
                       ( 0 0 1 );

{* real-space translation vector *}
{===>} nb_ncs_vector_4=( 0 0 0 );

{* NCS nonbonded operator 5 *}

{* use this operator *}
{+ choice: true false +}
{===>} nb_ncs_op_5=false;

{* real-space rotation matrix *}
{===>} nb_ncs_matrix_5=( 1 0 0 )
                       ( 0 1 0 )
                       ( 0 0 1 );

{* real-space translation vector *}
{===>} nb_ncs_vector_5=( 0 0 0 );

{====================== NCS restraint group 1 ========================}

{* NCS-restraint weight *}
{===>} group.1.weight=300;

{* target sigma for NCS related B-values *}
{===>} group.1.sigb=2;

{* equivalence selection 1 *}
{===>} group.1.equiv.1=(segid A and resid 120:220);

{* equivalence selection 2 *}
{===>} group.1.equiv.2=(segid B and resid 120:220);

{* equivalence selection 3 *}
{===>} group.1.equiv.3=(none);

{* equivalence selection 4 *}
{===>} group.1.equiv.4=(none);

{* equivalence selection 5 *}
{===>} group.1.equiv.5=(none);

{* equivalence selection 6 *}
{===>} group.1.equiv.6=(none);

{* equivalence selection 7 *}
{===>} group.1.equiv.7=(none);

{* equivalence selection 8 *}
{===>} group.1.equiv.8=(none);

{* equivalence selection 9 *}
{===>} group.1.equiv.9=(none);

{* equivalence selection 10 *}
{===>} group.1.equiv.10=(none);

{====================== NCS restraint group 2 ========================}

{* NCS-restraint weight *}
{===>} group.2.weight=300;

{* target sigma for NCS related B-values *}
{===>} group.2.sigb=2;

{* equivalence selection 1 *}
{===>} group.2.equiv.1=(none);

{* equivalence selection 2 *}
{===>} group.2.equiv.2=(none);

{* equivalence selection 3 *}
{===>} group.2.equiv.3=(none);

{* equivalence selection 4 *}
{===>} group.2.equiv.4=(none);

{* equivalence selection 5 *}
{===>} group.2.equiv.5=(none);

{* equivalence selection 6 *}
{===>} group.2.equiv.6=(none);

{* equivalence selection 7 *}
{===>} group.2.equiv.7=(none);

{* equivalence selection 8 *}
{===>} group.2.equiv.8=(none);

{* equivalence selection 9 *}
{===>} group.2.equiv.9=(none);

{* equivalence selection 10 *}
{===>} group.2.equiv.10=(none);

{====================== NCS restraint group 3 ========================}

{* NCS-restraint weight *}
{===>} group.3.weight=300;

{* target sigma for NCS related B-values *}
{===>} group.3.sigb=2;

{* equivalence selection 1 *}
{===>} group.3.equiv.1=(none);

{* equivalence selection 2 *}
{===>} group.3.equiv.2=(none);

{* equivalence selection 3 *}
{===>} group.3.equiv.3=(none);

{* equivalence selection 4 *}
{===>} group.3.equiv.4=(none);

{* equivalence selection 5 *}
{===>} group.3.equiv.5=(none);

{* equivalence selection 6 *}
{===>} group.3.equiv.6=(none);

{* equivalence selection 7 *}
{===>} group.3.equiv.7=(none);

{* equivalence selection 8 *}
{===>} group.3.equiv.8=(none);

{* equivalence selection 9 *}
{===>} group.3.equiv.9=(none);

{* equivalence selection 10 *}
{===>} group.3.equiv.10=(none);

{====================== NCS restraint group 4 ========================}

{* NCS-restraint weight *}
{===>} group.4.weight=300;

{* target sigma for NCS related B-values *}
{===>} group.4.sigb=2;

{* equivalence selection 1 *}
{===>} group.4.equiv.1=(none);

{* equivalence selection 2 *}
{===>} group.4.equiv.2=(none);

{* equivalence selection 3 *}
{===>} group.4.equiv.3=(none);

{* equivalence selection 4 *}
{===>} group.4.equiv.4=(none);

{* equivalence selection 5 *}
{===>} group.4.equiv.5=(none);

{* equivalence selection 6 *}
{===>} group.4.equiv.6=(none);

{* equivalence selection 7 *}
{===>} group.4.equiv.7=(none);

{* equivalence selection 8 *}
{===>} group.4.equiv.8=(none);

{* equivalence selection 9 *}
{===>} group.4.equiv.9=(none);

{* equivalence selection 10 *}
{===>} group.4.equiv.10=(none);

{===========================================================================}
{        things below this line do not normally need to be changed          }
{===========================================================================}

 ) {- end block parameter definition -}

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

 checkversion 1.1

 if ( &ncs_type = "strict" ) then

   ncs strict

     if ( &skew_use = true ) then
       skew
         matrix=&skew_matrix
         translation=&skew_vector
       end
     end if

     evaluate ($counter=1)
     evaluate ($done=false)

     while ( $done = false ) loop xncs

       if ( &exist_ncs_op_$counter = true ) then
         if ( &ncs_op_$counter = true ) then    
           xncsrel
             matrix=&ncs_matrix_$counter
             translation=&ncs_vector_$counter
           end
         end if
       else
         evaluate ($done=true)
       end if

       evaluate ($counter=$counter+1)

     end loop xncs

     evaluate ($counter=1)
     evaluate ($done=false)

     while ( $done = false ) loop ncs

       if ( &exist_nb_ncs_op_$counter = true ) then
         if ( &nb_ncs_op_$counter = true ) then    
           ncsrel
             matrix=&nb_ncs_matrix_$counter
             translation=&nb_ncs_vector_$counter
           end
         end if
       else
         evaluate ($done=true)
       end if

       evaluate ($counter=$counter+1)

     end loop ncs

   end

   ncs strict ? end

 elseif ( &ncs_type = "restrain" ) then

   evaluate ($1=1)
   evaluate ($group=1)
   evaluate ($done=false)

   while ( $done = false ) loop ncs

     if ( &exist_group.$group.equiv.1 = true ) then

       evaluate ($nequiv=0)
       evaluate ($counter=1)
       evaluate ($innerdone=false)
       while ( $innerdone = false ) loop equiv
         if ( &exist_group.$group.equiv.$counter = true ) then
           show sum(1) (&group.$group.equiv.$counter)
           if ( $result > 0 ) then
             evaluate ($nequiv=$nequiv+1)
           else
             evaluate ($innerdone=true)
           end if
         else
           evaluate ($innerdone=true)
         end if
         evaluate ($counter=$counter+1)
       end loop equiv

       ncs restraint
         group
           evaluate ($counter=1)
           while ( $counter <= $nequiv ) loop equiv
             equiv=(&group.$group.equiv.$counter)
             evaluate ($counter=$counter+1)
           end loop equiv
           weight=&group.$group.weight
           sigb=&group.$group.sigb
         end
         ?
       end
     else
       evaluate ($done=true)
     end if

     evaluate ($group=$group+1)

   end loop ncs

   flags include ncs end

 else

   display >>> Error: unknown NCS type, aborting
   abort

 end if

 set message=$message_old echo=$echo_old end



