Repository navigation
Expand file tree
/
Copy pathrun_fsc.sh
More file actions
executable file
·290 lines (244 loc) · 8.58 KB
/
Copy pathrun_fsc.sh
File metadata and controls
executable file
·290 lines (244 loc) · 8.58 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/bash
# sh doesn't know about range expansion on linux {1..3}
# Works on linux. "Should" work on mac, but untested. Won't even
# pretend to work on windows at this point.
# 2/7/15 iao
# Inputs
# Takes 2 optional flags
# -n <int> : number of parameter estimation iterations
# -t : Run in 'test' mode, sets number of iterations and number of
# simulations to reasonable values so it runs quick
# Output
# This script generates an 'out' directory in cwd. Inside this directory
# it creates a folder for each time it is invoked. The folder is the working
# directory, and it is timestamped so multiple runs won't step on each other.
# Inside the working directory it creates a directory for each parameter
# estimation iteration, and a bootstrap directory with number folders
# for each bootstrap replicate.
#
# The working directory contains a file called Results.txt which is
# a concatenation of all parameter estimation interations sorted by
# estimated likelihood value.
# Created 2.7.14 by Isaac Overcast
# Based heavily on work by Xander Xue
###################################################################
# Update the script for new versions of fsc. Right now this
# script will only want to run the most recent version, if it doesn't
# find it it'll go out and grab it.
CUR_FSC_VRS=27
###################################################################
# A couple utility functions
###################################################################
# print usage information, try to be helpful
usage() {
echo "Usage: run_fsc [-n <num_iterations>] [-p <file_prefix>] [-t] [-m] [-d]"
echo " -d : Use unfolded sfs (and the *jointDAF.obs file name)"
echo " -m : Do --multiSFS. This requires the *_MSFS.obs file format"
echo " -n <num_iterations> : how many parameter estimation iterations to run"
echo " -t : Run in TEST mode. Set values small so it runs quick."
echo " -p <prefix> : The prefix for the .tpl, .est, and .obs files. If you don't give it a value it assumes 'input'"
}
# Locate the fsc binary and set the $FSC_BIN variable.
# First test $PATH, then test cwd, then dl if you can't find it.
# Die on failure, pointless to continue if you can't
# get fsc to run.
getfsc(){
# TODO: this part might not work right.
FSC_BIN=`which fsc$CUR_FSC_VRS`
if [ $? == 0 ];
then
# Found fsc executable in $PATH
echo "Found fsc binary"
else
# fsc binary not in PATH. Look in CWD."
`./fsc$CUR_FSC_VRS &> /dev/null`
if [ $? == 1 ];
then
# Found fsc in CWD
FSC_BIN="$PWD/fsc$CUR_FSC_VRS"
else
echo "Fetching fsc2 executable...."
OS_fingerprint=`uname`
FSC_DL=""
if [ $OS_fingerprint == "Linux" ];
then
echo "OS=linux"
`wget http://cmpg.unibe.ch/software/fastsimcoal2/downloads/fsc27_linux64.zip > fsc_linux64.zip`
FSC_DL=fsc27_linux64
else
echo "OS=Mac"
`curl http://cmpg.unibe.ch/software/fastsimcoal2/downloads/fsc27_mac64.zip > fsc_mac64.zip`
FSC_DL=fsc27_mac64
fi
# Extract the fsc binary, change mode to allow execution, and clean up the .zip file
unzip -p $FSC_DL.zip $FSC_DL/fsc$CUR_FSC_VRS* > fsc$CUR_FSC_VRS
chmod 777 fsc$CUR_FSC_VRS
rm -f fsc_*64.zip
FSC_BIN="$PWD/fsc$CUR_FSC_VRS"
fi
fi
if [ -s $FSC_BIN ];
then
echo "FSC executable: $FSC_BIN"
else
echo "No FSC binary. Error DL'ing. Contact your system administrator."
exit
fi
}
###################################################################
# Enter the main section of run_fsc.sh
###################################################################
# Set default options to sensible values
ITERATIONS=100
NSIMULATIONS=100000
NUM_PARAM_SETS=100
BOOTSTRAPS=50
# Set this variable to empty string. If the -m flag is passed in
# then this gets set to '--multiSFS'
DO_MULTI=""
# Assume input prefix is 'input'
PREFIX="input"
# Default observed file type is jointMAFpop1_0
OBSERVED_FILE_TYPE="_jointMAFpop1_0.obs"
# Flag for doing folded vs unfolded sfs
# default value is -m to do unfolded sfs
FSC_FOLDING_FLAG="-m"
# Define output file names
OUTTMP="Results.unsorted.txt"
OUTPUT_LIKELIHOODS="Results.txt"
# Are we doing a test run?
TEST=0
# Read in the number of iterations from the command line
# This allows for short runs w/ simple parameter tweaking in advance
# of full blown runs.
while getopts dmn:p:t flag; do
case $flag in
d)
echo "Do unfolded sfs (requires *jointDAF.obs)"
FSC_FOLDING_FLAG="-d"
OBSERVED_FILE_TYPE="_jointDAFpop1_0.obs"
;;
m)
echo "Do --multiSFS (requires *_MSFS.sfs format)"
DO_MULTI="--multiSFS"
OBSERVED_FILE_TYPE="_MSFS.obs"
;;
n)
echo "Doing Iterations: $OPTARG";
ITERATIONS=$OPTARG;
;;
p)
echo "Using prefix: $OPTARG";
PREFIX=$OPTARG
;;
t)
echo "Doing TEST run. Set defaults so it'll run quick."
NSIMULATIONS=10
ITERATIONS=5
NUM_PARAM_SETS=1
BOOTSTRAPS=5
TEST=1
;;
?)
usage;
exit;
;;
esac
done
# We wrap the $PREFIX in braces for the observed file because otherwise
# the shell things the underscore is doing something funny. The braces
# escape the underscore.
if [ $PREFIX != "input" ]; then
TEMPLATE_FILE="$PREFIX.tpl"
PARAM_FILE="$PREFIX.est"
OBSERVED_FILE="${PREFIX}${OBSERVED_FILE_TYPE}"
else
# Set template and parameter files in a sensible way
TEMPLATE_FILE="input.tpl"
PARAM_FILE="input.est"
OBSERVED_FILE="input${OBSERVED_FILE_TYPE}"
fi
# Generate a timestamp in number of seconds since the epoch
# and make an outdir for the results. This allows for multiple
# runs without stepping on previous output/results. The
# timestamp can be reformatted for easier reading, or different
# prefix can be appended depending on run options, but thats a V2 thing.
TIMESTAMP=`date +"%s"`
OUTDIR=out/${PREFIX}-$TIMESTAMP
if [ "$TEST" -eq 1 ]; then
OUTDIR=$OUTDIR-test
fi
mkdir -p $OUTDIR
FSCDIR=`pwd`
echo "Using files: $TEMPLATE_FILE, $PARAM_FILE, $OBSERVED_FILE"
if [ ! -f $TEMPLATE_FILE ]; then
echo "$TEMPLATE_FILE doesn't exist"
exit
fi
if [ ! -f $PARAM_FILE ]; then
echo "$PARAM_FILE doesn't exist"
exit
fi
if [ ! -f $OBSERVED_FILE ]; then
echo "$OBSERVED_FILE doesn't exist"
exit
fi
# Locate the fsc binary and set the $FSC_BIN.
getfsc
echo "#########################################"
echo "Begin Parameter Estimation Replicates"
echo "#########################################"
# Touch the replicate output files
touch $OUTDIR/$OUTTMP $OUTDIR/$OUTPUT_LIKELIHOODS
# This is a little tricky because bash implements brace expansion
# before it does variable conversion, so variables inside brace
# ranges don't work without using `eval`.
for i in $(eval echo {1..$ITERATIONS})
do
echo "Doing Iteration $i/$ITERATIONS"
mkdir $OUTDIR/$i
# FSC makes some stupid assumptions about where the observed file is
# and it won't let you pass it in, so you just have to copy these files
# over and over. Kinda stupid.
cp $TEMPLATE_FILE $OUTDIR/$i
cp $PARAM_FILE $OUTDIR/$i
cp $OBSERVED_FILE $OUTDIR/$i
cd $OUTDIR/$i
$FSC_BIN -t $TEMPLATE_FILE -n 10000 -N $NSIMULATIONS $FSC_FOLDING_FLAG -e $PARAM_FILE -E $NUM_PARAM_SETS -M 0.01 -l 10 -L 40 -0 -c 0 $DO_MULTI >output.log
# For ease of reading output, grab the header if its the first rep
# Not sure if this is useful.
if [ $i == 1 ];
then
echo "copy header"
head -n 1 ./$PREFIX/${PREFIX}.bestlhoods > ../$OUTPUT_LIKELIHOODS
fi
echo -n "REP $i - " >> ../$OUTTMP
tail -n 1 ./$PREFIX/${PREFIX}.bestlhoods >> ../$OUTTMP
# Go back up to execdir and do the next iteration.
cd ../../..
done
echo "#########################################"
echo "Parameter Estimation Results"
echo "#########################################"
# Sort results. Field 14 is Max Estimated Likelihood, sort low to high.
# Note, this doesn't handle duplicates intelligently.
# Doop likelihood values will result in the first one being duplicated
# in the results file.
# this is stupid. The field will change if you change the number of params
# so this should actually identify the right column in a smart way.
# Well we know the MaxEstLhood is the 2nd to last field so we do
# some goofy shell hacks, reverse the whole file, grap the 2nd field only
# reverse it again, cut off the header and sort by max likelihood.
# PARTY TIME!
LHOOD_COL=`cat $OUTDIR/$OUTTMP | rev | cut -f 2 | rev | grep -v MaxEstLhood | sort`
for i in $LHOOD_COL
do
echo $i
grep -e "$i" $OUTDIR/$OUTTMP >> $OUTDIR/$OUTPUT_LIKELIHOODS
done
# find the rep with the best likelihood value
BEST_PARAMS_ITERATION=`head -n 2 $OUTDIR/$OUTPUT_LIKELIHOODS | tail -n 1 | cut -f 2 -d " "`
echo "Best parameter estimation iteration - $BEST_PARAMS_ITERATION"
cat "$OUTDIR/$BEST_PARAMS_ITERATION/${PREFIX}/${PREFIX}.bestlhoods"
# Clean up the unsorted results file
rm -f $OUTDIR/$OUTTMP