#!/bin/csh -f #deshuffle a .gro file so that like molecules are #together and properly numbered # Check number of arguments set num_args = ${#argv} if( $num_args < 2 ) then echo "" echo " deshuffle usage: deshuffle [file.gro] [residue1] [residue2] ..." echo "" exit endif set file = $argv[1] set out = `echo $file | awk 'BEGIN{FS="."} {for(i=1;i<=NF-1;i++) printf "%s.",$i} END{printf "deshuf.gro"}'` echo '' echo "Creating $out now" # Write the header head -2 $file > $out set atom = 0 set mol = 0 foreach i (`echo $num_args | awk '{for(i=2;i<=$1;i++) print i}'`) echo $argv[$i] start at $mol $atom # Write the atoms awk -v na=$atom -v nmol=$mol -v res=$argv[$i] -v FIELDWIDTHS="5 5 5 5 48" \ 'NR>1&&$0~res{na++;if(resn!=$1){resn=$1;nmol++};printf "%5d%5s%5s%5d%48s\n",nmol,$2,$3,na,$5}' \ $file >> $out # Set new atom index set atom = `tail -1 $out | awk -v FIELDWIDTHS="5 5 5 5 48" '{print $4}'` set mol = `tail -1 $out | awk -v FIELDWIDTHS="5 5 5 5 48" '{print $1}'` end # Write the box tail -1 $file >> $out # Recount the atoms set natoms = `awk 'END{print NR-3}' $out` awk -v n=$natoms '{if(NR==2){print n} else{print}}' $out > $out.temp && mv $out.temp $out