-
Notifications
You must be signed in to change notification settings - Fork 6
/
ngsF-HMM.sh
executable file
·119 lines (99 loc) · 2.17 KB
/
ngsF-HMM.sh
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
#!/bin/bash
shopt -s extglob
#################
### Variables ###
#################
N_REP=20
# Check if ngsF-HMM is compiled
if [[ ! -x ${0%\.sh} ]]; then
echo "ERROR: ngsF-HMM binary not found! Did you compile it?"
exit -1
fi
# Set TMP folder
if [ -z $TMP_DIR ]; then
TMP_DIR=$HOME/scratch
fi
TMP_DIR=$TMP_DIR/ngsF-HMM_$USER
#################
### Functions ###
#################
in_array() {
idx=""
local CNT=0
local hay needle=$1
shift
for hay; do
CNT=$((CNT+1))
if [[ $hay == $needle ]]; then
idx=$CNT
return 1
fi
done
return 0
}
#######################
### Check arguments ###
#######################
args=( $@ )
ID=ngsF-HMM_$RANDOM
mkdir -p $TMP_DIR
# find -S/-seed/--seed argument
in_array "--seed" "${args[@]}"
if [[ $idx -eq 0 ]]; then
in_array "-seed" "${args[@]}"
fi
if [[ $idx -eq 0 ]]; then
in_array "-S" "${args[@]}"
fi
if [[ $idx -ne 0 ]]; then
RANDOM=${args[$idx]}
SEED_idx=$idx
fi
# find -o/-out/--out argument
in_array "--out" "${args[@]}"
if [[ $idx -eq 0 ]]; then
in_array "-out" "${args[@]}"
fi
if [[ $idx -eq 0 ]]; then
in_array "-o" "${args[@]}"
fi
if [[ $idx -eq 0 ]]; then
echo "ERROR: could not find argument for output files (-o / --out)"
exit -1
fi
OUT=${args[$idx]}
##########################
### Run each replicate ###
##########################
for REP in `seq -w 1 $N_REP`
do
if [[ $SEED_idx -gt 0 ]]; then
args[$SEED_idx]=$RANDOM
fi
args[$idx]=$TMP_DIR/$ID.REP_$REP
${0%\.sh} ${args[@]}
done
##########################
### Get best replicate ###
##########################
if [ -s $TMP_DIR/$ID.REP_*(0)1.indF ]; then
# Find best replicate
BEST=`awk 'FNR==1{print FILENAME"\t"$1}' $TMP_DIR/$ID.REP_*.indF | sort -k 2,2gr | awk 'NR==1{sub(".indF","",$1); print $1}'`
if [ -s $BEST.indF ]; then
# Get best replicate
mv $BEST.indF $OUT.indF
mv $BEST.ibd $OUT.ibd
mv $BEST.geno $OUT.geno
if [[ -s $BEST.log.gz ]]; then
mv $BEST.log.gz $OUT.log.gz
fi
else
echo "ERROR: invalid BEST output files"
exit -1
fi
else
echo "ERROR: no output files found"
exit -1
fi
# Clean-up
rm -f $TMP_DIR/$ID.REP_*