File: clean_jumbles

package info (click to toggle)
fastdnaml 1.2.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 576 kB
  • ctags: 384
  • sloc: ansic: 3,927; sh: 549; makefile: 22
file content (125 lines) | stat: -rw-r--r-- 3,504 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#! /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