#! /bin/csh -f ################################################################################ # Convert a CCP4 .mtz file into a script that will generate the .mtz file ################################################################################ # # Text file will look like this: # SKIP # TITLE blah blah blah # CELL # SYMMETRY # LABEL # CTYPE # FORMAT # data # . # ################################################################################ # echo "" | nawk 'BEGIN{exit}' >& /dev/null if($status) alias nawk awk # set mtzfile = "" set newmtzfile = "" set textfile = "" set tempfile = $CCP4_SCR/deleteme set integer_width = "6" set float_width = "15.7" set TITLE = "" set CELL = "" set SYMM = "" set LABEL = "" set CTYPE = "" set FORMAT = "" # read command-line args set i = $#argv while($i > 0) if("$argv[$i]" =~ *.mtz) then if(-e "$argv[$i]") then set mtzfile = "$argv[$i]" if("$textfile" == "") then set textfile = `basename $mtzfile .mtz`.csh endif else if($i > 1) set mtzfile = "$argv[$i]" endif else if(-e "$argv[$i]") then set textfile = "$argv[$i]" if("$mtzfile" == "") then set mtzfile = `basename $textfile .txt`.mtz endif else if($i > 1) set textfile = "$argv[$i]" endif endif @ i = ( $i - 1 ) end # new mtz filename set newmtzfile = new.`basename $mtzfile` if((! -e "$mtzfile")&&(! -e "$textfile")) then echo "" echo "usage: $0 [mtzfile.mtz|textfile.txt]" echo "" echo "where: " echo " mtzfile.mtz will be converted to mtzfile.txt" echo " OR" echo " textfile.txt will be converted to textfile.mtz" echo " depending on which file exists." echo "That is, if textfile.txt is formatted like the mtzfile.txt output." exit 1 endif if(-e "$mtzfile") then echo "converting $mtzfile to $textfile" # get the labels, etc from the header echo "header" | mtzdump hklin $mtzfile >! $tempfile set TITLE = `nawk '/^ \* Title:/{getline;getline;print}' $tempfile` set SYMM = `nawk '/^ \* Space group /{print $NF+0}' $tempfile` set CELL = `nawk '/^ \* Cell Dimensions :/{getline;getline;print}' $tempfile` set LABEL = `nawk '/^ \* Column Labels :/{getline; getline; while(NF != 0){print;getline}}' $tempfile` set CTYPE = `nawk '/^ \* Column Types :/{getline; getline; while(NF != 0){print;getline}}' $tempfile` set LABIN = `echo $LABEL | awk '{for(i=4;i<=NF;++i){++n;printf("DUM%d=%s\n",n,$i)}}'` dumpagain: # generate fortranish format string set integers = `echo $CTYPE | nawk 'BEGIN{RS=" "} ! /[HYBX]/{print NR-1}' | head -1` set FORMAT = ${integers}i${integer_width}, @ temp = ( $#CTYPE - $integers ) set FORMAT = ${FORMAT}${temp}f$float_width echo $CTYPE | awk '{for(i=1;i<=NF;++i)print $i}' |\ awk -v iw=$integer_width -v fw=$float_width 'BEGIN{w["i"]=iw;w["f"]=fw}\ {type="f"} /[HYBIX]/{type="i"} NR==1{last=type;n=0}\ type!=last{printf "%s", n last w[last]",";n=0} {++n;last=type}\ END{print n last w[last]}' >! $tempfile # set FORMAT = `cat $tempfile` rm -f $tempfile echo "FORMAT = $FORMAT" # for multirecord mtz, need set FORMAT = "5i6,123f15.7" mtzdump hklin $mtzfile << EOF | nawk '/ LIST OF REFLECTIONS/,/ MTZDUMP/' >! $tempfile FORMAT '(${FORMAT})' NREF -1 #VALM -999 GO EOF if($status && ! $?TRIED_MULTI) then set TRIED_MULTI set integers = 5 goto dumpagain endif cat << end-out >! $textfile #! /bin/csh -f f2mtz hklin \$0 hklout $newmtzfile << EOF TITLE $TITLE SYMM $SYMM CELL $CELL #FORMAT '($FORMAT)' LABOUT $LABEL CTYPOUT $CTYPE SKIP 14 END EOF if(! \$status) echo "$newmtzfile has been created." exit # the ,, denote missing values end-out # echo "$FORMAT" # exit # now append the data to this file (turning -999 into MNF) cat $tempfile |\ nawk -v w=$integer_width 'substr($0,1,3*w) == sprintf("%"w"d%"w"d%"w"d", $1, $2, $3)' |\ sed '/999/s/-999.0000000/ ,, /g' >> $textfile rm -f $tempfile chmod a+x $textfile echo "to convert $textfile back into new.$mtzfile, type: $textfile" else echo "converting $textfile to $mtzfile not done yet." endif exit mtz2various hklin $mtzfile hklout out.hkl << EOF LABIN $LABIN OUTPUT USER '(5i6,20f15.7)' EOF mtz2various hklin $mtzfile hklout out.hkl << EOF LABIN DUM1=LP OUTPUT USER '(3i6,f15.7)' EOF