#! /bin/sh # # clean_jumbles file_name_root # comm=`echo $0 | sed 's&^.*/&&g'` nosummaryflag="-nosummary" summaryflag="-summary" summary=1 while echo " $1" | grep "^ -" >/dev/null; do if test $# -lt 2; then break elif test $1 = "$nosummaryflag"; then summary=0; shift elif test $1 = "-n"; then summary=0; shift elif test $1 = "$summaryflag"; then summary=1; shift elif test $1 = "-s"; then summary=1; shift elif test $1 = "-"; then shift; break else echo "Bad flag: $*"; shift $#; break fi done if test $# -ne 1; then echo "Usage: $comm [ $nosummaryflag ] file_name_root" exit fi out="$1" # Check requested name if test `ls -d $out.[0-9]* 2>/dev/null | wc -l` -eq 0; then echo "$comm: No files found of form $out.(jumble_seed)" exit elif grep '^Ln Likelihood' $out.[0-9]* >/dev/null; then : else echo "$comm: No likelihoods found in files of form $out.(jumble_seed)" exit fi # Summary exists summarized=0 if test -f "$out.summary" -a $summary -gt 0; then if test ! -f "$out.tree" -o ! -f "$out.out"; then echo " $comm: Summary file $out.summary exists, but corresponding output and tree files ($out.out and $out.tree) cannot be found. Cleaning aborted. " exit else echo " Summary file $out.summary exists. New jumbles will be added to that summary without further checking. " summarized=1 fi fi # Don't clobber an existing file if test $summarized -eq 0 -a \( -f "$out.tree" -o -f "$out.out" \); then echo " $comm: File(s) with the name(s) $out.out and/or $out.tree already exist and would be clobbered by 'cleaning' the jumble output files. Move them to a new name and try again. " exit fi # Find best file PID with the given name if test $summarized -eq 0; then pid=`grep '^Ln Likelihood' $out.[0-9]* /dev/null | sed 's/^\(.*\):Ln Like.*=\(.*\)$/\2 \1/' | sort -nr +0 | head -1 | sed -e 's/^[^ ]* //' -e 's/^.*\.//'` fi # Write score summary file, if requested if test $summary -gt 0; then if test $summarized -eq 0; then grep '^Ln Likelihood' $out.[0-9]* /dev/null | sed 's/^\(.*\):\(Ln Like.*\)$/\2 (file: \1)/' | sort -nr +3 > "$out.summary" else temp_name="$out.`uname -n`.$$" mv "$out.summary" "$temp_name" grep '^Ln Likelihood' $out.[0-9]* /dev/null | sed 's/^\(.*\):\(Ln Like.*\)$/\2 (file: \1)/' | cat "$temp_name" - | sort -nr +3 > "$out.summary" rm -f "$temp_name" fi fi # Move output and treefile to new names if test $summarized -eq 0; then oldname="$out.$pid" if grep "^Ln Likelihood" "$oldname" >/dev/null; then newname="$out.out" treenew="$out.tree" treeold="treefile.$pid" checkpt="checkpoint.$pid" if test -f "$treeold"; then mv "$treeold" "$treenew" elif test -f "$checkpt"; then tail -1 "$checkpt" >"$treenew" else echo "$comm: Cannot find tree file. Cleaning aborted."; exit fi mv "$oldname" "$newname" rm -f "$checkpt" fi fi # Remove other output, tree and checkpoint files: if test `ls -d $out.[0-9]* 2>/dev/null | wc -l` -gt 0; then pids=`grep '^Ln Likelihood' $out.[0-9]* /dev/null | sed -e 's/^\(.*\):Ln Like.*$/\1/' -e 's/^.*\.//'` for pid in $pids; do rm -f "$out.$pid" "treefile.$pid" "checkpoint.$pid" done fi