{+ file: transform_map.inp +}
{+ directory: xtal_util +}
{+ description: Apply realspace transformation to a region of a map +}
{+ comment: 
           - Applies a specified rotation matrix and translation
             vector to a mask and the density within the mask.
           - This can be used to superpose electron density of an
             NCS related molecule on a reference molecule for
             subsequent comparison. +}
{+ authors: Axel T. Brunger, and Paul D. Adams +}
{+ copyright: Yale University +}

{- Guidelines for using this file:
   - all strings must be quoted by double-quotes
   - logical variables (true/false) must not be quoted
   - do not remove any evaluate statements from the file
   - the selections store1 through store4 are available for general use -}

{- begin block parameter definition -} define(

{====================== crystallographic data ========================}

{* space group *}
{* use International Table conventions with subscripts substituted
   by parenthesis *}
{===>} sg="P4(1)2(1)2";

{* unit cell parameters in Angstroms and degrees *}
{+ table: rows=1 "cell" cols=6 "a" "b" "c" "alpha" "beta" "gamma" +}
{===>} a=101.4;
{===>} b=101.4;
{===>} c=199.5;
{===>} alpha=90;
{===>} beta=90;
{===>} gamma=90;

{=========================== input map ===============================}

{* input electron density map *}
{===>} map_infile="eg1_dimer.map";

{* resolution limits used in map calculation *}
{* this must be the same as the limits used to calculate the map *}
{+ table: rows=1 "resolution" cols=2 "lowest" "highest" +}
{===>} low_res=500.0;
{===>} high_res=4.0;

{* map grid size: dmin*grid *}
{* this must be the same as the grid used to calculate the map *}
{===>} grid=0.33;

{* memory allocation for FFT calculation *}
{* this must be the same as the FFT memory allocation used
   to calculate the map *}
{===>} fft_memory=-1;

{========================= transformation ============================}

{* file containing mask to be transformed *}
{* this must be in compressed O format *}
{===>} mask_infile="eg1_monomer.mask";

{* real-space rotation matrix *}
{===>} matrix=( -0.99993  0.01083 -0.00435 )
              ( -0.01124 -0.79318  0.60889 )
              (  0.00314  0.60889  0.79325 );

{* real-space translation vector *}
{===>} vector=(  174.45038    0.33061   -0.18440 );

{====================== output map parameters ========================}

{* map format *}
{+ choice: "cns" "ezd" +}
{===>} map_format="cns";

{=========================== output files ============================}

{* output map file *}
{===>} map_outfile="transform_map.map";

{* output mask file *}
{===>} mask_outfile="transform_map.mask";

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

 ) {- end block parameter definition -}

 checkversion 1.3

 evaluate ($log_level=quiet)

 xray

   @CNS_XTALLIB:spacegroup.lib (sg=&sg;sgparam=$sgparam;)

   a=&a b=&b c=&c  alpha=&alpha beta=&beta gamma=&gamma

   binresolution &low_res &high_res
   mapresolution &high_res

   method=FFT

   fft
     grid=&grid
     if ( &fft_memory < 0 ) then
       automemory=true
     else
       memory=&fft_memory
     end if
   end

 end

 xray
   declare name=map domain=real end
   read map
     to=map
     input=&map_infile
   end
 end

 xray
   declare name=mask domain=real end
   read mask
     to=mask
     input=&mask_infile
   end
 end

 xray
   do (map=0) ( mask >= 0 )
 end

 xray
   average
     manipulate
       mask=mask
       map=map
       matrix=&matrix
       translation=&vector
     end
   end
 end

 xray
   do (map=0) ( mask >= 0 )
 end

 remark a= &a b= &b c= &c alpha= &alpha beta= &beta gamma= &gamma sg= &STRIP%sg

 xray
   write map
     auto=false
     from=map
     output=&map_outfile
     extent=mask
     mask=mask
     if ( &map_format = "ezd" ) then
       type=ezd
     else
       type=cns
     end if
   end
   write mask
     from=mask
     output=&mask_outfile
   end
   undeclare name=map  domain=real end
   undeclare name=mask domain=real end
 end

 stop