for

Create a ’for’ loop.

for { {atoms|residues|molecules|molfirstres|mollastres}   
   <var> inmask <mask> [parm <name> | parmindex <#> | <#>] |    
     <var> in <list> |   
     <var> oversets <list> |  
     <var> datasetblocks <set> blocksize <#> [blockoffset <#>][cumulative [firstblock <#>]] |    
   <var>=<start>;[<var><end OP><end>;]<var><increment OP>[<value>] ... }

END KEYWORD: done
Available end OP : ’<’ ’>’ ’<=’ ’>=’
Available increment OP : ’++’, ’–’, ’+=’, ’-=’

atoms|residues|molecules|molfirstres|mollastres <var> inmask <mask> Loop over atoms/residues/molecules/first residue in molecules/last residue in molecules selected by the given mask expression, set as script variable <var>.
parm <name> | parmindex <#> <#> Select topology that <mask> should be based on (default first topology).
<var> in <list> Loop over a comma-separated list of strings. File name wildcards can be used.
<var> in <data set name> Loop over elements of specified data set. Currently only 1D scalar sets and string sets can be specified.
<var> oversets <list> Loop over sets selected by comma-separated list of names.
<var> datasetblocks <set> Loop over blocks in specified DataSet.
blocksize <#> Size of blocks to use.
[blockoffset <#>] Offset between blocks.
[cumulative] Instead of blocks of fixed size, use blocks of increasing size incremented by blocksize.
[firstblock <#>] When cumulative, the size of the first block (default is first data set element).
<var>=<start>;[<var><end OP><end>;]<var><increment OP>[<value>] Loop over integer script variable <var> starting from <start>, optionally ending at <end>, increment by <value>.

Data Sets Created (datasetblocks loops):
<var>[block]:<start idx> (Data set blocks only) Data set block of blocksize starting at <start idx>.
<var>[cumul]:<end idx> (Cumulative data set blocks only) Data set block starting at firstblock and ending at <end idx>.

Create a for loop using one or more mask expressions, integers, etc. Loops can be nested inside each other. Integer loops may be used without an end condition, but in that case at least one descriptor in the loop should have an end condition or refer to a mask. Loops are ended by the done keyword.

Note that non-integer variables (e.g. ’inmask’ loops) are NOT incremented after the final loop iteration, i.e. these loop variables always retain their final value.

For example:

for atoms A0 inmask :1-3@CA i=1;i++    
   distance d$i :TCS $A0 out $i.dat
done

This loops over all atoms in the mask expression ’:1-3@CA’ (all atoms named CA in residues 1 to 3) and creates a variable named ’i’ that starts from 1 and is incremented by 1 each iteration. Inside the loop, the mask selection is referred to by $A0 and the integer by $i. This is equivalent to doing 3 distance commands like so:

distance d1 :TCS :1@CA out 1.dat
distance d2 :TCS :2@CA out 2.dat
distance d3 :TCS :3@CA out 3.dat

To loop over files named trajA*.nc and trajB*.nc:

for TRAJ in trajA*.nc,trajB*.nc   
   trajin $TRAJ 1 last 10
done

To loop over a trajectory while generating a single snapshot each frame:

parm ../tz2.parm7
trajin ../tz2.nc
# Set N to the total number of frames in the trajectory
set N = trajinframes
for i=1;i<=$N;i++
  # Clear the previous trajectory
  clear trajin
  # Read in a single frame
  trajin ../tz2.nc $i $i
  # Write it out
  trajout temp.$i.nc
  # Run
  run
done

Additional examples:

parm prod1/structure.psf

for i=1;i<21;i++
  trajin prod$i/output.xtc
  strip :SOD,CLA,TIP3
  autoimage 
  rms first @CA
  average crdset MyAvg$i @CA
  rms ref MyAvg$i @CA 
  atomicfluct F$i out rmsf_u2.dat @CA byres
  dataset F$i legend Fluct$i
  go
  clear trajin
done
parm structure.psf

for i=1;i<6;i++

   trajin prod$i/output.xtc 1 5000 50

done

strip :SOD,CLA,TIP3,POPC

autoimage 
rms first @CA

average crdset MyAvg @CA

go
parm prod1/structure.psf
parm myavg.mol2
reference myavg.mol2 
parm myavg.mol2
for i=1;i<6;i++
  trajin prod$i/output.xtc 1 5000 50
done
strip :SOD,CLA,TIP3,POPC
autoimage 
rms reference @CA
atomicfluct out rmsfA.dat ^3@CA byres
atomicfluct out rmsf465A.dat ^1,@CA byres
run

More examples of the use of this command are available in the CPPTRAJ one-liner section.